Class: Fontisan::Variation::Converter

Inherits:
Object
  • Object
show all
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:

  1. Extract tuple variations from gvar

  2. Map tuple regions to blend regions

  3. Embed blend operators in CharStrings at control points

  4. Encode delta values in blend format

Process for blend → gvar:

  1. Parse CharStrings with blend operators

  2. Extract blend deltas and regions

  3. Map to gvar tuple format

  4. Build gvar table structure

Examples:

Converting gvar to CFF2 blend

converter = Converter.new(font, axes)
blend_data = converter.gvar_to_blend(glyph_id)

Converting CFF2 blend to gvar

converter = Converter.new(font, axes)
tuple_data = converter.blend_to_gvar(glyph_id)

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:



46
47
48
49
50
# File 'lib/fontisan/variation/converter.rb', line 46

def initialize(font, axes)
  @font = font
  @axes = axes || []
  @variation_tables = {}
end

Instance Attribute Details

#axesArray<VariationAxisRecord> (readonly)

Returns Variation axes.

Returns:

  • (Array<VariationAxisRecord>)

    Variation axes



40
41
42
# File 'lib/fontisan/variation/converter.rb', line 40

def axes
  @axes
end

#fontTrueTypeFont, OpenTypeFont (readonly)

Returns Font instance.

Returns:



37
38
39
# File 'lib/fontisan/variation/converter.rb', line 37

def font
  @font
end

Instance Method Details

#blend_to_gvar(glyph_id) ⇒ Hash?

Convert CFF2 blend operators to gvar tuple format for a glyph

Parameters:

  • glyph_id (Integer)

    Glyph ID

Returns:

  • (Hash, nil)

    Tuple data or nil



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fontisan/variation/converter.rb', line 75

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

Returns:

  • (Boolean)

    True if conversion possible



123
124
125
126
127
128
# File 'lib/fontisan/variation/converter.rb', line 123

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

Parameters:

  • glyph_count (Integer)

    Number of glyphs

Returns:

  • (Hash<Integer, Hash>)

    Map of glyph_id to tuple data



111
112
113
114
115
116
117
118
# File 'lib/fontisan/variation/converter.rb', line 111

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

Parameters:

  • glyph_count (Integer)

    Number of glyphs

Returns:

  • (Hash<Integer, Hash>)

    Map of glyph_id to blend data



98
99
100
101
102
103
104
105
# File 'lib/fontisan/variation/converter.rb', line 98

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

Parameters:

  • glyph_id (Integer)

    Glyph ID

Returns:

  • (Hash, nil)

    Blend data or nil



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fontisan/variation/converter.rb', line 56

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