class TTFunk::File

OpenType fonts.
File represents an individual font. It can represents both TrueType and

def self.from_dfont(file, which = 0)

Returns:
  • (TTFunk::File) - ]

Parameters:
  • which (Integer, String) -- index or name of the font to load
  • file (String, Pathname) -- Path to the resource file.
def self.from_dfont(file, which = 0)
  new(ResourceFile.open(file) { |dfont| dfont['sfnt', which] })
end

def self.from_ttc(file, which = 0)

Returns:
  • (TTFunk::File) -
  • (TTFunk::File) -

Parameters:
  • which (Integer) -- index of the font to load
  • file_path (String, Pathname) -- Path to the resource file.
  • which (Integer) -- index of the font to load
  • file (IO) -- IO to read the collection from.

Overloads:
  • from_ttc(file_path, which = 0)
  • from_ttc(io, which = 0)
def self.from_ttc(file, which = 0)
  Collection.open(file) { |ttc| ttc[which] }
end

def self.open(io_or_path)

Returns:
  • (TTFunk::File) -
  • (TTFunk::File) -

Parameters:
  • path (String, Pathname) -- Path to file to read the font from.
  • io (IO) -- IO to read font content from. IO position and binmode

Overloads:
  • open(path)
  • open(io)
def self.open(io_or_path)
  new(verify_and_read(io_or_path))
end

def self.verify_and_open(io_or_path)

Other tags:
    See: .verify_and_read -

Deprecated:
  • This method might retain open files for longer than necessary.

Returns:
  • (IO) - newly opened IO for the path
  • (io) -

Parameters:
  • path (String, Pathname) -- path of the file to turn into an IO.
  • io (IO) -- IO to prepare. Its position and binmode might

Overloads:
  • verify_and_open(path)
  • verify_and_open(io)
def self.verify_and_open(io_or_path)
  # File or IO
  if io_or_path.respond_to?(:rewind)
    io = io_or_path
    # Rewind if the object we're passed is an IO, so that multiple embeds of
    # the same IO object will work
    io.rewind
    # read the file as binary so the size is calculated correctly
    # guard binmode because some objects acting io-like don't implement it
    io.binmode if io.respond_to?(:binmode)
    return io
  end
  # String or Pathname
  io_or_path = Pathname.new(io_or_path)
  raise ArgumentError, "#{io_or_path} not found" unless io_or_path.file?
  io_or_path.open('rb')
end

def self.verify_and_read(io_or_path)

Returns:
  • (String) -
  • (String) -

Parameters:
  • path (String, Pathname) -- path of the file to read.
  • io (IO) -- IO to read from. Its position and binmode might

Overloads:
  • verify_and_read(path)
  • verify_and_read(io)
def self.verify_and_read(io_or_path)
  # File or IO
  if io_or_path.respond_to?(:rewind)
    io = io_or_path
    # Rewind if the object we're passed is an IO, so that multiple embeds of
    # the same IO object will work
    io.rewind
    # read the file as binary so the size is calculated correctly
    # guard binmode because some objects acting io-like don't implement it
    io.binmode if io.respond_to?(:binmode)
    return io.read
  end
  # String or Pathname
  io_or_path = Pathname.new(io_or_path)
  raise ArgumentError, "#{io_or_path} not found" unless io_or_path.file?
  io_or_path.binread
end

def ascent

Returns:
  • (Integer) -
def ascent
  @ascent ||= (os2.exists? && os2.ascent && os2.ascent.nonzero?) ||
    horizontal_header.ascent
end

def bbox

Returns:
  • (Array(Integer, Integer, Integer, Integer)) -
def bbox
  [header.x_min, header.y_min, header.x_max, header.y_max]
end

def cff

Returns:
  • (Table::Table::Cff, nil) -
def cff
  @cff ||= TTFunk::Table::Cff.new(self)
end

def cmap

Returns:
  • (TTFunk::Tbale::Cmap, nil) -
def cmap
  @cmap ||= TTFunk::Table::Cmap.new(self)
end

def descent

Returns:
  • (Integer) -
def descent
  @descent ||= (os2.exists? && os2.descent && os2.descent.nonzero?) ||
    horizontal_header.descent
end

def digital_signature

Returns:
  • (TTFunk::Table::Dsig, nil) -
def digital_signature
  @digital_signature ||=
    if directory.tables.include?(TTFunk::Table::Dsig::TAG)
      TTFunk::Table::Dsig.new(self)
    end
end

def directory_info(tag)

Returns:
  • (Hash, nil) -

Parameters:
  • tag (String) -- table tab
def directory_info(tag)
  directory.tables[tag.to_s]
end

def find_glyph(glyph_id)

Returns:
  • (TTFunk::Table::Glyf::Simple, TTFunk::Table::Glyf::Compound) -
  • (TTFunk::Table::Cff::Charstring) - if it's a CFF-based OpenType font
def find_glyph(glyph_id)
  if cff.exists?
    cff.top_index[0].charstrings_index[glyph_id].glyph
  else
    glyph_outlines.for(glyph_id)
  end
end

def glyph_locations

Returns:
  • (TTFunk::Table::Loca, nil) -
def glyph_locations
  @glyph_locations ||= TTFunk::Table::Loca.new(self)
end

def glyph_outlines

Returns:
  • (TTFunk::Table::Glyf, nil) -
def glyph_outlines
  @glyph_outlines ||= TTFunk::Table::Glyf.new(self)
end

def header

Returns:
  • (TTFunk::Table::Head, nil) -
def header
  @header ||= TTFunk::Table::Head.new(self)
end

def horizontal_header

Returns:
  • (TTFunk::Table::Hhea, nil) -
def horizontal_header
  @horizontal_header ||= TTFunk::Table::Hhea.new(self)
end

def horizontal_metrics

Returns:
  • (TTFunk::Table::Hmtx, nil) -
def horizontal_metrics
  @horizontal_metrics ||= TTFunk::Table::Hmtx.new(self)
end

def initialize(contents, offset = 0)

Parameters:
  • offset (Integer) -- offset at which the font data starts
  • contents (String) -- binary string containg the font data
def initialize(contents, offset = 0)
  @contents = StringIO.new(contents)
  @directory = Directory.new(@contents, offset)
end

def kerning

Returns:
  • (TTFunk::Table::Kern, nil) -
def kerning
  @kerning ||= TTFunk::Table::Kern.new(self)
end

def line_gap

Returns:
  • (Integer) -
def line_gap
  @line_gap ||= (os2.exists? && os2.line_gap && os2.line_gap.nonzero?) ||
    horizontal_header.line_gap
end

def maximum_profile

Returns:
  • (TTFunk::Table::Maxp, nil) -
def maximum_profile
  @maximum_profile ||= TTFunk::Table::Maxp.new(self)
end

def name

Returns:
  • (TTFunk::Table::Name, nil) -
def name
  @name ||= TTFunk::Table::Name.new(self)
end

def os2

Returns:
  • (TTFunk::Table:OS2, nil) -
def os2
  @os2 ||= TTFunk::Table::OS2.new(self)
end

def postscript

Returns:
  • (TTFunk::Table::Post, nil) -
def postscript
  @postscript ||= TTFunk::Table::Post.new(self)
end

def sbix

Returns:
  • (TTFunk::Table::Sbix, nil) -
def sbix
  @sbix ||= TTFunk::Table::Sbix.new(self)
end

def vertical_origins

Returns:
  • (TTFunk::Table::Vorg, nil) -
def vertical_origins
  @vertical_origins ||=
    if directory.tables.include?(TTFunk::Table::Vorg::TAG)
      TTFunk::Table::Vorg.new(self)
    end
end