class TTFunk::Table::Hmtx

Horizontal Metrics (‘hmtx`) table.

Constants

HorizontalMetric

Horyzontal glyph metric.

@!attribute [rw] advance_width

@return [Integer] Advance width.

@!attribute [rw] left_side_bearing

@return [Integer] Left side bearing.

Attributes

left_side_bearings[R]

Left side bearings. @return [Array<Ingteger>]

metrics[R]

Glyph horizontal metrics. @return [Array<HorizontalMetric>]

widths[R]

Glyph widths. @return [Array<Integer>]

Public Class Methods

encode(hmtx, mapping) click to toggle source

Encode table.

@param hmtx [TTFunk::Table::Hmtx] @param mapping [Hash{Integer => Integer}] keys are new glyph IDs, values

are old glyph IDs

@return [Hash{:number_of_metrics => Integer, :table => String}]

* `:number_of_metrics` - number of mertrics is the table.
* `:table` - encoded table.
# File lib/ttfunk/table/hmtx.rb, line 29
def self.encode(hmtx, mapping)
  metrics =
    mapping.keys.sort.map { |new_id|
      metric = hmtx.for(mapping[new_id])
      [metric.advance_width, metric.left_side_bearing]
    }

  {
    number_of_metrics: metrics.length,
    table: metrics.flatten.pack('n*'),
  }
end

Public Instance Methods

for(glyph_id) click to toggle source

Get horizontal metric for glyph.

@param glyph_id [Integer] @return [HorizontalMetric]

# File lib/ttfunk/table/hmtx.rb, line 54
def for(glyph_id)
  @metrics[glyph_id] ||
    metrics_cache[glyph_id] ||=
      HorizontalMetric.new(
        @metrics.last.advance_width,
        @left_side_bearings[glyph_id - @metrics.length],
      )
end

Private Instance Methods

metrics_cache() click to toggle source
# File lib/ttfunk/table/hmtx.rb, line 65
def metrics_cache
  @metrics_cache ||= {}
end
parse!() click to toggle source
# File lib/ttfunk/table/hmtx.rb, line 69
def parse!
  @metrics = []

  file.horizontal_header.number_of_metrics.times do
    advance = read(2, 'n').first
    lsb = read_signed(1).first
    @metrics.push(HorizontalMetric.new(advance, lsb))
  end

  lsb_count = file.maximum_profile.num_glyphs -
    file.horizontal_header.number_of_metrics
  @left_side_bearings = read_signed(lsb_count)

  @widths = @metrics.map(&:advance_width)
  @widths += [@widths.last] * @left_side_bearings.length
end