class HexaPDF::Font::TrueType::Table

Implementation of a generic table inside a sfnt-formatted font file.

See: developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html

Constants

TIME_EPOCH

The time Epoch used in sfnt-formatted font files.

Attributes

font[R]

The TrueType font object associated with this table.

Public Class Methods

calculate_checksum(data) click to toggle source

Calculates the checksum for the given data.

# File lib/hexapdf/font/true_type/table.rb, line 64
def self.calculate_checksum(data)
  data.unpack('N*').inject(0) {|sum, long| sum + long } % 2**32
end
new(font, entry) click to toggle source

Creates a new Table object for the given font and initializes it by reading the data from the font's associated IO stream

See: parse_table

# File lib/hexapdf/font/true_type/table.rb, line 75
def initialize(font, entry)
  @font = font
  @directory_entry = entry
  load_from_io
end

Public Instance Methods

checksum_valid?() click to toggle source

Returns true if the checksum stored in the directory entry of the table matches the tables data.

# File lib/hexapdf/font/true_type/table.rb, line 90
def checksum_valid?
  directory_entry.checksum == self.class.calculate_checksum(raw_data)
end
directory_entry() click to toggle source

Returns the directory entry for this table.

See: Directory

# File lib/hexapdf/font/true_type/table.rb, line 84
def directory_entry
  @directory_entry
end
raw_data() click to toggle source

Returns the raw table data.

# File lib/hexapdf/font/true_type/table.rb, line 95
def raw_data
  with_io_pos(directory_entry.offset) { io.read(directory_entry.length) }
end

Private Instance Methods

io() click to toggle source

The IO stream of the associated font object.

# File lib/hexapdf/font/true_type/table.rb, line 102
def io
  @font.io
end
load_from_io() click to toggle source

Loads the data for this table from the IO stream of the associated font object into this object.

See parse_table for more information.

# File lib/hexapdf/font/true_type/table.rb, line 110
def load_from_io
  with_io_pos(directory_entry.offset) { parse_table }
end
parse_table() click to toggle source

Parses the table with the IO position already at the correct offset.

This method does the actual work of parsing a table entry and must be implemented by subclasses.

See: load_from_io

# File lib/hexapdf/font/true_type/table.rb, line 120
def parse_table
  # noop for unsupported tables
end
read_fixed() click to toggle source

Reads a 16.16-bit signed fixed-point integer and returns a Rational as result.

# File lib/hexapdf/font/true_type/table.rb, line 141
def read_fixed
  Rational(io.read(4).unpack1('i>'), 65536)
end
read_formatted(count, format) click to toggle source

Reads count bytes from the current position of the font's associated IO stream, unpacks them using the provided format specifier and returns the result.

# File lib/hexapdf/font/true_type/table.rb, line 136
def read_formatted(count, format)
  io.read(count).unpack(format)
end
with_io_pos(pos) { || ... } click to toggle source

Sets the IO cursor to the given position while yielding to the block and returns the block's return value.

# File lib/hexapdf/font/true_type/table.rb, line 126
def with_io_pos(pos)
  old_pos = io.pos
  io.pos = pos
  yield
ensure
  io.pos = old_pos
end