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)
131 (define canvas-width 65)
132 (define canvas-height 20)
141 (define line-char "-")
144 (define half-char-kludge #f)
146 ;; the plotting fields
149 ;; make-uniform array of characters,
150 ;; or 1-dim array of strings?
151 ;; (set! canvas (make-array " " canvas-height canvas-width))
153 ;; urg, this kind of naming costs too much indenting
154 (define (split c s r)
155 (separate-fields-discarding-char c s r))
158 (sans-surrounding-whitespace s))
163 (define (af-gulp-file name)
165 (let ((old-load-path %load-path))
168 (or (getenv 'LILYPONDPREFIX) ".") "/mf")
169 (cons (string-append lily-home "/mf") %load-path)))
170 (let* ((path (%search-load-path name))
174 (set! %load-path old-load-path)
177 (define (char-width c)
178 (let ((bbox (car c)))
179 (inexact->exact (* .001 (caddr bbox)))))
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 "|")) "+")
188 (define (merge-chars old new)
189 (cond ((equal? new " ") old)
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")))))
199 (define (plot-char 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))
212 (plot-string (+ cur-x dx) (+ (- cur-y i) dy) (car line))))))
214 (define (plot-string x y s)
216 ((= i (string-length s)))
217 (plot (+ x i) y (substring s i (+ i 1)))))
219 (define (show-char char)
222 (define (show-font name)
223 (let ((font (assoc name fonts)))
224 (map (lambda (x) (show-char x)) font)))
229 (inexact->exact (/ x (abs x)))))
231 (define (generate-default-font)
232 (let loop ((chars '()) (i 0))
236 (cons (list i '(0 0 1000 1000)
237 (list (make-string 1 (integer->char i))))
241 (define (get-font name)
243 (if (equal? (substring name 0 (min (string-length name) 8)) "as-dummy")
245 (let ((entry (assoc name fonts)))
250 (string-append "warning: no such font: " name "\n")
251 (current-error-port))
252 (get-font "default"))))))
254 (define (get-char font n)
255 (let ((entry (assoc n font)))
260 (string-append "warning: no such char: ("
263 (number->string n ) ")\n")
264 (current-error-port))
268 ;;; AsciiScript commands
271 (let* ((font (get-font cur-font))
273 (if (and half-char-kludge
274 (assoc (+ n 0.5) font))
275 (get-char font (+ n 0.5))
281 (display (string-append
283 (- canvas-width (string-length tag-line)) #\space)
286 (close cur-output-file)
287 (set! cur-output-file '()))
290 (let ((step (sign len)))
291 (do ((i 0 (+ i step)))
293 (plot (+ cur-x i) cur-y line-char))))
296 (let ((step (sign len)))
297 (do ((i 0 (+ i step)))
298 ((= i len)) (plot cur-x (+ cur-y i) line-char))))
301 ;(display (string-append x y "\n") (current-error-port))
304 (define (header-end) "")
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))
315 (plot (inexact->exact (+ cur-x hx)) (inexact->exact (+ cur-y hy)) line-char)
322 (define (dissect-char text)
323 (let* ((char (split #\nl text list))
325 (code (string->number
328 (+ (string-index id #\C) 1)
329 (string-index id #\;)))))
330 (bbox (map string->number
331 (split #\space (strip (substring
333 (+ (string-rindex id #\B) 1)
334 (string-rindex id #\;)))
336 (list (list code bbox (cdr char)))))
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
345 (regexp-substitute/global
346 #f "\t[^\n]*\n" text 'pre "" 'post)
348 (font (apply append (map dissect-char char-list))))
349 (if (< 0 (length font))
350 (set! fonts (cons (cons name font) fonts))))))))
352 (define (number->rounded-exact x)
353 (* (sign x) (inexact->exact (abs x))))
355 (define (move-to x y)
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))
364 (plot cur-x cur-y c))
366 (define (rmove-to dx dy)
367 (set! cur-x (+ cur-x dx))
368 (set! cur-y (+ cur-y dy)))
370 (define (select-font name)
371 (set! cur-font name))
373 (define (set-line-char c)
376 (define (start-line height)
379 (set! fonts (cons (cons "default" (generate-default-font)) fonts))
380 (display "\n" (current-error-port))
381 (if (and (defined? 'lilypondpaperlinewidth)
382 (> (string->number lilypondpaperlinewidth) 0))
384 (inexact->exact (string->number lilypondpaperlinewidth))))))
385 (set! canvas-height height)
386 (set! canvas (make-array " " canvas-height canvas-width)))
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 "-")
397 (set! cur-output-file output-file)
398 (display (string-append "text output to " output-name "...\n")
399 (current-error-port))))
402 (map (lambda (x) (string-append (apply string-append x) "\n"))
403 (array->list canvas)))
407 (let ((n (string-length s))
408 (font (get-font cur-font)))
411 (let* ((n (char->integer (string-ref s i)))
412 (c (get-char font n)))
414 (rmove-to (char-width c) 0)))))