class HexaPDF::Font::TrueType::Font

Represents a font in the TrueType font file format.

Constants

DEFAULT_CONFIG

The default configuration:

font.ttf.table_mapping

The default mapping from table tag as symbol to table class name.

font.ttf.unknown_format

Action to take when encountering unknown subtables. Can either be :ignore which ignores them or :raise which raises an error.

Attributes

config[R]

The configuration for the TrueType font.

io[R]

The IO stream associated with this file.

Public Class Methods

new(io, config: {}) click to toggle source

Creates a new TrueType font file object for the given IO object.

The config hash can contain configuration options.

# File lib/hexapdf/font/true_type/font.rb, line 80
def initialize(io, config: {})
  @io = io
  @config = DEFAULT_CONFIG.merge(config)
  @tables = {}
end

Public Instance Methods

[](tag) click to toggle source

Returns the table instance for the given tag (a symbol), or nil if no such table exists.

# File lib/hexapdf/font/true_type/font.rb, line 87
def [](tag)
  return @tables[tag] if @tables.key?(tag)

  entry = directory.entry(tag.to_s.b)
  entry ? @tables[tag] = table_class(tag).new(self, entry) : nil
end
ascender() click to toggle source

Returns the ascender of the font.

# File lib/hexapdf/font/true_type/font.rb, line 144
def ascender
  self[:"OS/2"].typo_ascender || self[:hhea].ascent
end
bounding_box() click to toggle source

Returns the bounding of the font.

# File lib/hexapdf/font/true_type/font.rb, line 129
def bounding_box
  self[:head].bbox
end
cap_height() click to toggle source

Returns the cap height of the font.

# File lib/hexapdf/font/true_type/font.rb, line 134
def cap_height
  self[:"OS/2"].cap_height
end
descender() click to toggle source

Returns the descender of the font.

# File lib/hexapdf/font/true_type/font.rb, line 149
def descender
  self[:"OS/2"].typo_descender || self[:hhea].descent
end
directory() click to toggle source

Returns the font directory.

# File lib/hexapdf/font/true_type/font.rb, line 95
def directory
  @directory ||= Table::Directory.new(self, io ? Table::Directory::SELF_ENTRY : nil)
end
dominant_vertical_stem_width() click to toggle source

Returns the dominant width of vertical stems.

Note: This attribute does not actually exist in TrueType fonts, so it is estimated based on the weight.

# File lib/hexapdf/font/true_type/font.rb, line 162
def dominant_vertical_stem_width
  weight / 5
end
family_name() click to toggle source

Returns the family name of the font.

# File lib/hexapdf/font/true_type/font.rb, line 119
def family_name
  self[:name][:font_family].preferred_record
end
features() click to toggle source

Returns a set of features this font supports.

Features that may be available are for example :kern or :liga.

# File lib/hexapdf/font/true_type/font.rb, line 102
def features
  @features ||= Set.new.tap do |set|
    set << :kern if self[:kern]&.horizontal_kerning_subtable
  end
end
font_name() click to toggle source

Returns the PostScript font name.

# File lib/hexapdf/font/true_type/font.rb, line 109
def font_name
  self[:name][:postscript_name].preferred_record
end
full_name() click to toggle source

Returns the full name of the font.

# File lib/hexapdf/font/true_type/font.rb, line 114
def full_name
  self[:name][:font_name].preferred_record
end
italic_angle() click to toggle source

Returns the italic angle of the font, in degrees counter-clockwise from the vertical.

# File lib/hexapdf/font/true_type/font.rb, line 154
def italic_angle
  self[:post].italic_angle.to_f
end
missing_glyph_id() click to toggle source

Returns th glyph ID of the missing glyph, i.e. 0.

# File lib/hexapdf/font/true_type/font.rb, line 187
def missing_glyph_id
  0
end
strikeout_position() click to toggle source

Returns the distance from the baseline to the top of the strikeout line.

# File lib/hexapdf/font/true_type/font.rb, line 177
def strikeout_position
  self[:"OS/2"].strikeout_position
end
strikeout_thickness() click to toggle source

Returns the stroke width for the strikeout line.

# File lib/hexapdf/font/true_type/font.rb, line 182
def strikeout_thickness
  self[:"OS/2"].strikeout_size
end
underline_position() click to toggle source

Returns the distance from the baseline to the top of the underline.

# File lib/hexapdf/font/true_type/font.rb, line 167
def underline_position
  self[:post].underline_position
end
underline_thickness() click to toggle source

Returns the stroke width for the underline.

# File lib/hexapdf/font/true_type/font.rb, line 172
def underline_thickness
  self[:post].underline_thickness
end
weight() click to toggle source

Returns the weight of the font.

# File lib/hexapdf/font/true_type/font.rb, line 124
def weight
  self[:"OS/2"].weight_class || 0
end
x_height() click to toggle source

Returns the x-height of the font.

# File lib/hexapdf/font/true_type/font.rb, line 139
def x_height
  self[:"OS/2"].x_height
end

Private Instance Methods

table_class(tag) click to toggle source

Returns the class that is used for handling tables of the given tag.

# File lib/hexapdf/font/true_type/font.rb, line 194
def table_class(tag)
  k = config['font.true_type.table_mapping'].fetch(tag, 'HexaPDF::Font::TrueType::Table')
  ::Object.const_get(k)
end