Module: ExtractTtc

Defined in:
lib/extract_ttc.rb,
lib/extract_ttc/cli.rb,
lib/extract_ttc/version.rb,
lib/extract_ttc/constants.rb,
lib/extract_ttc/commands/info.rb,
lib/extract_ttc/commands/list.rb,
lib/extract_ttc/configuration.rb,
lib/extract_ttc/true_type_font.rb,
lib/extract_ttc/commands/extract.rb,
lib/extract_ttc/true_type_collection.rb,
lib/extract_ttc/models/extraction_result.rb,
lib/extract_ttc/models/validation_result.rb,
lib/extract_ttc/utilities/checksum_calculator.rb,
lib/extract_ttc/utilities/output_path_generator.rb

Defined Under Namespace

Modules: Constants, Models, Utilities Classes: Cli, Configuration, Error, ExtractCommand, InfoCommand, InvalidFileError, ListCommand, OffsetTable, ReadFileError, TableDirectory, TrueTypeCollection, TrueTypeFont, UnknownResultError, WriteFileError

Constant Summary collapse

VERSION =
"0.3.7".freeze

Class Method Summary collapse

Class Method Details

.extract(path, output_dir: nil, config: nil) ⇒ Array<String>

Extract all fonts from a TTC file

This is the main public API for the gem. It extracts all fonts from a TrueType Collection (TTC) file and writes them as separate TTF files.

Uses the object-oriented architecture where domain objects (TrueTypeCollection, TrueTypeFont) encapsulate their own persistence logic.

Examples:

Extract fonts to current directory

ExtractTtc.extract("Helvetica.ttc")
# => ["Helvetica_00.ttf", "Helvetica_01.ttf", ...]

Extract fonts to specific directory

ExtractTtc.extract("Helvetica.ttc", output_dir: "/tmp/fonts")

Parameters:

  • path (String)

    Path to the input TTC file

  • output_dir (String, nil) (defaults to: nil)

    Optional output directory

  • config (Configuration, nil) (defaults to: nil)

    Optional configuration object

Returns:

  • (Array<String>)

    Array of output file paths

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/extract_ttc.rb', line 61

def self.extract(path, output_dir: nil, config: nil)
  ensure_output_directory_exists(output_dir)

  File.open(path, "rb") do |file|
    ttc = TrueTypeCollection.read(file)
    fonts = ttc.extract_fonts(file)

    fonts.map.with_index do |font, index|
      output_path = Utilities::OutputPathGenerator.generate(
        path, index, output_dir: output_dir || config&.output_directory
      )
      font.to_file(output_path)
      output_path
    end
  end
rescue Errno::ENOENT
  raise ReadFileError, "Could not open file: #{path}"
rescue Errno::EACCES => e
  raise WriteFileError, "Failed to open output file: #{e.message}"
rescue IOError, RuntimeError, StandardError => e
  raise invalid_file?(e) ? invalid_file_error : write_file_error(e)
end