Class: TTFunk::Table::Kern::Format0

Inherits:
Object
  • Object
show all
Includes:
Reader
Defined in:
lib/ttfunk/table/kern/format0.rb

Overview

Format 0 kerning subtable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Format0

Returns a new instance of Format0.

Parameters:

  • attributes (Hash{Symbol => any}) (defaults to: {})


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ttfunk/table/kern/format0.rb', line 21

def initialize(attributes = {})
  @attributes = attributes

  num_pairs, *pairs = attributes.delete(:data).unpack('nx6n*')

  @pairs = {}
  num_pairs.times do |i|
    # sanity check, in case there's a bad length somewhere
    break if (i * 3) + 2 > pairs.length

    left = pairs[i * 3]
    right = pairs[(i * 3) + 1]
    value = to_signed(pairs[(i * 3) + 2])
    @pairs[[left, right]] = value
  end
end

Instance Attribute Details

#attributesHash{Symbol => any} (readonly)

Subtable attributes.

Returns:

  • (Hash{Symbol => any})


14
15
16
# File 'lib/ttfunk/table/kern/format0.rb', line 14

def attributes
  @attributes
end

#pairsHash{Array(Integer, Integer) => Integer} (readonly)

Kerning pairs.

Returns:

  • (Hash{Array(Integer, Integer) => Integer})


18
19
20
# File 'lib/ttfunk/table/kern/format0.rb', line 18

def pairs
  @pairs
end

Instance Method Details

#cross_stream?Boolean

Is this cross-stream kerning?

Returns:

  • (Boolean)


52
53
54
# File 'lib/ttfunk/table/kern/format0.rb', line 52

def cross_stream?
  @attributes[:cross]
end

#horizontal?Boolean

Is this horizontal kerning?

Returns:

  • (Boolean)


46
47
48
# File 'lib/ttfunk/table/kern/format0.rb', line 46

def horizontal?
  !vertical?
end

#recode(mapping) ⇒ String

Recode this subtable using the specified mapping.

Parameters:

  • mapping (Hash{Integer => Integer})

    keys are new glyph IDs, values are old glyph IDs

Returns:

  • (String)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ttfunk/table/kern/format0.rb', line 61

def recode(mapping)
  subset = []
  pairs.each do |(left, right), value|
    if mapping[left] && mapping[right]
      subset << [mapping[left], mapping[right], value]
    end
  end

  return if subset.empty?

  num_pairs = subset.length
  search_range = 2 * (2**Integer(Math.log(num_pairs) / Math.log(2)))
  entry_selector = Integer(Math.log(search_range / 2) / Math.log(2))
  range_shift = (2 * num_pairs) - search_range

  [
    attributes[:version],
    (num_pairs * 6) + 14,
    attributes[:coverage],
    num_pairs,
    search_range,
    entry_selector,
    range_shift,
    subset,
  ].flatten.pack('n*')
end

#vertical?Boolean

Is this vertical kerning?

Returns:

  • (Boolean)


40
41
42
# File 'lib/ttfunk/table/kern/format0.rb', line 40

def vertical?
  @attributes[:vertical]
end