Class: Pdfrb::Font::TrueType::Subsetter

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

Instance Method Summary collapse

Constructor Details

#initialize(ttf, glyph_ids) ⇒ Subsetter

Returns a new instance of Subsetter.

Parameters:



24
25
26
27
# File 'lib/pdfrb/font/true_type/subsetter.rb', line 24

def initialize(ttf, glyph_ids)
  @ttf = ttf
  @glyph_ids = ([NOTDEF_GID] + glyph_ids).sort.uniq
end

Instance Attribute Details

#glyph_idsObject (readonly)

Returns the value of attribute glyph_ids.



20
21
22
# File 'lib/pdfrb/font/true_type/subsetter.rb', line 20

def glyph_ids
  @glyph_ids
end

#ttfObject (readonly)

Returns the value of attribute ttf.



20
21
22
# File 'lib/pdfrb/font/true_type/subsetter.rb', line 20

def ttf
  @ttf
end

Instance Method Details

#glyph_mapObject

Returns the mapping old_gid → new_gid.



35
36
37
38
39
40
41
# File 'lib/pdfrb/font/true_type/subsetter.rb', line 35

def glyph_map
  @glyph_map ||= begin
    map = {}
    resolved_gids.each_with_index { |old_gid, new_gid| map[old_gid] = new_gid }
    map
  end
end

#subsetObject

Returns the subset font bytes.



30
31
32
# File 'lib/pdfrb/font/true_type/subsetter.rb', line 30

def subset
  @subset ||= build_subset
end