1 ; lily.scm -- implement Scheme output routines for TeX and PostScript
3 ; source file of the GNU LilyPond music typesetter
5 ; (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
9 ; This file contains various routines in Scheme that are easier to
10 ; do here than in C++. At present it is an unorganised mess. Sorry.
14 ;(debug-enable 'backtrace)
18 (use-modules (ice-9 regex))
20 ;; The regex module may not be available, or may be broken.
22 (let ((os (string-downcase (vector-ref (uname) 0))))
23 (not (equal? "cygwin" (substring os 0 (min 6 (string-length os)))))))
25 ;; If you have trouble with regex, define #f
27 ;;(define use-regex #f)
29 ;; do nothing in .scm output
30 (define (comment s) "")
32 ;; URG guile-1.3/1.4 compatibility
33 (define (ly-eval x) (eval2 x #f))
35 (define (comment s) "")
41 (define (cons-map f x)
42 (cons (f (car x)) (f (cdr x))))
44 (define (reduce operator list)
45 (if (null? (cdr list)) (car list)
46 (operator (car list) (reduce operator (cdr list)))
51 (define (numbers->string l)
52 (apply string-append (map ly-number->string l)))
54 ; (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
56 (define (number->octal-string x)
57 (let* ((n (inexact->exact x))
59 (n8 (quotient (- n (* n64 64)) 8)))
63 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
65 (define (inexact->string x radix)
66 (let ((n (inexact->exact x)))
67 (number->string n radix)))
70 (define (control->string c)
71 (string-append (number->string (car c)) " "
72 (number->string (cdr c)) " "))
78 (make-string 1 (integer->char (+ (char->integer #\A) i)))
83 (define (scm-scm action-name)
86 (define security-paranoia #f)
90 (define (find-notehead-symbol duration style)
93 ((harmonic) "0mensural")
95 (string-append (number->string duration)
96 (if (< duration 0) "mensural" "")))
97 ((default) (number->string duration))
99 (string-append (number->string duration) (symbol->string style)))))
104 ;; this is silly, can't we use something like
105 ;; roman-0, roman-1 roman+1 ?
108 ("brace" . "feta-braces")
109 ("default" . "cmr10")
110 ("dynamic" . "feta-din")
114 ("typewriter" . "cmtt")
121 ("mark" . "feta-nummer")
122 ("finger" . "feta-nummer")
123 ("timesig" . "feta-nummer")
124 ("number" . "feta-nummer")
125 ("volta" . "feta-nummer"))
128 (define (string-encode-integer i)
131 ((< i 0) (string-append "n" (string-encode-integer (- i))))
133 (make-string 1 (integer->char (+ 65 (modulo i 26))))
134 (string-encode-integer (quotient i 26))
141 (cdr (assoc i '((-4 . 482)
154 (define default-script-alist '())
156 (define font-name-alist '())
157 (define (font-command name-mag)
159 (string-append "magfont"
160 (string-encode-integer (hashq (car name-mag) 1000000))
162 (string-encode-integer (cdr name-mag)))
166 (define (define-fonts names)
167 (set! font-name-alist (map font-command names))
170 (font-load-command (car x) (cdr x))) font-name-alist)
173 (define (fontify name exp)
174 (string-append (select-font name)
181 ; Make a function that checks score element for being of a specific type.
182 (define (make-type-checker symbol)
185 ;;(eq? #t (ly-get-elt-property elt symbol))
186 (not (eq? #f (memq symbol (ly-get-elt-property elt 'interfaces))))
189 ;;;;;;;;;;;;;;;;;;; TeX output
190 (define (tex-scm action-name)
195 (define (select-font font-name-symbol)
198 (c (assoc font-name-symbol font-name-alist))
203 (ly-warn (string-append
204 "Programming error: No such font known " (car font-name-symbol)))
205 "") ; issue no command
206 (string-append "\\" (cdr c)))
211 (define (beam width slope thick)
212 (embedded-ps ((ps-scm 'beam) width slope thick)))
214 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
215 (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
217 (define (dashed-slur thick dash l)
218 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
220 (define (crescendo thick w h cont)
221 (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
224 (string-append "\\char" (inexact->string i 10) " "))
226 (define (dashed-line thick dash w)
227 (embedded-ps ((ps-scm 'dashed-line) thick dash w)))
229 (define (decrescendo thick w h cont)
230 (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
232 (define (font-load-command name-mag command)
234 "\\font\\" command "="
235 (symbol->string (car name-mag))
237 (number->string (magstep (cdr name-mag)))
240 (define (embedded-ps s)
241 (string-append "\\embeddedps{" s "}"))
244 (string-append "% " s))
248 ; uncomment for some stats about lily memory
249 ; (display (gc-stats))
250 (string-append "\n\\EndLilyPondOutput"
254 (define (experimental-on)
257 (define (font-switch i)
261 (define (font-def i s)
263 "\\font" (font-switch i) "=" s "\n"))
269 ;; URG: ly-gulp-file: now we can't use scm output without Lily
271 ;; fixed in 1.3.4 for powerpc -- broken on Windows
272 (regexp-substitute/global #f "\n"
273 (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
274 (ly-gulp-file "lily.ps"))
276 "\\input lilyponddefs \\turnOnPostScript"))
278 (define (header creator generate)
280 "%created by: " creator generate "\n"))
282 (define (invoke-char s i)
284 "\n\\" s "{" (inexact->string i 10) "}" ))
286 (define (invoke-dim1 s d)
288 "\n\\" s "{" (number->dim d) "}"))
293 ;; need to do something to make this really safe.
295 (define (output-tex-string s)
296 (if security-paranoia
298 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
299 (begin (display "warning: not paranoid") (newline) s))
302 (define (lily-def key val)
306 ;; fixed in 1.3.4 for powerpc -- broken on Windows
307 (regexp-substitute/global #f "_"
308 (output-tex-string key) 'pre "X" 'post)
309 (output-tex-string key))
310 "{" (output-tex-string val) "}\n"))
312 (define (number->dim x)
314 (ly-number->string x) " pt "))
316 (define (placebox x y s)
319 (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
321 (define (bezier-sandwich l thick)
322 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
324 (define (start-line ht)
325 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
328 "}\\vss}\\interscoreline\n")
329 (define (stop-last-line)
331 (define (filledbox breapth width depth height)
333 "\\kern" (number->dim (- breapth))
334 "\\vrule width " (number->dim (+ breapth width))
335 "depth " (number->dim depth)
336 "height " (number->dim height) " "))
339 (string-append "\\hbox{" (output-tex-string s) "}"))
341 (define (tuplet ht gapx dx dy thick dir)
342 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
344 (define (volta h w thick vert_start vert_end)
345 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
347 (define (define-origin file line col)
348 ; use this for column positions
349 (string-append "\\special{src:" (number->string line) ":"
350 (number->string col) " " file "}")
353 ;(string-append "\\special{src:" (number->string line) " " file "}")
355 ; no origin info: return empty string
357 ; no-origin not yet supported by Xdvi
358 (define (no-origin) "")
361 ;; The procedures listed below form the public interface of TeX-scm.
362 ;; (should merge the 2 lists)
363 (cond ((eq? action-name 'all-definitions)
365 (define font-load-command ,font-load-command)
367 (define bezier-sandwich ,bezier-sandwich)
368 (define bracket ,bracket)
370 (define crescendo ,crescendo)
371 (define dashed-line ,dashed-line)
372 (define dashed-slur ,dashed-slur)
373 (define decrescendo ,decrescendo)
374 (define end-output ,end-output)
375 (define experimental-on ,experimental-on)
376 (define filledbox ,filledbox)
377 (define font-def ,font-def)
378 (define font-switch ,font-switch)
379 (define header-end ,header-end)
380 (define lily-def ,lily-def)
381 (define header ,header)
382 (define invoke-char ,invoke-char)
383 (define invoke-dim1 ,invoke-dim1)
384 (define placebox ,placebox)
385 (define select-font ,select-font)
386 (define start-line ,start-line)
387 (define stop-line ,stop-line)
388 (define stop-last-line ,stop-last-line)
390 (define tuplet ,tuplet)
391 (define volta ,volta)
392 (define define-origin ,define-origin)
393 (define no-origin ,no-origin)
396 ((eq? action-name 'beam) beam)
397 ((eq? action-name 'tuplet) tuplet)
398 ((eq? action-name 'bracket) bracket)
399 ((eq? action-name 'crescendo) crescendo)
400 ((eq? action-name 'dashed-line) dashed-line)
401 ((eq? action-name 'dashed-slur) dashed-slur)
402 ((eq? action-name 'decrescendo) decrescendo)
403 ((eq? action-name 'end-output) end-output)
404 ((eq? action-name 'experimental-on) experimental-on)
405 ((eq? action-name 'font-def) font-def)
406 ((eq? action-name 'font-switch) font-switch)
407 ((eq? action-name 'header-end) header-end)
408 ((eq? action-name 'lily-def) lily-def)
409 ((eq? action-name 'header) header)
410 ((eq? action-name 'invoke-char) invoke-char)
411 ((eq? action-name 'invoke-dim1) invoke-dim1)
412 ((eq? action-name 'placebox) placebox)
413 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
414 ((eq? action-name 'start-line) start-line)
415 ((eq? action-name 'stem) stem)
416 ((eq? action-name 'stop-line) stop-line)
417 ((eq? action-name 'stop-last-line) stop-last-line)
418 ((eq? action-name 'volta) volta)
419 (else (error "unknown tag -- PS-TEX " action-name))
425 (define (ps-scm action-name)
427 ;; alist containing fontname -> fontcommand assoc (both strings)
428 (define font-alist '())
429 (define font-count 0)
430 (define current-font "")
433 (define (cached-fontname i)
436 (make-string 1 (integer->char (+ 65 i)))))
438 (define (mag-to-size m)
439 (number->string (case m
442 (2 14) ; really: 14.400
443 (3 17) ; really: 17.280
444 (4 21) ; really: 20.736
445 (5 24) ; really: 24.888
446 (6 30) ; really: 29.856
450 (define (select-font font-name-symbol)
453 (c (assoc font-name-symbol font-name-alist))
458 (ly-warn (string-append
459 "Programming error: No such font known " (car font-name-symbol)))
460 "") ; issue no command
461 (string-append " " (cdr c) " "))
466 (define (font-load-command name-mag command)
470 (symbol->string (car name-mag))
472 (number->string (magstep (cdr name-mag)))
473 " 1000 div 12 mul scalefont setfont } bind def "
477 (define (beam width slope thick)
479 (numbers->string (list width slope thick)) " draw_beam" ))
482 (string-append "% " s))
484 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
486 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
489 (invoke-char " show" i))
491 (define (crescendo thick w h cont )
493 (numbers->string (list w h (inexact->exact cont) thick))
496 ;; what the heck is this interface ?
497 (define (dashed-slur thick dash l)
499 (apply string-append (map control->string l))
500 (number->string thick)
502 (number->string dash)
504 (number->string (* 10 thick)) ;UGH. 10 ?
505 " ] 0 draw_dashed_slur"))
507 (define (dashed-line thick dash width)
509 (number->string width)
511 (number->string thick)
513 (number->string dash)
515 (number->string dash)
516 " ] 0 draw_dashed_line"))
518 (define (decrescendo thick w h cont)
520 (numbers->string (list w h (inexact->exact cont) thick))
521 " draw_decrescendo"))
527 (define (experimental-on) "")
529 (define (filledbox breapth width depth height)
530 (string-append (numbers->string (list breapth width depth height))
534 (define (font-def i s)
537 (substring s 0 (- (string-length s) 4))
538 " findfont 12 scalefont setfont} bind def \n"))
540 (define (font-switch i)
541 (string-append (font i) " "))
545 ;; URG: now we can't use scm output without Lily
546 (ly-gulp-file "lilyponddefs.ps")
547 " {exch pop //systemdict /run get exec} "
548 (ly-gulp-file "lily.ps")
549 "{ exch pop //systemdict /run get exec } "
552 (define (lily-def key val)
554 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
555 (string-append "/" key " {" val "} bind def\n")
556 (string-append "/" key " (" val ") def\n")
560 (define (header creator generate)
563 "%%Creator: " creator generate "\n"))
565 (define (invoke-char s i)
567 "(\\" (inexact->string i 8) ") " s " " ))
569 (define (invoke-dim1 s d)
571 (number->string (* d (/ 72.27 72))) " " s ))
573 (define (placebox x y s)
575 (number->string x) " " (number->string y) " {" s "} placebox "))
577 (define (bezier-sandwich l thick)
579 (apply string-append (map control->string l))
580 (number->string thick)
581 " draw_bezier_sandwich"))
583 (define (start-line height)
586 (define (stem breapth width depth height)
587 (string-append (numbers->string (list breapth width depth height))
594 (string-append "(" s ") show "))
597 (define (volta h w thick vert_start vert_end)
599 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
602 (define (tuplet ht gap dx dy thick dir)
604 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
612 (define (define-origin a b c ) "")
613 (define (no-origin) "")
616 (cond ((eq? action-name 'all-definitions)
619 (define tuplet ,tuplet)
620 (define bracket ,bracket)
622 (define crescendo ,crescendo)
623 (define volta ,volta)
624 (define bezier-sandwich ,bezier-sandwich)
625 (define dashed-line ,dashed-line)
626 (define dashed-slur ,dashed-slur)
627 (define decrescendo ,decrescendo)
628 (define end-output ,end-output)
629 (define experimental-on ,experimental-on)
630 (define filledbox ,filledbox)
631 (define font-def ,font-def)
632 (define font-switch ,font-switch)
633 (define header-end ,header-end)
634 (define lily-def ,lily-def)
635 (define font-load-command ,font-load-command)
636 (define header ,header)
637 (define invoke-char ,invoke-char)
638 (define invoke-dim1 ,invoke-dim1)
639 (define placebox ,placebox)
640 (define select-font ,select-font)
641 (define start-line ,start-line)
643 (define stop-line ,stop-line)
644 (define stop-last-line ,stop-line)
646 (define no-origin ,no-origin)
647 (define define-origin ,define-origin)
649 ((eq? action-name 'tuplet) tuplet)
650 ((eq? action-name 'beam) beam)
651 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
652 ((eq? action-name 'bracket) bracket)
653 ((eq? action-name 'char) char)
654 ((eq? action-name 'crescendo) crescendo)
655 ((eq? action-name 'dashed-line) dashed-line)
656 ((eq? action-name 'dashed-slur) dashed-slur)
657 ((eq? action-name 'decrescendo) decrescendo)
658 ((eq? action-name 'experimental-on) experimental-on)
659 ((eq? action-name 'filledbox) filledbox)
660 ((eq? action-name 'select-font) select-font)
661 ((eq? action-name 'volta) volta)
662 (else (error "unknown tag -- PS-SCM " action-name))
667 (define (arg->string arg)
668 (cond ((number? arg) (inexact->string arg 10))
669 ((string? arg) (string-append "\"" arg "\""))
670 ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
672 (define (func name . args)
678 (map (lambda (x) (string-append " " (arg->string x))) args)))
687 (define (as-scm action-name)
689 (define (beam width slope thick)
691 (func "set-line-char" "#")
692 (func "rline-to" width (* width slope))
696 (define (bezier-sandwich l thick)
703 (dy (- (cdr c3) (cdr c0)))
705 (c1-dx (- (car c1) x))
706 (c1-line-y (+ (cdr c0) (* c1-dx rc)))
707 (dir (if (< c1-line-y (cdr c1)) 1 -1))
708 (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
710 (func "rmove-to" x y)
711 (func "put" (if (< 0 dir) "/" "\\\\"))
712 (func "rmove-to" 1 (if (< 0 dir) 1 0))
713 (func "set-line-char" "_")
714 (func "h-line" (- dx 1))
715 (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
716 (func "put" (if (< 0 dir) "\\\\" "/"))))))
718 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
720 (func "rmove-to" (+ width 1) (- (/ height -2) 1))
722 (func "set-line-char" "|")
723 (func "rmove-to" 0 1)
724 (func "v-line" (+ height 1))
725 (func "rmove-to" 0 (+ height 1))
732 (define (define-origin a b c ) "")
737 (define (experimental-on)
740 (define (filledbox breapth width depth height)
741 (let ((dx (+ width breapth))
742 (dy (+ depth height)))
744 (func "rmove-to" (* -1 breapth) (* -1 depth))
747 (func "set-line-char"
748 (if (<= dx 1) "|" "#"))
751 (func "set-line-char"
752 (if (<= dy 1) "-" "="))
753 (func "h-line" dx))))))
755 (define (font-load-command name-mag command)
756 (func "load-font" (car name-mag) (magstep (cdr name-mag))))
758 (define (header creator generate)
759 (func "header" creator generate))
764 ;; urg: this is good for half of as2text's execution time
765 (define (xlily-def key val)
766 (string-append "(define " key " " (arg->string val) ")\n"))
768 (define (lily-def key val)
770 (or (equal? key "mudelapaperlinewidth")
771 (equal? key "mudelapaperstaffheight"))
772 (string-append "(define " key " " (arg->string val) ")\n")
775 (define (no-origin) "")
777 (define (placebox x y s)
778 (let ((ey (inexact->exact y)))
779 (string-append "(move-to " (number->string (inexact->exact x)) " "
780 (if (= 0.5 (- (abs y) (abs ey)))
785 (define (select-font font-name-symbol)
786 (let* ((c (assoc font-name-symbol font-name-alist)))
791 "Programming error: No such font known "
792 (car font-name-symbol)))
793 "") ; issue no command
794 (func "select-font" (car font-name-symbol)))))
796 (define (start-line height)
797 (func "start-line" height))
805 (define (tuplet ht gap dx dy thick dir) "")
807 (define (volta h w thick vert-start vert-end)
810 (func "set-line-char" "|")
811 (func "rmove-to" 0 -4)
812 ;; definition strange-way around
816 (func "rmove-to" 1 h)
817 (func "set-line-char" "_")
818 (func "h-line" (- w 1))
819 (func "set-line-char" "|")
822 (func "rmove-to" (- w 1) (* -1 h))
823 (func "v-line" (* -1 h)))
826 (cond ((eq? action-name 'all-definitions)
829 (define bracket ,bracket)
831 (define define-origin ,define-origin)
832 ;;(define crescendo ,crescendo)
833 (define bezier-sandwich ,bezier-sandwich)
834 ;;(define dashed-slur ,dashed-slur)
835 ;;(define decrescendo ,decrescendo)
836 (define end-output ,end-output)
837 (define experimental-on ,experimental-on)
838 (define filledbox ,filledbox)
839 ;;(define font-def ,font-def)
840 (define font-load-command ,font-load-command)
841 ;;(define font-switch ,font-switch)
842 (define header ,header)
843 (define header-end ,header-end)
844 (define lily-def ,lily-def)
845 ;;(define invoke-char ,invoke-char)
846 ;;(define invoke-dim1 ,invoke-dim1)
847 (define no-origin ,no-origin)
848 (define placebox ,placebox)
849 (define select-font ,select-font)
850 (define start-line ,start-line)
851 ;;(define stem ,stem)
852 (define stop-line ,stop-line)
853 (define stop-last-line ,stop-line)
855 (define tuplet ,tuplet)
856 (define volta ,volta)
858 ((eq? action-name 'tuplet) tuplet)
859 ;;((eq? action-name 'beam) beam)
860 ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
861 ;;((eq? action-name 'bracket) bracket)
862 ((eq? action-name 'char) char)
863 ;;((eq? action-name 'crescendo) crescendo)
864 ;;((eq? action-name 'dashed-slur) dashed-slur)
865 ;;((eq? action-name 'decrescendo) decrescendo)
866 ;;((eq? action-name 'experimental-on) experimental-on)
867 ((eq? action-name 'filledbox) filledbox)
868 ((eq? action-name 'select-font) select-font)
869 ;;((eq? action-name 'volta) volta)
870 (else (error "unknown tag -- MUSA-SCM " action-name))
875 (define (gulp-file name)
876 (let* ((port (open-file name "r"))
877 (content (let loop ((text ""))
878 (let ((line (read-line port)))
879 (if (or (eof-object? line)
882 (loop (string-append text line "\n")))))))
886 ;; urg: Use when standalone, do:
887 ;; (define ly-gulp-file scm-gulp-file)
888 (define (scm-gulp-file name)
890 (cons (string-append (getenv 'LILYPONDPREFIX) "/ly")
891 (cons (string-append (getenv 'LILYPONDPREFIX) "/ps")
893 (let ((path (%search-load-path name)))
898 (define (scm-tex-output)
899 (ly-eval (tex-scm 'all-definitions)))
901 (define (scm-ps-output)
902 (ly-eval (ps-scm 'all-definitions)))
904 (define (scm-as-output)
905 (ly-eval (as-scm 'all-definitions)))
907 (define (index-cell cell dir)