4 ;;;; as2text.scm -- Translate AsciiScript to Text
6 ;;;; source file of the GNU LilyPond music typesetter
8 ;;;; (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
19 (define program-name "as2text")
21 (define subst-version "@TOPLEVEL_VERSION@")
23 (define program-version
24 (if (eq? subst-version (string-append "@" "TOPLEVEL_VERSION" "@"))
28 (define (show-version)
29 (display (string-append
30 program-name " - LilyPond version " program-version "\n")
31 (current-error-port)))
34 (display "Convert AsciiScript to text.
36 Usage: as2text [OPTION]... AS-FILE
40 -o,--output=FILE set output file
41 -v,--version show version
42 " (current-error-port)))
44 (define (gulp-file name)
45 (let ((port (catch 'system-error (lambda () (open-file name "r"))
48 (let ((content (let loop ((text ""))
49 (let ((line (read-line port)))
50 (if (or (eof-object? line)
53 (loop (string-append text line "\n")))))))
58 (string-append "warning: no such file: " name "\n")
62 (define (with-exention name ext)
63 (if (equal? ext (substring name (max 0 (- (string-length name)
64 (string-length ext)))))
66 (string-append name ext)))
68 (define (do-file file-name output-name)
69 (let ((output-file (current-output-port))
70 (ascii-script (gulp-file (with-exention file-name ".as"))))
71 (eval-string ascii-script)))
73 ;;; Script entry point
76 (let ((options (getopt-long args
77 `((output (single-char #\o)
79 (help (single-char #\h))
80 (version (single-char #\v))))))
81 (define (opt tag default)
82 (let ((pair (assq tag options)))
83 (if pair (cdr pair) default)))
85 (if (assq 'help options)
86 (begin (show-help) (exit 0)))
88 (if (assq 'version options)
91 (let ((output-name (opt 'output-name "-"))
92 (files (let ((foo (opt '() '())))
96 (do-file (car files) output-name))))
99 ;;;; Ascii Script plotting
104 ;; Ascii-Art signature
105 (define tag-line "lily")
107 (define first-line #t)
114 (define canvas-width 65)
115 (define canvas-height 20)
124 (define line-char "-")
126 ;; the plotting fields
129 ;; make-uniform array of characters,
130 ;; or 1-dim array of strings?
131 ;; (set! canvas (make-array " " canvas-height canvas-width)))
133 ;; urg, this kind of naming costs too much indenting
134 (define (split c s r)
135 (separate-fields-discarding-char c s r))
138 (sans-surrounding-whitespace s))
143 (define (af-gulp-file name)
146 (getenv 'LILYPONDPREFIX) "/mf") %load-path))
147 (let ((path (%search-load-path name)))
152 (define (char-width c)
153 (let ((bbox (car c)))
154 (inexact->exact (* .001 (caddr bbox)))))
156 ;; urg: use smart table
157 (define (xmerge-chars old new)
158 (cond ((equal? new " ") old)
159 ((and (equal? old "|") (equal? new "-")) "+")
160 ((and (equal? old "-") (equal? new "|")) "+")
163 (define (merge-chars old new)
164 (cond ((equal? new " ") old)
168 (let ((ny (- (* -1 y) 1)))
169 (if (array-in-bounds? canvas ny x)
170 (array-set! canvas (merge-chars (array-ref canvas ny x) c) ny x)
171 (display (string-append "ouch: " (number->string x) ","
172 (number->string ny) "\n")))))
174 (define (plot-char c)
177 ;; BBox: (llx lly urx ury) * 1000
178 (let ((dx (inexact->exact (* .001 (car bbox))))
179 ;(dy (inexact->exact (* .001 (cadr bbox))))
180 (dy (inexact->exact (- (* .001 (cadddr bbox)) 1)))
181 (len (length glyph)))
182 ;;(display "Bbox: ") (display bbox) (newline)
183 ;;(display "dy: ") (display dy) (newline)
184 (do ((line glyph (cdr line))
187 (plot-string (+ cur-x dx) (+ (- cur-y i) dy) (car line))))))
189 (define (plot-string x y s)
191 ((= i (string-length s)))
192 (plot (+ x i) y (substring s i (+ i 1)))))
194 (define (show-char char)
197 (define (show-font name)
198 (let ((font (assoc name fonts)))
199 (map (lambda (x) (show-char x)) font)))
201 (define (generate-default-font)
202 (let loop ((chars '()) (i 32))
206 (cons (list i '(0 0 1000 1000)
207 (list (make-string 1 (integer->char i))))
211 (define (get-font name)
212 (let ((entry (assoc name fonts)))
217 (string-append "warning: no such font: " name "\n")
218 (current-error-port))
219 (get-font "default")))))
221 (define (get-char font n)
222 (let ((entry (assoc n font)))
227 (string-append "warning: no such char: ("
230 (number->string n ) ")\n")
231 (current-error-port))
235 ;;; AsciiScript commands
238 (let* ((font (get-font cur-font))
239 (c (get-char font n)))
244 (display (string-append
245 (make-string (- canvas-width (string-length tag-line)) #\space)
251 (inexact->exact (/ x (abs x)))))
254 (let ((step (sign len)))
255 (do ((i 0 (+ i step)))
257 (plot (+ cur-x i) cur-y line-char))))
260 (let ((step (sign len)))
261 (do ((i 0 (+ i step)))
262 ((= i len)) (plot cur-x (+ cur-y i) line-char))))
265 (display (string-append x y "\n") (current-error-port)))
267 (define (header-end) "")
269 (define (rline-to dx dy)
270 (plot (inexact->exact cur-x) (inexact->exact cur-y) line-char)
271 (plot (inexact->exact (+ cur-x dx)) (inexact->exact (+ cur-y dy)) line-char)
272 (if (or (> (abs dx) 1) (> (abs dy) 1))
278 (plot (inexact->exact (+ cur-x hx)) (inexact->exact (+ cur-y hy)) line-char)
285 (define (dissect-char text)
286 (let* ((char (split #\nl text list))
288 (code (string->number
291 (+ (string-index id #\C) 1)
292 (string-index id #\;)))))
293 (bbox (map string->number
294 (split #\space (strip (substring
296 (+ (string-rindex id #\B) 1)
297 (string-rindex id #\;)))
299 (list (list code bbox (cdr char)))))
301 (define (load-font name mag)
302 (let ((text (af-gulp-file (string-append name ".af"))))
303 (if (< 0 (string-length text))
304 (let* ((char-list (cdr (split #\np
305 (regexp-substitute/global
306 #f "\t[^\n]*\n" text 'pre "" 'post)
308 (font (apply append (map dissect-char char-list))))
309 (set! fonts (cons (cons name font) fonts))))))
311 (define (move-to x y)
316 (plot cur-x cur-y c))
318 (define (rmove-to dx dy)
319 (set! cur-x (+ cur-x dx))
320 (set! cur-y (+ cur-y dy)))
322 (define (select-font name)
323 (set! cur-font name))
325 (define (set-line-char c)
328 (define (start-line height)
332 (set! fonts (cons (cons "default" (generate-default-font)) fonts))))
333 (if (defined? 'mudelapaperlinewidth)
335 (inexact->exact (string->number mudelapaperlinewidth))))
336 (set! canvas-height height)
337 (set! canvas (make-array " " canvas-height canvas-width)))
342 (map (lambda (x) (string-append (apply string-append x) "\n"))
343 (array->list canvas)))))
346 (let ((n (string-length s))
347 (font (get-font cur-font)))
350 (let* ((n (char->integer (string-ref s i)))
351 (c (get-char font n)))
353 (rmove-to (char-width c) 0)))))