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


15
16
17
# File 'lib/ttfunk/table/hmtx.rb', line 15

def left_side_bearings
  @left_side_bearings
end

#metricsArray<HorizontalMetric> (readonly)

Glyph horizontal metrics.

Returns:



11
12
13
# File 'lib/ttfunk/table/hmtx.rb', line 11

def metrics
  @metrics
end

#widthsArray<Integer> (readonly)

Glyph widths.

Returns:

  • (Array<Integer>)


19
20
21
# File 'lib/ttfunk/table/hmtx.rb', line 19

def widths
  @widths
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.



29
30
31
32
33
34
35
36
37
38
39
40
# 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

Instance Method Details

#for(glyph_id) ⇒ HorizontalMetric

Get horizontal metric for glyph.

Parameters:

  • glyph_id (Integer)

Returns:



54
55
56
57
58
59
60
61
# 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