Module: Fontisan::Ufo::Compile::Hmtx

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

Overview

Builds the OpenType hmtx (horizontal metrics) table. One LongHorMetric per glyph (4 bytes each):

uint16 advanceWidth, int16 lsb

No trailing "leftSideBearing" array (use numberOfHMetrics = numGlyphs).

Class Method Summary collapse

Class Method Details

.build(font, glyphs:) ⇒ String

Returns hmtx table bytes.

Parameters:

Returns:

  • (String)

    hmtx table bytes



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fontisan/ufo/compile/hmtx.rb', line 15

def self.build(font, glyphs:)
  upm = font.info.units_per_em&.to_i || 1000
  data = +""
  glyphs.each do |glyph|
    bbox = glyph.bbox
    lsb = bbox ? bbox.x_min.to_i : 0
    width = glyph.width.to_i

    # Safety net: never emit advance_width=0 for a non-empty
    # glyph. This happens when donor hmtx parsing fails during
    # stitching and the width wasn't caught upstream.
    if width <= 0
      width = bbox ? (bbox.x_max.to_i - bbox.x_min.to_i) : upm
      width = [width, upm].max
    end

    data << [width, lsb].pack("nn")
  end
  data
end