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

Defined Under Namespace

Classes: TupleVariationHeader

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



92
93
94
# File 'lib/fontisan/tables/cvar.rb', line 92

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



86
87
88
# File 'lib/fontisan/tables/cvar.rb', line 86

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

#summaryHash

Get summary of CVT variations

Returns:

  • (Hash)

    Summary information



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/fontisan/tables/cvar.rb', line 217

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



79
80
81
# File 'lib/fontisan/tables/cvar.rb', line 79

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/fontisan/tables/cvar.rb', line 197

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



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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/fontisan/tables/cvar.rb', line 97

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 = 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



237
238
239
# File 'lib/fontisan/tables/cvar.rb', line 237

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

#variation_dataString?

Get variation data section

Returns:

  • (String, nil)

    Raw variation data



178
179
180
181
182
183
184
185
186
187
# File 'lib/fontisan/tables/cvar.rb', line 178

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)



72
73
74
# File 'lib/fontisan/tables/cvar.rb', line 72

def version
  major_version + (minor_version / 10.0)
end