class TTFunk::File

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

Attributes

contents[R]

Raw content of the font. @return [String]

directory[R]

Font tables directory. @return [TTFunk::Directory]

Public Class Methods

from_dfont(file, which = 0) click to toggle source

Load a font from a resource file.

@param file [String, Pathname] Path to the resource file. @param which [Integer, String] index or name of the font to load @return [TTFunk::File]]

# File lib/ttfunk.rb, line 68
def self.from_dfont(file, which = 0)
  new(ResourceFile.open(file) { |dfont| dfont['sfnt', which] })
end
from_ttc(file, which = 0) click to toggle source

Load a font from a TrueType collection.

@overload from_ttc(io, which = 0)

@param file [IO] IO to read the collection from.
@param which [Integer] index of the font to load
@return [TTFunk::File]

@overload from_ttc(file_path, which = 0)

@param file_path [String, Pathname] Path to the resource file.
@param which [Integer] index of the font to load
@return [TTFunk::File]
# File lib/ttfunk.rb, line 82
def self.from_ttc(file, which = 0)
  Collection.open(file) { |ttc| ttc[which] }
end
new(contents, offset = 0) click to toggle source

@param contents [String] binary string containg the font data @param offset [Integer] offset at which the font data starts

# File lib/ttfunk.rb, line 148
def initialize(contents, offset = 0)
  @contents = StringIO.new(contents)
  @directory = Directory.new(@contents, offset)
end
open(io_or_path) click to toggle source

Open font file

@overload open(io)

@param io [IO] IO to read font content from. IO position and binmode
  might change.
@return [TTFunk::File]

@overload open(path)

@param path [String, Pathname] Path to file to read the font from.
@return [TTFunk::File]
# File lib/ttfunk.rb, line 59
def self.open(io_or_path)
  new(verify_and_read(io_or_path))
end
verify_and_open(io_or_path) click to toggle source

Turn a path or IO into an IO convenient for TTFunk. The resulting IO is going to be in bin mode and its position set to the beginning.

@overload verify_and_open(io)

@param io [IO] IO to prepare. Its position and binmode might
  change.
@return [io]

@overload verify_and_open(path)

@param path [String, Pathname] path of the file to turn into an IO.
@return [IO] newly opened IO for the path

@deprecated This method might retain open files for longer than necessary. @see .verify_and_read

# File lib/ttfunk.rb, line 98
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
verify_and_read(io_or_path) click to toggle source

Read contents of a path or IO.

@overload verify_and_read(io)

@param io [IO] IO to read from. Its position and binmode might
  change. IO is read from the beginning regardless of its initial
  position.
@return [String]

@overload verify_and_read(path)

@param path [String, Pathname] path of the file to read.
@return [String]
# File lib/ttfunk.rb, line 127
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

Public Instance Methods

ascent() click to toggle source

Glyphs ascent as defined for in the font.

@return [Integer]

# File lib/ttfunk.rb, line 156
def ascent
  @ascent ||= (os2.exists? && os2.ascent && os2.ascent.nonzero?) ||
    horizontal_header.ascent
end
bbox() click to toggle source

Glyps bounding box as defined in the font.

@return [Array(Integer, Integer, Integer, Integer)]

# File lib/ttfunk.rb, line 180
def bbox
  [header.x_min, header.y_min, header.x_max, header.y_max]
end
cff() click to toggle source

Compact Font Format (‘CFF `) table

@return [Table::Table::Cff, nil]

# File lib/ttfunk.rb, line 279
def cff
  @cff ||= TTFunk::Table::Cff.new(self)
end
cmap() click to toggle source

Character to Glyph Index Mapping (‘cmap`) table

@return [TTFunk::Tbale::Cmap, nil]

# File lib/ttfunk.rb, line 202
def cmap
  @cmap ||= TTFunk::Table::Cmap.new(self)
end
descent() click to toggle source

Glyphs descent as defined in the font.

@return [Integer]

# File lib/ttfunk.rb, line 164
def descent
  @descent ||= (os2.exists? && os2.descent && os2.descent.nonzero?) ||
    horizontal_header.descent
end
digital_signature() click to toggle source

Digital Signature (‘DSIG`) table

@return [TTFunk::Table::Dsig, nil]

# File lib/ttfunk.rb, line 296
def digital_signature
  @digital_signature ||=
    if directory.tables.include?(TTFunk::Table::Dsig::TAG)
      TTFunk::Table::Dsig.new(self)
    end
end
directory_info(tag) click to toggle source

Font directory entry for the table with the provided tag.

@param tag [String] table tab @return [Hash, nil]

# File lib/ttfunk.rb, line 188
def directory_info(tag)
  directory.tables[tag.to_s]
end
find_glyph(glyph_id) click to toggle source

Find glyph by its index.

@return [TTFunk::Table::Cff::Charstring] if it’s a CFF-based OpenType font @return [TTFunk::Table::Glyf::Simple, TTFunk::Table::Glyf::Compound]

if it's a TrueType font
# File lib/ttfunk.rb, line 308
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
glyph_locations() click to toggle source

Index to Location (‘loca`) table

@return [TTFunk::Table::Loca, nil]

# File lib/ttfunk.rb, line 258
def glyph_locations
  @glyph_locations ||= TTFunk::Table::Loca.new(self)
end
glyph_outlines() click to toggle source

Glyph Data (‘glyf`) table

@return [TTFunk::Table::Glyf, nil]

# File lib/ttfunk.rb, line 265
def glyph_outlines
  @glyph_outlines ||= TTFunk::Table::Glyf.new(self)
end
header() click to toggle source

Font Header (‘head`) table

@return [TTFunk::Table::Head, nil]

# File lib/ttfunk.rb, line 195
def header
  @header ||= TTFunk::Table::Head.new(self)
end
horizontal_header() click to toggle source

Horizontal Header (‘hhea`) table

@return [TTFunk::Table::Hhea, nil]

# File lib/ttfunk.rb, line 209
def horizontal_header
  @horizontal_header ||= TTFunk::Table::Hhea.new(self)
end
horizontal_metrics() click to toggle source

Horizontal Metrics (‘hmtx`) table

@return [TTFunk::Table::Hmtx, nil]

# File lib/ttfunk.rb, line 216
def horizontal_metrics
  @horizontal_metrics ||= TTFunk::Table::Hmtx.new(self)
end
kerning() click to toggle source

Kerning (‘kern`) table

@return [TTFunk::Table::Kern, nil]

# File lib/ttfunk.rb, line 230
def kerning
  @kerning ||= TTFunk::Table::Kern.new(self)
end
line_gap() click to toggle source

Line gap as defined in the font.

@return [Integer]

# File lib/ttfunk.rb, line 172
def line_gap
  @line_gap ||= (os2.exists? && os2.line_gap && os2.line_gap.nonzero?) ||
    horizontal_header.line_gap
end
maximum_profile() click to toggle source

Maximum Profile (‘maxp`) table

@return [TTFunk::Table::Maxp, nil]

# File lib/ttfunk.rb, line 223
def maximum_profile
  @maximum_profile ||= TTFunk::Table::Maxp.new(self)
end
name() click to toggle source

Naming (‘name`) table

@return [TTFunk::Table::Name, nil]

# File lib/ttfunk.rb, line 237
def name
  @name ||= TTFunk::Table::Name.new(self)
end
os2() click to toggle source

OS/2 and Windows Metrics (‘OS/2`) table

@return [TTFunk::Table:OS2, nil]

# File lib/ttfunk.rb, line 244
def os2
  @os2 ||= TTFunk::Table::OS2.new(self)
end
postscript() click to toggle source

PostScript (‘post`) table

@return [TTFunk::Table::Post, nil]

# File lib/ttfunk.rb, line 251
def postscript
  @postscript ||= TTFunk::Table::Post.new(self)
end
sbix() click to toggle source

Standard Bitmap Graphics (‘sbix`) table

@return [TTFunk::Table::Sbix, nil]

# File lib/ttfunk.rb, line 272
def sbix
  @sbix ||= TTFunk::Table::Sbix.new(self)
end
vertical_origins() click to toggle source

Vertical Origin (‘VORG`) table

@return [TTFunk::Table::Vorg, nil]

# File lib/ttfunk.rb, line 286
def vertical_origins
  @vertical_origins ||=
    if directory.tables.include?(TTFunk::Table::Vorg::TAG)
      TTFunk::Table::Vorg.new(self)
    end
end