Class: HexaPDF::Font::TrueType::Subsetter
- Inherits:
-
Object
- Object
- HexaPDF::Font::TrueType::Subsetter
- Defined in:
- lib/hexapdf/font/true_type/subsetter.rb
Overview
Subsets a TrueType font in the context of PDF.
TrueType fonts can be embedded into PDF either as a simple font or as a composite font. This subsetter implements the functionality needed when embedding a TrueType subset for a composite font.
This means in particular that the resulting font file cannot be used outside of the PDF.
Instance Method Summary collapse
-
#build_font ⇒ Object
Builds the subset font file and returns it as a binary string.
-
#initialize(font) ⇒ Subsetter
constructor
Creates a new Subsetter for the given TrueType Font object.
-
#subset_glyph_id(glyph_id) ⇒ Object
Returns the new subset glyph ID for the given glyph ID, or
nil
if the glyph isn’t subset. -
#use_glyph(glyph_id) ⇒ Object
Includes the glyph with the given ID in the subset and returns the new subset glyph ID.
Constructor Details
#initialize(font) ⇒ Subsetter
Creates a new Subsetter for the given TrueType Font object.
53 54 55 56 57 |
# File 'lib/hexapdf/font/true_type/subsetter.rb', line 53 def initialize(font) @font = font @glyph_map = {0 => 0} @last_id = 0 end |
Instance Method Details
#build_font ⇒ Object
Builds the subset font file and returns it as a binary string.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/hexapdf/font/true_type/subsetter.rb', line 86 def build_font glyf, locations = build_glyf_table loca = build_loca_table(locations) hmtx = build_hmtx_table head = build_head_table(modified: Time.now, loca_type: 1) hhea = build_hhea_table(@glyph_map.size) maxp = build_maxp_table(@glyph_map.size) tables = { 'head' => head, 'hhea' => hhea, 'maxp' => maxp, 'glyf' => glyf, 'loca' => loca, 'hmtx' => hmtx, } tables['cvt '] = @font[:'cvt '].raw_data if @font[:'cvt '] tables['fpgm'] = @font[:fpgm].raw_data if @font[:fpgm] tables['prep'] = @font[:prep].raw_data if @font[:prep] Builder.build(tables) end |
#subset_glyph_id(glyph_id) ⇒ Object
Returns the new subset glyph ID for the given glyph ID, or nil
if the glyph isn’t subset.
81 82 83 |
# File 'lib/hexapdf/font/true_type/subsetter.rb', line 81 def subset_glyph_id(glyph_id) @glyph_map[glyph_id] end |
#use_glyph(glyph_id) ⇒ Object
Includes the glyph with the given ID in the subset and returns the new subset glyph ID.
Can be called multiple times with the same glyph ID, always returning the correct new subset glyph ID.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/hexapdf/font/true_type/subsetter.rb', line 63 def use_glyph(glyph_id) return @glyph_map[glyph_id] if @glyph_map.key?(glyph_id) @last_id += 1 # Handle codes for ASCII characters \r (13), (, ) (40, 41) and \ (92) specially so that # they never appear in the output (PDF serialization would need to escape them) if @last_id == 13 || @last_id == 40 || @last_id == 92 @glyph_map[:"s#{@last_id}"] = @last_id if @last_id == 40 @last_id += 1 @glyph_map[:"s#{@last_id}"] = @last_id end @last_id += 1 end @glyph_map[glyph_id] = @last_id end |