Class: Fontisan::Tables::Cvar

Inherits:
Binary::BaseRecord show all
Defined in:
lib/fontisan/tables/cvar.rb

Overview

Parser for the 'cvar' (CVT Variations) table

The cvar table provides variation data for the Control Value Table (CVT) in TrueType variable fonts with hinting. The CVT contains scalar values that are referenced by TrueType instructions for grid-fitting.

Like gvar, this table uses a TupleVariationStore structure with packed delta values rather than ItemVariationStore.

Reference: OpenType specification, cvar table

Examples:

Reading a cvar table

data = font.table_data("cvar")
cvar = Fontisan::Tables::Cvar.read(data)
cvt_deltas = cvar.cvt_variations

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Binary::BaseRecord

#raw_data, read

Instance Attribute Details

#axis_countObject

Get axis count from fvar table (needs to be provided externally) This is a placeholder that should be set by the caller



51
52
53
# File 'lib/fontisan/tables/cvar.rb', line 51

def axis_count
  @axis_count
end

Instance Method Details

#shared_point_numbers?Boolean

Check if using shared point numbers

Returns:

  • (Boolean)

    True if shared points



45
46
47
# File 'lib/fontisan/tables/cvar.rb', line 45

def shared_point_numbers?
  (tuple_variation_count & 0x8000) != 0
end

#summaryHash

Get summary of CVT variations

Returns:

  • (Hash)

    Summary information



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fontisan/tables/cvar.rb', line 176

def summary
  {
    version: version,
    tuple_count: tuple_count,
    shared_points: shared_point_numbers?,
    data_offset: data_offset,
    tuples: tuple_variations.map do |t|
      {
        embedded_peak: t[:embedded_peak],
        intermediate: t[:intermediate],
        private_points: t[:private_points],
        peak: t[:peak],
      }
    end,
  }
end

#tuple_countInteger

Get tuple count

Returns:

  • (Integer)

    Number of tuple variations



38
39
40
# File 'lib/fontisan/tables/cvar.rb', line 38

def tuple_count
  tuple_variation_count & 0x0FFF
end

#tuple_variation_data(tuple_index) ⇒ Hash?

Parse CVT deltas for a specific tuple

This is a simplified parser that returns the raw delta data. Full delta unpacking would require knowing point counts and delta formats.

Parameters:

  • tuple_index (Integer)

    Tuple index

Returns:

  • (Hash, nil)

    Tuple info with data offset



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fontisan/tables/cvar.rb', line 156

def tuple_variation_data(tuple_index)
  return nil if tuple_index >= tuple_count

  tuples = tuple_variations
  return nil if tuple_index >= tuples.length

  tuple = tuples[tuple_index]

  # Calculate data offset for this tuple
  # This is complex and requires walking through all previous tuples
  # For now, return tuple metadata
  {
    tuple: tuple,
    data_size: tuple[:data_size],
  }
end

#tuple_variationsArray<Hash>

Parse tuple variation headers

Returns:

  • (Array<Hash>)

    Array of tuple information



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fontisan/tables/cvar.rb', line 56

def tuple_variations
  return @tuple_variations if @tuple_variations
  return @tuple_variations = [] if tuple_count.zero?

  data = raw_data
  # Tuple records start after header (8 bytes)
  offset = 8

  count = tuple_count
  tuples = []

  count.times do |_i|
    break if offset + 4 > data.bytesize

    header_data = data.byteslice(offset, 4)
    header = Variation::TupleVariationHeader.read(header_data)
    offset += 4

    tuple_info = {
      data_size: header.variation_data_size,
      embedded_peak: header.embedded_peak_tuple?,
      intermediate: header.intermediate_region?,
      private_points: header.private_point_numbers?,
      shared_index: header.shared_tuple_index,
    }

    # Read peak tuple if embedded
    if header.embedded_peak_tuple? && axis_count
      peak = Array.new(axis_count) do
        next nil if offset + 2 > data.bytesize

        coord_data = data.byteslice(offset, 2)
        offset += 2

        value = coord_data.unpack1("n")
        signed = value > 0x7FFF ? value - 0x10000 : value
        signed / 16384.0
      end.compact
      tuple_info[:peak] = peak
    end

    # Read intermediate region if present
    if header.intermediate_region? && axis_count
      start_tuple = Array.new(axis_count) do
        next nil if offset + 2 > data.bytesize

        coord_data = data.byteslice(offset, 2)
        offset += 2

        value = coord_data.unpack1("n")
        signed = value > 0x7FFF ? value - 0x10000 : value
        signed / 16384.0
      end.compact

      end_tuple = Array.new(axis_count) do
        next nil if offset + 2 > data.bytesize

        coord_data = data.byteslice(offset, 2)
        offset += 2

        value = coord_data.unpack1("n")
        signed = value > 0x7FFF ? value - 0x10000 : value
        signed / 16384.0
      end.compact

      tuple_info[:start] = start_tuple
      tuple_info[:end] = end_tuple
    end

    tuples << tuple_info
  end

  @tuple_variations = tuples
rescue StandardError => e
  warn "Failed to parse cvar tuple variations: #{e.message}"
  @tuple_variations = []
end

#valid?Boolean

Check if table is valid

Returns:

  • (Boolean)

    True if valid



196
197
198
# File 'lib/fontisan/tables/cvar.rb', line 196

def valid?
  major_version == 1 && minor_version.zero?
end

#variation_dataString?

Get variation data section

Returns:

  • (String, nil)

    Raw variation data



137
138
139
140
141
142
143
144
145
146
# File 'lib/fontisan/tables/cvar.rb', line 137

def variation_data
  return @variation_data if defined?(@variation_data)

  data = raw_data
  offset = data_offset

  return @variation_data = nil if offset >= data.bytesize

  @variation_data = data.byteslice(offset..-1)
end

#versionFloat

Get version as a float

Returns:

  • (Float)

    Version number (e.g., 1.0)



31
32
33
# File 'lib/fontisan/tables/cvar.rb', line 31

def version
  major_version + (minor_version / 10.0)
end