File: cedilla.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 (178 lines) | stat: -rw-r--r-- 7,393 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
;;; 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")

(defun cedilla (in-filename &optional (out-filename *standard-output*)
                &key
                (wrap nil)
                (header nil) (header-baseline (/ 72 2.54))
                (footer nil) (footer-baseline (/ 72 2.54))
                (fontset "courier")
                (paper-size "a4")
                (size 11)
                (baselineskip size))
  "Main Cedilla entry point."
  (let ((font-list (find-fontset fontset))
        (paper (find-paper-size paper-size)))
    (let ((*known-glyphs* (make-hash-table :test 'equal)))
      (dolist (s (append header footer))
        (when s (compute-header-glyphs s font-list)))
      (with-open-file (in in-filename :external-format 'charset:utf-8)
        (compute-stream-glyphs in font-list))
      (with-open-file (in in-filename :external-format 'charset:utf-8)
        (with-open-file-or-stream (out out-filename :direction :output)
          (typeset-file in out font-list
                        paper
                        :wrap wrap
                        :header header :footer footer
                        :header-baseline header-baseline
                        :footer-baseline footer-baseline
                        :size size :baselineskip baselineskip))))))

(defun cedilla-list-all (&optional (fontset "courier"))
  (let ((font-list (find-fontset fontset)))
    (let ((*known-glyphs* (make-hash-table :test 'equal)))
      (loop for i from 0 upto #xFFFF
            do (let* ((c (code-char i))
                      (ccs (normalise-ccs c))
                      (g (compute-glyph ccs font-list nil)))
                 (when g
                   (if (ccs-combining-class ccs)
                       (format t "~04X:  ~C ~S~%" i c c)
                     (format t "~04X: ~C ~S~%" i c c))))))))

(defun find-fontset (name)
  (let ((spec (get (intern (string-upcase name) "CEDILLA") 'fontset)))
    (when (null spec)
      (error "Unknown fontset ~A" name))
    (or
     (mapcan
      #'(lambda (entry)
          (let ((font
                 (ecase (car entry)
                   ((:afm) (apply #'read-afm-file (cdr entry)))
                   ((:space) (apply #'make-space-font (cdr entry)))
                   ((:built-in) (apply #'make-built-in-font (cdr entry))))))
           (if font (list font) '())))
      spec)
     (error "Fontset ~A contains no useable fonts" name))))
                 
(defun find-paper-size (name)
  (or (get (intern (string-upcase name) "CEDILLA") 'paper-size)
      (error "Unknown paper size ~A" name)))

(defun split-comma-string (string)
  (do* ((strings '())
        (s 0 (and e (+ e 1)))
        (e (position #\, string) (and s (position #\, string :start s))))
      ((not s) (nreverse strings))
    (push (subseq string s e) strings)))

(defun parse-header (string)
  (let ((l (split-comma-string string)))
    (case (length l)
      ((1) (list nil (car l) nil))
      ((2) (list (car l) (cadr l) nil))
      ((3) l)
      (t (error "A header or footer may have at most three elements")))))

(defun cedilla-with-args (args)
  (let ((fontset nil) (paper-size nil) (size nil) (baselineskip nil)
        (wrap nil)
        (header nil) (header-baseline nil) (footer nil) (footer-baseline nil)
        (list nil))
    (loop
     (cond
      ((null args) (return nil))
      ((equal (car args) "-fs")
       (setf fontset (cadr args) args (cddr args)))
      ((equal (car args) "-p")
       (setf paper-size (cadr args) args (cddr args)))
      ((equal (car args) "-s")
       (let ((s (read-from-string (cadr args))))
         (unless (realp s)
           (error "Size should be a number, not ~A" (cadr args)))
         (setf size s args (cddr args))))
      ((equal (car args) "-bs")
       (let ((b (read-from-string (cadr args))))
         (unless (realp b)
           (error "Baselineskip should be a number, not ~A" (cadr args)))
         (setf baselineskip b args (cddr args))))
      ((equal (car args) "-w")
       (setf wrap t args (cdr args)))
      ((equal (car args) "-h")
       (setf header (parse-header (cadr args)) args (cddr args)))
      ((equal (car args) "-hb")
       (let ((hb (read-from-string (cadr args))))
         (unless (realp hb)
           (error "Header-baseline should be a number, not ~A" (cadr args)))
         (setf header-baseline hb args (cddr args))))
      ((equal (car args) "-f")
       (setf footer (parse-header (cadr args)) args (cddr args)))
      ((equal (car args) "-fb")
       (let ((fb (read-from-string (cadr args))))
         (unless (realp fb)
           (error "Footer-baseline should be a number, not ~A" (cadr args)))
         (setf footer-baseline fb args (cddr args))))
      ((equal (car args) "-v")
       (setf *cedilla-verbose* t args (cdr args)))
      ((equal (car args) "-l")
       (setf list t args (cdr args)))
      ((equal (car args) "-V")
       (format *error-output* 
               "Cedilla v. ~A, by Juliusz Chroboczek.~%" *cedilla-version*)
       (quit 0))
      ((or (equal (car args) "-?")
           (equal (car args) "-help") (equal (car args) "--help"))
       (format *error-output*
               "cedilla [-v] [-fs fontset] [-p papersize] [-s size] ~
                   [-bs baselineskip]~%")
       (format *error-output*
               "        [-h header] [-hb header-baseline ~
                         [-f footer] [-fb footer-baseline]~%")
       (format *error-output*
               "        infile [outfile]~%")
       (quit 0))
      ((equal (car args) "--")
       (setf args (cdr args)) (return nil))
      ((eql (aref (car args) 0) #\-)
       (error "Unknown option ~A -- try \"cedilla -?\" for help" (car args)))
      (t (return nil))))
    (cond
     (list (apply #'cedilla-list-all (and fontset (list fontset))))
     (t    
      (unless (<= 1 (length args) 2)
        (error "Wrong number of arguments -- try \"cedilla -?\" for help"
               (length args)))
      (apply #'cedilla
             (car args) (or (cadr args) *standard-output*)
             (nconc
              (if fontset (list :fontset fontset))
              (if wrap (list :wrap wrap))
              (if header (list :header header))
              (if header-baseline (list :header-baseline header-baseline))
              (if footer (list :footer footer))
              (if footer-baseline (list :footer-baseline footer-baseline))
              (if paper-size (list :paper-size paper-size))
              (if size (list :size size))
              (if baselineskip (list :baselineskip baselineskip))))))))

(defun cedilla-main ()
  "Establish an error-handler and run Cedilla."
  (handler-bind ((error 
                  #'(lambda (c) 
                      (format *error-output* "Cedilla: ~A." c)
                      (quit 1))))
    (cedilla-with-args *args*)
    (quit 0)))