Class: TTFunk::Table::Hmtx

Inherits:
TTFunk::Table show all
Defined in:
lib/ttfunk/table/hmtx.rb

Overview

Horizontal Metrics (hmtx) table.

Defined Under Namespace

Classes: HorizontalMetric

Instance Attribute Summary collapse

Attributes inherited from TTFunk::Table

#file, #length, #offset

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TTFunk::Table

#exists?, #initialize, #raw, #tag

Constructor Details

This class inherits a constructor from TTFunk::Table

Instance Attribute Details

#left_side_bearingsArray<Ingteger> (readonly)

Left side bearings.

Returns:

  • (Array<Ingteger>)
Source Code
lib/ttfunk/table/hmtx.rb, line 15
15
def left_side_bearings
16
  @left_side_bearings
17
end

#metricsArray<HorizontalMetric> (readonly)

Glyph horizontal metrics.

Returns:

Source Code
lib/ttfunk/table/hmtx.rb, line 11
11
def metrics
12
  @metrics
13
end

#widthsArray<Integer> (readonly)

Glyph widths.

Returns:

  • (Array<Integer>)
Source Code
lib/ttfunk/table/hmtx.rb, line 19
19
def widths
20
  @widths
21
end

Class Method Details

.encode(hmtx, mapping) ⇒ Hash{:number_of_metrics => Integer, :table => String}

Encode table.

Parameters:

  • hmtx (TTFunk::Table::Hmtx)
  • mapping (Hash{Integer => Integer})

    keys are new glyph IDs, values are old glyph IDs

Returns:

  • (Hash{:number_of_metrics => Integer, :table => String})
    • :number_of_metrics - number of mertrics is the table.
    • :table - encoded table.
Source Code
lib/ttfunk/table/hmtx.rb, line 29
29
def self.encode(hmtx, mapping)
30
  metrics =
31
    mapping.keys.sort.map { |new_id|
32
      metric = hmtx.for(mapping[new_id])
33
      [metric.advance_width, metric.left_side_bearing]
34
    }
35
36
  {
37
    number_of_metrics: metrics.length,
38
    table: metrics.flatten.pack('n*'),
39
  }
40
end

Instance Method Details

#for(glyph_id) ⇒ HorizontalMetric

Get horizontal metric for glyph.

Parameters:

  • glyph_id (Integer)

Returns:

Source Code
lib/ttfunk/table/hmtx.rb, line 54
54
def for(glyph_id)
55
  @metrics[glyph_id] ||
56
    metrics_cache[glyph_id] ||=
57
      HorizontalMetric.new(
58
        @metrics.last.advance_width,
59
        @left_side_bearings[glyph_id - @metrics.length],
60
      )
61
end