]> git.donarmstrong.com Git - lilypond.git/blob - scripts/as2text.scm
patch::: 1.3.102.jcn1
[lilypond.git] / scripts / as2text.scm
1 #!@GUILE@ \
2 -e main -s
3 !#
4 ;;;; as2text.scm -- Translate AsciiScript to Text
5 ;;;;
6 ;;;; source file of the GNU LilyPond music typesetter
7 ;;;; 
8 ;;;; (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
9
10 ;;;; library funtions
11 (use-modules
12    (ice-9 debug)
13   (ice-9 getopt-long)
14   (ice-9 string-fun)
15   (ice-9 regex))
16
17 ;;; Script stuff
18 (define program-name "as2text")
19
20 (define lily-home "/usr/share/lilypond")
21 (define cur-output-name "-")
22 (define cur-output-file '())
23
24 (define subst-version "@TOPLEVEL_VERSION@")
25
26 (define program-version         
27   (if (eq? subst-version (string-append "@" "TOPLEVEL_VERSION" "@"))
28       "unknown"
29       subst-version))
30
31 (define (show-version port)
32   (display (string-append 
33             program-name " - LilyPond version " program-version "\n")
34            port))
35
36 (define (show-help)
37   (display "Convert AsciiScript to text.
38
39
40 Usage: as2text [OPTION]... AS-FILE
41
42 Options:
43   -h,--help          this help
44   -o,--output=FILE   set output file
45   -v,--version       show version
46 "))
47
48 ;;
49 ;; FIXME: use (separate-fields-discarding-char) and (read-delimited "")
50 ;;
51 (define (gulp-file name)
52   (let ((port (if (equal? name "-")
53                   (current-input-port)
54                   (catch 'system-error (lambda () (open-file name "r"))
55                          (lambda args #f)))))
56         (if port 
57             (begin
58              (display (string-append "[" name) (current-error-port))
59              (let ((content (let loop ((text ""))
60                                  (let ((line (read-line port)))
61                                       (if (or (eof-object? line)
62                                               (not line)) 
63                                           text
64                                           (loop (string-append text line "\n")))))))
65                   (close port)
66                   (display "]" (current-error-port))
67                   content))
68             (begin
69              (display 
70               (string-append "warning: no such file: " name "\n")
71               (current-error-port))
72              "")))) 
73
74 (define (with-extention name ext)
75   (if (or (equal? name "-") 
76           (equal? ext (substring name (max 0 (- (string-length name) 
77                                                 (string-length ext))))))
78       name 
79       (string-append name ext)))
80
81 (define (do-file file-name output-name)
82   (let ((ascii-script (gulp-file (with-extention file-name ".as"))))
83        ;; urg
84        (set! cur-output-name output-name)
85        (eval-string ascii-script)))
86
87 ;;; Script entry point
88 (define (main args)
89   (set! lily-home (string-append 
90                      (dirname (dirname (car args))) 
91                      "/share/lilypond"))
92   (let ((options (getopt-long args
93                               `((output (single-char #\o)
94                                           (value #t))
95                                 (help (single-char #\h))
96                                 (version (single-char #\v))))))
97     (define (opt tag default)
98       (let ((pair (assq tag options)))
99         (if pair (cdr pair) default)))
100
101     (if (assq 'help options)
102         (begin (show-version (current-output-port)) (show-help) (exit 0)))
103
104     (if (assq 'version options)
105         (begin (show-version (current-output-port)) (exit 0)))
106
107     (show-version (current-error-port))
108     (let ((output-name (opt 'output "-"))
109           (files (let ((foo (opt '() '())))
110                       (if (null? foo) 
111                           (list "-")
112                           foo))))
113          (do-file (car files) output-name))))
114
115 ;;;;
116 ;;;; Ascii Script plotting
117 ;;;;
118
119 ;;; Global variables
120
121 ;; Ascii-Art signature
122 (define tag-line "lily")
123
124 (define first-line #t)
125
126 ;; cursor
127 (define cur-x 0)
128 (define cur-y 0)
129
130 ;; canvas dimensions
131 (define canvas-width 65)
132 (define canvas-height 20)
133
134 ;; font database
135 (define fonts '())
136
137 ;; current font
138 (define cur-font "")
139
140 ;; 
141 (define line-char "-")
142
143 ;; 
144 (define half-char-kludge #f)
145
146 ;; the plotting fields
147 (define canvas 0)
148 ;; urg: 
149 ;; make-uniform array of characters,
150 ;; or 1-dim array of strings?
151 ;; (set! canvas (make-array " " canvas-height canvas-width))
152
153 ;; urg, this kind of naming costs too much indenting
154 (define (split c s r)
155   (separate-fields-discarding-char c s r))
156
157 (define (strip s)
158   (sans-surrounding-whitespace s))
159
160
161 ;;; Helper functions
162
163 (define (af-gulp-file name)
164   ;; urg
165   (let ((old-load-path %load-path))
166        (set! %load-path 
167              (cons (string-append 
168                     (or (getenv 'LILYPONDPREFIX) ".") "/mf")
169                    (cons (string-append lily-home "/mf") %load-path)))
170        (let* ((path (%search-load-path name)) 
171               (text (if path
172                         (gulp-file path)
173                         (gulp-file name))))
174              (set! %load-path old-load-path)
175              text)))
176
177 (define (char-width c)
178   (let ((bbox (car c)))
179        (inexact->exact (* .001 (caddr bbox)))))
180
181 ;; urg: use smart table
182 (define (xmerge-chars old new)
183   (cond ((equal? new " ") old)
184         ((and (equal? old "|") (equal? new "-")) "+")
185         ((and (equal? old "-") (equal? new "|")) "+")
186         (else new)))
187
188 (define (merge-chars old new)
189   (cond ((equal? new " ") old)
190         (else new)))
191
192 (define (plot x y c)
193   (let ((ny (- (* -1 y) 1)))
194        (if (array-in-bounds? canvas ny x)
195            (array-set! canvas (merge-chars (array-ref canvas ny x) c) ny x)
196            (display (string-append "ouch: " (number->string x)  ","
197                                    (number->string ny) "\n")))))
198
199 (define (plot-char c)
200   (let ((bbox (car c))
201         (glyph (cadr c))) 
202        ;; BBox: (llx lly urx ury) * 1000
203        (let ((dx (inexact->exact (* .001 (car bbox))))
204              ;(dy (inexact->exact (* .001 (cadr bbox))))
205              (dy (inexact->exact (- (* .001 (cadddr bbox)) 1)))
206              (len (length glyph)))
207             ;;(display "Bbox: ") (display bbox) (newline)
208             ;;(display "dy: ") (display dy) (newline)
209             (do ((line glyph (cdr line))
210                  (i 0 (+ i 1)))
211                 ((= i len))
212                 (plot-string (+ cur-x dx) (+ (- cur-y i) dy) (car line))))))
213
214 (define (plot-string x y s)
215   (do ((i 0 (+ i 1)))
216       ((= i (string-length s)))
217       (plot (+ x i) y (substring s i (+ i 1)))))
218
219 (define (show-char char)
220   (display char))
221
222 (define (show-font name)
223         (let ((font (assoc name fonts)))
224              (map (lambda (x) (show-char x)) font)))
225
226 (define (sign x)
227   (if (= x 0)
228       1
229       (inexact->exact (/ x (abs x)))))
230
231 (define (generate-default-font)
232   (let loop ((chars '()) (i 0))
233        (if (= 256 i) 
234            chars 
235            (loop 
236             (cons (list i '(0 0 1000 1000) 
237                         (list (make-string 1 (integer->char i)))) 
238                   chars) 
239             (+ i 1)))))
240
241 (define (get-font name)
242   ;; urg
243   (if (equal? (substring name 0 (min (string-length name) 8)) "as-dummy")
244       (get-font "default")
245       (let ((entry (assoc name fonts)))
246            (if entry
247                (cdr entry)
248                (begin
249                 (display 
250                  (string-append "warning: no such font: " name "\n")
251                  (current-error-port))
252                 (get-font "default"))))))
253
254 (define (get-char font n)
255   (let ((entry (assoc n font)))
256        (if entry
257            (cdr entry)
258            (begin
259             (display 
260              (string-append "warning: no such char: (" 
261                             cur-font
262                             ", "
263                             (number->string n ) ")\n")
264              (current-error-port))
265             '()))))
266
267
268 ;;; AsciiScript commands
269
270 (define (char n)
271   (let* ((font (get-font cur-font))
272          (c
273           (if (and half-char-kludge
274                    (assoc (+ n 0.5) font))
275               (get-char font (+ n 0.5))
276               (get-char font n))))
277          (if c
278              (plot-char c))))
279
280 (define (end-output) 
281   (display (string-append 
282             (make-string 
283              (- canvas-width (string-length tag-line)) #\space)
284             tag-line "\n")
285            cur-output-file)
286   (close cur-output-file)
287   (set! cur-output-file '()))
288
289 (define (h-line len)
290   (let ((step (sign len)))
291        (do ((i 0 (+ i step)))
292            ((= i len))
293            (plot (+ cur-x i) cur-y line-char))))
294
295 (define (v-line len)
296   (let ((step (sign len)))
297        (do ((i 0 (+ i step)))
298            ((= i len)) (plot cur-x (+ cur-y i) line-char))))
299
300 (define (header x y)
301   ;(display (string-append x y "\n") (current-error-port))
302   "")
303
304 (define (header-end) "")
305
306 (define (rline-to dx dy)
307   (plot (inexact->exact cur-x) (inexact->exact cur-y) line-char)
308   (plot (inexact->exact (+ cur-x dx)) (inexact->exact (+ cur-y dy)) line-char)
309   (if (or (> (abs dx) 1) (> (abs dy) 1))
310       (let ((x cur-x)
311             (y cur-y)
312             (hx (/ dx 2))
313             (hy (/ dy 2))
314             )
315            (plot (inexact->exact (+ cur-x hx)) (inexact->exact (+ cur-y hy)) line-char)
316            (rline-to hx hy)
317            (move-to x y)
318            (rmove-to hx hy)
319            (rline-to hx hy)
320            )))
321
322 (define (dissect-char text)
323   (let* ((char (split #\nl text list))
324          (id (car char))
325          (code (string->number 
326                 (strip 
327                  (substring id 
328                             (+ (string-index id #\C) 1)
329                             (string-index id #\;)))))
330          (bbox (map string->number 
331                     (split #\space (strip (substring
332                                            id 
333                                            (+ (string-rindex id #\B) 1)
334                                            (string-rindex id #\;)))
335                            list))))
336         (list (list code bbox (cdr char)))))
337
338 (define (load-font name mag)
339   ;; urg: don't load dummy font
340   (if (not (equal? (substring name 0 (min (string-length name) 8)) "as-dummy"))
341       (let ((text (af-gulp-file (string-append name ".af"))))
342            (if (< 0 (string-length text))
343                (let* ((char-list (cdr 
344                                   (split #\np 
345                                          (regexp-substitute/global 
346                                           #f "\t[^\n]*\n" text 'pre "" 'post) 
347                                          list)))
348                       (font (apply append (map dissect-char char-list))))
349                      (if (< 0 (length font))
350                          (set! fonts (cons (cons name font) fonts))))))))
351
352 (define (number->rounded-exact x)
353   (* (sign x) (inexact->exact (abs x))))
354   
355 (define (move-to x y)
356   (set! cur-x x)
357   (let ((ey (number->rounded-exact y)))
358     (if (= 0.5 (- (abs ey) (abs y)))
359         (set! half-char-kludge #t)
360         (set! half-char-kludge #f))
361     (set! cur-y ey)))
362
363 (define (put c)
364   (plot cur-x cur-y c))
365
366 (define (rmove-to dx dy)
367   (set! cur-x (+ cur-x dx))
368   (set! cur-y (+ cur-y dy)))
369
370 (define (select-font name)
371   (set! cur-font name))
372
373 (define (set-line-char c)
374   (set! line-char c))
375
376 (define (start-line height)
377   (if first-line 
378       (begin
379        (set! fonts (cons (cons "default" (generate-default-font)) fonts))
380        (display "\n" (current-error-port))
381        (if (and (defined? 'mudelapaperlinewidth)
382                 (> (string->number mudelapaperlinewidth) 0))
383            (set! canvas-width 
384                  (inexact->exact (string->number mudelapaperlinewidth))))))
385   (set! canvas-height height)
386   (set! canvas (make-array " " canvas-height canvas-width)))
387
388 (define (stop-line)
389   (if first-line
390       (let ((output-file (if (equal? cur-output-name "-")
391                               (current-output-port)
392                               (open-file cur-output-name "w")))
393              (output-name (if (equal? cur-output-name "-")
394                               "<stdout>"
395                               cur-output-name)))
396             (set! first-line #f)
397             (set! cur-output-file output-file)
398             (display (string-append "text output to " output-name "...\n")
399                      (current-error-port))))
400   (display 
401    (apply string-append 
402           (map (lambda (x) (string-append (apply string-append x) "\n")) 
403                (array->list canvas)))
404    cur-output-file))
405
406 (define (text s)
407   (let ((n (string-length s))
408         (font (get-font cur-font)))
409        (do ((i 0 (+ i 1)))
410            ((= i n)) 
411             (let* ((n (char->integer (string-ref s i)))
412                    (c (get-char font n)))
413                   (plot-char c)
414                   (rmove-to (char-width c) 0)))))
415