class AFM::Font

Attributes

char_metrics[R]
char_metrics_by_code[R]
kern_pairs[R]
metadata[R]

Public Class Methods

from_file(file) click to toggle source

alias for new()

# File lib/afm.rb, line 80
def self.from_file(file)
  new(file)
end
new(filename) click to toggle source

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

# File lib/afm.rb, line 36
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/ then mode = :meta
      when /^StartCharMetrics/ then mode = :char_metrics
      when /^EndCharMetrics/ then mode = :meta
      when /^StartKernData/ then mode = :kern_data
      when /^StartKernPairs/ then mode = :kern_pairs
      when /^EndKernPairs/ then mode = :kern_data
      when /^EndKernData/ then 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

Public Instance Methods

[](key) click to toggle source

Get metadata by key

# File lib/afm.rb, line 86
def [](key)
  @metadata[key]
end
metrics_for(char) click to toggle source

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)

# File lib/afm.rb, line 95
def metrics_for(char)
  glyph = if char.is_a?(Integer)
    ISO_LATIN1_ENCODING[char]
  else
    ISO_LATIN1_ENCODING[char.unpack1("C")]
  end
  @char_metrics[glyph]
end