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
61 62 63 |
# File 'lib/fontisan/optimizers/subroutine_optimizer.rb', line 61 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.
51 52 53 54 |
# File 'lib/fontisan/optimizers/subroutine_optimizer.rb', line 51 def optimize_ordering(subroutines) # Higher frequency = lower ID (shorter encoding) subroutines.sort_by { |subr| -subr.frequency } 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 |
# File 'lib/fontisan/optimizers/subroutine_optimizer.rb', line 31 def optimize_selection selected = [] remaining = @patterns.sort_by { |p| -p.savings } remaining.each do |pattern| break if selected.length >= @max_subrs next if conflicts_with_selected?(pattern, selected) selected << pattern end selected end |