module HexaPDF::Font::TrueType::Builder

Builds a TrueType font file given a hash of TrueType tables.

Public Class Methods

build(tables) click to toggle source

Returns a TrueType font file representing the given TrueType tables (a hash mapping table names (strings) to table data).

# File lib/hexapdf/font/true_type/builder.rb, line 45
def self.build(tables)
  search_range = 2**(tables.length.bit_length - 1) * 16
  entry_selector = tables.length.bit_length - 1
  range_shift = tables.length * 16 - search_range

  font_data = "\x0\x1\x0\x0".b + \
    [tables.length, search_range, entry_selector, range_shift].pack('n4')

  offset = font_data.length + tables.length * 16
  checksum = Table.calculate_checksum(font_data)

  # Prepare head table for checksumming
  head_table = tables['head']
  head_table[8, 4] = "\0\0\0\0"

  # The directory table needs to be sorted in ascending order by tag
  tables = tables.sort

  tables.each do |tag, data|
    original_data_length = data.length
    table_checksum = Table.calculate_checksum(data)
    data << "\0" * (4 - original_data_length % 4) if original_data_length % 4 != 0
    # tag, offset, data.length are all 32bit uint, table_checksum for header and body
    checksum += tag.unpack1('N') + 2 * table_checksum + offset + data.length
    font_data << [tag, table_checksum, offset, original_data_length].pack('a4N3')
    offset += data.length
  end

  head_table[8, 4] = [0xB1B0AFBA - checksum].pack('N')
  tables.each {|_, data| font_data << data }

  font_data
end