4 ;;;; as2text.scm -- Translate AsciiScript to Text
6 ;;;; source file of the GNU LilyPond music typesetter
8 ;;;; (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
18 (define program-name "as2text")
20 (define lily-home "/usr/share/lilypond")
21 (define cur-output-name "-")
22 (define cur-output-file '())
24 (define subst-version "@TOPLEVEL_VERSION@")
26 (define program-version
27 (if (eq? subst-version (string-append "@" "TOPLEVEL_VERSION" "@"))
31 (define (show-version port)
32 (display (string-append
33 program-name " - LilyPond version " program-version "\n")
37 (display "Convert AsciiScript to text.
40 Usage: as2text [OPTION]... AS-FILE
44 -o,--output=FILE set output file
45 -v,--version show version
49 ;; FIXME: use (separate-fields-discarding-char) and (read-delimited "")
51 (define (gulp-file name)
52 (let ((port (if (equal? name "-")
54 (catch 'system-error (lambda () (open-file name "r"))
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)
64 (loop (string-append text line "\n")))))))
66 (display "]" (current-error-port))
70 (string-append "warning: no such file: " name "\n")
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))))))
79 (string-append name ext)))
81 (define (do-file file-name output-name)
82 (let ((ascii-script (gulp-file (with-extention file-name ".as"))))
84 (set! cur-output-name output-name)
85 (eval-string ascii-script)))
87 ;;; Script entry point
89 (set! lily-home (string-append
90 (dirname (dirname (car args)))
92 (let ((options (getopt-long args
93 `((output (single-char #\o)
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)))
101 (if (assq 'help options)
102 (begin (show-version (current-output-port)) (show-help) (exit 0)))
104 (if (assq 'version options)
105 (begin (show-version (current-output-port)) (exit 0)))
107 (show-version (current-error-port))
108 (let ((output-name (opt 'output "-"))
109 (files (let ((foo (opt '() '())))
113 (do-file (car files) output-name))))
116 ;;;; Ascii Script plotting
121 ;; Ascii-Art signature
122 (define tag-line "lily")
124 (define first-line #t)
133 (define canvas-width 65)
134 (define canvas-height 20)
143 (define line-char "-")
146 (define half-char-kludge #f)
148 ;; the plotting fields
151 ;; make-uniform array of characters,
152 ;; or 1-dim array of strings?
153 ;; (set! canvas (make-array " " canvas-height canvas-width))
155 ;; urg, this kind of naming costs too much indenting
156 (define (split c s r)
157 (separate-fields-discarding-char c s r))
160 (sans-surrounding-whitespace s))
165 (define (af-gulp-file name)
167 (let ((old-load-path %load-path))
170 (or (getenv 'LILYPONDPREFIX) ".") "/mf")
171 (cons (string-append lily-home "/mf") %load-path)))
172 (let* ((path (%search-load-path name))
176 (set! %load-path old-load-path)
179 (define (char-width c)
180 (let ((bbox (car c)))
181 (inexact->exact (* .001 (caddr bbox)))))
183 ;; urg: use smart table
184 (define (xmerge-chars old new)
185 (cond ((equal? new " ") old)
186 ((and (equal? old "|") (equal? new "-")) "+")
187 ((and (equal? old "-") (equal? new "|")) "+")
190 (define (merge-chars old new)
191 (cond ((equal? new " ") old)
194 (define (plot-raw x y c)
195 (if (array-in-bounds? canvas y x)
196 (array-set! canvas (merge-chars (array-ref canvas y x) c) y x)
197 (display (string-append "ouch: " (number->string x) ","
198 (number->string y) "\n")
199 (current-error-port))))
201 (define (plot x neg-y c)
202 (let ((y (- (* -1 neg-y) 1)))
203 (plot-raw (inexact->exact x) (inexact->exact y) c)))
205 (define (plot-char c)
208 (scur-x (* scaling cur-x))
209 (scur-y (* scaling cur-y)))
210 ;; BBox: (llx lly urx ury) * 1000
211 (let ((dx (inexact->exact (* .001 (car bbox))))
212 ;;(dy (inexact->exact (* .001 (cadr bbox))))
213 (dy (inexact->exact (- (* .001 (cadddr bbox)) 1)))
214 (len (length glyph)))
215 ;;(display "Bbox: ") (display bbox) (newline)
216 ;;(display "dy: ") (display dy) (newline)
217 (do ((line glyph (cdr line))
220 (plot-string (+ scur-x dx) (+ (- scur-y i) dy) (car line))))))
222 (define (plot-string x y s)
224 ((= i (string-length s)))
225 (plot (+ x i) y (substring s i (+ i 1)))))
227 (define (show-char char)
230 (define (show-font name)
231 (let ((font (assoc name fonts)))
232 (map (lambda (x) (show-char x)) font)))
237 (inexact->exact (/ x (abs x)))))
239 (define (generate-default-font)
240 (let loop ((chars '()) (i 0))
244 (cons (list i '(0 0 1000 1000)
245 (list (make-string 1 (integer->char i))))
249 (define (get-font name)
251 (if (equal? (substring name 0 (min (string-length name) 8)) "as-dummy")
253 (let ((entry (assoc name fonts)))
258 (string-append "warning: no such font: " name "\n")
259 (current-error-port))
260 (get-font "default"))))))
262 (define (get-char font n)
263 (let ((entry (assoc n font)))
268 (string-append "warning: no such char: ("
271 (number->string n ) ")\n")
272 (current-error-port))
276 ;;; AsciiScript commands
279 (let* ((font (get-font cur-font))
281 (if (and half-char-kludge
282 (assoc (+ n 0.5) font))
283 (get-char font (+ n 0.5))
289 (display (string-append
291 (- canvas-width (string-length tag-line)) #\space)
294 (close cur-output-file)
295 (set! cur-output-file '()))
299 (let ((scur-x (* scaling cur-x))
300 (scur-y (* scaling cur-y))
301 (slen (* scaling len)))
302 (let ((step (sign len)))
303 (do ((i 0 (+ i step)))
305 (plot (+ scur-x i) scur-y line-char)))))
308 (let ((scur-x (* scaling cur-x))
309 (scur-y (* scaling cur-y))
310 (slen (* scaling len)))
311 (let ((step (sign len)))
312 (do ((i 0 (+ i step)))
314 (plot scur-x (+ scur-y i) line-char)))))
317 ;(display (string-append x y "\n") (current-error-port))
320 (define (header-end) "")
323 (define (rline-to dx dy)
324 (plot (inexact->exact cur-x) (inexact->exact cur-y) line-char)
325 (plot (inexact->exact (+ cur-x dx)) (inexact->exact (+ cur-y dy)) line-char)
326 (if (or (> (abs dx) 1) (> (abs dy) 1))
332 (plot (inexact->exact (+ cur-x hx)) (inexact->exact (+ cur-y hy)) line-char)
339 (define (dissect-char text)
340 (let* ((char (split #\nl text list))
342 (code (string->number
345 (+ (string-index id #\C) 1)
346 (string-index id #\;)))))
347 (bbox (map string->number
348 (split #\space (strip (substring
350 (+ (string-rindex id #\B) 1)
351 (string-rindex id #\;)))
353 (list (list code bbox (cdr char)))))
355 (define (load-font name mag)
356 ;; urg: don't load dummy font
357 (if (not (equal? (substring name 0 (min (string-length name) 8)) "as-dummy"))
358 (let ((text (af-gulp-file (string-append name ".af"))))
359 (if (< 0 (string-length text))
360 (let* ((char-list (cdr
362 (regexp-substitute/global
363 #f "\t[^\n]*\n" text 'pre "" 'post)
365 (font (apply append (map dissect-char char-list))))
366 (if (< 0 (length font))
367 (set! fonts (cons (cons name font) fonts))))))))
369 (define (number->rounded-exact x)
370 (* (sign x) (inexact->exact (abs x))))
372 (define (move-to x y)
374 (let ((ey (number->rounded-exact y)))
375 (if (= 0.5 (- (abs ey) (abs y)))
376 (set! half-char-kludge #t)
377 (set! half-char-kludge #f))
381 (plot cur-x cur-y c))
383 (define (rmove-to dx dy)
384 (set! cur-x (+ cur-x dx))
385 (set! cur-y (+ cur-y dy)))
387 (define (select-font name)
388 (set! cur-font name))
390 (define (set-line-char c)
393 (define (start-line height)
396 (set! fonts (cons (cons "default" (generate-default-font)) fonts))
397 (display "\n" (current-error-port))
398 (if (defined? 'lilypondpaperoutputscale)
399 (set! scaling (inexact->exact (string->number lilypondpaperoutputscale))))
400 (if (defined? 'lilypondpaperlinewidth)
401 (let ((width (inexact->exact
402 (string->number lilypondpaperlinewidth))))
404 (set! canvas-width width)
406 ;;(set! canvas-width 200)
407 (set! canvas-width 80)
410 (set! canvas-height (inexact->exact (* scaling height)))
411 (set! canvas (make-array " " canvas-height canvas-width)))
415 (let ((output-file (if (equal? cur-output-name "-")
416 (current-output-port)
417 (open-file cur-output-name "w")))
418 (output-name (if (equal? cur-output-name "-")
422 (set! cur-output-file output-file)
423 (display (string-append "text output to " output-name "...\n")
424 (current-error-port))))
427 (map (lambda (x) (string-append (apply string-append x) "\n"))
428 (array->list canvas)))
432 (let ((n (string-length s))
433 (font (get-font cur-font)))
436 (let* ((n (char->integer (string-ref s i)))
437 (c (get-char font n)))
438 (if (pair? c) (plot-char c))
439 (rmove-to (char-width c) 0)))))