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

Inherits:
Object
  • Object
show all
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

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_fontObject

Builds the subset font file and returns it as a binary string.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hexapdf/font/true_type/subsetter.rb', line 76

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.



71
72
73
# File 'lib/hexapdf/font/true_type/subsetter.rb', line 71

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
# 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
  @glyph_map[glyph_id] = @last_id
end