]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
patch::: 1.3.95.jcn3
[lilypond.git] / scm / lily.scm
1 ; lily.scm -- implement Scheme output routines for TeX and PostScript
2 ;
3 ;  source file of the GNU LilyPond music typesetter
4
5 ; (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
6
7
8 ;
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. 
11 ;
12
13
14 ;(debug-enable 'backtrace)
15
16 ;;; library funtions
17
18 (use-modules (ice-9 regex))
19
20 ;; The regex module may not be available, or may be broken.
21 (define use-regex
22   (let ((os (string-downcase (vector-ref (uname) 0))))
23     (not (equal? "cygwin" (substring os 0 (min 6 (string-length os)))))))
24
25 ;; If you have trouble with regex, define #f
26 (define use-regex #t)
27 ;;(define use-regex #f)
28
29 ;; do nothing in .scm output
30 (define (comment s) "")
31
32 ;; URG guile-1.3/1.4 compatibility
33 (define (ly-eval x) (eval2 x #f))
34
35 (define (comment s) "")
36
37 (define (mm-to-pt x)
38   (* (/ 72.27 25.40) x)
39   )
40
41 (define (cons-map f x)
42   (cons (f (car x)) (f (cdr x))))
43
44 (define (reduce operator list)
45       (if (null? (cdr list)) (car list)
46           (operator (car list) (reduce operator (cdr list)))
47           )
48       )
49
50
51 (define (numbers->string l)
52   (apply string-append (map ly-number->string l)))
53
54 ; (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
55
56 (define (number->octal-string x)
57   (let* ((n (inexact->exact x))
58          (n64 (quotient n 64))
59          (n8 (quotient (- n (* n64 64)) 8)))
60     (string-append
61      (number->string n64)
62      (number->string n8)
63      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
64
65 (define (inexact->string x radix)
66   (let ((n (inexact->exact x)))
67     (number->string n radix)))
68
69
70 (define (control->string c)
71   (string-append (number->string (car c)) " "
72                  (number->string (cdr c)) " "))
73
74
75 (define (font i)
76   (string-append
77    "font"
78    (make-string 1 (integer->char (+ (char->integer #\A) i)))
79    ))
80
81
82
83 (define (scm-scm action-name)
84   1)
85
86 (define security-paranoia #f)
87
88
89 ;; silly, use alist? 
90 (define (find-notehead-symbol duration style)
91   (case style
92    ((cross) "2cross")
93    ((harmonic) "0mensural")
94    ((baroque) 
95     (string-append (number->string duration)
96                    (if (< duration 0) "mensural" "")))
97    ((default) (number->string duration))
98    (else
99     (string-append (number->string duration) (symbol->string style)))))
100
101
102 ;;;;;;;; TeX
103
104 ;; this is silly, can't we use something like
105 ;; roman-0, roman-1 roman+1 ?
106 (define cmr-alist 
107   '(("bold" . "cmbx") 
108     ("brace" . "feta-braces")
109     ("default" . "cmr10")
110     ("dynamic" . "feta-din") 
111     ("feta" . "feta") 
112     ("feta-1" . "feta") 
113     ("feta-2" . "feta") 
114     ("typewriter" . "cmtt") 
115     ("italic" . "cmti") 
116     ("msam" . "msam") 
117     ("roman" . "cmr") 
118     ("script" . "cmr") 
119     ("large" . "cmbx") 
120     ("Large" . "cmbx") 
121     ("mark" . "feta-nummer") 
122     ("finger" . "feta-nummer")
123     ("timesig" . "feta-nummer")
124     ("number" . "feta-nummer") 
125     ("volta" . "feta-nummer"))
126 )
127
128 (define (string-encode-integer i)
129   (cond
130    ((= i  0) "o")
131    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
132    (else (string-append
133           (make-string 1 (integer->char (+ 65 (modulo i 26))))
134           (string-encode-integer (quotient i 26))
135          )
136    )
137   )
138   )
139
140 (define (magstep i)
141   (cdr (assoc i '((-4 . 482)
142                   (-3 . 579)
143                   (-2 . 694)
144                   (-1 . 833)
145                   (0 . 1000)
146                   (1 . 1200) 
147                   (2 . 1440)
148                   (3 . 1728)
149                   (4 . 2074))
150               )
151        )
152   )
153              
154 (define default-script-alist '())
155
156 (define font-name-alist  '())
157 (define (font-command name-mag)
158     (cons name-mag
159           (string-append  "magfont"
160                           (string-encode-integer (hashq (car name-mag) 1000000))
161                           "m"
162                           (string-encode-integer (cdr name-mag)))
163
164           )
165     )
166 (define (define-fonts names)
167   (set! font-name-alist (map font-command names))
168   (apply string-append
169          (map (lambda (x)
170                 (font-load-command (car x) (cdr x))) font-name-alist)
171   ))
172
173 (define (fontify name exp)
174   (string-append (select-font name)
175                  exp)
176   )
177
178 ;;;;;;;;;;;;;;;;;;;;
179
180
181 ; Make a function that checks score element for being of a specific type. 
182 (define (make-type-checker symbol)
183   (lambda (elt)
184     ;;(display  symbol)
185     ;;(eq? #t (ly-get-elt-property elt symbol))
186     (not (eq? #f (memq symbol (ly-get-elt-property elt 'interfaces))))
187     ))
188
189 ;;;;;;;;;;;;;;;;;;; TeX output
190 (define (tex-scm action-name)
191   (define (unknown) 
192     "%\n\\unknown%\n")
193
194
195   (define (select-font font-name-symbol)
196     (let*
197         (
198          (c (assoc font-name-symbol font-name-alist))
199          )
200
201       (if (eq? c #f)
202           (begin
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)))
207       
208       
209       ))
210   
211   (define (beam width slope thick)
212     (embedded-ps ((ps-scm 'beam) width slope thick)))
213
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)))
216
217   (define (dashed-slur thick dash l)
218     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
219
220   (define (crescendo thick w h cont)
221     (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
222
223   (define (char i)
224     (string-append "\\char" (inexact->string i 10) " "))
225   
226   (define (dashed-line thick on off dx dy)
227     (embedded-ps ((ps-scm 'dashed-line) thick on off dx dy)))
228
229   (define (decrescendo thick w h cont)
230     (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
231
232   (define (font-load-command name-mag command)
233     (string-append
234      "\\font\\" command "="
235      (symbol->string (car name-mag))
236      " scaled "
237      (number->string (magstep (cdr name-mag)))
238      "\n"))
239
240   (define (embedded-ps s)
241     (string-append "\\embeddedps{" s "}"))
242
243   (define (comment s)
244     (string-append "% " s))
245   
246   (define (end-output) 
247         (begin
248 ; uncomment for some stats about lily memory      
249 ;               (display (gc-stats))
250     (string-append "\n\\EndLilyPondOutput"
251                    ; Put GC stats here.
252                    )))
253   
254   (define (experimental-on)
255     "")
256
257   (define (font-switch i)
258     (string-append
259      "\\" (font i) "\n"))
260
261   (define (font-def i s)
262     (string-append
263      "\\font" (font-switch i) "=" s "\n"))
264
265   (define (header-end)
266     (string-append
267      "\\special{! "
268
269      ;; URG: ly-gulp-file: now we can't use scm output without Lily
270      (if use-regex
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"))
275      "}"
276      "\\input lilyponddefs \\turnOnPostScript"))
277
278   (define (header creator generate) 
279     (string-append
280      "%created by: " creator generate "\n"))
281
282   (define (invoke-char s i)
283     (string-append 
284      "\n\\" s "{" (inexact->string i 10) "}" ))
285
286   (define (invoke-dim1 s d)
287     (string-append
288      "\n\\" s "{" (number->dim d) "}"))
289   (define (pt->sp x)
290     (* 65536 x))
291   
292   ;;
293   ;; need to do something to make this really safe.
294   ;;
295   (define (output-tex-string s)
296       (if security-paranoia
297           (if use-regex
298               (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
299               (begin (display "warning: not paranoid") (newline) s))
300           s))
301       
302   (define (lily-def key val)
303     (string-append
304      "\\def\\"
305      (if use-regex
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"))
311
312   (define (number->dim x)
313     (string-append 
314      (ly-number->string x) " pt "))
315
316   (define (placebox x y s) 
317     (string-append 
318      "\\placebox{"
319      (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
320
321   (define (bezier-sandwich l thick)
322     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
323
324   (define (start-line ht)
325       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
326
327   (define (stop-line) 
328     "}\\vss}\\interscoreline\n")
329   (define (stop-last-line)
330     "}\\vss}")
331   (define (filledbox breapth width depth height) 
332     (string-append 
333      "\\kern" (number->dim (- breapth))
334      "\\vrule width " (number->dim (+ breapth width))
335      "depth " (number->dim depth)
336      "height " (number->dim height) " "))
337
338   (define (text s)
339     (string-append "\\hbox{" (output-tex-string s) "}"))
340   
341   (define (tuplet ht gapx dx dy thick dir)
342     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
343
344   (define (volta h w thick vert_start vert_end)
345     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
346
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
352      ; line numbers only:
353     ;(string-append "\\special{src:" (number->string line) " " file "}")
354 )
355     ; no origin  info: return empty string
356     ; ""
357   ; no-origin not yet supported by Xdvi
358   (define (no-origin) "")
359   
360   ;; TeX
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)
364          `(begin
365             (define font-load-command ,font-load-command)
366             (define beam ,beam)
367             (define bezier-sandwich ,bezier-sandwich)
368             (define bracket ,bracket)
369             (define char ,char)
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)
389             (define text ,text)
390             (define tuplet ,tuplet)
391             (define volta ,volta)
392             (define define-origin ,define-origin)
393             (define no-origin ,no-origin)
394             ))
395
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))
420         )
421   )
422
423
424 ;;;;;;;;;;;; PS
425 (define (ps-scm action-name)
426
427   ;; alist containing fontname -> fontcommand assoc (both strings)
428   (define font-alist '())
429   (define font-count 0)
430   (define current-font "")
431
432   
433   (define (cached-fontname i)
434     (string-append
435      "lilyfont"
436      (make-string 1 (integer->char (+ 65 i)))))
437     
438   (define (mag-to-size m)
439     (number->string (case m 
440                       (0 12)
441                       (1 12)
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
447                       )))
448   
449   
450   (define (select-font font-name-symbol)
451     (let*
452         (
453          (c (assoc font-name-symbol font-name-alist))
454          )
455
456       (if (eq? c #f)
457           (begin
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) " "))
462       
463       
464       ))
465
466     (define (font-load-command name-mag command)
467       (string-append
468        "/" command
469        " { /"
470        (symbol->string (car name-mag))
471        " findfont "
472        (number->string (magstep (cdr name-mag)))
473        " 1000 div 12 mul  scalefont setfont } bind def "
474        "\n"))
475
476
477   (define (beam width slope thick)
478     (string-append
479      (numbers->string (list width slope thick)) " draw_beam" ))
480
481   (define (comment s)
482     (string-append "% " s))
483
484   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
485     (string-append
486      (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
487
488   (define (char i)
489     (invoke-char " show" i))
490
491   (define (crescendo thick w h cont )
492     (string-append 
493      (numbers->string (list w h (inexact->exact cont) thick))
494      " draw_crescendo"))
495
496   ;; what the heck is this interface ?
497   (define (dashed-slur thick dash l)
498     (string-append 
499      (apply string-append (map control->string l)) 
500      (number->string thick) 
501      " [ "
502      (number->string dash)
503      " "
504      (number->string (* 10 thick))      ;UGH.  10 ?
505      " ] 0 draw_dashed_slur"))
506
507   (define (dashed-line thick on off dx dy)
508     (string-append 
509      (number->string dx)
510      " "
511      (number->string dy)
512      " "
513      (number->string thick) 
514      " [ "
515      (number->string on)
516      " "
517      (number->string off)
518      " ] 0 draw_dashed_line"))
519
520   (define (decrescendo thick w h cont)
521     (string-append 
522      (numbers->string (list w h (inexact->exact cont) thick))
523      " draw_decrescendo"))
524
525
526   (define (end-output)
527     "\nshowpage\n")
528   
529   (define (experimental-on) "")
530   
531   (define (filledbox breapth width depth height) 
532     (string-append (numbers->string (list breapth width depth height))
533                    " draw_box" ))
534
535   ;; obsolete?
536   (define (font-def i s)
537     (string-append
538      "\n/" (font i) " {/" 
539      (substring s 0 (- (string-length s) 4))
540      " findfont 12 scalefont setfont} bind def \n"))
541
542   (define (font-switch i)
543     (string-append (font i) " "))
544
545   (define (header-end)
546     (string-append
547      ;; URG: now we can't use scm output without Lily
548      (ly-gulp-file "lilyponddefs.ps")
549      " {exch pop //systemdict /run get exec} "
550      (ly-gulp-file "lily.ps")
551      "{ exch pop //systemdict /run get exec } "
552     ))
553   
554   (define (lily-def key val)
555
556      (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
557          (string-append "/" key " {" val "} bind def\n")
558          (string-append "/" key " (" val ") def\n")
559          )
560      )
561
562   (define (header creator generate) 
563     (string-append
564      "%!PS-Adobe-3.0\n"
565      "%%Creator: " creator generate "\n"))
566   
567   (define (invoke-char s i)
568     (string-append 
569      "(\\" (inexact->string i 8) ") " s " " ))
570   
571   (define (invoke-dim1 s d) 
572     (string-append
573      (number->string (* d  (/ 72.27 72))) " " s ))
574
575   (define (placebox x y s) 
576     (string-append 
577      (number->string x) " " (number->string y) " {" s "} placebox "))
578
579   (define (bezier-sandwich l thick)
580     (string-append 
581      (apply string-append (map control->string l))
582      (number->string  thick)
583      " draw_bezier_sandwich"))
584
585   (define (start-line height)
586           "\nstart_line {\n")
587   
588   (define (stem breapth width depth height) 
589     (string-append (numbers->string (list breapth width depth height))
590                    " draw_box" ))
591
592   (define (stop-line)
593       "}\nstop_line\n")
594
595   (define (text s)
596     (string-append "(" s ") show  "))
597
598
599   (define (volta h w thick vert_start vert_end)
600     (string-append 
601      (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
602      " draw_volta"))
603
604   (define (tuplet ht gap dx dy thick dir)
605     (string-append 
606      (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
607      " draw_tuplet"))
608
609
610   (define (unknown) 
611     "\n unknown\n")
612
613
614   (define (define-origin a b c ) "")
615   (define (no-origin) "")
616   
617   ;; PS
618   (cond ((eq? action-name 'all-definitions)
619          `(begin
620             (define beam ,beam)
621             (define tuplet ,tuplet)
622             (define bracket ,bracket)
623             (define char ,char)
624             (define crescendo ,crescendo)
625             (define volta ,volta)
626             (define bezier-sandwich ,bezier-sandwich)
627             (define dashed-line ,dashed-line) 
628             (define dashed-slur ,dashed-slur) 
629             (define decrescendo ,decrescendo) 
630             (define end-output ,end-output)
631             (define experimental-on ,experimental-on)
632             (define filledbox ,filledbox)
633             (define font-def ,font-def)
634             (define font-switch ,font-switch)
635             (define header-end ,header-end)
636             (define lily-def ,lily-def)
637             (define font-load-command ,font-load-command)
638             (define header ,header) 
639             (define invoke-char ,invoke-char) 
640             (define invoke-dim1 ,invoke-dim1)
641             (define placebox ,placebox)
642             (define select-font ,select-font)
643             (define start-line ,start-line)
644             (define stem ,stem)
645             (define stop-line ,stop-line)
646             (define stop-last-line ,stop-line)
647             (define text ,text)
648             (define no-origin ,no-origin)
649             (define define-origin ,define-origin)
650             ))
651         ((eq? action-name 'tuplet) tuplet)
652         ((eq? action-name 'beam) beam)
653         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
654         ((eq? action-name 'bracket) bracket)
655         ((eq? action-name 'char) char)
656         ((eq? action-name 'crescendo) crescendo)
657         ((eq? action-name 'dashed-line) dashed-line) 
658         ((eq? action-name 'dashed-slur) dashed-slur) 
659         ((eq? action-name 'decrescendo) decrescendo)
660         ((eq? action-name 'experimental-on) experimental-on)
661         ((eq? action-name 'filledbox) filledbox)
662         ((eq? action-name 'select-font) select-font)
663         ((eq? action-name 'volta) volta)
664         (else (error "unknown tag -- PS-SCM " action-name))
665         )
666   )
667
668
669 (define (arg->string arg)
670   (cond ((number? arg) (inexact->string arg 10))
671         ((string? arg) (string-append "\"" arg "\""))
672         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
673
674 (define (func name . args)
675   (string-append 
676    "(" name 
677    (if (null? args) 
678        ""
679        (apply string-append 
680               (map (lambda (x) (string-append " " (arg->string x))) args)))
681    ")\n"))
682
683 (define (sign x)
684   (if (= x 0)
685       1
686       (if (< x 0) -1 1)))
687
688 ;;;; AsciiScript as
689 (define (as-scm action-name)
690
691   (define (beam width slope thick)
692           (string-append
693            (func "set-line-char" "#")
694            (func "rline-to" width (* width slope))
695            ))
696
697   ; simple flat slurs
698   (define (bezier-sandwich l thick)
699           (let (
700                 (c0 (cadddr l))
701                 (c1 (cadr l))
702                 (c3 (caddr l)))
703                (let* ((x (car c0))
704                       (dx (- (car c3) x))
705                       (dy (- (cdr c3) (cdr c0)))
706                       (rc (/ dy dx))
707                       (c1-dx (- (car c1) x))
708                       (c1-line-y (+ (cdr c0) (* c1-dx rc)))
709                       (dir (if (< c1-line-y (cdr c1)) 1 -1))
710                       (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
711                      (string-append
712                       (func "rmove-to" x y)
713                       (func "put" (if (< 0 dir) "/" "\\\\"))
714                       (func "rmove-to" 1 (if (< 0 dir) 1 0))
715                       (func "set-line-char" "_")
716                       (func "h-line" (- dx 1))
717                       (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
718                       (func "put" (if (< 0 dir) "\\\\" "/"))))))
719
720   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
721           (string-append
722            (func "rmove-to" (+ width 1) (- (/ height -2) 1))
723            (func "put" "\\\\")
724            (func "set-line-char" "|")
725            (func "rmove-to" 0 1)
726            (func "v-line" (+ height 1))
727            (func "rmove-to" 0 (+ height 1))
728            (func "put" "/")
729            ))
730
731   (define (char i)
732     (func "char" i))
733
734   (define (define-origin a b c ) "")
735
736   (define (end-output) 
737     (func "end-output"))
738   
739   (define (experimental-on)
740           "")
741
742   (define (filledbox breapth width depth height)
743           (let ((dx (+ width breapth))
744                 (dy (+ depth height)))
745                (string-append 
746                 (func "rmove-to" (* -1 breapth) (* -1 depth))
747                 (if (< dx dy)
748                     (string-append
749                      (func "set-line-char" 
750                            (if (<= dx 1) "|" "#"))
751                      (func "v-line" dy))
752                     (string-append
753                      (func "set-line-char" 
754                            (if (<= dy 1) "-" "="))
755                     (func "h-line" dx))))))
756
757   (define (font-load-command name-mag command)
758     (func "load-font" (car name-mag) (magstep (cdr name-mag))))
759
760   (define (header creator generate) 
761     (func "header" creator generate))
762
763   (define (header-end) 
764     (func "header-end"))
765
766   ;; urg: this is good for half of as2text's execution time
767   (define (xlily-def key val)
768           (string-append "(define " key " " (arg->string val) ")\n"))
769
770   (define (lily-def key val)
771           (if 
772            (or (equal? key "mudelapaperlinewidth")
773                (equal? key "mudelapaperstaffheight"))
774            (string-append "(define " key " " (arg->string val) ")\n")
775            ""))
776
777   (define (no-origin) "")
778   
779   (define (placebox x y s) 
780     (let ((ey (inexact->exact y)))
781           (string-append "(move-to " (number->string (inexact->exact x)) " "
782                          (if (= 0.5 (- (abs y) (abs ey)))
783                              (number->string y)
784                              (number->string ey))
785                          ")\n" s)))
786                        
787   (define (select-font font-name-symbol)
788     (let* ((c (assoc font-name-symbol font-name-alist)))
789       (if (eq? c #f)
790           (begin
791             (ly-warn 
792              (string-append 
793               "Programming error: No such font known " 
794               (car font-name-symbol)))
795             "")                         ; issue no command
796           (func "select-font" (car font-name-symbol)))))
797
798   (define (start-line height)
799           (func "start-line" height))
800
801   (define (stop-line)
802           (func "stop-line"))
803
804   (define (text s)
805           (func "text" s))
806
807   (define (tuplet ht gap dx dy thick dir) "")
808
809   (define (volta h w thick vert-start vert-end)
810           ;; urg
811           (string-append
812            (func "set-line-char" "|")
813            (func "rmove-to" 0 -4)
814            ;; definition strange-way around
815            (if (= 0 vert-start)
816               (func "v-line" h)
817                "")
818            (func "rmove-to" 1 h)
819            (func "set-line-char" "_")
820            (func "h-line" (- w 1))
821            (func "set-line-char" "|")
822            (if (= 0 vert-end)
823                (string-append
824                 (func "rmove-to" (- w 1) (* -1 h))
825                 (func "v-line" (* -1 h)))
826                "")))
827
828   (cond ((eq? action-name 'all-definitions)
829          `(begin
830             (define beam ,beam)
831             (define bracket ,bracket)
832             (define char ,char)
833             (define define-origin ,define-origin)
834             ;;(define crescendo ,crescendo)
835             (define bezier-sandwich ,bezier-sandwich)
836             ;;(define dashed-slur ,dashed-slur) 
837             ;;(define decrescendo ,decrescendo) 
838             (define end-output ,end-output)
839             (define experimental-on ,experimental-on)
840             (define filledbox ,filledbox)
841             ;;(define font-def ,font-def)
842             (define font-load-command ,font-load-command)
843             ;;(define font-switch ,font-switch)
844             (define header ,header) 
845             (define header-end ,header-end)
846             (define lily-def ,lily-def)
847             ;;(define invoke-char ,invoke-char) 
848             ;;(define invoke-dim1 ,invoke-dim1)
849             (define no-origin ,no-origin)
850             (define placebox ,placebox)
851             (define select-font ,select-font)
852             (define start-line ,start-line)
853             ;;(define stem ,stem)
854             (define stop-line ,stop-line)
855             (define stop-last-line ,stop-line)
856             (define text ,text)
857             (define tuplet ,tuplet)
858             (define volta ,volta)
859             ))
860         ((eq? action-name 'tuplet) tuplet)
861         ;;((eq? action-name 'beam) beam)
862         ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
863         ;;((eq? action-name 'bracket) bracket)
864         ((eq? action-name 'char) char)
865         ;;((eq? action-name 'crescendo) crescendo)
866         ;;((eq? action-name 'dashed-slur) dashed-slur) 
867         ;;((eq? action-name 'decrescendo) decrescendo)
868         ;;((eq? action-name 'experimental-on) experimental-on)
869         ((eq? action-name 'filledbox) filledbox)
870         ((eq? action-name 'select-font) select-font)
871         ;;((eq? action-name 'volta) volta)
872         (else (error "unknown tag -- MUSA-SCM " action-name))
873         )
874   )
875
876
877 (define (gulp-file name)
878   (let* ((port (open-file name "r"))
879          (content (let loop ((text ""))
880                        (let ((line (read-line port)))
881                             (if (or (eof-object? line)
882                                     (not line)) 
883                                 text
884                                 (loop (string-append text line "\n")))))))
885         (close port)
886         content))
887
888 ;; urg: Use when standalone, do:
889 ;; (define ly-gulp-file scm-gulp-file)
890 (define (scm-gulp-file name)
891   (set! %load-path 
892         (cons (string-append (getenv 'LILYPONDPREFIX) "/ly")
893               (cons (string-append (getenv 'LILYPONDPREFIX) "/ps")
894                     %load-path)))
895   (let ((path (%search-load-path name)))
896        (if path
897            (gulp-file path)
898            (gulp-file name))))
899
900 (define (scm-tex-output)
901   (ly-eval (tex-scm 'all-definitions)))
902                                 
903 (define (scm-ps-output)
904   (ly-eval (ps-scm 'all-definitions)))
905
906 (define (scm-as-output)
907   (ly-eval (as-scm 'all-definitions)))
908         
909 (define (index-cell cell dir)
910   (if (equal? dir 1)
911       (cdr cell)
912       (car cell)))
913
914 (define major-scale
915   '(
916     (0 . 0)
917     (1 . 0)
918     (2 . 0)
919     (3 . 0)
920     (4 . 0)
921     (5 . 0)
922     (6 . 0)
923     )
924   )