Class: Fontisan::Subset::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/subset/builder.rb

Overview

Main font subsetting engine

The Builder class orchestrates the entire subsetting process:

  1. Validates input parameters
  2. Calculates glyph closure (including composite dependencies)
  3. Builds glyph ID mapping (old GID → new GID)
  4. Subsets each table according to the selected profile
  5. Assembles the final subset font binary

The subsetting process ensures that .notdef (GID 0) is always included as the first glyph, as required by the OpenType specification.

Reference: docs/ttfunk-feature-analysis.md:455-492

Examples:

Basic subsetting

font = Fontisan::TrueTypeFont.from_file('font.ttf')
builder = Fontisan::Subset::Builder.new(
  font,
  [0, 65, 66, 67],  # .notdef, A, B, C
  Options.new(profile: 'pdf')
)
subset_data = builder.build

Subsetting with retain_gids

options = Options.new(profile: 'pdf', retain_gids: true)
builder = Fontisan::Subset::Builder.new(font, glyph_ids, options)
subset_data = builder.build

Web subsetting with dropped hints

options = Options.new(profile: 'web', drop_hints: true, drop_names: true)
builder = Fontisan::Subset::Builder.new(font, glyph_ids, options)
subset_data = builder.build

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font, glyph_ids, options = {}) ⇒ Builder

Initialize a new subsetting builder

Examples:

builder = Builder.new(font, [0, 65, 66], Options.new(profile: 'pdf'))

Parameters:

  • font (TrueTypeFont, OpenTypeFont)

    Font to subset

  • glyph_ids (Array<Integer>)

    Base glyph IDs to include

  • options (Options, Hash) (defaults to: {})

    Subsetting options

Raises:

  • (ArgumentError)

    If parameters are invalid



68
69
70
71
72
73
74
# File 'lib/fontisan/subset/builder.rb', line 68

def initialize(font, glyph_ids, options = {})
  @font = font
  @glyph_ids = Array(glyph_ids)
  @options = options.is_a?(Options) ? options : Options.new(options)
  @closure = nil
  @mapping = nil
end

Instance Attribute Details

#closureSet<Integer> (readonly)

Complete set of glyph IDs after closure calculation

Returns:

  • (Set<Integer>)


53
54
55
# File 'lib/fontisan/subset/builder.rb', line 53

def closure
  @closure
end

#fontTrueTypeFont, OpenTypeFont (readonly)

Font instance to subset



41
42
43
# File 'lib/fontisan/subset/builder.rb', line 41

def font
  @font
end

#glyph_idsArray<Integer> (readonly)

Base set of glyph IDs requested for subsetting

Returns:

  • (Array<Integer>)


45
46
47
# File 'lib/fontisan/subset/builder.rb', line 45

def glyph_ids
  @glyph_ids
end

#mappingGlyphMapping (readonly)

Glyph ID mapping (old GID → new GID)

Returns:



57
58
59
# File 'lib/fontisan/subset/builder.rb', line 57

def mapping
  @mapping
end

#optionsOptions (readonly)

Subsetting options

Returns:



49
50
51
# File 'lib/fontisan/subset/builder.rb', line 49

def options
  @options
end

Instance Method Details

#buildString

Build the subset font

This is the main entry point that performs the entire subsetting workflow:

  1. Validates all input parameters
  2. Calculates the glyph closure (composite dependencies)
  3. Builds the glyph ID mapping
  4. Subsets all required tables
  5. Assembles the final font binary

Examples:

subset_binary = builder.build
File.binwrite('subset.ttf', subset_binary)

Returns:

  • (String)

    Binary data of the subset font

Raises:



93
94
95
96
97
98
99
# File 'lib/fontisan/subset/builder.rb', line 93

def build
  validate_input!
  calculate_closure
  build_mapping
  tables = subset_tables
  assemble_font(tables)
end