Class: Fontisan::Optimizers::SubroutineOptimizer
- Inherits:
-
Object
- Object
- Fontisan::Optimizers::SubroutineOptimizer
- Defined in:
- lib/fontisan/optimizers/subroutine_optimizer.rb
Overview
Optimizes subroutine selection and ordering for maximum file size reduction. Uses a greedy algorithm to select the most beneficial patterns while avoiding conflicts, then orders them by frequency for efficient encoding.
Instance Method Summary collapse
-
#initialize(patterns, max_subrs: 65535) ⇒ SubroutineOptimizer
constructor
Initialize optimizer with patterns.
-
#optimize_nesting(subroutines) ⇒ Array<Pattern>
Check if nesting would be beneficial TODO: Phase 2.1 - check if subroutines contain common patterns.
-
#optimize_ordering(subroutines) ⇒ Array<Pattern>
Optimize subroutine ordering by frequency Higher frequency patterns get lower IDs for more efficient encoding in CFF format.
-
#optimize_selection ⇒ Array<Pattern>
Select optimal subset of patterns to subroutinize Uses greedy algorithm: select by highest savings first, checking for conflicts with already selected patterns.
Constructor Details
#initialize(patterns, max_subrs: 65535) ⇒ SubroutineOptimizer
Initialize optimizer with patterns
21 22 23 24 |
# File 'lib/fontisan/optimizers/subroutine_optimizer.rb', line 21 def initialize(patterns, max_subrs: 65535) @patterns = patterns @max_subrs = max_subrs end |
Instance Method Details
#optimize_nesting(subroutines) ⇒ Array<Pattern>
Check if nesting would be beneficial TODO: Phase 2.1 - check if subroutines contain common patterns
64 65 66 |
# File 'lib/fontisan/optimizers/subroutine_optimizer.rb', line 64 def optimize_nesting(subroutines) subroutines end |
#optimize_ordering(subroutines) ⇒ Array<Pattern>
Optimize subroutine ordering by frequency Higher frequency patterns get lower IDs for more efficient encoding in CFF format.
53 54 55 56 57 |
# File 'lib/fontisan/optimizers/subroutine_optimizer.rb', line 53 def optimize_ordering(subroutines) # Higher frequency = lower ID (shorter encoding) # Use same comprehensive sort keys as optimize_selection for consistency subroutines.sort_by { |subr| [-subr.frequency, -subr.length, subr.glyphs.min, subr.bytes.bytes] } end |
#optimize_selection ⇒ Array<Pattern>
Select optimal subset of patterns to subroutinize Uses greedy algorithm: select by highest savings first, checking for conflicts with already selected patterns.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fontisan/optimizers/subroutine_optimizer.rb', line 31 def optimize_selection selected = [] # Sort by savings (descending), then by length (descending), then by min glyph ID, # then by byte values for complete determinism across platforms remaining = @patterns.sort_by { |p| [-p.savings, -p.length, p.glyphs.min, p.bytes.bytes] } remaining.each do |pattern| break if selected.length >= @max_subrs next if conflicts_with_selected?(pattern, selected) selected << pattern end selected end |