]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
release: 1.3.26
[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 ;(debug-enable 'backtrace)
9
10 ;;; library funtions
11
12 (use-modules (ice-9 regex))
13
14 ;; do nothing in .scm output
15 (define (comment s) "")
16
17 (define (mm-to-pt x)
18   (* (/ 72.27 25.40) x)
19   )
20
21 (define (cons-map f x)
22   (cons (f (car x)) (f (cdr x))))
23
24 (define (reduce operator list)
25       (if (null? (cdr list)) (car list)
26           (operator (car list) (reduce operator (cdr list)))
27           )
28       )
29
30 (define (glue-2-strings a b) 
31   (string-append a " " b))
32
33 (define (numbers->string l)
34   (reduce glue-2-strings (map number->string l)))
35
36 (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
37
38 (define (number->octal-string x)
39   (let* ((n (inexact->exact x))
40          (n64 (quotient n 64))
41          (n8 (quotient (- n (* n64 64)) 8)))
42     (string-append
43      (number->string n64)
44      (number->string n8)
45      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
46
47 (define (inexact->string x radix)
48   (let ((n (inexact->exact x)))
49     (number->string n radix)))
50
51
52 (define
53   (control->string c)
54   (string-append
55    (string-append (number->string (car c)) " ")
56    (string-append (number->string (cdr c)) " ")))
57
58
59 (define (font i)
60   (string-append
61    "font"
62    (make-string 1 (integer->char (+ (char->integer #\A) i)))
63    ))
64
65
66
67 (define (scm-scm action-name)
68   1)
69
70 (define security-paranoia #f)
71
72
73 ;; See documentation of Item::visibility_lambda_
74 (define (postbreak-only-visibility d) (if (= d 1) '(#f . #f) '(#t . #t)))
75 (define (spanbar-non-postbreak-visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
76 (define (all-visibility d) '(#f . #f))
77 (define (non-postbreak-visibility d) (if (= d 1) '(#t . #t) '(#f . #f)))
78 (define (non-prebreak-visibility d) (if (= d -1) '(#t . #t) '(#f . #f)))
79
80
81 ;; Score_span_bars are only visible at start of line
82 ;; i.e. if break_dir == RIGHT == 1
83 (define Span_bar_engraver-visibility non-postbreak-visibility)
84 (define Base_span_bar_engraver-visibility non-postbreak-visibility)
85 (define mark-visibility non-prebreak-visibility)
86 (define Span_score_bar_engraver-visibility postbreak-only-visibility)
87 (define Piano_bar_engraver-visibility postbreak-only-visibility)
88 (define Staff_group_bar_engraver-visibility postbreak-only-visibility)
89
90 ;; Spacing constants for prefatory matter.
91 ;;
92 ;; rules for this spacing are much more complicated than this. See [Wanske] page 126 -- 134, [Ross] pg 143 -- 147
93 ;;
94 ;;
95
96 ;; (Measured in staff space)
97 (define space-alist
98  '(
99    (("" "Clef_item") . (minimum-space 1.0))
100    (("" "Staff_bar") . (minimum-space 0.0))
101    (("" "Clef_item") . (minimum-space 1.0))
102    (("" "Key_item") . (minimum-space 0.5))
103    (("" "Span_bar") . (extra-space 0.0))
104    (("" "Time_signature") . (extra-space 0.0))
105    (("" "begin-of-note") . (minimum-space 1.5))
106    (("Clef_item" "Key_item") . (minimum-space 4.0))
107    (("Key_item" "Time_signature") . (extra-space 1.0))
108    (("Clef_item"  "Time_signature") . (minimum-space 3.5))
109    (("Staff_bar" "Clef_item") .   (minimum-space 1.0))
110    (("Clef_item"  "Staff_bar") .  (minimum-space 3.7))
111    (("Time_signature" "Staff_bar") .  (minimum-space 2.0))
112    (("Key_item"  "Staff_bar") .  (extra-space 1.0))
113    (("Span_bar" "Clef_item") .   (extra-space 1.0))
114    (("Clef_item"  "Span_bar") . (minimum-space 3.7))
115    (("Time_signature" "Span_bar") . (minimum-space 2.0))
116    (("Key_item"  "Span_bar") . (minimum-space 2.5))
117    (("Staff_bar" "Time_signature") . (minimum-space 1.5)) ;double check this.
118    (("Time_signature" "begin-of-note") . (extra-space 2.0)) ;double check this.
119    (("Key_item" "begin-of-note") . (extra-space 2.5))
120    (("Staff_bar" "begin-of-note") . (extra-space 1.0))
121    (("Clef_item" "begin-of-note") . (minimum-space 5.0))
122    (("" "Breathing_sign") . (minimum-space 0.0))
123    (("Breathing_sign" "Key_item") . (minimum-space 1.5))
124    (("Breathing_sign" "begin-of-note") . (minimum-space 1.0))
125    (("Breathing_sign" "Staff_bar") . (minimum-space 1.5))
126    (("Breathing_sign" "Clef_item") . (minimum-space 2.0))
127    )
128 )
129  
130 (define (break-align-spacer this next)
131   (let ((entry (assoc `(,this ,next) space-alist)))
132     (if entry
133         (cdr entry)
134         (begin (ly-warn (string-append "Unknown spacing pair `" this "', `" next "'"))
135                '(minimum-space 0.0)))))
136   
137
138
139 ;;;;;;;; TeX
140
141 ;; this is silly, can't we use something like
142 ;; roman-0, roman-1 roman+1 ?
143 (define cmr-alist 
144   '(("bold" . "cmbx") 
145     ("brace" . "feta-braces")
146     ("default" . "cmr10")
147     ("dynamic" . "feta-din") 
148     ("feta" . "feta") 
149     ("feta-1" . "feta") 
150     ("feta-2" . "feta") 
151     ("finger" . "feta-nummer") 
152     ("typewriter" . "cmtt") 
153     ("italic" . "cmti") 
154     ("roman" . "cmr") 
155     ("script" . "cmr") 
156     ("large" . "cmbx") 
157     ("Large" . "cmbx") 
158     ("mark" . "feta-nummer") 
159     ("number" . "feta-nummer") 
160     ("volta" . "feta-nummer"))
161 )
162
163 (define (string-encode-integer i)
164   (cond
165    ((= i  0) "o")
166    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
167    (else (string-append
168           (make-string 1 (integer->char (+ 65 (modulo i 26))))
169           (string-encode-integer (quotient i 26))
170          )
171    )
172   )
173   )
174
175 (define (magstep i)
176   (cdr (assoc i '((-4 . 482)
177                   (-3 . 579)
178                   (-2 . 694)
179                   (-1 . 833)
180                   (0 . 1000)
181                   (1 . 1200) 
182                   (2 . 1440)
183                   (3 . 1728)
184                   (4 . 2074))
185               )
186        )
187   )
188              
189 (define script-alist '())
190 (define (articulation-to-scriptdef a)
191   (assoc a script-alist)
192   )
193
194 ;; Map style names to TeX font names.  Return false if 
195 ;; no font name found. 
196 (define (style-to-cmr s)
197   (assoc s cmr-alist )
198   )
199             
200
201
202 (define font-name-alist  '())
203 (define (font-command name-mag)
204     (cons name-mag
205           (string-append  "magfont"
206                           (string-encode-integer (hashq (car name-mag) 1000000))
207                           "m"
208                           (string-encode-integer (cdr name-mag)))
209
210           )
211     )
212 (define (define-fonts names)
213   (set! font-name-alist (map font-command names))
214   (apply string-append
215          (map (lambda (x)
216                 (font-load-command (car x) (cdr x))) font-name-alist)
217   ))
218   
219
220 (define (tex-scm action-name)
221   (define (unknown) 
222     "%\n\\unknown%\n")
223
224
225   (define (select-font font-name-symbol)
226     (let*
227         (
228          (c (assoc font-name-symbol font-name-alist))
229          )
230
231       (if (eq? c #f)
232           (begin
233             (ly-warn (string-append
234                       "Programming error: No such font known " (car font-name-symbol)))
235             "")                         ; issue no command
236           (string-append "\\" (cdr c)))
237       
238       
239       ))
240   
241   (define (beam width slope thick)
242     (embedded-ps ((ps-scm 'beam) width slope thick)))
243
244   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
245     (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height width height arch_thick thick)))
246
247   (define (dashed-slur thick dash l)
248     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
249
250   (define (crescendo thick w h cont)
251     (embedded-ps ((ps-scm 'crescendo) thick w h cont)))
252
253   (define (char i)
254     (string-append "\\char" (inexact->string i 10) " "))
255   
256   (define (decrescendo thick w h cont)
257     (embedded-ps ((ps-scm 'decrescendo) thick w h cont)))
258
259    ;This sets CTM so that you get to the currentpoint
260   ; by executing a 0 0 moveto
261
262   
263  
264
265   (define (font-load-command name-mag command)
266     (string-append
267      "\\font\\" command "="
268      (symbol->string (car name-mag))
269      " scaled "
270      (number->string (magstep (cdr name-mag)))
271      "\n"))
272
273
274   (define (embedded-ps s)
275     (string-append "\\embeddedps{" s "}"))
276
277   (define (comment s)
278     (string-append "% " s))
279   
280   (define (end-output) 
281     "\n\\EndLilyPondOutput")
282   
283   (define (experimental-on)
284     "")
285
286   (define (font-switch i)
287     (string-append
288      "\\" (font i) "\n"))
289
290   (define (font-def i s)
291     (string-append
292      "\\font" (font-switch i) "=" s "\n"))
293
294   (define (header-end)
295     (string-append
296      "\\special{! "
297      ; fixed in 1.3.4
298      ;(ly-gulp-file "lily.ps")
299
300      (regexp-substitute/global #f "\n" (ly-gulp-file "lily.ps") 'pre " %\n" 'post)
301      "}"
302      "\\input lilyponddefs \\turnOnPostScript"))
303
304   (define (header creator generate) 
305     (string-append
306      "%created by: " creator generate "\n"))
307
308   (define (invoke-char s i)
309     (string-append 
310      "\n\\" s "{" (inexact->string i 10) "}" ))
311
312   (define (invoke-dim1 s d)
313     (string-append
314      "\n\\" s "{" (number->dim d) "}"))
315   (define (pt->sp x)
316     (* 65536 x))
317   
318   ;;
319   ;; need to do something to make this really safe.
320   ;;
321   (define (output-tex-string s)
322       (if security-paranoia
323           (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
324           s))
325       
326   (define (lily-def key val)
327     (string-append
328      "\\def\\"
329      ; fixed in 1.3.4
330      (regexp-substitute/global #f "_" (output-tex-string key) 'pre "X" 'post)
331      ;(output-tex-string key)
332      "{" (output-tex-string val) "}\n"))
333
334   (define (number->dim x)
335     (string-append 
336      (number->string  (chop-decimal x)) " pt "))
337
338   (define (placebox x y s) 
339     (string-append 
340      "\\placebox{"
341      (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
342
343   (define (bezier-sandwich l thick)
344     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
345
346   (define (start-line ht)
347       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
348
349   (define (stop-line) 
350     "}\\vss}\\interscoreline")
351   (define (stop-last-line)
352     "}\\vss}")
353   (define (filledbox breapth width depth height) 
354     (string-append 
355      "\\kern" (number->dim (- breapth))
356      "\\vrule width " (number->dim (+ breapth width))
357      "depth " (number->dim depth)
358      "height " (number->dim height) " "))
359
360   (define (text s)
361     (string-append "\\hbox{" (output-tex-string s) "}"))
362   
363   (define (tuplet ht gapx dx dy thick dir)
364     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
365
366   (define (volta h w thick vert_start vert_end)
367     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
368
369   ;; TeX
370   ;; The procedures listed below form the public interface of TeX-scm.
371   ;; (should merge the 2 lists)
372   (cond ((eq? action-name 'all-definitions)
373          `(begin
374             (define font-load-command ,font-load-command)
375             (define beam ,beam)
376             (define bezier-sandwich ,bezier-sandwich)
377             (define bracket ,bracket)
378             (define char ,char)
379             (define crescendo ,crescendo)
380             (define dashed-slur ,dashed-slur) 
381             (define decrescendo ,decrescendo) 
382             (define end-output ,end-output)
383             (define experimental-on ,experimental-on)
384             (define filledbox ,filledbox)
385             (define font-def ,font-def)
386             (define font-switch ,font-switch)
387             (define header-end ,header-end)
388             (define lily-def ,lily-def)
389             (define header ,header) 
390             (define invoke-char ,invoke-char) 
391             (define invoke-dim1 ,invoke-dim1)
392             (define placebox ,placebox)
393             (define select-font ,select-font)
394             (define start-line ,start-line)
395             (define stop-line ,stop-line)
396             (define stop-last-line ,stop-last-line)
397             (define text ,text)
398             (define tuplet ,tuplet)
399             (define volta ,volta)
400             ))
401
402         ((eq? action-name 'beam) beam)
403         ((eq? action-name 'tuplet) tuplet)
404         ((eq? action-name 'bracket) bracket)
405         ((eq? action-name 'crescendo) crescendo)
406         ((eq? action-name 'dashed-slur) dashed-slur) 
407         ((eq? action-name 'decrescendo) decrescendo) 
408         ((eq? action-name 'end-output) end-output)
409         ((eq? action-name 'experimental-on) experimental-on)
410         ((eq? action-name 'font-def) font-def)
411         ((eq? action-name 'font-switch) font-switch)
412         ((eq? action-name 'header-end) header-end)
413         ((eq? action-name 'lily-def) lily-def)
414         ((eq? action-name 'header) header) 
415         ((eq? action-name 'invoke-char) invoke-char) 
416         ((eq? action-name 'invoke-dim1) invoke-dim1)
417         ((eq? action-name 'placebox) placebox)
418         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
419         ((eq? action-name 'start-line) start-line)
420         ((eq? action-name 'stem) stem)
421         ((eq? action-name 'stop-line) stop-line)
422         ((eq? action-name 'stop-last-line) stop-last-line)
423         ((eq? action-name 'volta) volta)
424         (else (error "unknown tag -- PS-TEX " action-name))
425         )
426   )
427
428
429 ;;;;;;;;;;;; PS
430 (define (ps-scm action-name)
431
432
433   ;; alist containing fontname -> fontcommand assoc (both strings)
434   (define font-alist '())
435   (define font-count 0)
436   (define current-font "")
437
438   
439   (define (cached-fontname i)
440     (string-append
441      "lilyfont"
442      (make-string 1 (integer->char (+ 65 i)))))
443     
444   (define (mag-to-size m)
445     (number->string (case m 
446                       (0 12)
447                       (1 12)
448                       (2 14) ; really: 14.400
449                       (3 17) ; really: 17.280
450                       (4 21) ; really: 20.736
451                       (5 24) ; really: 24.888
452                       (6 30) ; really: 29.856
453                       )))
454   
455   
456   (define (select-font font-name-symbol)
457     (let*
458         (
459          (c (assoc font-name-symbol font-name-alist))
460          )
461
462       (if (eq? c #f)
463           (begin
464             (ly-warn (string-append
465                       "Programming error: No such font known " (car font-name-symbol)))
466             "")                         ; issue no command
467           (string-append " " (cdr c) " "))
468       
469       
470       ))
471
472     (define (font-load-command name-mag command)
473       (string-append
474        "/" command
475        " { /"
476        (symbol->string (car name-mag))
477        " findfont "
478        (number->string (magstep (cdr name-mag)))
479        " 1000 div 12 mul  scalefont setfont } bind def "
480        "\n"))
481
482
483   (define (beam width slope thick)
484     (string-append
485      (numbers->string (list width slope thick)) " draw_beam" ))
486
487   (define (comment s)
488     (string-append "% " s))
489
490   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
491     (string-append
492      (numbers->string (list arch_angle arch_width arch_height width height arch_thick thick)) " draw_bracket" ))
493
494   (define (char i)
495     (invoke-char " show" i))
496
497   (define (crescendo thick w h cont )
498     (string-append 
499      (numbers->string (list w h (inexact->exact cont) thick))
500      " draw_crescendo"))
501
502   ;; what the heck is this interface ?
503   (define (dashed-slur thick dash l)
504     (string-append 
505      (apply string-append (map control->string l)) 
506      (number->string thick) 
507      " [ "
508      (number->string dash)
509      " "
510      (number->string (* 10 thick))      ;UGH.  10 ?
511      " ] 0 draw_dashed_slur"))
512
513   (define (decrescendo thick w h cont)
514     (string-append 
515      (numbers->string (list w h (inexact->exact cont) thick))
516      " draw_decrescendo"))
517
518
519   (define (end-output)
520     "\nshowpage\n")
521   
522   (define (experimental-on) "")
523   
524   (define (filledbox breapth width depth height) 
525     (string-append (numbers->string (list breapth width depth height))
526                    " draw_box" ))
527
528   ;; obsolete?
529   (define (font-def i s)
530     (string-append
531      "\n/" (font i) " {/" 
532      (substring s 0 (- (string-length s) 4))
533      " findfont 12 scalefont setfont} bind def \n"))
534
535   (define (font-switch i)
536     (string-append (font i) " "))
537
538   (define (header-end)
539     (string-append
540      (ly-gulp-file "lilyponddefs.ps")
541      " {exch pop //systemdict /run get exec} "
542      (ly-gulp-file "lily.ps")
543      "{ exch pop //systemdict /run get exec } "
544     ))
545   
546   (define (lily-def key val)
547
548      (if (string=? (substring key 0 (min (string-length "mudelapaper") (string-length key))) "mudelapaper")
549          (string-append "/" key " {" val "} bind def\n")
550          (string-append "/" key " (" val ") def\n")
551          )
552      )
553
554   (define (header creator generate) 
555     (string-append
556      "%!PS-Adobe-3.0\n"
557      "%%Creator: " creator generate "\n"))
558   
559   (define (invoke-char s i)
560     (string-append 
561      "(\\" (inexact->string i 8) ") " s " " ))
562   
563   (define (invoke-dim1 s d) 
564     (string-append
565      (number->string (* d  (/ 72.27 72))) " " s ))
566
567   (define (placebox x y s) 
568     (string-append 
569      (number->string x) " " (number->string y) " {" s "} placebox "))
570
571   (define (bezier-sandwich l thick)
572     (string-append 
573      (apply string-append (map control->string l))
574      (number->string  thick)
575      " draw_bezier_sandwich"))
576
577   (define (start-line height)
578           "\nstart_line {\n")
579   
580   (define (stem breapth width depth height) 
581     (string-append (numbers->string (list breapth width depth height))
582                    " draw_box" ))
583
584   (define (stop-line)
585       "}\nstop_line\n")
586
587   (define (text s)
588     (string-append "(" s ") show  "))
589
590
591   (define (volta h w thick vert_start vert_end)
592     (string-append 
593      (numbers->string (list h w thick (inexact->exact vert_start) (inexact->exact vert_end)))
594      " draw_volta"))
595
596   (define (tuplet ht gap dx dy thick dir)
597     (string-append 
598      (numbers->string (list ht gap dx dy thick (inexact->exact dir)))
599      " draw_tuplet"))
600
601
602   (define (unknown) 
603     "\n unknown\n")
604
605
606   ;; PS
607   (cond ((eq? action-name 'all-definitions)
608          `(begin
609             (define beam ,beam)
610             (define tuplet ,tuplet)
611             (define bracket ,bracket)
612             (define char ,char)
613             (define crescendo ,crescendo)
614             (define volta ,volta)
615             (define bezier-sandwich ,bezier-sandwich)
616             (define dashed-slur ,dashed-slur) 
617             (define decrescendo ,decrescendo) 
618             (define end-output ,end-output)
619             (define experimental-on ,experimental-on)
620             (define filledbox ,filledbox)
621             (define font-def ,font-def)
622             (define font-switch ,font-switch)
623             (define header-end ,header-end)
624             (define lily-def ,lily-def)
625             (define font-load-command ,font-load-command)
626             (define header ,header) 
627             (define invoke-char ,invoke-char) 
628             (define invoke-dim1 ,invoke-dim1)
629             (define placebox ,placebox)
630             (define select-font ,select-font)
631             (define start-line ,start-line)
632             (define stem ,stem)
633             (define stop-line ,stop-line)
634             (define stop-last-line ,stop-line)
635             (define text ,text)
636             ))
637         ((eq? action-name 'tuplet) tuplet)
638         ((eq? action-name 'beam) beam)
639         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
640         ((eq? action-name 'bracket) bracket)
641         ((eq? action-name 'char) char)
642         ((eq? action-name 'crescendo) crescendo)
643         ((eq? action-name 'dashed-slur) dashed-slur) 
644         ((eq? action-name 'decrescendo) decrescendo)
645         ((eq? action-name 'experimental-on) experimental-on)
646         ((eq? action-name 'filledbox) filledbox)
647         ((eq? action-name 'select-font) select-font)
648         ((eq? action-name 'volta) volta)
649         (else (error "unknown tag -- PS-SCM " action-name))
650         )
651   )
652
653
654 (define (arg->string arg)
655   (cond ((number? arg) (inexact->string arg 10))
656         ((string? arg) (string-append "\"" arg "\""))
657         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
658
659 (define (func name . args)
660   (string-append 
661    "(" name 
662    (if (null? args) 
663        ""
664        (apply string-append 
665               (map (lambda (x) (string-append " " (arg->string x))) args)))
666    ")\n"))
667
668 (define (sign x)
669   (if (= x 0)
670       1
671       (inexact->exact (/ x (abs x)))))
672
673 ;;;; AsciiScript as
674 (define (as-scm action-name)
675
676   (define (beam width slope thick)
677           (string-append
678            (func "set-line-char" "#")
679            (func "rline-to" width (* width slope))
680            ))
681
682   ; simple flat slurs
683   (define (bezier-sandwich l thick)
684           (let (
685                 (c0 (cadddr l))
686                 (c1 (cadr l))
687                 (c3 (caddr l)))
688                (let* ((x (car c0))
689                       (dx (- (car c3) x))
690                       (dy (- (cdr c3) (cdr c0)))
691                       (rc (/ dy dx))
692                       (c1-dx (- (car c1) x))
693                       (c1-line-y (+ (cdr c0) (* c1-dx rc)))
694                       (dir (if (< c1-line-y (cdr c1)) 1 -1))
695                       (y (+ -1 (* dir (max (* dir (cdr c0)) (* dir (cdr c3)))))))
696                      (string-append
697                       (func "rmove-to" x y)
698                       (func "put" (if (< 0 dir) "/" "\\\\"))
699                       (func "rmove-to" 1 (if (< 0 dir) 1 0))
700                       (func "set-line-char" "_")
701                       (func "h-line" (- dx 1))
702                       (func "rmove-to" (- dx 1) (if (< 0 dir) -1 0))
703                       (func "put" (if (< 0 dir) "\\\\" "/"))))))
704
705   (define (bracket arch_angle arch_width arch_height width height arch_thick thick)
706           (string-append
707            (func "rmove-to" (+ width 1) (- (/ height -2) 1))
708            (func "put" "\\\\")
709            (func "set-line-char" "|")
710            (func "rmove-to" 0 1)
711            (func "v-line" (+ height 1))
712            (func "rmove-to" 0 (+ height 1))
713            (func "put" "/")
714            ))
715
716   (define (char i)
717     (func "char" i))
718
719   (define (end-output) 
720     (func "end-output"))
721   
722   (define (experimental-on)
723           "")
724
725   (define (filledbox breapth width depth height)
726           (let ((dx (+ width breapth))
727                 (dy (+ depth height)))
728                (string-append 
729                 (func "rmove-to" (* -1 breapth) (* -1 depth))
730                 (if (< dx dy)
731                     (string-append
732                      (func "set-line-char" 
733                            (if (<= dx 1) "|" "#"))
734                      (func "v-line" dy))
735                     (string-append
736                      (func "set-line-char" 
737                            (if (<= dy 1) "-" "="))
738                     (func "h-line" dx))))))
739
740   (define (font-load-command name-mag command)
741     (func "load-font" (car name-mag) (magstep (cdr name-mag))))
742
743   (define (header creator generate) 
744     (func "header" creator generate))
745
746   (define (header-end) 
747     (func "header-end"))
748
749   ;; urg: this is good for half of as2text's execution time
750   (define (xlily-def key val)
751           (string-append "(define " key " " (arg->string val) ")\n"))
752
753   (define (lily-def key val)
754           (if 
755            (equal? key "mudelapaperlinewidth")
756            (string-append "(define " key " " (arg->string val) ")\n")
757            ""))
758
759   (define (placebox x y s) 
760     (string-append (func "move-to" x y) s))
761
762   (define (select-font font-name-symbol)
763     (let* ((c (assoc font-name-symbol font-name-alist)))
764       (if (eq? c #f)
765           (begin
766             (ly-warn 
767              (string-append 
768               "Programming error: No such font known " 
769               (car font-name-symbol)))
770             "")                         ; issue no command
771           (func "select-font" (car font-name-symbol)))))
772
773   (define (start-line height)
774           (func "start-line" height))
775
776   (define (stop-line)
777           (func "stop-line"))
778
779   (define (text s)
780           (func "text" s))
781
782   (define (volta h w thick vert-start vert-end)
783           ;; urg
784           (set! h 1)
785           (string-append
786            (func "set-line-char" "|")
787            (func "rmove-to" 0 -4)
788            ;; definition strange-way around
789            (if (= 0 vert-start)
790               (func "v-line" h)
791                "")
792            (func "rmove-to" 1 h)
793            (func "set-line-char" "_")
794            (func "h-line" (- w 1))
795            (func "set-line-char" "|")
796            (if (= 0 vert-end)
797                (string-append
798                 (func "rmove-to" (- w 1) (* -1 h))
799                 (func "v-line" (* -1 h)))
800                "")))
801
802   (cond ((eq? action-name 'all-definitions)
803          `(begin
804             (define beam ,beam)
805             (define bracket ,bracket)
806             (define char ,char)
807             ;;(define crescendo ,crescendo)
808             (define bezier-sandwich ,bezier-sandwich)
809             ;;(define dashed-slur ,dashed-slur) 
810             ;;(define decrescendo ,decrescendo) 
811             (define end-output ,end-output)
812             (define experimental-on ,experimental-on)
813             (define filledbox ,filledbox)
814             ;;(define font-def ,font-def)
815             (define font-load-command ,font-load-command)
816             ;;(define font-switch ,font-switch)
817             (define header ,header) 
818             (define header-end ,header-end)
819             (define lily-def ,lily-def)
820             ;;(define invoke-char ,invoke-char) 
821             ;;(define invoke-dim1 ,invoke-dim1)
822             (define placebox ,placebox)
823             (define select-font ,select-font)
824             (define start-line ,start-line)
825             ;;(define stem ,stem)
826             (define stop-line ,stop-line)
827             (define stop-last-line ,stop-line)
828             (define text ,text)
829             ;;(define tuplet ,tuplet)
830             (define volta ,volta)
831             ))
832         ;;((eq? action-name 'tuplet) tuplet)
833         ;;((eq? action-name 'beam) beam)
834         ;;((eq? action-name 'bezier-sandwich) bezier-sandwich)
835         ;;((eq? action-name 'bracket) bracket)
836         ((eq? action-name 'char) char)
837         ;;((eq? action-name 'crescendo) crescendo)
838         ;;((eq? action-name 'dashed-slur) dashed-slur) 
839         ;;((eq? action-name 'decrescendo) decrescendo)
840         ;;((eq? action-name 'experimental-on) experimental-on)
841         ((eq? action-name 'filledbox) filledbox)
842         ((eq? action-name 'select-font) select-font)
843         ;;((eq? action-name 'volta) volta)
844         (else (error "unknown tag -- MUSA-SCM " action-name))
845         )
846   )
847
848
849 (define (gulp-file name)
850   (let* ((port (open-file name "r"))
851          (content (let loop ((text ""))
852                        (let ((line (read-line port)))
853                             (if (or (eof-object? line)
854                                     (not line)) 
855                                 text
856                                 (loop (string-append text line "\n")))))))
857         (close port)
858         content))
859
860 (define (scm-gulp-file name)
861   (set! %load-path 
862         (cons (string-append 
863                (getenv 'LILYPONDPREFIX) "/ps") %load-path))
864   (let ((path (%search-load-path name)))
865        (if path
866            (gulp-file path)
867            (gulp-file name))))
868
869 (define (scm-tex-output)
870   (eval (tex-scm 'all-definitions)))
871                                 
872 (define (scm-ps-output)
873   (eval (ps-scm 'all-definitions)))
874
875 (define (scm-as-output)
876   (eval (as-scm 'all-definitions)))
877                                 
878 ; Russ McManus, <mcmanus@IDT.NET>  
879
880 ; I use the following, which should definitely be provided somewhere
881 ; in guile, but isn't, AFAIK:
882
883
884
885 (define (hash-table-for-each fn ht)
886   (do ((i 0 (+ 1 i)))
887       ((= i (vector-length ht)))
888     (do ((alist (vector-ref ht i) (cdr alist)))
889         ((null? alist) #t)
890       (fn (car (car alist)) (cdr (car alist))))))
891
892 (define (hash-table-map fn ht)
893   (do ((i 0 (+ 1 i))
894        (ret-ls '()))
895       ((= i (vector-length ht)) (reverse ret-ls))
896     (do ((alist (vector-ref ht i) (cdr alist)))
897         ((null? alist) #t)
898       (set! ret-ls (cons (fn (car (car alist)) (cdr (car alist))) ret-ls)))))
899
900
901
902 (define (index-cell cell dir)
903   (if (equal? dir 1)
904       (cdr cell)
905       (car cell)))
906
907 ;
908 ; How should a  bar line behave at a break? 
909 ;
910 (define (break-barline glyph dir)
911    (let ((result (assoc glyph 
912                         '((":|:" . (":|" . "|:"))
913                           ("|" . ("|" . ""))
914                           ("|s" . (nil . "|"))
915                           ("|:" . ("|" . "|:"))
916                           ("|." . ("|." . nil))
917                           (":|" . (":|" . nil))
918                           ("||" . ("||" . nil))
919                           (".|." . (".|." . nil))
920                           ("scorebar" . (nil . "scorepostbreak"))
921                           ("brace" . (nil . "brace"))
922                           ("bracket" . (nil . "bracket"))  
923                           )
924                         )))
925
926      (if (equal? result #f)
927          (ly-warn (string-append "Unknown bar glyph: `" glyph "'"))
928          (index-cell (cdr result) dir))
929      )
930    )
931      
932
933 (define (slur-ugly ind ht)
934   (if (and
935 ;       (< ht 4.0)
936        (< ht (* 4 ind))
937        (> ht (* 0.4 ind))
938        (> ht (+ (* 2 ind) -4))
939        (< ht (+ (* -2 ind) 8)))
940       #f
941       (cons ind  ht)
942   ))