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 ; We should repartition the entire scm side of lily in a
15 ; more sane way, using namesspaces/modules?
17 ;(debug-enable 'backtrace)
21 (use-modules (ice-9 regex))
23 ;; The regex module may not be available, or may be broken.
25 (let ((os (string-downcase (vector-ref (uname) 0))))
26 (not (equal? "cygwin" (substring os 0 (min 6 (string-length os)))))))
28 ;; If you have trouble with regex, define #f
30 ;;(define use-regex #f)
32 ;; do nothing in .scm output
33 (define (comment s) "")
35 ;; URG guile-1.3/1.4 compatibility
36 (define (ly-eval x) (eval2 x #f))
38 (define (comment s) "")
44 (define (cons-map f x)
45 (cons (f (car x)) (f (cdr x))))
47 (define (reduce operator list)
48 (if (null? (cdr list)) (car list)
49 (operator (car list) (reduce operator (cdr list)))
54 (define (numbers->string l)
55 (apply string-append (map ly-number->string l)))
57 ; (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
59 (define (number->octal-string x)
60 (let* ((n (inexact->exact x))
62 (n8 (quotient (- n (* n64 64)) 8)))
66 (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
68 (define (inexact->string x radix)
69 (let ((n (inexact->exact x)))
70 (number->string n radix)))
73 (define (control->string c)
74 (string-append (number->string (car c)) " "
75 (number->string (cdr c)) " "))
80 (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 on off dx dy)
227 (embedded-ps ((ps-scm 'dashed-line) thick on off dx dy)))
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 "}"
351 ;; arg, the clueless take over the mailing list...
352 ; "\\special{-****-These-warnings-are-harmless-***}"
353 ; "\\special{-****-PLEASE-read-http://appel.lilypond.org/wiki/index.php3?PostProcessing-****}"
357 ;(string-append "\\special{src:" (number->string line) " " file "}")
359 ; no origin info: return empty string
361 ; no-origin not yet supported by Xdvi
362 (define (no-origin) "")
365 ;; The procedures listed below form the public interface of TeX-scm.
366 ;; (should merge the 2 lists)
367 (cond ((eq? action-name 'all-definitions)
369 (define font-load-command ,font-load-command)
371 (define bezier-sandwich ,bezier-sandwich)
372 (define bracket ,bracket)
374 (define crescendo ,crescendo)
375 (define dashed-line ,dashed-line)
376 (define dashed-slur ,dashed-slur)
377 (define decrescendo ,decrescendo)
378 (define end-output ,end-output)
379 (define experimental-on ,experimental-on)
380 (define filledbox ,filledbox)
381 (define font-def ,font-def)
382 (define font-switch ,font-switch)
383 (define header-end ,header-end)
384 (define lily-def ,lily-def)
385 (define header ,header)
386 (define invoke-char ,invoke-char)
387 (define invoke-dim1 ,invoke-dim1)
388 (define placebox ,placebox)
389 (define select-font ,select-font)
390 (define start-line ,start-line)
391 (define stop-line ,stop-line)
392 (define stop-last-line ,stop-last-line)
394 (define tuplet ,tuplet)
395 (define volta ,volta)
396 (define define-origin ,define-origin)
397 (define no-origin ,no-origin)
400 ((eq? action-name 'beam) beam)
401 ((eq? action-name 'tuplet) tuplet)
402 ((eq? action-name 'bracket) bracket)
403 ((eq? action-name 'crescendo) crescendo)
404 ((eq? action-name 'dashed-line) dashed-line)
405 ((eq? action-name 'dashed-slur) dashed-slur)
406 ((eq? action-name 'decrescendo) decrescendo)
407 ((eq? action-name 'end-output) end-output)
408 ((eq? action-name 'experimental-on) experimental-on)
409 ((eq? action-name 'font-def) font-def)
410 ((eq? action-name 'font-switch) font-switch)
411 ((eq? action-name 'header-end) header-end)
412 ((eq? action-name 'lily-def) lily-def)
413 ((eq? action-name 'header) header)
414 ((eq? action-name 'invoke-char) invoke-char)
415 ((eq? action-name 'invoke-dim1) invoke-dim1)
416 ((eq? action-name 'placebox) placebox)
417 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
418 ((eq? action-name 'start-line) start-line)
419 ((eq? action-name 'stem) stem)
420 ((eq? action-name 'stop-line) stop-line)
421 ((eq? action-name 'stop-last-line) stop-last-line)
422 ((eq? action-name 'volta) volta)
423 (else (error "unknown tag -- PS-TEX " action-name))
429 (define (ps-scm action-name)
431 ;; alist containing fontname -> fontcommand assoc (both strings)
432 (define font-alist '())
433 (define font-count 0)
434 (define current-font "")
437 (define (cached-fontname i)
440 (make-string 1 (integer->char (+ 65 i)))))
442 (define (mag-to-size m)
443 (number->string (case m
446 (2 14) ; really: 14.400
447 (3 17) ; really: 17.280
448 (4 21) ; really: 20.736
449 (5 24) ; really: 24.888
450 (6 30) ; really: 29.856
454 (define (select-font font-name-symbol)
457 (c (assoc font-name-symbol font-name-alist))
462 (ly-warn (string-append
463 "Programming error: No such font known " (car font-name-symbol)))
464 "") ; issue no command
465 (string-append " " (cdr c) " "))
470 (define (font-load-command name-mag command)
474 (symbol->string (car name-mag))
476 (number->string (magstep (cdr name-mag)))
477 " 1000 div 12 mul scalefont setfont } bind def "
481 (define (beam width slope thick)
483 (numbers->string (list width slope thick)) " draw_beam" ))
486 (string-append "% " s))
488 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
490 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
493 (invoke-char " show" i))
495 (define (crescendo thick w h cont )
497 (numbers->string (list w h (inexact->exact cont) thick))
500 ;; what the heck is this interface ?
501 (define (dashed-slur thick dash l)
503 (apply string-append (map control->string l))
504 (number->string thick)
506 (number->string dash)
508 (number->string (* 10 thick)) ;UGH. 10 ?
509 " ] 0 draw_dashed_slur"))
511 (define (dashed-line thick on off dx dy)
517 (number->string thick)
522 " ] 0 draw_dashed_line"))
524 (define (decrescendo thick w h cont)
526 (numbers->string (list w h (inexact->exact cont) thick))
527 " draw_decrescendo"))
533 (define (experimental-on) "")
535 (define (filledbox breapth width depth height)
536 (string-append (numbers->string (list breapth width depth height))
540 (define (font-def i s)
543 (substring s 0 (- (string-length s) 4))
544 " findfont 12 scalefont setfont} bind def \n"))
546 (define (font-switch i)
547 (string-append (font i) " "))
551 ;; URG: now we can't use scm output without Lily
552 (ly-gulp-file "lilyponddefs.ps")
553 " {exch pop //systemdict /run get exec} "
554 (ly-gulp-file "lily.ps")
555 "{ exch pop //systemdict /run get exec } "
558 (define (lily-def key val)
560 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
561 (string-append "/" key " {" val "} bind def\n")
562 (string-append "/" key " (" val ") def\n")
566 (define (header creator generate)
569 "%%Creator: " creator generate "\n"))
571 (define (invoke-char s i)
573 "(\\" (inexact->string i 8) ") " s " " ))
575 (define (invoke-dim1 s d)
577 (number->string (* d (/ 72.27 72))) " " s ))
579 (define (placebox x y s)
581 (number->string x) " " (number->string y) " {" s "} placebox "))
583 (define (bezier-sandwich l thick)
585 (apply string-append (map control->string l))
586 (number->string thick)
587 " draw_bezier_sandwich"))
589 (define (start-line height)
592 (define (stem breapth width depth height)
593 (string-append (numbers->string (list breapth width depth height))
600 (string-append "(" s ") show "))
603 (define (volta h w thick vert_start vert_end)
605 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
608 (define (tuplet ht gap dx dy thick dir)
610 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
618 (define (define-origin a b c ) "")
619 (define (no-origin) "")
622 (cond ((eq? action-name 'all-definitions)
625 (define tuplet ,tuplet)
626 (define bracket ,bracket)
628 (define crescendo ,crescendo)
629 (define volta ,volta)
630 (define bezier-sandwich ,bezier-sandwich)
631 (define dashed-line ,dashed-line)
632 (define dashed-slur ,dashed-slur)
633 (define decrescendo ,decrescendo)
634 (define end-output ,end-output)
635 (define experimental-on ,experimental-on)
636 (define filledbox ,filledbox)
637 (define font-def ,font-def)
638 (define font-switch ,font-switch)
639 (define header-end ,header-end)
640 (define lily-def ,lily-def)
641 (define font-load-command ,font-load-command)
642 (define header ,header)
643 (define invoke-char ,invoke-char)
644 (define invoke-dim1 ,invoke-dim1)
645 (define placebox ,placebox)
646 (define select-font ,select-font)
647 (define start-line ,start-line)
649 (define stop-line ,stop-line)
650 (define stop-last-line ,stop-line)
652 (define no-origin ,no-origin)
653 (define define-origin ,define-origin)
655 ((eq? action-name 'tuplet) tuplet)
656 ((eq? action-name 'beam) beam)
657 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
658 ((eq? action-name 'bracket) bracket)
659 ((eq? action-name 'char) char)
660 ((eq? action-name 'crescendo) crescendo)
661 ((eq? action-name 'dashed-line) dashed-line)
662 ((eq? action-name 'dashed-slur) dashed-slur)
663 ((eq? action-name 'decrescendo) decrescendo)
664 ((eq? action-name 'experimental-on) experimental-on)
665 ((eq? action-name 'filledbox) filledbox)
666 ((eq? action-name 'select-font) select-font)
667 ((eq? action-name 'volta) volta)
668 (else (error "unknown tag -- PS-SCM " action-name))
673 (define (arg->string arg)
674 (cond ((number? arg) (inexact->string arg 10))
675 ((string? arg) (string-append "\"" arg "\""))
676 ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
678 (define (func name . args)
684 (map (lambda (x) (string-append " " (arg->string x))) args)))
693 (define (as-scm action-name)
695 (define (beam width slope thick)
697 (func "set-line-char" "#")
698 (func "rline-to" width (* width slope))
702 (define (bezier-sandwich l thick)
709 (dy (- (cdr c3) (cdr c0)))
711 (c1-dx (- (car c1) x))
712 (c1-line-y (+ (cdr c0) (* c1-dx rc)))
713 (dir (if (< c1-line-y (cdr c1)) 1 -1))
714 (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
716 (func "rmove-to" x y)
717 (func "put" (if (< 0 dir) "/" "\\\\"))
718 (func "rmove-to" 1 (if (< 0 dir) 1 0))
719 (func "set-line-char" "_")
720 (func "h-line" (- dx 1))
721 (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
722 (func "put" (if (< 0 dir) "\\\\" "/"))))))
724 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
726 (func "rmove-to" (+ width 1) (- (/ height -2) 1))
728 (func "set-line-char" "|")
729 (func "rmove-to" 0 1)
730 (func "v-line" (+ height 1))
731 (func "rmove-to" 0 (+ height 1))
738 (define (define-origin a b c ) "")
743 (define (experimental-on)
746 (define (filledbox breapth width depth height)
747 (let ((dx (+ width breapth))
748 (dy (+ depth height)))
750 (func "rmove-to" (* -1 breapth) (* -1 depth))
753 (func "set-line-char"
754 (if (<= dx 1) "|" "#"))
757 (func "set-line-char"
758 (if (<= dy 1) "-" "="))
759 (func "h-line" dx))))))
761 (define (font-load-command name-mag command)
762 (func "load-font" (car name-mag) (magstep (cdr name-mag))))
764 (define (header creator generate)
765 (func "header" creator generate))
770 ;; urg: this is good for half of as2text's execution time
771 (define (xlily-def key val)
772 (string-append "(define " key " " (arg->string val) ")\n"))
774 (define (lily-def key val)
776 (or (equal? key "mudelapaperlinewidth")
777 (equal? key "mudelapaperstaffheight"))
778 (string-append "(define " key " " (arg->string val) ")\n")
781 (define (no-origin) "")
783 (define (placebox x y s)
784 (let ((ey (inexact->exact y)))
785 (string-append "(move-to " (number->string (inexact->exact x)) " "
786 (if (= 0.5 (- (abs y) (abs ey)))
791 (define (select-font font-name-symbol)
792 (let* ((c (assoc font-name-symbol font-name-alist)))
797 "Programming error: No such font known "
798 (car font-name-symbol)))
799 "") ; issue no command
800 (func "select-font" (car font-name-symbol)))))
802 (define (start-line height)
803 (func "start-line" height))
811 (define (tuplet ht gap dx dy thick dir) "")
813 (define (volta h w thick vert-start vert-end)
816 (func "set-line-char" "|")
817 (func "rmove-to" 0 -4)
818 ;; definition strange-way around
822 (func "rmove-to" 1 h)
823 (func "set-line-char" "_")
824 (func "h-line" (- w 1))
825 (func "set-line-char" "|")
828 (func "rmove-to" (- w 1) (* -1 h))
829 (func "v-line" (* -1 h)))
832 (cond ((eq? action-name 'all-definitions)
835 (define bracket ,bracket)
837 (define define-origin ,define-origin)
838 ;;(define crescendo ,crescendo)
839 (define bezier-sandwich ,bezier-sandwich)
840 ;;(define dashed-slur ,dashed-slur)
841 ;;(define decrescendo ,decrescendo)
842 (define end-output ,end-output)
843 (define experimental-on ,experimental-on)
844 (define filledbox ,filledbox)
845 ;;(define font-def ,font-def)
846 (define font-load-command ,font-load-command)
847 ;;(define font-switch ,font-switch)
848 (define header ,header)
849 (define header-end ,header-end)
850 (define lily-def ,lily-def)
851 ;;(define invoke-char ,invoke-char)
852 ;;(define invoke-dim1 ,invoke-dim1)
853 (define no-origin ,no-origin)
854 (define placebox ,placebox)
855 (define select-font ,select-font)
856 (define start-line ,start-line)
857 ;;(define stem ,stem)
858 (define stop-line ,stop-line)
859 (define stop-last-line ,stop-line)
861 (define tuplet ,tuplet)
862 (define volta ,volta)
864 ((eq? action-name 'tuplet) tuplet)
865 ;;((eq? action-name 'beam) beam)
866 ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
867 ;;((eq? action-name 'bracket) bracket)
868 ((eq? action-name 'char) char)
869 ;;((eq? action-name 'crescendo) crescendo)
870 ;;((eq? action-name 'dashed-slur) dashed-slur)
871 ;;((eq? action-name 'decrescendo) decrescendo)
872 ;;((eq? action-name 'experimental-on) experimental-on)
873 ((eq? action-name 'filledbox) filledbox)
874 ((eq? action-name 'select-font) select-font)
875 ;;((eq? action-name 'volta) volta)
876 (else (error "unknown tag -- MUSA-SCM " action-name))
881 (define (gulp-file name)
882 (let* ((port (open-file name "r"))
883 (content (let loop ((text ""))
884 (let ((line (read-line port)))
885 (if (or (eof-object? line)
888 (loop (string-append text line "\n")))))))
892 ;; urg: Use when standalone, do:
893 ;; (define ly-gulp-file scm-gulp-file)
894 (define (scm-gulp-file name)
896 (cons (string-append (getenv 'LILYPONDPREFIX) "/ly")
897 (cons (string-append (getenv 'LILYPONDPREFIX) "/ps")
899 (let ((path (%search-load-path name)))
904 (define (scm-tex-output)
905 (ly-eval (tex-scm 'all-definitions)))
907 (define (scm-ps-output)
908 (ly-eval (ps-scm 'all-definitions)))
910 (define (scm-as-output)
911 (ly-eval (as-scm 'all-definitions)))
913 (define (index-cell cell dir)
931 (eval-string (ly-gulp-file "interface.scm"))
932 (eval-string (ly-gulp-file "slur.scm"))
933 (eval-string (ly-gulp-file "font.scm"))
934 (eval-string (ly-gulp-file "generic-property.scm"))
935 (eval-string (ly-gulp-file "basic-properties.scm"))
936 (eval-string (ly-gulp-file "chord-names.scm"))
937 (eval-string (ly-gulp-file "element-descriptions.scm"))