Class: Fontisan::Variation::Converter
- Inherits:
-
Object
- Object
- Fontisan::Variation::Converter
- Includes:
- TableAccessor
- Defined in:
- lib/fontisan/variation/converter.rb
Overview
Converts variation data between TrueType (gvar) and CFF2 (blend) formats
This class enables format conversion while preserving variation data:
- gvar tuples → CFF2 blend operators
- CFF2 blend operators → gvar tuples
Process for gvar → blend:
- Extract tuple variations from gvar
- Map tuple regions to blend regions
- Embed blend operators in CharStrings at control points
- Encode delta values in blend format
Process for blend → gvar:
- Parse CharStrings with blend operators
- Extract blend deltas and regions
- Map to gvar tuple format
- Build gvar table structure
Instance Attribute Summary collapse
-
#axes ⇒ Array<VariationAxisRecord>
readonly
Variation axes.
-
#font ⇒ TrueTypeFont, OpenTypeFont
readonly
Font instance.
Instance Method Summary collapse
-
#blend_to_gvar(glyph_id) ⇒ Hash?
Convert CFF2 blend operators to gvar tuple format for a glyph.
-
#can_convert? ⇒ Boolean
Check if variation data can be converted.
-
#convert_all_blend_to_gvar(glyph_count) ⇒ Hash<Integer, Hash>
Convert all glyphs from blend to gvar format.
-
#convert_all_gvar_to_blend(glyph_count) ⇒ Hash<Integer, Hash>
Convert all glyphs from gvar to blend format.
-
#gvar_to_blend(glyph_id) ⇒ Hash?
Convert gvar tuples to CFF2 blend format for a glyph.
-
#initialize(font, axes) ⇒ Converter
constructor
Initialize converter.
Methods included from TableAccessor
#clear_variation_cache, #clear_variation_table, #has_variation_table?, #require_variation_table, #variation_table
Constructor Details
#initialize(font, axes) ⇒ Converter
Initialize converter
43 44 45 46 47 |
# File 'lib/fontisan/variation/converter.rb', line 43 def initialize(font, axes) @font = font @axes = axes || [] @variation_tables = {} end |
Instance Attribute Details
#axes ⇒ Array<VariationAxisRecord> (readonly)
Returns Variation axes.
37 38 39 |
# File 'lib/fontisan/variation/converter.rb', line 37 def axes @axes end |
#font ⇒ TrueTypeFont, OpenTypeFont (readonly)
Returns Font instance.
34 35 36 |
# File 'lib/fontisan/variation/converter.rb', line 34 def font @font end |
Instance Method Details
#blend_to_gvar(glyph_id) ⇒ Hash?
Convert CFF2 blend operators to gvar tuple format for a glyph
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/fontisan/variation/converter.rb', line 72 def blend_to_gvar(glyph_id) return nil unless has_variation_table?("CFF2") cff2 = variation_table("CFF2") return nil unless cff2 # Get CharString with blend operators charstring = cff2.charstring_for_glyph(glyph_id) return nil unless charstring # Parse CharString to extract blend data charstring.parse unless charstring.instance_variable_get(:@parsed) blend_data = charstring.blend_data return nil if blend_data.nil? || blend_data.empty? # Convert blend data to tuple format convert_blend_to_tuples_for_glyph(blend_data) end |
#can_convert? ⇒ Boolean
Check if variation data can be converted
120 121 122 123 124 125 |
# File 'lib/fontisan/variation/converter.rb', line 120 def can_convert? !@axes.empty? && ( has_variation_table?("gvar") || has_variation_table?("CFF2") ) end |
#convert_all_blend_to_gvar(glyph_count) ⇒ Hash<Integer, Hash>
Convert all glyphs from blend to gvar format
108 109 110 111 112 113 114 115 |
# File 'lib/fontisan/variation/converter.rb', line 108 def convert_all_blend_to_gvar(glyph_count) return {} unless can_convert? (0...glyph_count).each_with_object({}) do |glyph_id, result| tuple_data = blend_to_gvar(glyph_id) result[glyph_id] = tuple_data if tuple_data end end |
#convert_all_gvar_to_blend(glyph_count) ⇒ Hash<Integer, Hash>
Convert all glyphs from gvar to blend format
95 96 97 98 99 100 101 102 |
# File 'lib/fontisan/variation/converter.rb', line 95 def convert_all_gvar_to_blend(glyph_count) return {} unless can_convert? (0...glyph_count).each_with_object({}) do |glyph_id, result| blend_data = gvar_to_blend(glyph_id) result[glyph_id] = blend_data if blend_data end end |
#gvar_to_blend(glyph_id) ⇒ Hash?
Convert gvar tuples to CFF2 blend format for a glyph
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fontisan/variation/converter.rb', line 53 def gvar_to_blend(glyph_id) return nil unless has_variation_table?("gvar") return nil unless has_variation_table?("glyf") gvar = variation_table("gvar") return nil unless gvar # Get tuple variations for this glyph tuple_data = gvar.glyph_tuple_variations(glyph_id) return nil unless tuple_data # Convert tuples to blend format convert_tuples_to_blend(tuple_data) end |