Class: Fontisan::Type1::AFMParser

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

Overview

AFM (Adobe Font Metrics) file parser

AFMParser parses Adobe Font Metrics files which contain font metric information for Type 1 fonts.

AFM files include:

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

Examples:

Parse an AFM file

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

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAFMParser

Initialize a new AFMParser



88
89
90
91
92
93
94
# File 'lib/fontisan/type1/afm_parser.rb', line 88

def initialize
  @character_widths = {}
  @kerning_pairs = {}
  @character_bboxes = {}
  @raw_data = {}
  @font_bbox = nil
end

Instance Attribute Details

#character_bboxesHash (readonly)

Returns Character bounding boxes (glyph name => lly, urx, ury).

Returns:

  • (Hash)

    Character bounding boxes (glyph name => lly, urx, ury)



49
50
51
# File 'lib/fontisan/type1/afm_parser.rb', line 49

def character_bboxes
  @character_bboxes
end

#character_widthsHash<String, Integer> (readonly)

Returns Character widths (glyph name => width).

Returns:

  • (Hash<String, Integer>)

    Character widths (glyph name => width)



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

def character_widths
  @character_widths
end

Returns Copyright notice.

Returns:

  • (String)

    Copyright notice



40
41
42
# File 'lib/fontisan/type1/afm_parser.rb', line 40

def copyright
  @copyright
end

#family_nameString (readonly)

Returns Family name.

Returns:

  • (String)

    Family name



31
32
33
# File 'lib/fontisan/type1/afm_parser.rb', line 31

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]



52
53
54
# File 'lib/fontisan/type1/afm_parser.rb', line 52

def font_bbox
  @font_bbox
end

#font_nameString (readonly)

Returns Font name.

Returns:

  • (String)

    Font name



25
26
27
# File 'lib/fontisan/type1/afm_parser.rb', line 25

def font_name
  @font_name
end

#full_nameString (readonly)

Returns Full name.

Returns:

  • (String)

    Full name



28
29
30
# File 'lib/fontisan/type1/afm_parser.rb', line 28

def full_name
  @full_name
end

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

Returns Kerning pairs ([left, right] => adjustment).

Returns:

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

    Kerning pairs ([left, right] => adjustment)



46
47
48
# File 'lib/fontisan/type1/afm_parser.rb', line 46

def kerning_pairs
  @kerning_pairs
end

#raw_dataHash (readonly)

Returns Raw AFM data.

Returns:

  • (Hash)

    Raw AFM data



55
56
57
# File 'lib/fontisan/type1/afm_parser.rb', line 55

def raw_data
  @raw_data
end

#versionString (readonly)

Returns Version.

Returns:

  • (String)

    Version



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

def version
  @version
end

#weightString (readonly)

Returns Weight.

Returns:

  • (String)

    Weight



34
35
36
# File 'lib/fontisan/type1/afm_parser.rb', line 34

def weight
  @weight
end

Class Method Details

.parse(content) ⇒ AFMParser

Parse AFM content

Parameters:

  • content (String)

    AFM file content

Returns:



78
79
80
# File 'lib/fontisan/type1/afm_parser.rb', line 78

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

.parse_file(path) ⇒ AFMParser

Parse AFM file

Parameters:

  • path (String)

    Path to AFM file

Returns:

Raises:

  • (ArgumentError)

    If path is nil

  • (Fontisan::Error)

    If file cannot be read or parsed



63
64
65
66
67
68
69
70
71
72
# File 'lib/fontisan/type1/afm_parser.rb', line 63

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

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

  content = File.read(path, encoding: "ISO-8859-1")
  parse(content)
end

.parse_string(content) ⇒ Object

Alias for parse method



83
84
85
# File 'lib/fontisan/type1/afm_parser.rb', line 83

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

Instance Method Details

#has_character?(glyph_name) ⇒ Boolean

Check if character exists

Parameters:

  • glyph_name (String)

    Glyph name

Returns:

  • (Boolean)

    True if character exists



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

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

#kerning(left, right) ⇒ Integer?

Get kerning adjustment for character pair

Parameters:

  • left (String)

    Left glyph name

  • right (String)

    Right glyph name

Returns:

  • (Integer, nil)

    Kerning adjustment or nil if not found



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

def kerning(left, right)
  @kerning_pairs[[left, right]]
end

#parse(content) ⇒ AFMParser

Parse AFM content

Parameters:

  • content (String)

    AFM file content

Returns:



100
101
102
103
104
105
# File 'lib/fontisan/type1/afm_parser.rb', line 100

def parse(content)
  parse_global_metrics(content)
  parse_character_metrics(content)
  parse_kerning_data(content)
  self
end

#width(glyph_name) ⇒ Integer?

Get character width for glyph

Parameters:

  • glyph_name (String)

    Glyph name

Returns:

  • (Integer, nil)

    Character width or nil if not found



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

def width(glyph_name)
  @character_widths[glyph_name]
end