Class: HexaPDF::Font::TrueType::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/font/true_type/font.rb

Overview

Represents a font in the TrueType font file format.

Constant Summary collapse

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.

{
  'font.true_type.table_mapping' => {
    head: 'HexaPDF::Font::TrueType::Table::Head',
    cmap: 'HexaPDF::Font::TrueType::Table::Cmap',
    hhea: 'HexaPDF::Font::TrueType::Table::Hhea',
    hmtx: 'HexaPDF::Font::TrueType::Table::Hmtx',
    loca: 'HexaPDF::Font::TrueType::Table::Loca',
    maxp: 'HexaPDF::Font::TrueType::Table::Maxp',
    name: 'HexaPDF::Font::TrueType::Table::Name',
    post: 'HexaPDF::Font::TrueType::Table::Post',
    glyf: 'HexaPDF::Font::TrueType::Table::Glyf',
    'OS/2': 'HexaPDF::Font::TrueType::Table::OS2',
    kern: 'HexaPDF::Font::TrueType::Table::Kern',
  },
  'font.true_type.unknown_format' => :ignore,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, config: {}) ⇒ Font

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

The config hash can contain configuration options.



82
83
84
85
86
# File 'lib/hexapdf/font/true_type/font.rb', line 82

def initialize(io, config: {})
  @io = io
  @config = DEFAULT_CONFIG.merge(config)
  @tables = {}
end

Instance Attribute Details

#configObject (readonly)

The configuration for the TrueType font.



77
78
79
# File 'lib/hexapdf/font/true_type/font.rb', line 77

def config
  @config
end

#ioObject (readonly)

The IO stream associated with this file.



74
75
76
# File 'lib/hexapdf/font/true_type/font.rb', line 74

def io
  @io
end

Instance Method Details

#[](tag) ⇒ Object

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



101
102
103
104
105
106
# File 'lib/hexapdf/font/true_type/font.rb', line 101

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

#ascenderObject

Returns the ascender of the font.



158
159
160
# File 'lib/hexapdf/font/true_type/font.rb', line 158

def ascender
  self[:'OS/2'].typo_ascender || self[:hhea].ascent
end

#bounding_boxObject

Returns the bounding of the font.



143
144
145
# File 'lib/hexapdf/font/true_type/font.rb', line 143

def bounding_box
  self[:head].bbox
end

#build(table_overrides = {}) ⇒ Object

Uses Builder to build a font file for this font.

The table_overrides argument can be used to supply mappings from table names (in string form) to raw table data that should override the respective font’s tables.



92
93
94
95
96
97
98
# File 'lib/hexapdf/font/true_type/font.rb', line 92

def build(table_overrides = {})
  tables = directory.table_names.each_with_object({}) do |name, hash|
    hash[name] = self[name.to_sym].raw_data
  end
  tables.merge!(table_overrides)
  Builder.build(tables)
end

#cap_heightObject

Returns the cap height of the font.



148
149
150
# File 'lib/hexapdf/font/true_type/font.rb', line 148

def cap_height
  self[:'OS/2'].cap_height
end

#descenderObject

Returns the descender of the font.



163
164
165
# File 'lib/hexapdf/font/true_type/font.rb', line 163

def descender
  self[:'OS/2'].typo_descender || self[:hhea].descent
end

#directoryObject

Returns the font directory.



109
110
111
# File 'lib/hexapdf/font/true_type/font.rb', line 109

def directory
  @directory ||= Table::Directory.new(self, io ? Table::Directory::SELF_ENTRY : nil)
end

#dominant_vertical_stem_widthObject

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.



176
177
178
# File 'lib/hexapdf/font/true_type/font.rb', line 176

def dominant_vertical_stem_width
  weight / 5
end

#family_nameObject

Returns the family name of the font.



133
134
135
# File 'lib/hexapdf/font/true_type/font.rb', line 133

def family_name
  self[:name][:font_family].preferred_record
end

#featuresObject

Returns a set of features this font supports.

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



116
117
118
119
120
# File 'lib/hexapdf/font/true_type/font.rb', line 116

def features
  @features ||= Set.new.tap do |set|
    set << :kern if self[:kern]&.horizontal_kerning_subtable
  end
end

#font_nameObject

Returns the PostScript font name.



123
124
125
# File 'lib/hexapdf/font/true_type/font.rb', line 123

def font_name
  self[:name][:postscript_name].preferred_record
end

#full_nameObject

Returns the full name of the font.



128
129
130
# File 'lib/hexapdf/font/true_type/font.rb', line 128

def full_name
  self[:name][:font_name].preferred_record
end

#italic_angleObject

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



168
169
170
# File 'lib/hexapdf/font/true_type/font.rb', line 168

def italic_angle
  self[:post].italic_angle.to_f
end

#missing_glyph_idObject

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



201
202
203
# File 'lib/hexapdf/font/true_type/font.rb', line 201

def missing_glyph_id
  0
end

#strikeout_positionObject

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



191
192
193
# File 'lib/hexapdf/font/true_type/font.rb', line 191

def strikeout_position
  self[:'OS/2'].strikeout_position
end

#strikeout_thicknessObject

Returns the stroke width for the strikeout line.



196
197
198
# File 'lib/hexapdf/font/true_type/font.rb', line 196

def strikeout_thickness
  self[:'OS/2'].strikeout_size
end

#underline_positionObject

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



181
182
183
# File 'lib/hexapdf/font/true_type/font.rb', line 181

def underline_position
  self[:post].underline_position
end

#underline_thicknessObject

Returns the stroke width for the underline.



186
187
188
# File 'lib/hexapdf/font/true_type/font.rb', line 186

def underline_thickness
  self[:post].underline_thickness
end

#weightObject

Returns the weight of the font.



138
139
140
# File 'lib/hexapdf/font/true_type/font.rb', line 138

def weight
  self[:'OS/2'].weight_class || 0
end

#x_heightObject

Returns the x-height of the font.



153
154
155
# File 'lib/hexapdf/font/true_type/font.rb', line 153

def x_height
  self[:'OS/2'].x_height
end