class TTFunk::Table::Kern::Format0

Format 0 kerning subtable.

Attributes

attributes[R]

Subtable attributes. @return [Hash{Symbol => any}]

pairs[R]

Kerning pairs. @return [Hash{Array(Integer, Integer) => Integer}]

Public Class Methods

new(attributes = {}) click to toggle source

@param attributes [Hash{Symbol => any}]

# 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

Public Instance Methods

cross_stream?() click to toggle source

Is this cross-stream kerning? @return [Boolean]

# File lib/ttfunk/table/kern/format0.rb, line 52
def cross_stream?
  @attributes[:cross]
end
horizontal?() click to toggle source

Is this horizontal kerning? @return [Boolean]

# File lib/ttfunk/table/kern/format0.rb, line 46
def horizontal?
  !vertical?
end
recode(mapping) click to toggle source

Recode this subtable using the specified mapping.

@param mapping [Hash{Integer => Integer}] keys are new glyph IDs,

values are old glyph IDs

@return [String]

# 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?() click to toggle source

Is this vertical kerning? @return [Boolean]

# File lib/ttfunk/table/kern/format0.rb, line 40
def vertical?
  @attributes[:vertical]
end