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