1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
# frozen_string_literal: true
require_relative '../table'
module TTFunk
class Table
# Index to Location table.
class Loca < Table
# Glyph ofsets
# @return [Array<Integer>]
attr_reader :offsets
# 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
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
# Glyph offset by ID.
#
# @param glyph_id [Integer]
# @return [Integer] - offset of the glyph in the `glyf` table
def index_of(glyph_id)
@offsets[glyph_id]
end
# Size of encoded glyph.
#
# @param glyph_id [Integer]
# @return [Integer]
def size_of(glyph_id)
@offsets[glyph_id + 1] - @offsets[glyph_id]
end
private
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
end
end
end
|