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)
184 (not (eq? #f (memq symbol (ly-get-elt-property elt 'interfaces))))
187 ;;;;;;;;;;;;;;;;;;; TeX output
188 (define (tex-scm action-name)
193 (define (select-font font-name-symbol)
196 (c (assoc font-name-symbol font-name-alist))
201 (ly-warn (string-append
202 "Programming error: No such font known " (car font-name-symbol)))
203 "") ; issue no command
204 (string-append "\\" (cdr c)))
209 (define (beam width slope thick)
210 (embedded-ps ((ps-scm 'beam) width slope thick)))
212 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
213 (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
215 (define (dashed-slur thick dash l)
216 (embedded-ps ((ps-scm 'dashed-slur) thick dash l)))
218 (define (crescendo thick w h cont)
219 (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
222 (string-append "\\char" (inexact->string i 10) " "))
224 (define (dashed-line thick dash w)
225 (embedded-ps ((ps-scm 'dashed-line) thick dash w)))
227 (define (decrescendo thick w h cont)
228 (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
230 (define (font-load-command name-mag command)
232 "\\font\\" command "="
233 (symbol->string (car name-mag))
235 (number->string (magstep (cdr name-mag)))
238 (define (embedded-ps s)
239 (string-append "\\embeddedps{" s "}"))
242 (string-append "% " s))
246 ; uncomment for some stats about lily memory
247 ; (display (gc-stats))
248 (string-append "\n\\EndLilyPondOutput"
252 (define (experimental-on)
255 (define (font-switch i)
259 (define (font-def i s)
261 "\\font" (font-switch i) "=" s "\n"))
267 ;; URG: ly-gulp-file: now we can't use scm output without Lily
269 ;; fixed in 1.3.4 for powerpc -- broken on Windows
270 (regexp-substitute/global #f "\n"
271 (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
272 (ly-gulp-file "lily.ps"))
274 "\\input lilyponddefs \\turnOnPostScript"))
276 (define (header creator generate)
278 "%created by: " creator generate "\n"))
280 (define (invoke-char s i)
282 "\n\\" s "{" (inexact->string i 10) "}" ))
284 (define (invoke-dim1 s d)
286 "\n\\" s "{" (number->dim d) "}"))
291 ;; need to do something to make this really safe.
293 (define (output-tex-string s)
294 (if security-paranoia
296 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
297 (begin (display "warning: not paranoid") (newline) s))
300 (define (lily-def key val)
304 ;; fixed in 1.3.4 for powerpc -- broken on Windows
305 (regexp-substitute/global #f "_"
306 (output-tex-string key) 'pre "X" 'post)
307 (output-tex-string key))
308 "{" (output-tex-string val) "}\n"))
310 (define (number->dim x)
312 (ly-number->string x) " pt "))
314 (define (placebox x y s)
317 (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
319 (define (bezier-sandwich l thick)
320 (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
322 (define (start-line ht)
323 (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
326 "}\\vss}\\interscoreline\n")
327 (define (stop-last-line)
329 (define (filledbox breapth width depth height)
331 "\\kern" (number->dim (- breapth))
332 "\\vrule width " (number->dim (+ breapth width))
333 "depth " (number->dim depth)
334 "height " (number->dim height) " "))
337 (string-append "\\hbox{" (output-tex-string s) "}"))
339 (define (tuplet ht gapx dx dy thick dir)
340 (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
342 (define (volta h w thick vert_start vert_end)
343 (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
345 (define (define-origin file line col)
346 ; use this for column positions
347 (string-append "\\special{src:" (number->string line) ":"
348 (number->string col) " " file "}")
351 ;(string-append "\\special{src:" (number->string line) " " file "}")
353 ; no origin info: return empty string
355 ; no-origin not yet supported by Xdvi
356 (define (no-origin) "")
359 ;; The procedures listed below form the public interface of TeX-scm.
360 ;; (should merge the 2 lists)
361 (cond ((eq? action-name 'all-definitions)
363 (define font-load-command ,font-load-command)
365 (define bezier-sandwich ,bezier-sandwich)
366 (define bracket ,bracket)
368 (define crescendo ,crescendo)
369 (define dashed-line ,dashed-line)
370 (define dashed-slur ,dashed-slur)
371 (define decrescendo ,decrescendo)
372 (define end-output ,end-output)
373 (define experimental-on ,experimental-on)
374 (define filledbox ,filledbox)
375 (define font-def ,font-def)
376 (define font-switch ,font-switch)
377 (define header-end ,header-end)
378 (define lily-def ,lily-def)
379 (define header ,header)
380 (define invoke-char ,invoke-char)
381 (define invoke-dim1 ,invoke-dim1)
382 (define placebox ,placebox)
383 (define select-font ,select-font)
384 (define start-line ,start-line)
385 (define stop-line ,stop-line)
386 (define stop-last-line ,stop-last-line)
388 (define tuplet ,tuplet)
389 (define volta ,volta)
390 (define define-origin ,define-origin)
391 (define no-origin ,no-origin)
394 ((eq? action-name 'beam) beam)
395 ((eq? action-name 'tuplet) tuplet)
396 ((eq? action-name 'bracket) bracket)
397 ((eq? action-name 'crescendo) crescendo)
398 ((eq? action-name 'dashed-line) dashed-line)
399 ((eq? action-name 'dashed-slur) dashed-slur)
400 ((eq? action-name 'decrescendo) decrescendo)
401 ((eq? action-name 'end-output) end-output)
402 ((eq? action-name 'experimental-on) experimental-on)
403 ((eq? action-name 'font-def) font-def)
404 ((eq? action-name 'font-switch) font-switch)
405 ((eq? action-name 'header-end) header-end)
406 ((eq? action-name 'lily-def) lily-def)
407 ((eq? action-name 'header) header)
408 ((eq? action-name 'invoke-char) invoke-char)
409 ((eq? action-name 'invoke-dim1) invoke-dim1)
410 ((eq? action-name 'placebox) placebox)
411 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
412 ((eq? action-name 'start-line) start-line)
413 ((eq? action-name 'stem) stem)
414 ((eq? action-name 'stop-line) stop-line)
415 ((eq? action-name 'stop-last-line) stop-last-line)
416 ((eq? action-name 'volta) volta)
417 (else (error "unknown tag -- PS-TEX " action-name))
423 (define (ps-scm action-name)
425 ;; alist containing fontname -> fontcommand assoc (both strings)
426 (define font-alist '())
427 (define font-count 0)
428 (define current-font "")
431 (define (cached-fontname i)
434 (make-string 1 (integer->char (+ 65 i)))))
436 (define (mag-to-size m)
437 (number->string (case m
440 (2 14) ; really: 14.400
441 (3 17) ; really: 17.280
442 (4 21) ; really: 20.736
443 (5 24) ; really: 24.888
444 (6 30) ; really: 29.856
448 (define (select-font font-name-symbol)
451 (c (assoc font-name-symbol font-name-alist))
456 (ly-warn (string-append
457 "Programming error: No such font known " (car font-name-symbol)))
458 "") ; issue no command
459 (string-append " " (cdr c) " "))
464 (define (font-load-command name-mag command)
468 (symbol->string (car name-mag))
470 (number->string (magstep (cdr name-mag)))
471 " 1000 div 12 mul scalefont setfont } bind def "
475 (define (beam width slope thick)
477 (numbers->string (list width slope thick)) " draw_beam" ))
480 (string-append "% " s))
482 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
484 (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
487 (invoke-char " show" i))
489 (define (crescendo thick w h cont )
491 (numbers->string (list w h (inexact->exact cont) thick))
494 ;; what the heck is this interface ?
495 (define (dashed-slur thick dash l)
497 (apply string-append (map control->string l))
498 (number->string thick)
500 (number->string dash)
502 (number->string (* 10 thick)) ;UGH. 10 ?
503 " ] 0 draw_dashed_slur"))
505 (define (dashed-line thick dash width)
507 (number->string width)
509 (number->string thick)
511 (number->string dash)
513 (number->string dash)
514 " ] 0 draw_dashed_line"))
516 (define (decrescendo thick w h cont)
518 (numbers->string (list w h (inexact->exact cont) thick))
519 " draw_decrescendo"))
525 (define (experimental-on) "")
527 (define (filledbox breapth width depth height)
528 (string-append (numbers->string (list breapth width depth height))
532 (define (font-def i s)
535 (substring s 0 (- (string-length s) 4))
536 " findfont 12 scalefont setfont} bind def \n"))
538 (define (font-switch i)
539 (string-append (font i) " "))
543 ;; URG: now we can't use scm output without Lily
544 (ly-gulp-file "lilyponddefs.ps")
545 " {exch pop //systemdict /run get exec} "
546 (ly-gulp-file "lily.ps")
547 "{ exch pop //systemdict /run get exec } "
550 (define (lily-def key val)
552 (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
553 (string-append "/" key " {" val "} bind def\n")
554 (string-append "/" key " (" val ") def\n")
558 (define (header creator generate)
561 "%%Creator: " creator generate "\n"))
563 (define (invoke-char s i)
565 "(\\" (inexact->string i 8) ") " s " " ))
567 (define (invoke-dim1 s d)
569 (number->string (* d (/ 72.27 72))) " " s ))
571 (define (placebox x y s)
573 (number->string x) " " (number->string y) " {" s "} placebox "))
575 (define (bezier-sandwich l thick)
577 (apply string-append (map control->string l))
578 (number->string thick)
579 " draw_bezier_sandwich"))
581 (define (start-line height)
584 (define (stem breapth width depth height)
585 (string-append (numbers->string (list breapth width depth height))
592 (string-append "(" s ") show "))
595 (define (volta h w thick vert_start vert_end)
597 (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
600 (define (tuplet ht gap dx dy thick dir)
602 (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
610 (define (define-origin a b c ) "")
611 (define (no-origin) "")
614 (cond ((eq? action-name 'all-definitions)
617 (define tuplet ,tuplet)
618 (define bracket ,bracket)
620 (define crescendo ,crescendo)
621 (define volta ,volta)
622 (define bezier-sandwich ,bezier-sandwich)
623 (define dashed-line ,dashed-line)
624 (define dashed-slur ,dashed-slur)
625 (define decrescendo ,decrescendo)
626 (define end-output ,end-output)
627 (define experimental-on ,experimental-on)
628 (define filledbox ,filledbox)
629 (define font-def ,font-def)
630 (define font-switch ,font-switch)
631 (define header-end ,header-end)
632 (define lily-def ,lily-def)
633 (define font-load-command ,font-load-command)
634 (define header ,header)
635 (define invoke-char ,invoke-char)
636 (define invoke-dim1 ,invoke-dim1)
637 (define placebox ,placebox)
638 (define select-font ,select-font)
639 (define start-line ,start-line)
641 (define stop-line ,stop-line)
642 (define stop-last-line ,stop-line)
644 (define no-origin ,no-origin)
645 (define define-origin ,define-origin)
647 ((eq? action-name 'tuplet) tuplet)
648 ((eq? action-name 'beam) beam)
649 ((eq? action-name 'bezier-sandwich) bezier-sandwich)
650 ((eq? action-name 'bracket) bracket)
651 ((eq? action-name 'char) char)
652 ((eq? action-name 'crescendo) crescendo)
653 ((eq? action-name 'dashed-line) dashed-line)
654 ((eq? action-name 'dashed-slur) dashed-slur)
655 ((eq? action-name 'decrescendo) decrescendo)
656 ((eq? action-name 'experimental-on) experimental-on)
657 ((eq? action-name 'filledbox) filledbox)
658 ((eq? action-name 'select-font) select-font)
659 ((eq? action-name 'volta) volta)
660 (else (error "unknown tag -- PS-SCM " action-name))
665 (define (arg->string arg)
666 (cond ((number? arg) (inexact->string arg 10))
667 ((string? arg) (string-append "\"" arg "\""))
668 ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
670 (define (func name . args)
676 (map (lambda (x) (string-append " " (arg->string x))) args)))
685 (define (as-scm action-name)
687 (define (beam width slope thick)
689 (func "set-line-char" "#")
690 (func "rline-to" width (* width slope))
694 (define (bezier-sandwich l thick)
701 (dy (- (cdr c3) (cdr c0)))
703 (c1-dx (- (car c1) x))
704 (c1-line-y (+ (cdr c0) (* c1-dx rc)))
705 (dir (if (< c1-line-y (cdr c1)) 1 -1))
706 (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
708 (func "rmove-to" x y)
709 (func "put" (if (< 0 dir) "/" "\\\\"))
710 (func "rmove-to" 1 (if (< 0 dir) 1 0))
711 (func "set-line-char" "_")
712 (func "h-line" (- dx 1))
713 (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
714 (func "put" (if (< 0 dir) "\\\\" "/"))))))
716 (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
718 (func "rmove-to" (+ width 1) (- (/ height -2) 1))
720 (func "set-line-char" "|")
721 (func "rmove-to" 0 1)
722 (func "v-line" (+ height 1))
723 (func "rmove-to" 0 (+ height 1))
730 (define (define-origin a b c ) "")
735 (define (experimental-on)
738 (define (filledbox breapth width depth height)
739 (let ((dx (+ width breapth))
740 (dy (+ depth height)))
742 (func "rmove-to" (* -1 breapth) (* -1 depth))
745 (func "set-line-char"
746 (if (<= dx 1) "|" "#"))
749 (func "set-line-char"
750 (if (<= dy 1) "-" "="))
751 (func "h-line" dx))))))
753 (define (font-load-command name-mag command)
754 (func "load-font" (car name-mag) (magstep (cdr name-mag))))
756 (define (header creator generate)
757 (func "header" creator generate))
762 ;; urg: this is good for half of as2text's execution time
763 (define (xlily-def key val)
764 (string-append "(define " key " " (arg->string val) ")\n"))
766 (define (lily-def key val)
768 (or (equal? key "mudelapaperlinewidth")
769 (equal? key "mudelapaperstaffheight"))
770 (string-append "(define " key " " (arg->string val) ")\n")
773 (define (no-origin) "")
775 (define (placebox x y s)
776 (let ((ey (inexact->exact y)))
777 (string-append "(move-to " (number->string (inexact->exact x)) " "
778 (if (= 0.5 (- (abs y) (abs ey)))
783 (define (select-font font-name-symbol)
784 (let* ((c (assoc font-name-symbol font-name-alist)))
789 "Programming error: No such font known "
790 (car font-name-symbol)))
791 "") ; issue no command
792 (func "select-font" (car font-name-symbol)))))
794 (define (start-line height)
795 (func "start-line" height))
803 (define (tuplet ht gap dx dy thick dir) "")
805 (define (volta h w thick vert-start vert-end)
808 (func "set-line-char" "|")
809 (func "rmove-to" 0 -4)
810 ;; definition strange-way around
814 (func "rmove-to" 1 h)
815 (func "set-line-char" "_")
816 (func "h-line" (- w 1))
817 (func "set-line-char" "|")
820 (func "rmove-to" (- w 1) (* -1 h))
821 (func "v-line" (* -1 h)))
824 (cond ((eq? action-name 'all-definitions)
827 (define bracket ,bracket)
829 (define define-origin ,define-origin)
830 ;;(define crescendo ,crescendo)
831 (define bezier-sandwich ,bezier-sandwich)
832 ;;(define dashed-slur ,dashed-slur)
833 ;;(define decrescendo ,decrescendo)
834 (define end-output ,end-output)
835 (define experimental-on ,experimental-on)
836 (define filledbox ,filledbox)
837 ;;(define font-def ,font-def)
838 (define font-load-command ,font-load-command)
839 ;;(define font-switch ,font-switch)
840 (define header ,header)
841 (define header-end ,header-end)
842 (define lily-def ,lily-def)
843 ;;(define invoke-char ,invoke-char)
844 ;;(define invoke-dim1 ,invoke-dim1)
845 (define no-origin ,no-origin)
846 (define placebox ,placebox)
847 (define select-font ,select-font)
848 (define start-line ,start-line)
849 ;;(define stem ,stem)
850 (define stop-line ,stop-line)
851 (define stop-last-line ,stop-line)
853 (define tuplet ,tuplet)
854 (define volta ,volta)
856 ((eq? action-name 'tuplet) tuplet)
857 ;;((eq? action-name 'beam) beam)
858 ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
859 ;;((eq? action-name 'bracket) bracket)
860 ((eq? action-name 'char) char)
861 ;;((eq? action-name 'crescendo) crescendo)
862 ;;((eq? action-name 'dashed-slur) dashed-slur)
863 ;;((eq? action-name 'decrescendo) decrescendo)
864 ;;((eq? action-name 'experimental-on) experimental-on)
865 ((eq? action-name 'filledbox) filledbox)
866 ((eq? action-name 'select-font) select-font)
867 ;;((eq? action-name 'volta) volta)
868 (else (error "unknown tag -- MUSA-SCM " action-name))
873 (define (gulp-file name)
874 (let* ((port (open-file name "r"))
875 (content (let loop ((text ""))
876 (let ((line (read-line port)))
877 (if (or (eof-object? line)
880 (loop (string-append text line "\n")))))))
884 ;; urg: Use when standalone, do:
885 ;; (define ly-gulp-file scm-gulp-file)
886 (define (scm-gulp-file name)
888 (cons (string-append (getenv 'LILYPONDPREFIX) "/ly")
889 (cons (string-append (getenv 'LILYPONDPREFIX) "/ps")
891 (let ((path (%search-load-path name)))
896 (define (scm-tex-output)
897 (ly-eval (tex-scm 'all-definitions)))
899 (define (scm-ps-output)
900 (ly-eval (ps-scm 'all-definitions)))
902 (define (scm-as-output)
903 (ly-eval (as-scm 'all-definitions)))
905 (define (index-cell cell dir)