Class: Fontisan::Type1::PFBGenerator

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

Overview

PFB (Printer Font Binary) Generator

PFBGenerator generates Type 1 PFB files from TrueType fonts.

PFB files are segmented binary files used by Windows for Type 1 fonts. They contain:

  • ASCII segment: Font dictionary
  • Binary segment: CharString data
  • ASCII segment: Trailer

Examples:

Generate PFB from TTF

font = Fontisan::FontLoader.load("font.ttf")
pfb_data = Fontisan::Type1::PFBGenerator.generate(font)
File.binwrite("font.pfb", pfb_data)

Generate PFB with custom options

options = { upm_scale: 1000, format: :pfb }
pfb_data = Fontisan::Type1::PFBGenerator.generate(font, options)

See Also:

Constant Summary collapse

ASCII_SEGMENT =

PFB segment markers

0x01
BINARY_SEGMENT =
0x02
END_SEGMENT =
0x03
PFB_HEADER =

Header format string

"%%!PS-AdobeFont-1.0: %s 1.0\n"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PFBGenerator.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fontisan/type1/pfb_generator.rb', line 47

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 PFB 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)

    PFB file content (binary)



43
44
45
# File 'lib/fontisan/type1/pfb_generator.rb', line 43

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

Instance Method Details

#generateString

Generate PFB file content

Returns:

  • (String)

    PFB binary content



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

def generate
  # Build PFB segments
  ascii_segment1 = build_ascii_segment_1
  binary_segment = build_binary_segment
  ascii_segment2 = build_ascii_segment_2

  # Combine with segment headers
  [
    segment_header(ASCII_SEGMENT, ascii_segment1.bytesize),
    ascii_segment1,
    segment_header(BINARY_SEGMENT, binary_segment.bytesize),
    binary_segment,
    segment_header(ASCII_SEGMENT, ascii_segment2.bytesize),
    ascii_segment2,
    [END_SEGMENT, 0, 0, 0, 0, 0].pack("CV"),
  ].join
end