Class: Fontisan::Cli
- Inherits:
-
Thor
- Object
- Thor
- Fontisan::Cli
- 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.
Instance Method Summary collapse
-
#audit(font_file) ⇒ Object
Produce a structured font audit report (YAML or JSON).
-
#convert(font_file) ⇒ Object
Convert a font to a different format using the universal transformation pipeline.
-
#dump_table(font_file, table_tag) ⇒ Object
Dump raw binary table data to stdout.
- #export(font_file) ⇒ Object
-
#features(font_file) ⇒ Object
List OpenType features available for scripts.
-
#glyphs(font_file) ⇒ Object
List glyph names from the font file.
-
#info(path) ⇒ Object
Extract and display comprehensive font metadata.
-
#instance(font_file) ⇒ Object
Generate static font instance from variable font.
-
#ls(file) ⇒ Object
List contents of font files with auto-detection.
-
#optical_size(font_file) ⇒ Object
Display optical size information from the font file.
-
#pack(*font_files) ⇒ Object
Create a TTC (TrueType Collection), OTC (OpenType Collection), or dfont (Apple Data Fork Font) from multiple font files.
-
#scripts(font_file) ⇒ Object
List all scripts supported by the font from GSUB and GPOS tables.
-
#subset(font_file) ⇒ Object
Subset a font to specific glyphs.
-
#tables(font_file) ⇒ Object
List all OpenType tables in the font file.
-
#unicode(font_file) ⇒ Object
List Unicode to glyph index mappings from the font file.
-
#unpack(font_file) ⇒ Object
Extract individual fonts from a TTC (TrueType Collection) or OTC (OpenType Collection) file.
- #validate(*font_files) ⇒ Object
- #validate_collection(path) ⇒ Object
-
#variable(font_file) ⇒ Object
Display variable font variation axes and instances.
-
#version ⇒ Object
Display the Fontisan version.
Instance Method Details
#audit(font_file) ⇒ Object
Produce a structured font audit report (YAML or JSON).
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/fontisan/cli.rb', line 42 def audit(font_file) = .dup [:include_codepoints] = ![:no_codepoints] command = Commands::AuditCommand.new(font_file, ) result = command.run write_audit_result(result, [:output]) rescue Errno::ENOENT warn "File not found: #{font_file}" unless [: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.
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/fontisan/cli.rb', line 376 def convert(font_file) # Detect source format from file source_format = detect_source_format(font_file) # Handle --show-options if [:show_options] (source_format, [:to].is_a?(Array) ? [:to].first : [:to]) return end # Validate output is provided when not using --show-options unless [:output] raise Thor::Error, "Output path is required. Use --output option." end # Build ConversionOptions = (source_format, Array([:to]).first, ) # Build instance coordinates from axis options instance_coords = build_instance_coordinates() # Merge coordinates and ConversionOptions into convert_options = .to_h.dup if instance_coords.any? [:instance_coordinates] = instance_coords end # Add ConversionOptions if built [:options] = if command = Commands::ConvertCommand.new(font_file, ) 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.
470 471 472 473 474 475 476 477 478 479 |
# File 'lib/fontisan/cli.rb', line 470 def dump_table(font_file, table_tag) command = Commands::DumpTableCommand.new(font_file, table_tag, ) 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
585 586 587 588 589 590 591 592 593 594 |
# File 'lib/fontisan/cli.rb', line 585 def export(font_file) command = Commands::ExportCommand.new( font_file, output: [:output], format: [:format].to_sym, tables: [:tables], binary_format: [: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.
174 175 176 177 178 179 180 |
# File 'lib/fontisan/cli.rb', line 174 def features(font_file) command = Commands::FeaturesCommand.new(font_file, ) 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.
110 111 112 113 114 115 116 |
# File 'lib/fontisan/cli.rb', line 110 def glyphs(font_file) command = Commands::GlyphsCommand.new(font_file, ) result = command.run output_result(result) rescue Errno::ENOENT, Error => e handle_error(e) end |
#info(path) ⇒ Object
Extract and display comprehensive font metadata.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fontisan/cli.rb', line 60 def info(path) command = Commands::InfoCommand.new(path, ) info = command.run output_result(info) unless [:quiet] rescue Errno::ENOENT if [:verbose] raise else warn "File not found: #{path}" unless [: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.
458 459 460 461 462 463 |
# File 'lib/fontisan/cli.rb', line 458 def instance(font_file) command = Commands::InstanceCommand.new command.execute(font_file, ) 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
86 87 88 89 90 91 92 |
# File 'lib/fontisan/cli.rb', line 86 def ls(file) command = Commands::LsCommand.new(file, ) 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.
146 147 148 149 150 151 152 |
# File 'lib/fontisan/cli.rb', line 146 def optical_size(font_file) command = Commands::OpticalSizeCommand.new(font_file, ) 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.
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 |
# File 'lib/fontisan/cli.rb', line 703 def pack(*font_files) command = Commands::PackCommand.new(font_files, ) result = command.run unless [: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.
158 159 160 161 162 163 164 |
# File 'lib/fontisan/cli.rb', line 158 def scripts(font_file) command = Commands::ScriptsCommand.new(font_file, ) 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.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/fontisan/cli.rb', line 212 def subset(font_file) command = Commands::SubsetCommand.new(font_file, ) result = command.run unless [: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.
98 99 100 101 102 103 104 |
# File 'lib/fontisan/cli.rb', line 98 def tables(font_file) command = Commands::TablesCommand.new(font_file, ) 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.
122 123 124 125 126 127 128 |
# File 'lib/fontisan/cli.rb', line 122 def unicode(font_file) command = Commands::UnicodeCommand.new(font_file, ) 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.
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 |
# File 'lib/fontisan/cli.rb', line 654 def unpack(font_file) command = Commands::UnpackCommand.new(font_file, ) result = command.run unless [: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
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 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 |
# File 'lib/fontisan/cli.rb', line 526 def validate(*font_files) if [: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: [:profile], exclude: [:exclude] || [], output: [:output], format: [:format].to_sym, full_report: [:full_report], summary_report: [:summary_report], table_report: [:table_report], verbose: [:verbose], suppress_warnings: [:suppress_warnings], return_value_results: [:return_value_results], ) exit cmd.run rescue Thor::Error => e unless [:quiet] warn "ERROR: #{e.}" warn help("validate") end exit 1 rescue StandardError => e error "Validation failed: #{e.}" exit 1 end |
#validate_collection(path) ⇒ Object
604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/fontisan/cli.rb', line 604 def validate_collection(path) cmd = Commands::ValidateCollectionCommand.new( input: path, expected_faces: [:expected_faces], max_glyphs: [:max_glyphs], expected_cmap_union: [:expected_cmap_union], ) exit cmd.run rescue ArgumentError => e warn "ERROR: #{e.}" exit 1 end |
#variable(font_file) ⇒ Object
Display variable font variation axes and instances.
134 135 136 137 138 139 140 |
# File 'lib/fontisan/cli.rb', line 134 def variable(font_file) command = Commands::VariableCommand.new(font_file, ) result = command.run output_result(result) rescue Errno::ENOENT, Error => e handle_error(e) end |