summaryrefslogtreecommitdiff
path: root/support/rfil/lib/rfil/font/afm.rb
blob: 3a087d36c8e45d13055bba7f72f1f402fa9d182c (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# Last Change: Tue May 16 19:11:20 2006

# require 'rfi'

require 'strscan'
require 'pathname'

require 'rfil/font/metric'

module RFIL # :nodoc:
  module Font  # :nodoc:
    # = AFM -- Access type1 font metric files
    #
    # == General information
    #
    # Read and parse a (type1) afm file. The afm file must be compliant to
    # the afm specification as described in 'Adobe Font Metrics File
    # Format Specification' Version 4.1, dated October 7 1998.
    #
    # == Example usage
    #
    # === Read an afm file
    #  filename = "/opt/tetex/3.0/texmf/fonts/afm/urw/palatino/uplb8a.afm"
    #  afm=AFM.new
    #  afm.read(filename)
    #  afm.filename           # => "/opt/..../uplb8a.afm"
    #  afm.count_charmetrics  # => 316
    #  afm.encodingscheme     # => "AdobeStandardEncoding"
    #  # ....
    #
    class AFM < Metric

      # This is set to true if there is something wrong in the afm file.
      # Diagnostics can be turned on with <tt>:verbose</tt> set to true
      # when creating the object.
      attr_reader :something_strange

      # Number of characters found in the afm file.
      attr_accessor :count_charmetrics

      # Number of encoded character found in the afm file.
      attr_accessor :count_charmetrics_encoded

      # Number of unencoded character found in the afm file.
      attr_accessor :count_charmetrics_unencoded

      # The default encoding of the font.
      attr_accessor :encodingscheme

      # Boundingbox of the font. Array of for elements.
      attr_accessor :fontbbox

      # Underline position of the font.
      attr_accessor :underlineposition

      # Underline thickness.
      attr_accessor :underlinethickness

      # Height of caps.
      attr_accessor :capheight

      # Height of ascender.
      attr_accessor :ascender

      # Height of descender.
      attr_accessor :descender

      
      # Create an empty afm file. If _afm_ is set, use this to initialize
      # the object. _afm_ is either a string with the contents of an afm
      # file or a File object that points to the afm file. _options_
      # currently only accepts <tt>:verbose</tt> (true/false), that prints
      # out some diagnostic information on STDERR.
      def initialize(options={})
        @something_strange = false
        super()
        @outlinetype=:type1
        @comment = ""
        @verbose=options[:verbose]==true
      end

      # Read the afm file given with _filename_. _filename_ must be full
      # path to the afm file, it does not perform any lookups. Returns self.
      def read (filename)
        @filename=File.basename(filename)
        @name=@filename.chomp(".afm")
        self.pathname=Pathname.new(filename).realpath.to_s
        parse(File.read(filename))
      end

      # Return a string representation of the afm file that is compliant
      # with the afm spec.
      def to_s
        s ="StartFontMetrics 2.0\n"
        s << "Comment Generated using the RFI Library\n"
        %w( FontName FullName FamilyName Weight Notice ItalicAngle
        IsFixedPitch UnderlinePosition UnderlineTickness Version
        EncodingScheme CapHeight XHeight Descender Ascender ).each {|kw|

          meth=kw.downcase.to_sym
          value=self.send(meth) if self.respond_to?(meth)
          if value
            s << kw << " " << value.to_s << "\n"
          end
        }
        s << "FontBBox " << @fontbbox.join(" ") << "\n"
        s << "StartCharMetrics #@count_charmetrics\n"
        @chars.sort{ |a,b|
          # puts "a=#{a[1].c}, b=#{b[1].c}"
          if a[1].c == -1
            b[1].c == -1 ? 0 : 1
          else
            b[1].c == -1 ? -1 :  a[1].c <=> b[1].c
          end      
        }.each { |a,b|
          s << "C #{b.c} ; WX #{b.wx} ; N #{a} ; B #{b.b.join(" ")}\n"
        }
        s << "EndCharMetrics\nStartKernData\nStartKernPairs"
        count=0
        @chars.each_value { |c|
          count += c.kern_data.size
        }
        s << " #{count}\n"
        @chars.sort{ |a,b| a[0] <=> b[0] }.each { |name,char|
          char.kern_data.each { |destname, value|
            s << "KPX #{name} #{destname} #{value[0]}\n"
          }
        }
        s << "EndKernPairs\nEndKernData\nEndFontMetrics\n"
        s
      end
      
      # Parse the contents of the String _txt_. Returns self.
      def parse(txt)
        @chars ||= Hash.new
        @s=StringScanner.new(txt.gsub(/\r\n/,"\n"))
        @s.scan(/StartFontMetrics/)
        get_fontmetrics
        self
      end
      
      #######
      private
      #######

      def get_keyword
        @s.skip_until(/\s+/)
        @s.scan(/[A-Z][A-Za-z0-9]+/)
      end

      def get_integer
        @s.skip(/\s+/)
        @s.scan(/-?\d+/).to_i
      end

      def get_number
        @s.skip(/\s+/)
        @s.scan(/-?\d+(?:\.\d+)?/).to_f
      end

      def get_boolean
        @s.skip(/\s+/)
        @s.scan(/(true|false)/) == 'true'
      end

      def get_name
        @s.skip(/\s+/)
        @s.scan(/[^\s]+/)
      end

      def get_string
        @s.skip(/\s+/)
        @s.scan(/.*/)
      end

      def get_fontmetrics
        @version = get_number
        loop do
          kw=get_keyword
          STDERR.puts "KW: " + kw if @verbose
          case kw 
          when "FontName"
            @fontname=get_string
          when "FamilyName" 
            @familyname = get_string
          when "FullName" 
            @fullname = get_string
          when "EncodingScheme"
            @encodingscheme = get_string
          when "ItalicAngle"
            @italicangle = get_number
          when "IsFixedPitch"
            @isfixedpitch = get_boolean
          when "Weight" 
            @weight = get_string
          when "XHeight"
            @xheight= get_number
          when "Comment" 
            @comment << get_string << "\n"
          when "FontBBox"
            @fontbbox = [get_number,get_number, get_number, get_number]
          when "Version" 
            @version = get_string
          when "Notice" 
            @notice = get_string
          when "MappingScheme"
            @mappingscheme = get_integer
          when "EscChar"
            @escchar = get_integer
          when "CharacterSet"
            @characterset = get_string
          when "Characters"
            @characters = get_integer
          when "IsBaseFont"
            @isbasefont = get_boolean
          when "VVector"
            @vvector = [get_number,get_number]
          when "IsFixedV"
            @isfixedv = get_boolean
          when "CapHeight" 
            @capheight = get_number
          when "Ascender" 
            @ascender = get_number
          when "Descender" 
            @descender = get_number
          when "UnderlinePosition"
            @underlineposition = get_number
          when "UnderlineThickness"
            @underlinethickness = get_number
          when "StartDirection"
            get_direction
          when "StartCharMetrics"
            get_charmetrics
          when "StartKernData"
            get_kerndata
          when "StartComposites"
            get_composites
          when "EndFontMetrics" 
            break 
          end
        end
      end
      def get_direction
        # ignored
      end
      def get_charmetrics
        @count_charmetrics = get_integer
        @count_charmetrics_encoded = 0
        @count_charmetrics_unencoded = 0
        loop do
          @s.skip_until(/\n/)
          nextstring =  @s.scan_until(/(?:StopCharMetrics|.*)/)
          return if nextstring=="EndCharMetrics"
          a=nextstring.split(';')
          # ["C 32 ", " WX 250 ", " N space ", " B 125 0 125 0 "]
          a.collect! { |elt|
            elt.strip.split(/ /,2)
          }
          # [["C", "32"], ["WX", "250"], ["N", "space"], ["B", "125 0 125 0"]]
          char=new_glyph
          a.each { |elt|
            key,value = elt
            case key
            when "N"
              char.name=value
            when "B"
              #special treatment for bounding box
              char.b = value.split.collect { |e| e.to_i }
              char.llx = char.llx
              char.urx = char.urx
              # We need to avoid negative heights or depths. They break
              # accents in math mode, among other things.
              char.lly = 0 if char.lly > 0
              char.ury = 0 if char.ury < 0
            when "C"
              char.c = value.to_i
            when "CH"
              # hex: '<20>' -> '0x20' -> .to_i -> 32
              char.c = value.sub(/</,'0x').sub(/>/,'').to_i(16)
            when "WX"
              char.wx = value.to_i
              # for "L", check  /var/www/mirror/system/tex/texmf-local/fonts/afm/jmn/hans/hans.afm
            when "L", nil
              #ignore
            else
              char.send((key.downcase + "=").to_sym,value.to_i)
            end
          }

          @chars[char.name]=char
          # update information about encoded/unencoded
          if char.c > -1
            @count_charmetrics_encoded += 1
          else
            @count_charmetrics_unencoded += 1
          end
        end
        raise "never reached"
      end
      def get_kerndata
        loop do
          kw = get_keyword
          STDERR.puts "kw=" + kw if @verbose
          case kw
          when "EndKernData"
            return
          when "StartKernPairs"
            get_kernpairs
          when "StartTrackKern"
            # TrackKern
            get_trackkern
          else
            # KernPairs0
            # KernPairs1
            raise "not implemented"
          end
        end
        raise "never reached"
      end
      def get_composites
        count = get_integer
        loop do
          kw = get_keyword
          STDERR.puts "get_composites keyword = '" + kw + "'" if @verbose
          case kw
          when "CC"
            get_composite
          when "EndComposites"
            return
          else
            STDERR.puts "next to read = " +  @s.string[@s.pos,40]
            raise "AFM error"
          end
        end
        raise "never reached"
      end
      def get_composite
        glyphname = get_name
        count = get_integer
        @s.skip_until(/;\s+/)
        count.times do 
          nextstring = get_name
          raise "AFM Error" unless nextstring == "PCC"
          [get_number,get_number]
          @s.skip_until(/;/)
        end
      end

      def get_trackkern
        count = get_integer
        loop do
          case get_keyword
          when "EndTrackKern"
            return
          when "TrackKern"
            # TrackKern degree min-ptsize min-kern max-ptsize max-kern
            [get_integer,get_number,get_number,get_number,get_number]
          else
            raise "afm error"
          end
        end
        raise "never reached"
      end

      def get_kernpairs
        count = get_integer
        loop do
          case get_keyword
          when "KPX"     # y is 0
            name=get_name
            # if @info['chars'][name]
            if @chars[name]
              # array is [x,y] kerning
              destname,num=get_name,get_number
              # somethimes something stupid like
              # KPX .notdef y -26
              # KPX A .notdef -43
              # is in the afm data... :-( -> reject those entries
              # if @info['chars'][destname]
              if @chars[destname]
                @chars[name].kern_data[destname] = [num,0]
              else
                STDERR.puts "info: unused kern data for " + name if @verbose
              end
            else
              # ignore this entry, print a message
              STDERR.puts "info: unused kern data for " + name if @verbose
              @something_strange=true
              [get_name,get_number] # ignored
            end
          when "EndKernPairs"
            return
          else
            STDERR.puts @s.pos
            raise "not implmented"
          end
        end
        raise "never reached"
      end
    end
  end
end