Class: Fontisan::Type1::PFMParser

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

Overview

PFM (Printer Font Metrics) file parser

PFMParser parses Printer Font Metrics files which contain font metric information for Type 1 fonts on Windows.

PFM files are binary files that include:

  • Character widths
  • Kerning pairs
  • Font metadata (name, version, copyright, etc.)
  • Extended text metrics

Examples:

Parse a PFM file

pfm = Fontisan::Type1::PFMParser.parse_file("font.pfm")
puts pfm.font_name
puts pfm.character_widths['A']
puts pfm.kerning_pairs[['A', 'V']]

See Also:

Constant Summary collapse

PFM_HEADER_SIZE =

PFM Header structure

256
PFM_VERSION =
0x0100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePFMParser

Initialize a new PFMParser



86
87
88
89
90
91
# File 'lib/fontisan/type1/pfm_parser.rb', line 86

def initialize
  @character_widths = {}
  @kerning_pairs = {}
  @extended_metrics = {}
  @font_bbox = nil
end

Instance Attribute Details

#character_widthsHash<String, Integer> (readonly)

Returns Character widths (glyph index => width).

Returns:

  • (Hash<String, Integer>)

    Character widths (glyph index => width)



41
42
43
# File 'lib/fontisan/type1/pfm_parser.rb', line 41

def character_widths
  @character_widths
end

Returns Copyright notice.

Returns:

  • (String)

    Copyright notice



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

def copyright
  @copyright
end

#extended_metricsHash (readonly)

Returns Extended text metrics.

Returns:

  • (Hash)

    Extended text metrics



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

def extended_metrics
  @extended_metrics
end

#family_nameString (readonly)

Returns Family name.

Returns:

  • (String)

    Family name



35
36
37
# File 'lib/fontisan/type1/pfm_parser.rb', line 35

def family_name
  @family_name
end

#font_bboxInteger (readonly)

Returns Font bounding box [llx, lly, urx, ury].

Returns:

  • (Integer)

    Font bounding box [llx, lly, urx, ury]



50
51
52
# File 'lib/fontisan/type1/pfm_parser.rb', line 50

def font_bbox
  @font_bbox
end

#font_nameString (readonly)

Returns Font name.

Returns:

  • (String)

    Font name



29
30
31
# File 'lib/fontisan/type1/pfm_parser.rb', line 29

def font_name
  @font_name
end

#full_nameString (readonly)

Returns Full name.

Returns:

  • (String)

    Full name



32
33
34
# File 'lib/fontisan/type1/pfm_parser.rb', line 32

def full_name
  @full_name
end

#kerning_pairsHash<Array(Integer), Integer> (readonly)

Returns Kerning pairs ([left_idx, right_idx] => adjustment).

Returns:

  • (Hash<Array(Integer), Integer>)

    Kerning pairs ([left_idx, right_idx] => adjustment)



44
45
46
# File 'lib/fontisan/type1/pfm_parser.rb', line 44

def kerning_pairs
  @kerning_pairs
end

#raw_dataString (readonly)

Returns Raw data.

Returns:

  • (String)

    Raw data



53
54
55
# File 'lib/fontisan/type1/pfm_parser.rb', line 53

def raw_data
  @raw_data
end

Class Method Details

.parse(content) ⇒ PFMParser

Parse PFM content

Parameters:

  • content (String)

    PFM file content (binary)

Returns:



76
77
78
# File 'lib/fontisan/type1/pfm_parser.rb', line 76

def self.parse(content)
  new.parse(content)
end

.parse_file(path) ⇒ PFMParser

Parse PFM file

Parameters:

  • path (String)

    Path to PFM file

Returns:

Raises:

  • (ArgumentError)

    If path is nil

  • (Fontisan::Error)

    If file cannot be read or parsed



61
62
63
64
65
66
67
68
69
70
# File 'lib/fontisan/type1/pfm_parser.rb', line 61

def self.parse_file(path)
  raise ArgumentError, "Path cannot be nil" if path.nil?

  unless File.exist?(path)
    raise Fontisan::Error, "PFM file not found: #{path}"
  end

  content = File.binread(path)
  parse(content)
end

.parse_string(content) ⇒ Object

Alias for parse method



81
82
83
# File 'lib/fontisan/type1/pfm_parser.rb', line 81

def self.parse_string(content)
  parse(content)
end

Instance Method Details

#has_character?(char_index) ⇒ Boolean

Check if character exists

Parameters:

  • char_index (Integer)

    Character index

Returns:

  • (Boolean)

    True if character exists



128
129
130
# File 'lib/fontisan/type1/pfm_parser.rb', line 128

def has_character?(char_index)
  @character_widths.key?(char_index)
end

#kerning(left_idx, right_idx) ⇒ Integer?

Get kerning adjustment for character pair

Parameters:

  • left_idx (Integer)

    Left character index

  • right_idx (Integer)

    Right character index

Returns:

  • (Integer, nil)

    Kerning adjustment or nil if not found



120
121
122
# File 'lib/fontisan/type1/pfm_parser.rb', line 120

def kerning(left_idx, right_idx)
  @kerning_pairs[[left_idx, right_idx]]
end

#parse(content) ⇒ PFMParser

Parse PFM content

Parameters:

  • content (String)

    PFM file content (binary)

Returns:



97
98
99
100
101
102
103
104
105
# File 'lib/fontisan/type1/pfm_parser.rb', line 97

def parse(content)
  @raw_data = content
  parse_header(content)
  parse_driver_info(content)
  parse_extended_metrics(content)
  parse_character_widths(content)
  parse_kerning_pairs(content)
  self
end

#width(char_index) ⇒ Integer?

Get character width for character index

Parameters:

  • char_index (Integer)

    Character index

Returns:

  • (Integer, nil)

    Character width or nil if not found



111
112
113
# File 'lib/fontisan/type1/pfm_parser.rb', line 111

def width(char_index)
  @character_widths[char_index]
end