Module: Fontisan::Ufo::Compile::Mvar

Defined in:
lib/fontisan/ufo/compile/mvar.rb

Overview

Builds the OpenType MVAR (Metrics Variation) table.

Stores deltas for font-wide metrics (ascender, descender, etc.) so they can vary across the design space.

Constant Summary collapse

HEADER_SIZE =

majorVersion(2) + minorVersion(2) + valueRecordSize(2) +

10
VALUE_RECORD_SIZE =

valueRecordCount(2) + itemVariationStoreOffset(2)

8

Class Method Summary collapse

Class Method Details

.build(default_metrics:, master_metrics:, axis_count:) ⇒ String

Returns MVAR table bytes.

Parameters:

  • default_metrics (Hash<Symbol, Integer>)

    e.g. { hasc: 800, hdsc: -200 }

  • master_metrics (Array<Hash<Symbol, Integer>>)

    per master

  • axis_count (Integer)

Returns:

  • (String)

    MVAR table bytes



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fontisan/ufo/compile/mvar.rb', line 21

def self.build(default_metrics:, master_metrics:, axis_count:)
  tags = default_metrics.keys
  return nil if tags.empty?

  master_count = master_metrics.size

  deltas = []
  records = +""
  tags.each_with_index do |tag, idx|
    tag_bytes = tag.to_s.ljust(4, " ")[0, 4]
    delta = master_metrics.dig(0, tag).to_i - default_metrics[tag].to_i
    records << tag_bytes
    records << [0, idx].pack("nn") # outerIndex=0, innerIndex=idx
    deltas << [delta]
  end

  store = ItemVariationStore.build(
    axis_count: axis_count,
    master_count: master_count,
    item_count: tags.size,
    deltas: deltas,
  )

  store_offset = HEADER_SIZE + records.bytesize

  io = +""
  io << [1].pack("n")                # majorVersion
  io << [0].pack("n")                # minorVersion
  io << [VALUE_RECORD_SIZE].pack("n")
  io << [tags.size].pack("n")        # valueRecordCount
  io << [store_offset].pack("n")     # itemVariationStoreOffset (Offset16)
  io << records
  io << store
  io
end