Class: Fontisan::Commands::PackCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/commands/pack_command.rb

Overview

Command for packing multiple fonts into a TTC/OTC collection

This command provides CLI access to font collection creation functionality. It loads multiple font files and combines them into a single TTC (TrueType Collection) or OTC (OpenType Collection) file with shared table deduplication to save space. It also supports creating dfont (Apple Data Fork Font) suitcases.

Examples:

Pack fonts into TTC

command = PackCommand.new(
  ['font1.ttf', 'font2.ttf', 'font3.ttf'],
  output: 'family.ttc',
  format: :ttc,
  optimize: true
)
result = command.run
puts "Saved #{result[:space_savings]} bytes through table sharing"

Pack with analysis

command = PackCommand.new(
  ['Regular.otf', 'Bold.otf', 'Italic.otf'],
  output: 'family.otc',
  format: :otc,
  analyze: true
)
result = command.run

Pack into dfont

command = PackCommand.new(
  ['font1.ttf', 'font2.otf'],
  output: 'family.dfont',
  format: :dfont
)
result = command.run

Instance Method Summary collapse

Constructor Details

#initialize(font_paths, options = {}) ⇒ PackCommand

Initialize pack command

Parameters:

  • font_paths (Array<String>)

    Paths to input font files

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

    Command options

Options Hash (options):

  • :output (String)

    Output file path (required)

  • :format (Symbol, String)

    Format type (:ttc, :otc, or :dfont, default: auto-detect)

  • :optimize (Boolean)

    Enable table sharing optimization (default: true)

  • :analyze (Boolean)

    Show analysis report before building (default: false)

  • :verbose (Boolean)

    Enable verbose output (default: false)

Raises:

  • (ArgumentError)

    if font_paths or output is invalid



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fontisan/commands/pack_command.rb', line 49

def initialize(font_paths, options = {})
  @font_paths = font_paths
  @options = options
  @output_path = options[:output]
  @format = options[:format] ? parse_format(options[:format]) : nil
  @optimize = options.fetch(:optimize, true)
  @analyze = options.fetch(:analyze, false)
  @verbose = options.fetch(:verbose, false)

  validate_options!
end

Instance Method Details

#runHash

Execute the pack command

Loads all fonts, analyzes tables, and creates a TTC/OTC/dfont collection. Optionally displays analysis before building.

Returns:

  • (Hash)

    Result information with:

    • :output [String] - Output file path
    • :output_size [Integer] - Output file size in bytes
    • :num_fonts [Integer] - Number of fonts packed
    • :format [Symbol] - Collection format (:ttc, :otc, or :dfont)
    • :space_savings [Integer] - Bytes saved through sharing (TTC/OTC only)
    • :sharing_percentage [Float] - Percentage of tables shared (TTC/OTC only)
    • :analysis [Hash] - Analysis report (if analyze option enabled)

Raises:

  • (ArgumentError)

    if options are invalid

  • (Fontisan::Error)

    if packing fails



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fontisan/commands/pack_command.rb', line 76

def run
  puts "Loading #{@font_paths.size} fonts..." if @verbose

  # Load all fonts
  fonts = load_fonts

  # Auto-detect format if not specified
  @format ||= auto_detect_format(fonts)
  puts "Auto-detected format: #{@format}" if @verbose && !@options[:format]

  # Build collection based on format
  if @format == :dfont
    build_dfont(fonts)
  else
    build_ttc_otc(fonts)
  end
rescue Fontisan::Error => e
  raise Fontisan::Error, "Collection packing failed: #{e.message}"
rescue StandardError => e
  raise Fontisan::Error, "Unexpected error during packing: #{e.message}"
end