Class: Fontisan::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/fontisan/cli.rb

Overview

Command-line interface for Fontisan.

This class provides the Thor-based CLI with commands for extracting font information and listing tables. It supports multiple output formats (text, YAML, JSON) and various options for controlling output behavior.

Examples:

Run the info command

Fontisan::Cli.start(['info', 'font.ttf'])

Instance Method Summary collapse

Instance Method Details

#audit(font_file = nil) ⇒ Object

Produce a structured font audit report (YAML or JSON).



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fontisan/cli.rb', line 51

def audit(font_file = nil)
  return list_audit_profiles if options[:list_profiles]

  raise Thor::Error, "FONT_FILE is required" if font_file.nil?

  cmd_options = options.dup
  cmd_options[:include_codepoints] = !options[:no_codepoints]
  cmd_options[:validate] = parse_validate_option(options[:validate])
  command = Commands::AuditCommand.new(font_file, cmd_options)
  result = command.run
  write_audit_result(result, options[:output])
  exit_on_validation_errors(result) if options[:validate] && options[:fail_on_error]
rescue Errno::ENOENT
  warn "File not found: #{font_file}" unless options[:quiet]
  exit 1
end

#convert(font_file) ⇒ Object

Convert a font to a different format using the universal transformation pipeline.

Supported conversions:

  • TTF ↔ OTF: Outline format conversion
  • Type 1 ↔ TTF/OTF: Adobe Type 1 font conversion
  • WOFF/WOFF2: Web font packaging
  • Variable fonts: Automatic variation preservation or instance generation
  • Collections (TTC/OTC/dfont): Preserve mixed TTF+OTF by default, or standardize with --target-format

Web fonts — WOFF vs WOFF2: WOFF uses zlib compression and works on every browser that supports web fonts at all (IE9+, all evergreen browsers). WOFF2 uses Brotli and is ~30% smaller but requires modern browsers (Chrome 36+, Firefox 39+, Safari 12+, Edge 14+). Old browsers cannot decode Brotli, so for legacy support serve WOFF. Tune knobs with --zlib-level / --brotli-quality / --uncompressed (WOFF only) / --transform-tables (WOFF2 only).

Collection Format Support: TTC, OTC, and dfont all support mixed TrueType and OpenType fonts. By default, original font formats are preserved during collection conversion (--target-format preserve). Use --target-format ttf to convert all fonts to TrueType, or --target-format otf to convert all to OpenType/CFF.

Variable Font Operations: The pipeline automatically detects whether variation data can be preserved based on source and target formats. For same outline family (TTF→WOFF or OTF→WOFF2), variation is preserved automatically. For cross-family conversions (TTF↔OTF), an instance is generated unless --preserve-variation is explicitly set.

Instance Generation: Use --coordinates to specify exact axis values (e.g., wght=700,wdth=100) or --instance-index to use a named instance. Individual axis options (--wght, --wdth) are also supported for convenience.

Examples:

Convert TTF to OTF

fontisan convert font.ttf --to otf --output font.otf

Convert Type 1 to OTF

fontisan convert font.pfb --to otf --output font.otf

Convert OTF to Type 1

fontisan convert font.otf --to type1 --output font.pfb

Convert TTC to OTC (preserves mixed formats by default)

fontisan convert family.ttc --to otc --output family.otc

Convert TTC to OTC with all fonts as TrueType

fontisan convert family.ttc --to otc --output family.otc --target-format ttf

Convert TTC to OTC with all fonts as OpenType/CFF

fontisan convert family.ttc --to otc --output family.otc --target-format otf

Generate bold instance at specific coordinates

fontisan convert variable.ttf --to ttf --output bold.ttf --coordinates "wght=700,wdth=100"

Generate bold instance using individual axis options

fontisan convert variable.ttf --to ttf --output bold.ttf --wght 700

Use named instance

fontisan convert variable.ttf --to woff2 --output bold.woff2 --instance-index 0

Force variation preservation (if compatible)

fontisan convert variable.ttf --to woff2 --output variable.woff2 --preserve-variation

Convert without validation

fontisan convert font.ttf --to otf --output font.otf --no-validate

Use named preset

fontisan convert font.pfb --to otf --output font.otf --preset type1_to_modern

Show recommended options for conversion

fontisan convert font.ttf --to otf --show-options

Convert with custom options

fontisan convert font.ttf --to otf --output font.otf --autohint --hinting-mode auto

Parameters:

  • font_file (String)

    Path to the font file



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/fontisan/cli.rb', line 391

def convert(font_file)
  # Detect source format from file
  source_format = detect_source_format(font_file)

  # Handle --show-options
  if options[:show_options]
    show_recommended_options(source_format,
                             options[:to].is_a?(Array) ? options[:to].first : options[:to])
    return
  end

  # Validate output is provided when not using --show-options
  unless options[:output]
    raise Thor::Error, "Output path is required. Use --output option."
  end

  # Build ConversionOptions
  conv_options = build_conversion_options(source_format,
                                          Array(options[:to]).first,
                                          options)

  # Build instance coordinates from axis options
  instance_coords = build_instance_coordinates(options)

  # Merge coordinates and ConversionOptions into convert_options
  convert_options = options.to_h.dup
  if instance_coords.any?
    convert_options[:instance_coordinates] = instance_coords
  end

  # Add ConversionOptions if built
  convert_options[:options] = conv_options if conv_options

  command = Commands::ConvertCommand.new(font_file, convert_options)
  command.run
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#dump_table(font_file, table_tag) ⇒ Object

Dump raw binary table data to stdout.

Parameters:

  • font_file (String)

    Path to the font file

  • table_tag (String)

    Four-character table tag (e.g., 'name', 'head')



485
486
487
488
489
490
491
492
493
494
# File 'lib/fontisan/cli.rb', line 485

def dump_table(font_file, table_tag)
  command = Commands::DumpTableCommand.new(font_file, table_tag, options)
  raw_data = command.run

  # Write binary data directly to stdout
  $stdout.binmode
  $stdout.write(raw_data)
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#export(font_file) ⇒ Object



600
601
602
603
604
605
606
607
608
609
# File 'lib/fontisan/cli.rb', line 600

def export(font_file)
  command = Commands::ExportCommand.new(
    font_file,
    output: options[:output],
    format: options[:format].to_sym,
    tables: options[:tables],
    binary_format: options[:binary_format].to_sym,
  )
  exit command.run
end

#features(font_file) ⇒ Object

List OpenType features available for scripts. If no script is specified, shows features for all scripts.

Parameters:

  • font_file (String)

    Path to the font file



189
190
191
192
193
194
195
# File 'lib/fontisan/cli.rb', line 189

def features(font_file)
  command = Commands::FeaturesCommand.new(font_file, options)
  result = command.run
  output_result(result)
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#glyphs(font_file) ⇒ Object

List glyph names from the font file.

Parameters:

  • font_file (String)

    Path to the font file



125
126
127
128
129
130
131
# File 'lib/fontisan/cli.rb', line 125

def glyphs(font_file)
  command = Commands::GlyphsCommand.new(font_file, options)
  result = command.run
  output_result(result)
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#info(path) ⇒ Object

Extract and display comprehensive font metadata.

Parameters:

  • path (String)

    Path to the font file or collection



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fontisan/cli.rb', line 75

def info(path)
  command = Commands::InfoCommand.new(path, options)
  info = command.run
  output_result(info) unless options[:quiet]
rescue Errno::ENOENT
  if options[:verbose]
    raise
  else
    warn "File not found: #{path}" unless options[:quiet]
    exit 1
  end
end

#instance(font_file) ⇒ Object

Generate static font instance from variable font.

You can specify axis coordinates using --wght, --wdth, etc., or use a predefined named instance with --named-instance. Use --list-instances to see available named instances.

Examples:

Generate bold instance

fontisan instance variable.ttf --wght=700 --output=bold.ttf

Use named instance

fontisan instance variable.ttf --named-instance="Bold" --output=bold.ttf

Instance with format conversion

fontisan instance variable.ttf --wght=700 --to=woff2 --output=bold.woff2

List available instances

fontisan instance variable.ttf --list-instances

Parameters:

  • font_file (String)

    Path to the variable font file



473
474
475
476
477
478
# File 'lib/fontisan/cli.rb', line 473

def instance(font_file)
  command = Commands::InstanceCommand.new
  command.execute(font_file, options)
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#ls(file) ⇒ Object

List contents of font files with auto-detection.

For collections (TTC/OTC): Lists all fonts in the collection For individual fonts (TTF/OTF): Shows quick font summary

Examples:

List fonts in collection

fontisan ls fonts.ttc

Show font summary

fontisan ls font.ttf

Parameters:

  • file (String)

    Path to the font or collection file



101
102
103
104
105
106
107
# File 'lib/fontisan/cli.rb', line 101

def ls(file)
  command = Commands::LsCommand.new(file, options)
  result = command.run
  output_result(result)
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#optical_size(font_file) ⇒ Object

Display optical size information from the font file.

Parameters:

  • font_file (String)

    Path to the font file



161
162
163
164
165
166
167
# File 'lib/fontisan/cli.rb', line 161

def optical_size(font_file)
  command = Commands::OpticalSizeCommand.new(font_file, options)
  result = command.run
  output_result(result)
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#pack(*font_files) ⇒ Object

Create a TTC (TrueType Collection), OTC (OpenType Collection), or dfont (Apple Data Fork Font) from multiple font files.

This command combines multiple fonts into a single collection file with shared table deduplication to save space. It supports TTC, OTC, and dfont formats.

Examples:

Pack fonts into TTC

fontisan pack font1.ttf font2.ttf font3.ttf --output family.ttc

Pack into OTC with analysis

fontisan pack Regular.otf Bold.otf Italic.otf --output family.otc --analyze

Pack into dfont (Apple suitcase)

fontisan pack font1.ttf font2.otf --output family.dfont --format dfont

Pack without optimization

fontisan pack font1.ttf font2.ttf --output collection.ttc --no-optimize

Parameters:

  • font_files (Array<String>)

    Paths to input font files (minimum 2 required)



718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'lib/fontisan/cli.rb', line 718

def pack(*font_files)
  command = Commands::PackCommand.new(font_files, options)
  result = command.run

  unless options[:quiet]
    puts "Collection created successfully:"
    puts "  Output: #{result[:output_path] || result[:output]}"
    puts "  Format: #{result[:format].upcase}"
    puts "  Fonts: #{result[:num_fonts]}"
    puts "  Size: #{format_size(result[:output_size] || result[:total_size])}"
    if result[:space_savings]&.positive?
      puts "  Space saved: #{format_size(result[:space_savings])}"
      puts "  Sharing: #{result[:statistics][:sharing_percentage]}%"
    end
  end
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#scripts(font_file) ⇒ Object

List all scripts supported by the font from GSUB and GPOS tables.

Parameters:

  • font_file (String)

    Path to the font file



173
174
175
176
177
178
179
# File 'lib/fontisan/cli.rb', line 173

def scripts(font_file)
  command = Commands::ScriptsCommand.new(font_file, options)
  result = command.run
  output_result(result)
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#subset(font_file) ⇒ Object

Subset a font to specific glyphs.

You must specify one of --text, --glyphs, or --unicode to define which glyphs to include in the subset.

Parameters:

  • font_file (String)

    Path to the font file



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/fontisan/cli.rb', line 227

def subset(font_file)
  command = Commands::SubsetCommand.new(font_file, options)
  result = command.run

  unless options[:quiet]
    puts "Subset font created:"
    puts "  Input: #{result[:input]}"
    puts "  Output: #{result[:output]}"
    puts "  Original glyphs: #{result[:original_glyphs]}"
    puts "  Subset glyphs: #{result[:subset_glyphs]}"
    puts "  Profile: #{result[:profile]}"
    puts "  Size: #{format_size(result[:size])}"
  end
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#tables(font_file) ⇒ Object

List all OpenType tables in the font file.

Parameters:

  • font_file (String)

    Path to the font file



113
114
115
116
117
118
119
# File 'lib/fontisan/cli.rb', line 113

def tables(font_file)
  command = Commands::TablesCommand.new(font_file, options)
  result = command.run
  output_result(result)
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#unicode(font_file) ⇒ Object

List Unicode to glyph index mappings from the font file.

Parameters:

  • font_file (String)

    Path to the font file



137
138
139
140
141
142
143
# File 'lib/fontisan/cli.rb', line 137

def unicode(font_file)
  command = Commands::UnicodeCommand.new(font_file, options)
  result = command.run
  output_result(result)
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#unpack(font_file) ⇒ Object

Extract individual fonts from a TTC (TrueType Collection) or OTC (OpenType Collection) file.

This command unpacks fonts from collection files, optionally converting them to different formats during extraction.

Examples:

Extract all fonts to directory

fontisan unpack family.ttc --output-dir extracted/

Extract specific font by index

fontisan unpack family.ttc --output-dir extracted/ --font-index 0

Extract with format conversion

fontisan unpack family.ttc --output-dir extracted/ --format woff2

Extract with custom prefix

fontisan unpack family.ttc --output-dir extracted/ --prefix "NotoSans"

Parameters:

  • font_file (String)

    Path to the TTC/OTC collection file



669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/fontisan/cli.rb', line 669

def unpack(font_file)
  command = Commands::UnpackCommand.new(font_file, options)
  result = command.run

  unless options[:quiet]
    puts "Collection unpacked successfully:"
    puts "  Input: #{result[:collection]}"
    puts "  Output directory: #{result[:output_dir]}"
    puts "  Fonts extracted: #{result[:fonts_extracted]}/#{result[:num_fonts]}"
    result[:extracted_files].each do |file|
      size = File.size(file)
      puts "  - #{File.basename(file)} (#{format_size(size)})"
    end
  end
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#validate(*font_files) ⇒ Object



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/fontisan/cli.rb', line 541

def validate(*font_files)
  if options[:list]
    list_available_tests
    return
  end

  # Validate argument count
  if font_files.empty?
    raise(Thor::Error, "FONT_FILE is required unless using --list")
  elsif font_files.size > 1
    raise(Thor::Error,
          "Too many arguments. validate accepts only one font file.\n" \
          "To validate multiple files, run validate separately for each.\n" \
          "To validate all fonts in a collection, use: fontisan validate collection.ttc")
  end

  font_file = font_files.first

  cmd = Commands::ValidateCommand.new(
    input: font_file,
    profile: options[:profile],
    exclude: options[:exclude] || [],
    output: options[:output],
    format: options[:format].to_sym,
    full_report: options[:full_report],
    summary_report: options[:summary_report],
    table_report: options[:table_report],
    verbose: options[:verbose],
    suppress_warnings: options[:suppress_warnings],
    return_value_results: options[:return_value_results],
  )

  exit cmd.run
rescue Thor::Error => e
  unless options[:quiet]
    warn "ERROR: #{e.message}"
    warn
    help("validate")
  end
  exit 1
rescue StandardError => e
  error "Validation failed: #{e.message}"
  exit 1
end

#validate_collection(path) ⇒ Object



619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/fontisan/cli.rb', line 619

def validate_collection(path)
  cmd = Commands::ValidateCollectionCommand.new(
    input: path,
    expected_faces: options[:expected_faces],
    max_glyphs: options[:max_glyphs],
    expected_cmap_union: options[:expected_cmap_union],
  )
  exit cmd.run
rescue ArgumentError => e
  warn "ERROR: #{e.message}"
  exit 1
end

#variable(font_file) ⇒ Object

Display variable font variation axes and instances.

Parameters:

  • font_file (String)

    Path to the font file



149
150
151
152
153
154
155
# File 'lib/fontisan/cli.rb', line 149

def variable(font_file)
  command = Commands::VariableCommand.new(font_file, options)
  result = command.run
  output_result(result)
rescue Errno::ENOENT, Error => e
  handle_error(e)
end

#versionObject

Display the Fontisan version.



634
635
636
# File 'lib/fontisan/cli.rb', line 634

def version
  puts "Fontisan version #{Fontisan::VERSION}"
end