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
48 (define (gulp-file name)
49 (let ((port (if (equal? name "-")
51 (catch 'system-error (lambda () (open-file name "r"))
55 (display (string-append "[" name) (current-error-port))
56 (let ((content (let loop ((text ""))
57 (let ((line (read-line port)))
58 (if (or (eof-object? line)
61 (loop (string-append text line "\n")))))))
63 (display "]" (current-error-port))
67 (string-append "warning: no such file: " name "\n")
71 (define (with-extention name ext)
72 (if (or (equal? name "-")
73 (equal? ext (substring name (max 0 (- (string-length name)
74 (string-length ext))))))
76 (string-append name ext)))
78 (define (do-file file-name output-name)
79 (let ((ascii-script (gulp-file (with-extention file-name ".as"))))
81 (set! cur-output-name output-name)
82 (eval-string ascii-script)))
84 ;;; Script entry point
86 (set! lily-home (string-append
87 (dirname (dirname (car args)))
89 (let ((options (getopt-long args
90 `((output (single-char #\o)
92 (help (single-char #\h))
93 (version (single-char #\v))))))
94 (define (opt tag default)
95 (let ((pair (assq tag options)))
96 (if pair (cdr pair) default)))
98 (if (assq 'help options)
99 (begin (show-version (current-output-port)) (show-help) (exit 0)))
101 (if (assq 'version options)
102 (begin (show-version (current-output-port)) (exit 0)))
104 (show-version (current-error-port))
105 (let ((output-name (opt 'output "-"))
106 (files (let ((foo (opt '() '())))
110 (do-file (car files) output-name))))
113 ;;;; Ascii Script plotting
118 ;; Ascii-Art signature
119 (define tag-line "lily")
121 (define first-line #t)
128 (define canvas-width 65)
129 (define canvas-height 20)
138 (define line-char "-")
140 ;; the plotting fields
143 ;; make-uniform array of characters,
144 ;; or 1-dim array of strings?
145 ;; (set! canvas (make-array " " canvas-height canvas-width))
147 ;; urg, this kind of naming costs too much indenting
148 (define (split c s r)
149 (separate-fields-discarding-char c s r))
152 (sans-surrounding-whitespace s))
157 (define (af-gulp-file name)
159 (let ((old-load-path %load-path))
162 (or (getenv 'LILYPONDPREFIX) ".") "/mf")
163 (cons (string-append lily-home "/mf") %load-path)))
164 (let* ((path (%search-load-path name))
168 (set! %load-path old-load-path)
171 (define (char-width c)
172 (let ((bbox (car c)))
173 (inexact->exact (* .001 (caddr bbox)))))
175 ;; urg: use smart table
176 (define (xmerge-chars old new)
177 (cond ((equal? new " ") old)
178 ((and (equal? old "|") (equal? new "-")) "+")
179 ((and (equal? old "-") (equal? new "|")) "+")
182 (define (merge-chars old new)
183 (cond ((equal? new " ") old)
187 (let ((ny (- (* -1 y) 1)))
188 (if (array-in-bounds? canvas ny x)
189 (array-set! canvas (merge-chars (array-ref canvas ny x) c) ny x)
190 (display (string-append "ouch: " (number->string x) ","
191 (number->string ny) "\n")))))
193 (define (plot-char c)
196 ;; BBox: (llx lly urx ury) * 1000
197 (let ((dx (inexact->exact (* .001 (car bbox))))
198 ;(dy (inexact->exact (* .001 (cadr bbox))))
199 (dy (inexact->exact (- (* .001 (cadddr bbox)) 1)))
200 (len (length glyph)))
201 ;;(display "Bbox: ") (display bbox) (newline)
202 ;;(display "dy: ") (display dy) (newline)
203 (do ((line glyph (cdr line))
206 (plot-string (+ cur-x dx) (+ (- cur-y i) dy) (car line))))))
208 (define (plot-string x y s)
210 ((= i (string-length s)))
211 (plot (+ x i) y (substring s i (+ i 1)))))
213 (define (show-char char)
216 (define (show-font name)
217 (let ((font (assoc name fonts)))
218 (map (lambda (x) (show-char x)) font)))
223 (inexact->exact (/ x (abs x)))))
225 (define (generate-default-font)
226 (let loop ((chars '()) (i 0))
230 (cons (list i '(0 0 1000 1000)
231 (list (make-string 1 (integer->char i))))
235 (define (get-font name)
237 (if (equal? name "as-dummy")
239 (let ((entry (assoc name fonts)))
244 (string-append "warning: no such font: " name "\n")
245 (current-error-port))
246 (get-font "default"))))))
248 (define (get-char font n)
249 (let ((entry (assoc n font)))
254 (string-append "warning: no such char: ("
257 (number->string n ) ")\n")
258 (current-error-port))
262 ;;; AsciiScript commands
265 (let* ((font (get-font cur-font))
266 (c (get-char font n)))
271 (display (string-append
273 (- canvas-width (string-length tag-line)) #\space)
276 (close cur-output-file)
277 (set! cur-output-file '()))
280 (let ((step (sign len)))
281 (do ((i 0 (+ i step)))
283 (plot (+ cur-x i) cur-y line-char))))
286 (let ((step (sign len)))
287 (do ((i 0 (+ i step)))
288 ((= i len)) (plot cur-x (+ cur-y i) line-char))))
291 ;(display (string-append x y "\n") (current-error-port))
294 (define (header-end) "")
296 (define (rline-to dx dy)
297 (plot (inexact->exact cur-x) (inexact->exact cur-y) line-char)
298 (plot (inexact->exact (+ cur-x dx)) (inexact->exact (+ cur-y dy)) line-char)
299 (if (or (> (abs dx) 1) (> (abs dy) 1))
305 (plot (inexact->exact (+ cur-x hx)) (inexact->exact (+ cur-y hy)) line-char)
312 (define (dissect-char text)
313 (let* ((char (split #\nl text list))
315 (code (string->number
318 (+ (string-index id #\C) 1)
319 (string-index id #\;)))))
320 (bbox (map string->number
321 (split #\space (strip (substring
323 (+ (string-rindex id #\B) 1)
324 (string-rindex id #\;)))
326 (list (list code bbox (cdr char)))))
328 (define (load-font name mag)
329 ;; urg: don't load dummy font
330 (if (not (equal? name "as-dummy"))
331 (let ((text (af-gulp-file (string-append name ".af"))))
332 (if (< 0 (string-length text))
333 (let* ((char-list (cdr
335 (regexp-substitute/global
336 #f "\t[^\n]*\n" text 'pre "" 'post)
338 (font (apply append (map dissect-char char-list))))
339 (if (< 0 (length font))
340 (set! fonts (cons (cons name font) fonts))))))))
342 (define (move-to x y)
347 (plot cur-x cur-y c))
349 (define (rmove-to dx dy)
350 (set! cur-x (+ cur-x dx))
351 (set! cur-y (+ cur-y dy)))
353 (define (select-font name)
354 (set! cur-font name))
356 (define (set-line-char c)
359 (define (start-line height)
362 (set! fonts (cons (cons "default" (generate-default-font)) fonts))
363 (display "\n" (current-error-port))
364 (if (defined? 'mudelapaperlinewidth)
366 (inexact->exact (string->number mudelapaperlinewidth))))))
367 (set! canvas-height height)
368 (set! canvas (make-array " " canvas-height canvas-width)))
372 (let ((output-file (if (equal? cur-output-name "-")
373 (current-output-port)
374 (open-file cur-output-name "w")))
375 (output-name (if (equal? cur-output-name "-")
379 (set! cur-output-file output-file)
380 (display (string-append "text output to " output-name "...\n")
381 (current-error-port))))
384 (map (lambda (x) (string-append (apply string-append x) "\n"))
385 (array->list canvas)))
389 (let ((n (string-length s))
390 (font (get-font cur-font)))
393 (let* ((n (char->integer (string-ref s i)))
394 (c (get-char font n)))
396 (rmove-to (char-width c) 0)))))