Class: Fontisan::Commands::SubsetCommand

Inherits:
BaseCommand show all
Defined in:
lib/fontisan/commands/subset_command.rb

Overview

Command for subsetting fonts

This command provides CLI access to font subsetting functionality. It supports multiple input methods for specifying glyphs:

  • Text input: Subset to characters in a text string

  • Glyph IDs: Subset to specific glyph IDs

  • Unicode codepoints: Subset to specific Unicode values

The command also supports various subsetting options:

  • Profile selection (pdf, web, minimal)

  • Glyph ID retention

  • Hint dropping

  • Name dropping

Examples:

Subset to text characters

command = SubsetCommand.new('font.ttf',
  text: 'Hello World',
  output: 'subset.ttf',
  profile: 'pdf'
)
command.run

Subset to specific glyphs

command = SubsetCommand.new('font.ttf',
  glyphs: [0, 1, 65, 66, 67],
  output: 'subset.ttf'
)
command.run

Instance Method Summary collapse

Constructor Details

#initialize(font_path, options = {}) ⇒ SubsetCommand

Initialize subset command

Parameters:

  • font_path (String)

    Path to input font file

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

    Command options

Options Hash (options):

  • :text (String)

    Text to subset

  • :glyphs (Array<Integer>)

    Glyph IDs to subset

  • :unicode (Array<Integer>)

    Unicode codepoints to subset

  • :output (String)

    Output file path (required)

  • :profile (String)

    Subsetting profile (pdf, web, minimal)

  • :retain_gids (Boolean)

    Retain original glyph IDs

  • :drop_hints (Boolean)

    Drop hinting instructions

  • :drop_names (Boolean)

    Drop glyph names

  • :unicode_ranges (Boolean)

    Prune OS/2 Unicode ranges



52
53
54
55
56
# File 'lib/fontisan/commands/subset_command.rb', line 52

def initialize(font_path, options = {})
  super(font_path, options)
  @output_path = options[:output]
  validate_options!
end

Instance Method Details

#runHash

Execute the subset command

Returns:

  • (Hash)

    Result information with output path and glyph count

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fontisan/commands/subset_command.rb', line 63

def run
  # Determine glyph IDs to subset
  glyph_ids = determine_glyph_ids

  # Build subsetting options
  subset_options = build_subset_options

  # Create builder and perform subsetting
  builder = Subset::Builder.new(font, glyph_ids, subset_options)
  subset_binary = builder.build

  # Write output file (create parent directories if needed)
  FileUtils.mkdir_p(File.dirname(@output_path))
  File.binwrite(@output_path, subset_binary)

  # Return result
  {
    input: font_path,
    output: @output_path,
    original_glyphs: font.table("maxp").num_glyphs,
    subset_glyphs: builder.mapping.size,
    profile: subset_options.profile,
    size: subset_binary.bytesize,
  }
rescue Fontisan::SubsettingError => e
  raise Fontisan::SubsettingError,
        "Subsetting failed: #{e.message}"
end