Class: Fontisan::Type1::AFMGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/type1/afm_generator.rb

Overview

AFM (Adobe Font Metrics) file generator

AFMGenerator generates Adobe Font Metrics files from TTF/OTF fonts.

AFM files include:

  • Character widths
  • Kerning pairs
  • Character bounding boxes
  • Font metadata (name, version, copyright, etc.)

Examples:

Generate AFM from TTF

font = Fontisan::FontLoader.load("font.ttf")
afm = Fontisan::Type1::AFMGenerator.generate(font)
File.write("font.afm", afm)

Generate AFM with 1000 UPM scaling

afm = Fontisan::Type1::AFMGenerator.generate(font, upm_scale: 1000)

Generate AFM with Unicode encoding

afm = Fontisan::Type1::AFMGenerator.generate(font, encoding: Fontisan::Type1::Encodings::Unicode)

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font, options = {}) ⇒ AFMGenerator

Initialize a new AFMGenerator

Parameters:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fontisan/type1/afm_generator.rb', line 70

def initialize(font, options = {})
  @font = font
  @metrics = MetricsCalculator.new(font)

  # Set up scaler
  upm_scale = options[:upm_scale] || 1000
  @scaler = if upm_scale == :native
              UPMScaler.native(font)
            else
              UPMScaler.new(font, target_upm: upm_scale)
            end

  # Set up encoding
  @encoding = options[:encoding] || Encodings::AdobeStandard
end

Class Method Details

.adobe_glyph_name(codepoint, encoding: nil) ⇒ String

Get Adobe glyph name from Unicode codepoint

Parameters:

  • codepoint (Integer)

    Unicode codepoint

  • encoding (Class) (defaults to: nil)

    Encoding class to use (default: nil for direct AGL lookup)

Returns:

  • (String)

    Adobe glyph name



57
58
59
60
61
62
63
# File 'lib/fontisan/type1/afm_generator.rb', line 57

def adobe_glyph_name(codepoint, encoding: nil)
  if encoding
    encoding.glyph_name_for_code(codepoint)
  else
    AGL.glyph_name_for_unicode(codepoint)
  end
end

.generate(font, options = {}) ⇒ String

Generate AFM content from a font

Parameters:

Options Hash (options):

  • :upm_scale (Integer, :native)

    Target UPM (1000 for Type 1, :native for no scaling)

  • :encoding (Class)

    Encoding class (default: AdobeStandard)

Returns:

  • (String)

    AFM file content



37
38
39
# File 'lib/fontisan/type1/afm_generator.rb', line 37

def generate(font, options = {})
  new(font, options).generate_afm
end

.generate_to_file(font, path, options = {}) ⇒ void

This method returns an undefined value.

Generate AFM file from a font and write to file

Parameters:



47
48
49
50
# File 'lib/fontisan/type1/afm_generator.rb', line 47

def generate_to_file(font, path, options = {})
  afm_content = generate(font, options)
  File.write(path, afm_content, encoding: "ISO-8859-1")
end

Instance Method Details

#generate_afmString

Generate AFM content

Returns:

  • (String)

    AFM file content



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fontisan/type1/afm_generator.rb', line 89

def generate_afm
  afm_lines = []

  # Header
  afm_lines << "StartFontMetrics 4.1"

  # Font metadata
  (afm_lines)

  # Font bounding box
  add_font_bounding_box(afm_lines)

  # Character metrics
  add_character_metrics(afm_lines)

  # Kerning data
  add_kerning_data(afm_lines)

  # Footer
  afm_lines << "EndFontMetrics"

  afm_lines.join("\n")
end