Class: Fontisan::Commands::UnpackCommand

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

Overview

Command for unpacking fonts from TTC/OTC collections

This command extracts individual font files from a TTC (TrueType Collection) or OTC (OpenType Collection) file. It can extract all fonts or a specific font by index, optionally converting to different formats during extraction.

Examples:

Extract all fonts

command = UnpackCommand.new(
  'family.ttc',
  output_dir: 'fonts/',
  format: :ttf
)
result = command.run
puts "Extracted #{result[:fonts_extracted]} fonts"

Extract specific font

command = UnpackCommand.new(
  'family.ttc',
  output_dir: 'fonts/',
  font_index: 2
)
result = command.run

Instance Method Summary collapse

Constructor Details

#initialize(collection_path, options = {}) ⇒ UnpackCommand

Initialize unpack command

Parameters:

  • collection_path (String)

    Path to TTC/OTC file

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

    Command options

Options Hash (options):

  • :output_dir (String)

    Output directory (required)

  • :font_index (Integer)

    Extract specific font index (optional)

  • :format (Symbol, String)

    Output format (ttf, otf, woff, woff2)

  • :prefix (String)

    Filename prefix for extracted fonts

  • :verbose (Boolean)

    Enable verbose output (default: false)

Raises:

  • (ArgumentError)

    if collection_path or output_dir is invalid



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

def initialize(collection_path, options = {})
  @collection_path = collection_path
  @options = options
  @output_dir = options[:output_dir]
  @font_index = options[:font_index]
  @format = parse_format(options[:format])
  @prefix = options[:prefix]
  @verbose = options.fetch(:verbose, false)

  validate_options!
end

Instance Method Details

#runHash

Execute the unpack command

Extracts fonts from the collection and writes them as individual files.

Returns:

  • (Hash)

    Result information with:

    • :collection [String] - Input collection path

    • :output_dir [String] - Output directory

    • :num_fonts [Integer] - Total fonts in collection

    • :fonts_extracted [Integer] - Number of fonts extracted

    • :extracted_files [Array<String>] - Paths to extracted files

Raises:



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
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fontisan/commands/unpack_command.rb', line 66

def run
  puts "Loading collection from #{File.basename(@collection_path)}..." if @verbose

  # Load collection
  collection = load_collection

  # Create output directory
  FileUtils.mkdir_p(@output_dir) unless Dir.exist?(@output_dir)

  # Determine which fonts to extract
  indices_to_extract = determine_indices(collection)

  puts "Extracting #{indices_to_extract.size} font(s)..." if @verbose

  # Extract fonts
  extracted_files = extract_fonts(collection, indices_to_extract)

  # Display results
  if @verbose
    display_results(collection, extracted_files)
  end

  {
    collection: @collection_path,
    output_dir: @output_dir,
    num_fonts: collection.font_count,
    fonts_extracted: extracted_files.size,
    extracted_files: extracted_files,
  }
rescue Fontisan::Error => e
  raise Fontisan::Error, "Collection unpacking failed: #{e.message}"
rescue ArgumentError
  # Let ArgumentError propagate for validation errors
  raise
rescue StandardError => e
  raise Fontisan::Error, "Unexpected error during unpacking: #{e.message}"
end