File: make-data.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 (320 lines) | stat: -rw-r--r-- 12,503 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
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
;;; 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.

(defpackage "MAKE-CEDILLA-DATA"
  (:use "CL")
  (:import-from "CEDILLA"
                "*COMBINING-CLASS-TABLE*"
                "*CANONICAL-DECOMPOSITION-TABLE*"
                "*GLYPH-NAMES*"
                "NORMALISE-CCS")
  (:export "MAKE-DATA"))

(in-package "MAKE-CEDILLA-DATA")

(defun split-string (string &optional (separator #\;))
  (do ((pos (position separator string)
            (and pos (position separator string :start (+ pos 1))))
       (old-pos -1 pos)
       (strings '()))
      ((null old-pos) (nreverse strings))
    (push (subseq string (+ 1 old-pos) pos) strings)))

(defun read-data-line (in &optional (separator #\;))
  (let ((line (read-line in nil in)))
    (cond
      ((eql line in) in)
      ((zerop (length line)) nil)
      ((eql (aref line 0) #\#) nil)
      (t (split-string line separator)))))

(defun read-unicode-data-line (stream)
  (let ((line (read-data-line stream)))
    (declare (list line))
    (cond
      ((eql line stream) stream)
      ((null line) nil)
      (t
       (unless (>= (length line) 10)
         (error "Malformed UnicodeData line"))
       (let ((codepoint
              (let ((*read-base* 16)) (read-from-string (car line))))
             (combining-class 
              (if (zerop (length (nth 3 line)))
                  nil
                  (and (equal (nth 4 line) "NSM")
                       (read-from-string (nth 3 line)))))
             (decomp (parse-decomp (nth 5 line))))
         (list codepoint combining-class decomp))))))

(defun parse-decomp (string)
  (if (zerop (length string))
      nil
      (let* ((start 
              (if (eql (aref string 0) #\<)
                  (+ 1 (position #\Space string))
                  0))
             (type
              (if (> start 0)
                  (intern (string-upcase (subseq string 1 (- start 2)))
                          "KEYWORD")
                  nil))
             (vals
              (with-input-from-string (in string :start start)
                (let ((v '()))
                  (loop
                   (let ((val 
                          (let ((*read-base* 16)) (read in nil in))))
                     (when (eql val in)
                       (return (nreverse v)))
                     (push val v)))))))
        (cons type vals))))

(defun read-data-file (filename parser)
  (let ((data '()))
    (with-open-file (in filename)
      (loop
       (let ((line (funcall parser in)))
         (when (eql line in)
           (return (nreverse data)))
         (when line
           (push line data)))))))

(defun read-glyph-list-line (stream)
  (let ((line (read-data-line stream)))
    (cond
      ((eql line stream) stream)
      ((null line) nil)
      (t
       (unless (>= (length line) 2)
         (error "Malformed Glyph List line"))
       (let ((codepoint
              (let ((*read-base* 16)) (read-from-string (cadr line))))
             (name (car line)))
         (if (<= #xE000 codepoint #xF8FF)
             nil
             (list codepoint name)))))))

(defun list-to-ccs-or-string (char list)
  "Takes a list of codepoints, returns a CCS-OR-STRING."
  (flet ((ccs (base pre-ccs)
           (if pre-ccs (append pre-ccs base) base))
         (check-consistency (base pre-ccs)
           (when (or (gethash char *combining-class-table*) 
                     (null pre-ccs))
             (unless (eql (not base)
                          (not (null (gethash char *combining-class-table*))))
               (warn "Inconsistent decomposition for U+~4,'0X"
                     (char-code char))
               (return-from list-to-ccs-or-string nil)))))
    (do ((l list (cdr l)) (base nil) (pre-ccs '()) (ccs-list '()))
        ((null l)
         (unless (and (null pre-ccs) (null base))
           (when (null ccs-list)
             (check-consistency base pre-ccs))
           (cond
             (base (push (ccs base pre-ccs) ccs-list))
             (t (push (ccs (car pre-ccs) (cdr pre-ccs)) ccs-list))))
         (when (null ccs-list) (error "No CCS in list"))
         (if (null (cdr ccs-list))
             (car ccs-list)
             (coerce (nreverse ccs-list) 'vector)))
      (let ((char (code-char (car l))))
        (cond
          ((gethash char *combining-class-table*)
           (push char pre-ccs))
          (t
           (unless (and (null pre-ccs) (null base))
             (when (null ccs-list)
               (check-consistency base pre-ccs))
             (cond
               (base (push (ccs base pre-ccs) ccs-list))
               (t (push (ccs (car pre-ccs) (cdr pre-ccs)) ccs-list))))
           (setf base char pre-ccs '())))))))

(defun printable-ccs (ccs)
  (typecase ccs
    (vector
     (if (every #'characterp ccs)
         `(coerce ',(coerce ccs 'list) 'string)
         `(vector ,@(mapcar #'printable-ccs (coerce ccs 'list)))))
    (cons `',ccs)
    (t ccs)))

(defun make-data (out-filename unicode-data-filename glyph-list-filename)
  (let ((unicode-data
         (read-data-file unicode-data-filename #'read-unicode-data-line))
        (glyph-list
         (read-data-file glyph-list-filename #'read-glyph-list-line))
        (combining-classes '())
        (canon-decomps '())
        (good-decomps-table (make-hash-table :test 'equal))
        (bad-decomps-table (make-hash-table :test 'equal))
        (good-decomps '()) (bad-decomps '()) (glyph-names '())
        (*combining-class-table* (make-hash-table :test 'equal))
        (*canonical-decomposition-table* (make-hash-table :test 'equal))
        (*glyph-names* (make-hash-table :test 'equal)))
    (dolist (entry unicode-data)
      (let ((char (code-char (car entry)))
            (class (cadr entry)))
        (when (cadr entry)
          (when char
            (setf (gethash char *combining-class-table*) class)
            (push (cons char class) combining-classes)))))
    ;; Build the canonical decomposition table.
    (dolist (entry unicode-data)
      (let ((char (code-char (car entry)))
            (decomp-type (car (caddr entry)))
            (decomp (cdr (caddr entry))))
        (when (and char decomp)
          (let ((ccs (list-to-ccs-or-string char decomp)))
            (when ccs
              (cond
                ((null decomp-type) 
                 (setf (gethash char *canonical-decomposition-table*)
                       ccs))))))))
    ;; Build the other tables.
    (dolist (entry unicode-data)
      (let ((char (code-char (car entry)))
            (decomp-type (car (caddr entry)))
            (decomp (cdr (caddr entry))))
        (when (and char decomp)
          (let ((ccs (list-to-ccs-or-string char decomp)))
            (when ccs
              (let ((n-char (normalise-ccs char))
                    (n-ccs (normalise-ccs ccs)))
                (cond
                  ((null decomp-type) 
                   (push (cons char n-ccs) canon-decomps))
                  ((member decomp-type '(:compat))
                   (push n-ccs (gethash n-char good-decomps-table))
                  ;; provide inverse mapping for spacing diacritics
                  (when (and (listp ccs) (eql (cdr ccs) #\Space))
                    (push char (gethash (normalise-ccs (car ccs))
                                        good-decomps-table))))
                  ((member decomp-type '(:circle :sub :super
                                         :small :narrow :wide))
                   (push (cons decomp-type n-ccs) 
                         (gethash n-char good-decomps-table)))
                  (t (push n-ccs (gethash n-char bad-decomps-table))))))))))
    (dolist (entry glyph-list)
      (let ((ccs (normalise-ccs (code-char (car entry))))
            (name (cadr entry)))
        (push name (gethash ccs *glyph-names*))))
    (setf combining-classes (nreverse combining-classes)
          canon-decomps (nreverse canon-decomps))
    (maphash
     #'(lambda (key val) (push (cons key val) good-decomps))
     good-decomps-table)
    (maphash
     #'(lambda (key val) (push (cons key val) bad-decomps))
     bad-decomps-table)
    (maphash
     #'(lambda (key val) (push (cons key val) glyph-names))
     *glyph-names*)
    (with-open-file (out out-filename :direction :output)
      (with-standard-io-syntax
        (let ((*print-pretty* nil)
              (*package* (find-package "MAKE-CEDILLA-DATA")))
          (format out ";;; Automatically generated file -- do not modify~%")
          (print '(in-package "CEDILLA") out)
          (print
           `(eval-when (load eval)
             (let ((cct *combining-class-table*))
               (flet ((frob (char ccs)
                       (setf (gethash char cct) ccs)))
                 ,@(mapcar
                    #'(lambda (entry) 
                        `(frob ,(car entry) ,(printable-ccs (cdr entry))))
                    combining-classes))))
           out)
          (print
           `(eval-when (load eval)
             (let ((cdt *canonical-decomposition-table*))
               (flet ((frob (char ccs)
                       (setf (gethash char cdt) ccs)))
                 ,@(mapcar
                    #'(lambda (entry) 
                        `(frob ,(car entry) 
                          ,(printable-ccs (cdr entry))))
                    canon-decomps))))
           out)
          (print
           `(eval-when (load eval)
             (let ((at *alternatives-table*))
               (flet ((frob (char list)
                       (setf (gethash char at) list)))
                 ,@(mapcar
                    #'(lambda (entry) 
                        `(frob ,(car entry) 
                          (list ,@(nreverse (mapcar #'printable-ccs
                                                    (cdr entry))))))
                    good-decomps))))
           out)
          (print
           `(eval-when (load eval)
             (let ((ft *fallbacks-table*))
               (flet ((frob (char list)
                       (setf (gethash char ft) list)))
                 ,@(mapcar
                    #'(lambda (entry) 
                        `(frob ,(car entry) 
                          (list ,@(nreverse (mapcar #'printable-ccs 
                                                    (cdr entry))))))
                    bad-decomps))))
           out)
          (print
           `(eval-when (load eval)
             (let ((gn *glyph-names*))
               (flet ((frob (char name)
                       (setf (gethash char gn) name)))
                 ,@(mapcar
                    #'(lambda (entry)
                        `(frob 
                          ,(printable-ccs (car entry))
                          ',(reverse (cdr entry))))
                    glyph-names))))
           out)))))
  nil)

(defun read-unicode-data-line-for-printing (stream)
  (let ((line (read-data-line stream)))
    (declare (list line))
    (cond
      ((eql line stream) stream)
      ((null line) nil)
      (t
       (unless (>= (length line) 10)
         (error "Malformed UnicodeData line"))
       (list (let ((*read-base* 16)) (read-from-string (car line)))
             (cadr line))))))

(defun make-unicode-sample (out-filename unicode-data-filename
                            &optional (from #x20) (to #x2FFF))
  (let ((data
         (read-data-file unicode-data-filename
                         #'read-unicode-data-line-for-printing)))
    (let ((a (make-array #x10000)))
      (dolist (e data)
        (when (and e (< (car e) #x10000))
          (setf (aref a (car e)) (cadr e))))
      (with-open-file (out out-filename :direction :output
                           :external-format charset:utf-8)
        (loop for i from from upto to
              when (aref a i)
              do (format out "~4,'0X ~A ~A~%"
                         i (code-char i) (aref a i)))))))

;;; (make-data "data.lisp" 
;;;            "/usr/local/share/doc/unicode/UNIDATA/UnicodeData.txt" 
;;;            "/usr/local/share/doc/adobe/glyphlist.txt")