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.



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

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

Instance Attribute Details

#configObject (readonly)

The configuration for the TrueType font.



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

def config
  @config
end

#ioObject (readonly)

The IO stream associated with this file.



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

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.



88
89
90
91
92
93
# File 'lib/hexapdf/font/true_type/font.rb', line 88

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.



145
146
147
# File 'lib/hexapdf/font/true_type/font.rb', line 145

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

#bounding_boxObject

Returns the bounding of the font.



130
131
132
# File 'lib/hexapdf/font/true_type/font.rb', line 130

def bounding_box
  self[:head].bbox
end

#cap_heightObject

Returns the cap height of the font.



135
136
137
# File 'lib/hexapdf/font/true_type/font.rb', line 135

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

#descenderObject

Returns the descender of the font.



150
151
152
# File 'lib/hexapdf/font/true_type/font.rb', line 150

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

#directoryObject

Returns the font directory.



96
97
98
# File 'lib/hexapdf/font/true_type/font.rb', line 96

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.



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

def dominant_vertical_stem_width
  weight / 5
end

#family_nameObject

Returns the family name of the font.



120
121
122
# File 'lib/hexapdf/font/true_type/font.rb', line 120

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.



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

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.



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

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

#full_nameObject

Returns the full name of the font.



115
116
117
# File 'lib/hexapdf/font/true_type/font.rb', line 115

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.



155
156
157
# File 'lib/hexapdf/font/true_type/font.rb', line 155

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

#missing_glyph_idObject

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



188
189
190
# File 'lib/hexapdf/font/true_type/font.rb', line 188

def missing_glyph_id
  0
end

#strikeout_positionObject

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



178
179
180
# File 'lib/hexapdf/font/true_type/font.rb', line 178

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

#strikeout_thicknessObject

Returns the stroke width for the strikeout line.



183
184
185
# File 'lib/hexapdf/font/true_type/font.rb', line 183

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

#underline_positionObject

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



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

def underline_position
  self[:post].underline_position
end

#underline_thicknessObject

Returns the stroke width for the underline.



173
174
175
# File 'lib/hexapdf/font/true_type/font.rb', line 173

def underline_thickness
  self[:post].underline_thickness
end

#weightObject

Returns the weight of the font.



125
126
127
# File 'lib/hexapdf/font/true_type/font.rb', line 125

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

#x_heightObject

Returns the x-height of the font.



140
141
142
# File 'lib/hexapdf/font/true_type/font.rb', line 140

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