Class: Pdfrb::Font::AFMParser

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/font/afm_parser.rb

Overview

AFM (Adobe Font Metrics) parser. Reads the text-based AFM format used by the 14 standard Type1 fonts.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAFMParser

Returns a new instance of AFMParser.



12
13
14
# File 'lib/pdfrb/font/afm_parser.rb', line 12

def initialize
  @char_metrics = {}
end

Instance Attribute Details

#ascenderObject (readonly)

Returns the value of attribute ascender.



8
9
10
# File 'lib/pdfrb/font/afm_parser.rb', line 8

def ascender
  @ascender
end

#bboxObject (readonly)

Returns the value of attribute bbox.



8
9
10
# File 'lib/pdfrb/font/afm_parser.rb', line 8

def bbox
  @bbox
end

#cap_heightObject (readonly)

Returns the value of attribute cap_height.



8
9
10
# File 'lib/pdfrb/font/afm_parser.rb', line 8

def cap_height
  @cap_height
end

#char_metricsObject (readonly)

Returns the value of attribute char_metrics.



8
9
10
# File 'lib/pdfrb/font/afm_parser.rb', line 8

def char_metrics
  @char_metrics
end

#descenderObject (readonly)

Returns the value of attribute descender.



8
9
10
# File 'lib/pdfrb/font/afm_parser.rb', line 8

def descender
  @descender
end

#encoding_schemeObject (readonly)

Returns the value of attribute encoding_scheme.



8
9
10
# File 'lib/pdfrb/font/afm_parser.rb', line 8

def encoding_scheme
  @encoding_scheme
end

#family_nameObject (readonly)

Returns the value of attribute family_name.



8
9
10
# File 'lib/pdfrb/font/afm_parser.rb', line 8

def family_name
  @family_name
end

#font_nameObject (readonly)

Returns the value of attribute font_name.



8
9
10
# File 'lib/pdfrb/font/afm_parser.rb', line 8

def font_name
  @font_name
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



8
9
10
# File 'lib/pdfrb/font/afm_parser.rb', line 8

def full_name
  @full_name
end

#x_heightObject (readonly)

Returns the value of attribute x_height.



8
9
10
# File 'lib/pdfrb/font/afm_parser.rb', line 8

def x_height
  @x_height
end

Class Method Details

.from_file(path) ⇒ Object



20
21
22
# File 'lib/pdfrb/font/afm_parser.rb', line 20

def self.from_file(path)
  parse(File.read(path, encoding: "UTF-8"))
end

.parse(text) ⇒ Object



16
17
18
# File 'lib/pdfrb/font/afm_parser.rb', line 16

def self.parse(text)
  new.tap { |p| p.parse(text) }
end

Instance Method Details

#parse(text) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pdfrb/font/afm_parser.rb', line 24

def parse(text)
  in_char_metrics = false
  text.each_line do |line|
    line = line.strip
    if line.start_with?("FontName ")
      @font_name = line.split(" ", 2)[1]
    elsif line.start_with?("FullName ")
      @full_name = line.split(" ", 2)[1]
    elsif line.start_with?("FamilyName ")
      @family_name = line.split(" ", 2)[1]
    elsif line.start_with?("EncodingScheme ")
      @encoding_scheme = line.split(" ", 2)[1]
    elsif line.start_with?("FontBBox ")
      @bbox = line.split.drop(1).map(&:to_f)
    elsif line.start_with?("CapHeight ")
      @cap_height = line.split[1].to_f
    elsif line.start_with?("XHeight ")
      @x_height = line.split[1].to_f
    elsif line.start_with?("Ascender ")
      @ascender = line.split[1].to_f
    elsif line.start_with?("Descender ")
      @descender = line.split[1].to_f
    elsif line == "StartCharMetrics" || line.start_with?("StartCharMetrics ")
      in_char_metrics = true
    elsif line == "EndCharMetrics" || line.start_with?("EndCharMetrics")
      in_char_metrics = false
    elsif in_char_metrics && line.start_with?("C ")
      parse_char_metric(line)
    end
  end
  self
end

#width_for(glyph_name_or_code) ⇒ Object



57
58
59
60
61
# File 'lib/pdfrb/font/afm_parser.rb', line 57

def width_for(glyph_name_or_code)
  metric = @char_metrics[glyph_name_or_code] ||
           @char_metrics[glyph_name_or_code.to_i]
  metric ? metric[:width] : 0
end