class TTFunk::Table::Loca

Index to Location table.

Attributes

offsets[R]

Glyph ofsets @return [Array<Integer>]

Public Class Methods

encode(offsets) click to toggle source

Encode table.

@param offsets [Array<Integer>] an array of offsets, with each index

corresponding to the glyph id with that index.

@return [Hash] result hash:

* `:type` - the type of offset (to be encoded in the 'head' table):
  * `0` - short offsets
  * `1` - long offsets
* `:table` - encoded bytes
# File lib/ttfunk/table/loca.rb, line 22
def self.encode(offsets)
  long_offsets =
    offsets.any? { |offset|
      short_offset = offset / 2
      short_offset * 2 != offset || short_offset > 0xffff
    }

  if long_offsets
    { type: 1, table: offsets.pack('N*') }
  else
    { type: 0, table: offsets.map { |o| o / 2 }.pack('n*') }
  end
end

Public Instance Methods

index_of(glyph_id) click to toggle source

Glyph offset by ID.

@param glyph_id [Integer] @return [Integer] - offset of the glyph in the ‘glyf` table

# File lib/ttfunk/table/loca.rb, line 40
def index_of(glyph_id)
  @offsets[glyph_id]
end
size_of(glyph_id) click to toggle source

Size of encoded glyph.

@param glyph_id [Integer] @return [Integer]

# File lib/ttfunk/table/loca.rb, line 48
def size_of(glyph_id)
  @offsets[glyph_id + 1] - @offsets[glyph_id]
end

Private Instance Methods

parse!() click to toggle source
# File lib/ttfunk/table/loca.rb, line 54
def parse!
  type = file.header.index_to_loc_format.zero? ? 'n' : 'N'
  @offsets = read(length, "#{type}*")

  if file.header.index_to_loc_format.zero?
    @offsets.map! { |v| v * 2 }
  end
end