Class: Pdfrb::Font::TrueType::Subsetter
- Inherits:
-
Object
- Object
- Pdfrb::Font::TrueType::Subsetter
- Defined in:
- lib/pdfrb/font/true_type/subsetter.rb
Overview
Glyph subsetting for embedded TrueType fonts. Given a set of used glyph IDs, produces a new TTF with only those glyphs.
Rewrites: glyf, loca, cmap, hmtx, hhea, maxp, head. Handles composite glyphs (diacritics) by recursively including referenced component glyphs.
Falls back to returning the full font if subsetting fails.
Constant Summary collapse
- NOTDEF_GID =
0- SFNT_VERSION =
[0x00010000].freeze
Instance Attribute Summary collapse
-
#glyph_ids ⇒ Object
readonly
Returns the value of attribute glyph_ids.
-
#ttf ⇒ Object
readonly
Returns the value of attribute ttf.
Instance Method Summary collapse
-
#glyph_map ⇒ Object
Returns the mapping old_gid → new_gid.
-
#initialize(ttf, codepoints) ⇒ Subsetter
constructor
A new instance of Subsetter.
-
#subset ⇒ Object
Returns the subset font bytes.
Constructor Details
#initialize(ttf, codepoints) ⇒ Subsetter
Returns a new instance of Subsetter.
23 24 25 26 27 28 |
# File 'lib/pdfrb/font/true_type/subsetter.rb', line 23 def initialize(ttf, codepoints) @ttf = ttf @codepoint_gids = codepoints.map { |cp| [cp, ttf.cmap.glyph_id_for(cp)] } .reject { |_cp, gid| gid.nil? || gid.zero? } @glyph_ids = ([NOTDEF_GID] + @codepoint_gids.map(&:last)).sort.uniq end |
Instance Attribute Details
#glyph_ids ⇒ Object (readonly)
Returns the value of attribute glyph_ids.
18 19 20 |
# File 'lib/pdfrb/font/true_type/subsetter.rb', line 18 def glyph_ids @glyph_ids end |
#ttf ⇒ Object (readonly)
Returns the value of attribute ttf.
18 19 20 |
# File 'lib/pdfrb/font/true_type/subsetter.rb', line 18 def ttf @ttf end |
Instance Method Details
#glyph_map ⇒ Object
Returns the mapping old_gid → new_gid.
36 37 38 39 40 41 42 |
# File 'lib/pdfrb/font/true_type/subsetter.rb', line 36 def glyph_map @glyph_map ||= begin map = {} resolved_gids.each_with_index { |old_gid, new_gid| map[old_gid] = new_gid } map end end |
#subset ⇒ Object
Returns the subset font bytes.
31 32 33 |
# File 'lib/pdfrb/font/true_type/subsetter.rb', line 31 def subset @subset ||= build_subset end |