Class: AFM::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/afm.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Font

Loading a Font Metrics file by absolute path (no automatic font path resolution)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/afm.rb', line 38

def initialize(filename)
  @metadata = {}
  @char_metrics = {}
  @char_metrics_by_code = {}
  @kern_pairs = []
  File.open(filename) do |file|
    mode = :meta
    file.each_line do |line|
      case(line)
      when /^StartFontMetrics/ ; mode = :meta
      when /^StartCharMetrics/ ; mode = :char_metrics
      when /^EndCharMetrics/ ; mode = :meta
      when /^StartKernData/ ; mode = :kern_data
      when /^StartKernPairs/ ; mode = :kern_pairs
      when /^EndKernPairs/ ; mode = :kern_data
      when /^EndKernData/ ; mode = :meta
      else
        case(mode)
        when :meta
          if match = line.match(/^([\w]+) (.*)$/)
            @metadata[match[1]] = match[2]
          end
        when :char_metrics
          metrics = {}
          metrics[:charcode] = match[1].to_i if match = line.match(/C (-?\d+) *?;/)
          metrics[:wx] = match[1].to_i if match = line.match(/WX (-?\d+) *?;/)
          metrics[:name] = match[1] if match = line.match(/N ([.\w]+) *?;/)
          if match = line.match(/B (-?\d+) (-?\d+) (-?\d+) (-?\d+) *?;/)
            metrics[:boundingbox] = [match[1].to_i, match[2].to_i, match[3].to_i, match[4].to_i] 
          end
          @char_metrics[metrics[:name]] = metrics if metrics[:name]
          @char_metrics_by_code[metrics[:charcode]] = metrics if metrics[:charcode] && metrics[:charcode] > 0
        when :kern_pairs
          if match = line.match(/^KPX ([.\w]+) ([.\w]+) (-?\d+)$/)
            @kern_pairs << [match[1], match[2], match[3].to_i]
          end
        end
      end
    end
  end
end

Instance Attribute Details

#char_metricsObject (readonly)

Returns the value of attribute char_metrics.



35
36
37
# File 'lib/afm.rb', line 35

def char_metrics
  @char_metrics
end

#char_metrics_by_codeObject (readonly)

Returns the value of attribute char_metrics_by_code.



35
36
37
# File 'lib/afm.rb', line 35

def char_metrics_by_code
  @char_metrics_by_code
end

#kern_pairsObject (readonly)

Returns the value of attribute kern_pairs.



35
36
37
# File 'lib/afm.rb', line 35

def kern_pairs
  @kern_pairs
end

#metadataObject (readonly)

Returns the value of attribute metadata.



35
36
37
# File 'lib/afm.rb', line 35

def 
  @metadata
end

Class Method Details

.from_file(file) ⇒ Object

alias for new()



82
83
84
# File 'lib/afm.rb', line 82

def self.from_file(file)
  self.new(file)
end

Instance Method Details

#[](key) ⇒ Object

Get metadata by key



88
89
90
# File 'lib/afm.rb', line 88

def [](key)
  @metadata[key]
end

#metrics_for(char) ⇒ Object

Get metrics for character. Takes an integer (charcode) or a one-char string. currently works only for Latin1 strings, since we only have a chartable for the Latin1 charset so far. (shamelessly stolen from AFM.pm by Gisle Aas)



96
97
98
99
100
101
102
103
# File 'lib/afm.rb', line 96

def metrics_for(char)
  glyph = if (char.kind_of?(Integer))
    ISO_LATIN1_ENCODING[char]
  else
    ISO_LATIN1_ENCODING[char.unpack("C*").first]
  end
  @char_metrics[glyph]
end