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, codepoints) ⇒ Subsetter

Returns a new instance of Subsetter.

Parameters:

  • ttf (Pdfrb::Font::TrueType::File)

    parsed TTF source.

  • codepoints (Array<Integer>)

    used Unicode codepoints; glyph IDs are resolved via the font's cmap.



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_idsObject (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

#ttfObject (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_mapObject

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

#subsetObject

Returns the subset font bytes.



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

def subset
  @subset ||= build_subset
end