Class: Fontisan::Type1::PFAGenerator

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

Overview

PFA (Printer Font ASCII) Generator

[‘PFAGenerator`](lib/fontisan/type1/pfa_generator.rb) generates Type 1 PFA files from TrueType fonts.

PFA files are ASCII-encoded Type 1 fonts used by Unix systems. They are the same as PFB files but with binary data hex-encoded.

Examples:

Generate PFA from TTF

font = Fontisan::FontLoader.load("font.ttf")
pfa_data = Fontisan::Type1::PFAGenerator.generate(font)
File.write("font.pfa", pfa_data)

Generate PFA with custom options

options = { upm_scale: 1000, format: :pfa }
pfa_data = Fontisan::Type1::PFAGenerator.generate(font, options)

See Also:

Constant Summary collapse

HEX_LINE_LENGTH =

Hex line length for ASCII encoding

64

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PFAGenerator.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fontisan/type1/pfa_generator.rb', line 42

def initialize(font, options = {})
  @font = font
  @options = options
  @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

  # Set up converter options
  @convert_curves = options.fetch(:convert_curves, true)
end

Class Method Details

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

Generate PFA from TTF font

Parameters:

  • font (Fontisan::Font)

    Source TTF font

  • options (Hash) (defaults to: {})

    Generation options

Options Hash (options):

  • :upm_scale (Integer, :native)

    Target UPM (default: 1000)

  • :encoding (Class)

    Encoding class (default: Encodings::AdobeStandard)

  • :convert_curves (Boolean)

    Convert quadratic to cubic (default: true)

Returns:

  • (String)

    PFA file content (ASCII text)



38
39
40
# File 'lib/fontisan/type1/pfa_generator.rb', line 38

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

Instance Method Details

#generateString

Generate PFA file content

Returns:

  • (String)

    PFA ASCII content



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fontisan/type1/pfa_generator.rb', line 65

def generate
  lines = []

  # Header (ASCII section 1)
  lines << build_pfa_header
  lines << build_font_dict
  lines << build_private_dict
  lines << ""
  lines << "currentdict end"
  lines << "dup /FontName get exch definefont pop"
  lines << ""

  # Binary section (hex-encoded)
  lines << "%--Data to be hex-encoded:"
  hex_data = build_hex_encoded_charstrings
  lines.concat(hex_data)
  lines << ""

  # Trailer
  lines << build_pfa_trailer

  lines.join("\n")
end