Class: Fontisan::Tables::Cff::CharStringRebuilder
- Inherits:
-
Object
- Object
- Fontisan::Tables::Cff::CharStringRebuilder
- Defined in:
- lib/fontisan/tables/cff/charstring_rebuilder.rb
Overview
Rebuilds CharStrings INDEX with modified CharStrings
CharStringRebuilder provides high-level interface for modifying CharStrings in a CFF font. It extracts all CharStrings from the source INDEX, allows modifications through a callback, and rebuilds the INDEX with updated CharString data.
Use Cases:
-
Per-glyph hint injection
-
CharString optimization
-
Subroutine insertion
-
Any operation requiring CharString modification
Instance Attribute Summary collapse
-
#modifications ⇒ Hash
readonly
Modified CharString data by glyph index.
-
#source_index ⇒ CharstringsIndex
readonly
Source CharStrings INDEX.
Instance Method Summary collapse
-
#batch_modify(glyph_indices) {|glyph_index, operations| ... } ⇒ Object
Batch modify multiple CharStrings.
-
#charstring_data(glyph_index) ⇒ String
Get CharString data (modified or original).
-
#clear_modifications ⇒ Object
Clear all modifications.
-
#initialize(source_index, stem_count: 0) ⇒ CharStringRebuilder
constructor
Initialize rebuilder with source CharStrings INDEX.
-
#modification_count ⇒ Integer
Get count of modified glyphs.
-
#modified?(glyph_index) ⇒ Boolean
Check if glyph has been modified.
-
#modify_all {|glyph_index, operations| ... } ⇒ Object
Modify all CharStrings.
-
#modify_charstring(glyph_index) {|operations| ... } ⇒ Object
Modify a CharString by glyph index.
-
#rebuild ⇒ String
Rebuild CharStrings INDEX with modifications.
-
#stem_count=(count) ⇒ Object
Update stem count (needed for hintmask parsing).
Constructor Details
#initialize(source_index, stem_count: 0) ⇒ CharStringRebuilder
Initialize rebuilder with source CharStrings INDEX
44 45 46 47 48 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 44 def initialize(source_index, stem_count: 0) @source_index = source_index @stem_count = stem_count @modifications = {} end |
Instance Attribute Details
#modifications ⇒ Hash (readonly)
Returns Modified CharString data by glyph index.
38 39 40 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 38 def modifications @modifications end |
#source_index ⇒ CharstringsIndex (readonly)
Returns Source CharStrings INDEX.
35 36 37 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 35 def source_index @source_index end |
Instance Method Details
#batch_modify(glyph_indices) {|glyph_index, operations| ... } ⇒ Object
Batch modify multiple CharStrings
More efficient than calling modify_charstring multiple times.
111 112 113 114 115 116 117 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 111 def batch_modify(glyph_indices) glyph_indices.each do |glyph_index| modify_charstring(glyph_index) do |operations| yield(glyph_index, operations) end end end |
#charstring_data(glyph_index) ⇒ String
Get CharString data (modified or original)
139 140 141 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 139 def charstring_data(glyph_index) @modifications[glyph_index] || @source_index[glyph_index] end |
#clear_modifications ⇒ Object
Clear all modifications
159 160 161 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 159 def clear_modifications @modifications.clear end |
#modification_count ⇒ Integer
Get count of modified glyphs
154 155 156 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 154 def modification_count @modifications.size end |
#modified?(glyph_index) ⇒ Boolean
Check if glyph has been modified
147 148 149 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 147 def modified?(glyph_index) @modifications.key?(glyph_index) end |
#modify_all {|glyph_index, operations| ... } ⇒ Object
Modify all CharStrings
Applies the same modification to every glyph.
127 128 129 130 131 132 133 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 127 def modify_all (0...@source_index.count).each do |i| modify_charstring(i) do |operations| yield(i, operations) end end end |
#modify_charstring(glyph_index) {|operations| ... } ⇒ Object
Modify a CharString by glyph index
The block receives the parsed operations for the glyph and should return modified operations.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 59 def modify_charstring(glyph_index) # Get original CharString data original_data = @source_index[glyph_index] return unless original_data # Parse to operations parser = CharStringParser.new(original_data, stem_count: @stem_count) operations = parser.parse # Apply modification modified_operations = yield(operations) # Build new CharString new_data = CharStringBuilder.build_from_operations(modified_operations) # Store modification @modifications[glyph_index] = new_data end |
#rebuild ⇒ String
Rebuild CharStrings INDEX with modifications
Creates new INDEX with modified CharStrings, keeping unmodified CharStrings unchanged.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 84 def rebuild # Collect all CharString data (modified and unmodified) charstrings = [] (0...@source_index.count).each do |i| charstrings << if @modifications.key?(i) # Use modified CharString @modifications[i] else # Use original CharString @source_index[i] end end # Build INDEX IndexBuilder.build(charstrings) end |
#stem_count=(count) ⇒ Object
Update stem count (needed for hintmask parsing)
166 167 168 |
# File 'lib/fontisan/tables/cff/charstring_rebuilder.rb', line 166 def stem_count=(count) @stem_count = count end |