File: afm.rb

package info (click to toggle)
ruby-afm 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 160 kB
  • sloc: ruby: 117; makefile: 2
file content (104 lines) | stat: -rw-r--r-- 4,492 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module AFM
  ISO_LATIN1_ENCODING = %w[
    .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
    .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
    .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
    .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef space
    exclam quotedbl numbersign dollar percent ampersand quoteright
    parenleft parenright asterisk plus comma minus period slash zero one
    two three four five six seven eight nine colon semicolon less equal
    greater question at A B C D E F G H I J K L M N O P Q R S
    T U V W X Y Z bracketleft backslash bracketright asciicircum
    underscore quoteleft a b c d e f g h i j k l m n o p q r s
    t u v w x y z braceleft bar braceright asciitilde .notdef .notdef
    .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
    .notdef .notdef .notdef .notdef .notdef .notdef .notdef dotlessi grave
    acute circumflex tilde macron breve dotaccent dieresis .notdef ring
    cedilla .notdef hungarumlaut ogonek caron space exclamdown cent
    sterling currency yen brokenbar section dieresis copyright ordfeminine
    guillemotleft logicalnot hyphen registered macron degree plusminus
    twosuperior threesuperior acute mu paragraph periodcentered cedilla
    onesuperior ordmasculine guillemotright onequarter onehalf threequarters
    questiondown Agrave Aacute Acircumflex Atilde Adieresis Aring AE
    Ccedilla Egrave Eacute Ecircumflex Edieresis Igrave Iacute Icircumflex
    Idieresis Eth Ntilde Ograve Oacute Ocircumflex Otilde Odieresis
    multiply Oslash Ugrave Uacute Ucircumflex Udieresis Yacute Thorn
    germandbls agrave aacute acircumflex atilde adieresis aring ae
    ccedilla egrave eacute ecircumflex edieresis igrave iacute icircumflex
    idieresis eth ntilde ograve oacute ocircumflex otilde odieresis divide
    oslash ugrave uacute ucircumflex udieresis yacute thorn ydieresis
  ]

  class Font
    attr_reader :metadata, :char_metrics, :char_metrics_by_code, :kern_pairs

    # Loading a Font Metrics file by absolute path (no automatic font path resolution)
    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

    #
    # alias for new()
    def self.from_file(file)
      new(file)
    end

    #
    # Get metadata by key
    def [](key)
      @metadata[key]
    end

    #
    # 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)
    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
  end
end