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.

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

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 or :otc, default: :ttc)

  • :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



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fontisan/commands/pack_command.rb', line 44

def initialize(font_paths, options = {})
  @font_paths = font_paths
  @options = options
  @output_path = options[:output]
  @format = parse_format(options[:format] || :ttc)
  @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 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 or :otc)

    • :space_savings [Integer] - Bytes saved through sharing

    • :sharing_percentage [Float] - Percentage of tables shared

    • :analysis [Hash] - Analysis report (if analyze option enabled)

Raises:

  • (ArgumentError)

    if options are invalid

  • (Fontisan::Error)

    if packing fails



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fontisan/commands/pack_command.rb', line 71

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

  # Load all fonts
  fonts = load_fonts

  # Create builder
  builder = Collection::Builder.new(fonts, {
                                      format: @format,
                                      optimize: @optimize,
                                    })

  # Validate before building
  builder.validate!

  # Show analysis if requested
  if @analyze || @verbose
    show_analysis(builder)
  end

  # Build collection
  puts "Building #{@format.upcase} collection..." if @verbose
  result = builder.build_to_file(@output_path)

  # Display results
  if @verbose
    display_results(result)
  end

  result
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