Class: ExtractTtc::Utilities::OutputPathGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/extract_ttc/utilities/output_path_generator.rb

Overview

OutputPathGenerator provides stateless utility methods for generating output file paths.

This class generates standardized output paths for extracted TTF files from TTC files, using a consistent naming convention with zero-padded indices.

Examples:

Generate output path with defaults

path = OutputPathGenerator.generate("Helvetica.ttc", 0)
# => "Helvetica_00.ttf"

Generate output path with custom directory

path = OutputPathGenerator.generate("Helvetica.ttc", 5, output_dir: "/tmp/fonts")
# => "/tmp/fonts/Helvetica_05.ttf"

Constant Summary collapse

DEFAULT_INDEX_FORMAT =

Default format string for zero-padded font indices. Produces two-digit indices (00, 01, 02, etc.)

"%02d"

Class Method Summary collapse

Class Method Details

.generate(input_path, font_index, output_dir: nil) ⇒ String

Generate an output TTF file path for an extracted font.

The output path is constructed from the input file’s basename, a zero-padded font index, and an optional output directory. The resulting filename follows the pattern: “basename_XX.ttf” where XX is the zero-padded index.

Examples:

Generate path in current directory

OutputPathGenerator.generate("fonts/Helvetica.ttc", 0)
# => "Helvetica_00.ttf"

Generate path in specific directory

OutputPathGenerator.generate("Helvetica.ttc", 3, output_dir: "/tmp")
# => "/tmp/Helvetica_03.ttf"

High font index

OutputPathGenerator.generate("Font.ttc", 15)
# => "Font_15.ttf"

Parameters:

  • input_path (String)

    path to the input TTC file

  • font_index (Integer)

    zero-based index of the font being extracted

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

    optional output directory (defaults to current directory “.”)

Returns:

  • (String)

    the generated output file path

Raises:

  • (ArgumentError)

    if font_index is negative



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/extract_ttc/utilities/output_path_generator.rb', line 45

def self.generate(input_path, font_index, output_dir: nil)
  if font_index.negative?
    raise ArgumentError,
          "font_index must be non-negative"
  end

  basename = File.basename(input_path, ".*")
  formatted_index = sprintf(DEFAULT_INDEX_FORMAT, font_index)
  filename = "#{basename}_#{formatted_index}.ttf"

  if output_dir.nil? || output_dir.empty? || output_dir == "."
    filename
  else
    File.join(output_dir, filename)
  end
end

.generate_with_format(input_path, font_index, index_format, output_dir: nil) ⇒ String

Generate output path with a custom index format.

Allows specifying a custom sprintf format string for the index padding, enabling different padding widths or styles.

Examples:

Three-digit padding

OutputPathGenerator.generate_with_format("Font.ttc", 5, "%03d")
# => "Font_005.ttf"

No padding

OutputPathGenerator.generate_with_format("Font.ttc", 5, "%d")
# => "Font_5.ttf"

Parameters:

  • input_path (String)

    path to the input TTC file

  • font_index (Integer)

    zero-based index of the font being extracted

  • index_format (String)

    sprintf format string for index formatting

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

    optional output directory

Returns:

  • (String)

    the generated output file path

Raises:

  • (ArgumentError)

    if font_index is negative



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/extract_ttc/utilities/output_path_generator.rb', line 81

def self.generate_with_format(input_path, font_index, index_format,
output_dir: nil)
  if font_index.negative?
    raise ArgumentError,
          "font_index must be non-negative"
  end

  basename = File.basename(input_path, ".*")
  formatted_index = sprintf(index_format, font_index)
  filename = "#{basename}_#{formatted_index}.ttf"

  if output_dir.nil? || output_dir.empty?
    filename
  else
    File.join(output_dir, filename)
  end
end