File: afm.lisp

package info (click to toggle)
cedilla 0.6-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 812 kB
  • ctags: 308
  • sloc: lisp: 3,716; makefile: 50; sh: 13
file content (283 lines) | stat: -rw-r--r-- 10,426 bytes parent folder | download | duplicates (2)
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
;;; This file is part of Cedilla.
;;; Copyright (C) 2002 by Juliusz Chroboczek.

;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.

;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.

(in-package "CEDILLA")

(defvar *afm-buffer*)

(defun read-afm-file (filename &key encoding fixed-encoding resources omit)
  (let ((res
         (mapcar #'identify-resource resources)))
    (when (member nil res)
      (return-from read-afm-file nil))
    (let ((font 
           (if fixed-encoding
               (make-fixed-encoding-ps-font :encoding fixed-encoding 
                                            :resources res)
               (make-named-glyph-ps-font :resources res
                                         :encoding encoding)))
          (*afm-buffer* (make-array 30 
                                    :element-type 'character
                                    :fill-pointer 0 :adjustable t)))
      (handler-bind
          ((error
            #'(lambda (c)
                (warn "Unable to read AFM file ~A: ~A"
                      filename c)
                (return-from read-afm-file nil))))
        (with-open-file (in filename :direction :input)
          (read-afm-data in font omit))
        (resolve-composite-glyphs font)
        font))))

(defun read-afm-data (in font omit)
  (let ((token (afm-token in)))
    (unless (equal token "StartFontMetrics")
      (error "Couldn't read header"))
    (afm-skip-eol in)
    (loop
     (let ((token (afm-token in)))
       (cond
         ((eql token :eof) (error "Unexpected EOF"))
         ((equal token "FontName")
          (let ((font-name (afm-string in)))
            (unless (stringp font-name)
              (error "Malformed FontName line"))
            (setf (font-name font) font-name)))
         ((equal token "FontBBox")
          (let ((x0 (afm-number in))
                (y0 (afm-number in))
                (x1 (afm-number in))
                (y1 (afm-number in)))
            (afm-eol in)
            (setf (font-x0 font) x0
                  (font-y0 font) y0
                  (font-x1 font) x1
                  (font-y1 font) y1)))
         ((equal token "StartCharMetrics")
          (afm-number in)
          (afm-eol in)
          (afm-read-char-metrics in font omit))
         ((equal token "StartComposites")
          (afm-number in)
          (afm-eol in)
          (afm-read-composites in font omit))
         ((equal token "EndFontMetrics")
          (return-from read-afm-data font))
         (t (afm-skip-eol in)))))))

(defun afm-read-char-metrics (in font omit)
  (loop
   (let ((token (afm-token in)))
     (cond
       ((eql token :eof) (error "Unexpected EOF"))
       ((equal token "EndCharMetrics") 
        (afm-eol in) 
        (return-from afm-read-char-metrics nil))
       ((equal token "C")
        (multiple-value-bind (glyph index) (afm-read-c-line in)
          (when glyph
            (unless (member (pre-glyph-name glyph) omit :test 'equal)
              (unless (get-glyph font (pre-glyph-name glyph) index)
                (put-glyph font glyph index))))))
       (t (afm-skip-eol in))))))

(defun afm-read-c-line (in)
  (let (c n w x0 y0 x1 y1)
    (setf c (afm-number in))
    (loop
     (tagbody
        (let ((token (afm-token in)))
          (unless (equal token ";")
            (error "Expected semicolon")))
      no-semicolon
        (let ((token (afm-token in)))
          (cond
            ((or (eql token :eof) (eql token :eol))
             (return-from afm-read-c-line
               (values
                (make-font-glyph :name n :width w :x0 x0 :y0 y0 :x1 x1 :y1 y1)
                c)))
            ((equal token ";") (error "Unexpected semicolon"))
            ((equal token "WX") (setf w (afm-number in)))
            ((equal token "N") (setf n (afm-name in)))
            ((equal token "B")
             (setf x0 (afm-number in)
                   y0 (afm-number in)
                   x1 (afm-number in)
                   y1 (afm-number in)))
            (t
             (loop for token = (afm-token in)
                   until (or (eql token :eof) (eql token :eol)
                             (equal token ";")))
             (go no-semicolon))))))))

(defun afm-read-composites (in font omit)
  (loop
   (let ((token (afm-token in)))
     (cond
       ((eql token :eof) (error "Unexpected EOF"))
       ((equal token "EndComposites") 
        (afm-eol in) 
        (return-from afm-read-composites nil))
       ((equal token "CC")
        (multiple-value-bind (glyph index) (afm-read-cc-line in)
          (when glyph
            (unless (member (pre-glyph-name glyph) omit :test 'equal)
              (unless (get-glyph font (pre-glyph-name glyph) index)
                (put-glyph font glyph))))))
       (t (afm-skip-eol in))))))

(defun afm-read-cc-line (in)
  (let (cc pcc)
    (setf cc (afm-name in))
    (afm-number in)
    (loop
     (tagbody
        (let ((token (afm-token in)))
          (unless (equal token ";")
            (error "Expected semicolon")))
        no-semicolon
        (let ((token (afm-token in)))
          (cond
            ((or (eql token :eof) (eql token :eol))
             (return-from afm-read-cc-line
               (make-unresolved-composite-glyph 
                :name cc :components (nreverse pcc))))
            ((equal token "PCC")
             (push (list (afm-name in) 
                         (afm-number in) (afm-number in))
                   pcc))
            ((equal token ";") (error "Unexpected semicolon"))
            (t
             (loop for token = (afm-token in)
                   until (or (eql token :eof) (eql token :eol)
                             (equal token ";")))
             (go no-semicolon))))))))

(defun afm-token (in)
  (let ((afm-buffer *afm-buffer*)
        (char (read-char in nil in)))
    (flet ((stuff ()
             (when (eql char in) (return-from afm-token :eof))
             (when (eql char #\Return)
               (setf char (read-char in nil in))
               (when (eql char in) (return-from afm-token :eof))
               (unless (eql char #\Newline) (unread-char char in)))
             (when (eql char #\Newline) (return-from afm-token :eol))))
      (stuff)
      (loop while (or (eql char #\Space) (eql char #\Tab))
            do (setf char (read-char in nil in)))
      (stuff)
      (setf (fill-pointer afm-buffer) 0)
      (loop
       do (vector-push-extend char afm-buffer)
       do (setf char (read-char in nil in))
       while (not (or (member char '(#\Space #\Tab #\Newline #\Return))
                      (eql char in))))
      (unless (eql char in)
        (unread-char char in)))
    afm-buffer))

(defun afm-number (in)
  (let ((buf (afm-token in)))
    (unless (and (stringp buf) (> (length buf) 0))
      (error "Expected a number"))
    (if (and (eql (aref buf 0) #\<)
             (eql (aref buf (- (length buf) 1)) #\>))
        (let* ((*read-base* 16)
               (n (ignore-errors 
                    (read-from-string buf :start 1 :end (- (length buf) 1)))))
          (unless (realp n) (error "Expected a number"))
          n)
        (let ((n (ignore-errors (read-from-string buf))))
          (unless (realp n) (error "Expected a number"))
          n))))

(defun afm-boolean (in)
  (let ((buf (afm-token in)))
    (unless (stringp buf)
      (error "Expected a Boolean"))
    (not (string-equal buf "false"))))

(defun afm-name (in)
  (let ((buf (afm-token in)))
    (unless (stringp buf)
      (error "Expected a name"))
    (copy-seq buf)))

(defun afm-string (in)
  (let ((afm-buffer *afm-buffer*))
    (let (char)
      (loop do (setf char (read-char in nil in))
            while (or (eql char #\Space) (eql char #\Tab)))
      (when (or (eql char in) (eql char #\Newline))
        (return-from afm-string ""))
      (when (eql char #\Return)
        (setf char (read-char in nil in))
        (unless (or (eql char #\Newline) (eql char in))
          (unread-char char in))
        (return-from afm-string ""))
      (setf (fill-pointer afm-buffer) 0)
      (vector-push-extend char afm-buffer))
    (loop for char = (read-char in nil in)
          while (not (or (eql char in) (eql char #\Newline)))
          do (vector-push-extend char afm-buffer))
    (copy-seq afm-buffer)))

(defun afm-skip-eol (in)
  (let (char)
    (loop do (setf char (read-char in nil in))
          while (not (or (eql char in) 
                         (eql char #\Return) (eql char #\Newline))))
    (when (eql char #\Return)
      (let ((char (read-char in nil in)))
        (unless (or (eql char #\Newline) (eql char in))
          (unread-char char in)))))
  nil)

(defun afm-eol (in)
  (let (char)
    (loop do (setf char (read-char in nil in))
          while (not (or (eql char in) 
                         (eql char #\Return) (eql char #\Newline)))
          unless (or (eql char #\Space) (eql char #\Tab))
          do (error "Unexpected input character ~A, should be eol" char))
    (when (eql char #\Return)
      (let ((char (read-char in nil in)))
        (unless (or (eql char #\Newline) (eql char in))
          (unread-char char in)))))
  nil)

(defun resolve-composite-glyphs (font)
  (when (typep font 'named-glyph-font)
    (maphash
     #'(lambda (name glyph)
         (when (unresolved-composite-glyph-p glyph)
           (let ((width
                  (glyph-width 
                   (gethash (caar (unresolved-composite-glyph-components glyph))
                            (font-glyphs font))))
                 (components
                  (mapcar
                   #'(lambda (c)
                       (make-composite-component
                        :glyph (gethash (car c) (font-glyphs font))
                        :dx (cadr c) :dy (caddr c)))
                   (unresolved-composite-glyph-components glyph))))
             (put-glyph font
                        (make-composite-glyph 
                         :name name :width width
                         :components components)))))
     (font-glyphs font)))
  font)