]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Merge branch 'master' into lilypond/translation
[lilypond.git] / scm / output-lib.scm
1 ;;;; output-lib.scm -- implement Scheme output helper functions
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8
9
10
11 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12 ;; general
13 (define-public (grob::has-interface grob iface)
14   (memq iface (ly:grob-interfaces grob)))
15
16 (define-public (make-stencil-boxer thickness padding callback)
17
18   "Return function that adds a box around the grob passed as argument."
19   (lambda (grob)
20     
21     (box-stencil (callback grob) thickness padding)))
22
23 (define-public (make-stencil-circler thickness padding callback)
24   "Return function that adds a circle around the grob passed as argument."
25   
26   (lambda (grob) (circle-stencil (callback grob) thickness padding)))
27
28 (define-public (print-circled-text-callback grob)
29   (grob-interpret-markup grob (make-circle-markup
30                    (ly:grob-property grob 'text))
31              ))
32
33 (define-public (event-cause grob)
34   (let*
35       ((cause (ly:grob-property  grob 'cause)))
36     
37     (cond
38      ((ly:stream-event? cause) cause)
39      ((ly:grob? cause) (event-cause cause))
40      (else #f))))
41
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 ;; tablature
44
45 ;; The TabNoteHead tablatureFormat callback.
46 ;; Compute the text grob-property
47 (define-public (fret-number-tablature-format string
48                                              context event)
49   (let*
50       ((tuning (ly:context-property context 'stringTunings))
51        (pitch (ly:event-property event 'pitch))
52        (is-harmonic (apply
53                      functional-or
54                      (map
55                       (lambda (ev)
56                         (eq? 'harmonic-event (ly:event-property ev 'class)))
57                       (ly:event-property event 'articulations)))))
58
59     
60     (make-whiteout-markup
61      (make-vcenter-markup
62       (format
63        "~a"
64        (- (ly:pitch-semitones pitch)
65           (list-ref tuning
66                     ;; remove 1 because list index starts at 0 and guitar string at 1. 
67                     (- string 1))))))
68     ))
69
70 ;; The 5-string banjo has got a extra string, the fifth (duh), wich
71 ;; starts at the fifth fret on the neck. Frets on the fifth string
72 ;; are referred to relative to the other frets:
73 ;;   the "first fret" on the fifth string is really the sixth fret
74 ;;   on the banjo neck.
75 ;; We solve this by defining a new fret-number-tablature function:
76 (define-public (fret-number-tablature-format-banjo string 
77                                              context event)
78   (let*
79       ((tuning (ly:context-property context 'stringTunings))
80        (pitch (ly:event-property event 'pitch))
81         )
82   (make-whiteout-markup
83    (make-vcenter-markup  
84     (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (- string 1)))))
85       (number->string (cond
86                        ((and (> fret 0) (= string 5))
87                         (+ fret 5))
88                        (else fret))))))
89   ))
90
91
92 ; default tunings for common string instruments
93 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
94 (define-public guitar-open-g-tuning '(2 -1 -5 -10 -17 -22))
95 (define-public bass-tuning '(-17 -22 -27 -32))
96 (define-public mandolin-tuning '(16 9 2 -5))
97
98 ;; tunings for 5-string banjo
99 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
100 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
101 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
102 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
103 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
104 ;; convert 5-string banjo tuning to 4-string by removing the 5th string
105 (define-public (four-string-banjo tuning)
106   (reverse (cdr (reverse tuning))))
107
108 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
109 ;; note heads
110
111
112 (define-public (stem::calc-duration-log grob)
113   (ly:duration-log
114    (ly:event-property (event-cause grob) 'duration)))
115
116 (define-public (note-head::calc-duration-log grob)
117   (min 2 
118        (ly:duration-log
119         (ly:event-property (event-cause grob) 'duration))))
120
121 (define-public (dots::calc-dot-count grob)
122   (ly:duration-dot-count
123    (ly:event-property (event-cause grob) 'duration)))
124
125 (define-public (dots::calc-staff-position grob)
126   (let*
127       ((head (ly:grob-parent grob Y))
128        (log (ly:grob-property head 'duration-log)))
129
130     (cond
131      ((or (not (grob::has-interface head 'rest-interface))
132          (not (integer? log))) 0)
133      ((= log 7) 4)
134      ((> log 4) 3)
135      ((= log 0) -1)
136      ((= log 1) 1)
137      ((= log -1) 1)
138      (else 0))))
139
140 (define (note-head::calc-tablature-stem-attachment grob)
141   (cons 0.0 1.35))
142
143
144
145 ;; silly, use alist? 
146 (define-public (note-head::calc-glyph-name grob)
147   (let*
148       ((style (ly:grob-property grob 'style))
149        (log (min 2 (ly:grob-property grob 'duration-log))))
150     
151     (case style
152       ;; "default" style is directly handled in note-head.cc as a
153       ;; special case (HW says, mainly for performance reasons).
154       ;; Therefore, style "default" does not appear in this case
155       ;; statement.  -- jr
156       ((xcircle) "2xcircle")
157       ((harmonic) "0harmonic")
158       ((harmonic-black) "2harmonic")
159       ((harmonic-mixed) (if (<= log 1) "0harmonic"
160                                        "2harmonic"))
161       ((baroque) 
162        ;; Oops, I actually would not call this "baroque", but, for
163        ;; backwards compatibility to 1.4, this is supposed to take
164        ;; brevis, longa and maxima from the neo-mensural font and all
165        ;; other note heads from the default font.  -- jr
166        (if (< log 0)
167            (string-append (number->string log) "neomensural")
168            (number->string log)))
169       ((mensural)
170        (string-append (number->string log) (symbol->string style)))
171       ((petrucci)
172        (if (< log 0)
173            (string-append (number->string log) "mensural")
174            (string-append (number->string log) (symbol->string style))))
175       ((neomensural)
176        (string-append (number->string log) (symbol->string style)))
177       (else
178        (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
179            (symbol->string style)
180            (string-append (number->string (max 0 log))
181                           (symbol->string style)))))))
182
183 ;; TODO junk completely?
184 (define (note-head-style->attachment-coordinates grob axis)
185   "Return pair (X . Y), containing multipliers for the note head
186 bounding box, where to attach the stem. e.g.: X==0 means horizontally
187 centered, X==1 is at the right, X == -1 is at the left."
188
189   '(1.0 . 0.0))
190
191
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 ;; bar numbers
194
195 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
196
197 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
198
199 (define-public ((set-bar-number-visibility n) tr)
200   (let* ((bn (ly:context-property tr 'currentBarNumber)))
201     (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
202
203 (define-public (first-bar-number-invisible barnum) (> barnum 1))
204
205 (define-public (all-bar-numbers-visible barnum) #t)
206
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 ;; percent repeat counters
209
210 (define-public ((every-nth-repeat-count-visible n) count context) (= 0 (modulo count n)))
211
212 (define-public (all-repeat-counts-visible count context) #t)
213
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
215 ;; break visibility
216
217 (define-public all-visible             #(#t #t #t))
218 (define-public begin-of-line-invisible #(#t #t #f))
219 (define-public center-invisible        #(#t #f #t))
220 (define-public end-of-line-invisible   #(#f #t #t))
221 (define-public begin-of-line-visible   #(#f #f #t))
222 (define-public center-visible          #(#f #t #f))
223 (define-public end-of-line-visible     #(#t #f #f))
224 (define-public all-invisible           #(#f #f #f))
225
226 (define-public spanbar-begin-of-line-invisible
227   #(#t #f #f))
228
229 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
230 ;; Bar lines.
231
232 ;;
233 ;; How should a  bar line behave at a break? 
234 (define bar-glyph-alist
235   '((":|:" . (":|" . "|:"))
236     (":|.|:" . (":|" . "|:"))
237     (":|.:" . (":|" . "|:"))
238     ("||:" . ("||" . "|:"))
239     ("dashed" . ("dashed" . '())) 
240     ("|" . ("|" . ()))
241     ("||:" . ("||" . "|:"))
242     ("|s" . (() . "|"))
243     ("|:" . ("|" . "|:"))
244     ("|." . ("|." . ()))
245     
246     ;; hmm... should we end with a bar line here?
247     (".|" . ("|" . ".|"))
248     (":|" . (":|" . ()))
249     ("||" . ("||" . ()))
250     (".|." . (".|." . ()))
251     ("|.|" . ("|.|" . ()))
252     ("" . ("" . ""))
253     (":" . (":" . ""))
254     ("." . ("." . ()))
255     ("'" . ("'" . ()))
256     ("empty" . (() . ()))
257     ("brace" . (() . "brace"))
258     ("bracket" . (() . "bracket")) 
259     ))
260
261 (define-public (bar-line::calc-glyph-name grob)
262   (let* (
263          (glyph (ly:grob-property grob 'glyph))
264          (dir (ly:item-break-dir grob))
265          (result (assoc glyph  bar-glyph-alist))
266          (glyph-name (if (= dir CENTER)
267                          glyph
268                          (if (and result (string? (index-cell (cdr result) dir)))
269                              (index-cell (cdr result) dir)
270                              #f)))
271          )
272     glyph-name))
273
274 (define-public (bar-line::calc-break-visibility grob)
275   (let* ((glyph (ly:grob-property grob 'glyph))
276          (result (assoc glyph bar-glyph-alist)))
277     (if result
278         (vector (string? (cadr result)) #t (string? (cddr result)))
279         #(#f #f #f))))
280
281
282 (define-public (shift-right-at-line-begin g)
283   "Shift an item to the right, but only at the start of the line."
284   (if (and (ly:item? g)
285            (equal? (ly:item-break-dir g) RIGHT))
286       (ly:grob-translate-axis! g 3.5 X)))
287
288
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
290 ;; Tuplets
291
292 (define-public (tuplet-number::calc-denominator-text grob)
293   (number->string (ly:event-property (event-cause grob) 'denominator)))
294
295 (define-public (tuplet-number::calc-fraction-text grob)
296   (let*
297       ((ev (event-cause grob)))
298
299     (format "~a:~a" 
300             (ly:event-property ev 'denominator)
301             (ly:event-property ev 'numerator))))
302
303
304 ; a formatter function, which is simply a wrapper around an existing 
305 ; tuplet formatter function. It takes the value returned by the given
306 ; function and appends a note of given length. 
307 (define-public ((tuplet-number::append-note-wrapper function note) grob)
308   (let* ((txt (if function (function grob) #f)))
309     (if txt 
310       (markup txt #:fontsize -5 #:note note UP)
311       (markup #:fontsize -5 #:note note UP))))
312
313 ; Print a tuplet denominator with a different number than the one derived from 
314 ; the actual tuplet fraction
315 (define-public ((tuplet-number::non-default-tuplet-denominator-text denominator) grob)
316 (number->string (if denominator 
317                     denominator 
318                     (ly:event-property (event-cause grob) 'denominator))))
319
320 ; Print a tuplet fraction with different numbers than the ones derived from 
321 ; the actual tuplet fraction
322 (define-public ((tuplet-number::non-default-tuplet-fraction-text denominator numerator) grob)
323   (let* ((ev (event-cause grob))
324          (den (if denominator denominator (ly:event-property ev 'denominator)))
325          (num (if numerator numerator (ly:event-property ev 'numerator))))
326      (format "~a:~a" den num)))
327
328 ; Print a tuplet fraction with note durations appended to the numerator and the 
329 ; denominator
330 (define-public ((tuplet-number::fraction-with-notes denominatornote numeratornote) grob)
331   (let* ((ev (event-cause grob))
332          (denominator (ly:event-property ev 'denominator))
333          (numerator (ly:event-property ev 'numerator)))
334     ((tuplet-number::non-default-fraction-with-notes denominator denominatornote numerator numeratornote) grob)))
335
336 ; Print a tuplet fraction with note durations appended to the numerator and the 
337 ; denominator
338 (define-public ((tuplet-number::non-default-fraction-with-notes denominator denominatornote numerator numeratornote) grob)
339   (let* ((ev (event-cause grob))
340          (den (if denominator denominator (ly:event-property ev 'denominator)))
341          (num (if numerator numerator (ly:event-property ev 'numerator))))
342      (make-concat-markup (list 
343           (make-simple-markup (format "~a" den)) 
344           (markup #:fontsize -5 #:note denominatornote UP)
345           (make-simple-markup " : ")
346           (make-simple-markup (format "~a" num)) 
347           (markup #:fontsize -5 #:note numeratornote UP)))))
348
349
350 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
351 ;; Color
352
353 (define-public color? list?)
354 (define-public (rgb-color r g b) (list r g b))
355
356 ; predefined colors
357 (define-public black       '(0.0 0.0 0.0))
358 (define-public white       '(1.0 1.0 1.0))
359 (define-public red         '(1.0 0.0 0.0))
360 (define-public green       '(0.0 1.0 0.0))
361 (define-public blue        '(0.0 0.0 1.0))
362 (define-public cyan        '(0.0 1.0 1.0))
363 (define-public magenta     '(1.0 0.0 1.0))
364 (define-public yellow      '(1.0 1.0 0.0))
365
366 (define-public grey        '(0.5 0.5 0.5))
367 (define-public darkred     '(0.5 0.0 0.0))
368 (define-public darkgreen   '(0.0 0.5 0.0))
369 (define-public darkblue    '(0.0 0.0 0.5))
370 (define-public darkcyan    '(0.0 0.5 0.5))
371 (define-public darkmagenta '(0.5 0.0 0.5))
372 (define-public darkyellow  '(0.5 0.5 0.0))
373
374 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
375 ;; key signature
376
377
378 (define-public (key-signature-interface::alteration-position step alter c0-position)
379   ;; TODO: memoize - this is mostly constant.
380   
381   ;; fes, ges, as and bes typeset in lower octave
382   (define FLAT_TOP_PITCH 2)
383   
384   ;; ais and bis typeset in lower octave
385   (define SHARP_TOP_PITCH 4)
386
387   (if (pair? step)
388       (+ (cdr step) (* (car step) 7)  c0-position)
389       (let*
390           ((from-bottom-pos (modulo (+ 4 49 c0-position)  7))
391            (p step)
392            (c0 (- from-bottom-pos  4)))
393         
394         (if
395          (or (and (< alter 0) (or (> p FLAT_TOP_PITCH) (> (+ p c0) 4)) (> (+ p c0) 1))
396              (and (> alter 0) (or (> p SHARP_TOP_PITCH) (> (+ p c0) 5)) (> (+ p c0) 2))
397              )
398
399          ;; Typeset below c_position 
400          (set! p (- p 7)))
401
402         ;; Provide for the four cases in which there's a glitch
403         ;; it's a hack, but probably not worth
404         ;; the effort of finding a nicer solution.
405         ;; --dl. 
406         (cond
407          ((and (= c0 2) (= p 3) (> alter 0))
408           (set! p (- p 7)))
409          ((and (= c0 -3) (= p -1) (> alter 0))
410           (set! p (+ p 7)))
411          ((and (= c0 -4) (= p -1) (< alter 0))
412           (set! p (+ p 7)))
413          ((and (= c0 -2) (= p -3) (< alter 0))
414           (set! p (+ p 7))))
415
416         (+ c0 p))))
417
418
419 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
420 ;; accidentals
421
422 (define-public (accidental-interface::calc-alteration grob)
423   (ly:pitch-alteration  (ly:event-property (event-cause grob) 'pitch)))
424
425
426 (define-public cancellation-glyph-name-alist
427   '((0 . "accidentals.natural")))
428
429 (define-public standard-alteration-glyph-name-alist
430      '(
431        ;; ordered for optimal performance.
432        (0 . "accidentals.natural")
433        (-1/2 . "accidentals.flat")
434        (1/2 . "accidentals.sharp")
435
436        (1 . "accidentals.doublesharp")
437        (-1 . "accidentals.flatflat")
438        
439        (3/4 . "accidentals.sharp.slashslash.stemstemstem")
440        (1/4 . "accidentals.sharp.slashslash.stem")
441        (-1/4 . "accidentals.mirroredflat")
442        (-3/4 . "accidentals.mirroredflat.flat")
443        ))
444
445 ;; FIXME: standard vs default, alteration-FOO vs FOO-alteration
446 (define-public alteration-default-glyph-name-alist standard-alteration-glyph-name-alist)
447
448 (define-public makam-alteration-glyph-name-alist
449      '((1 . "accidentals.doublesharp")
450        (8/9 . "accidentals.sharp.slashslashslash.stemstem")
451        (5/9 . "accidentals.sharp.slashslashslash.stem")
452        (4/9 . "accidentals.sharp")
453        (1/9 . "accidentals.sharp.slashslash.stem")
454        (0 . "accidentals.natural")
455        (-1/9 . "accidentals.mirroredflat")
456        (-4/9 . "accidentals.flat.slash")
457        (-5/9 . "accidentals.flat")
458        (-8/9 . "accidentals.flat.slashslash")
459        (-1 . "accidentals.flatflat")
460        ))
461   
462 (define-public alteration-hufnagel-glyph-name-alist
463    '((-1/2 . "accidentals.hufnagelM1")
464      (0 . "accidentals.vaticana0")
465      (1/2 . "accidentals.mensural1")))
466
467 (define-public alteration-medicaea-glyph-name-alist
468    '((-1/2 . "accidentals.medicaeaM1")
469      (0 . "accidentals.vaticana0")
470      (1/2 . "accidentals.mensural1")))
471
472 (define-public alteration-vaticana-glyph-name-alist
473    '((-1/2 . "accidentals.vaticanaM1")
474      (0 . "accidentals.vaticana0")
475      (1/2 . "accidentals.mensural1")))
476
477 (define-public alteration-mensural-glyph-name-alist
478    '((-1/2 . "accidentals.mensuralM1")
479      (0 . "accidentals.vaticana0")
480      (1/2 . "accidentals.mensural1")))
481
482
483 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
484 ;; * Pitch Trill Heads
485 ;; * Parentheses
486
487 (define-public (parentheses-item::calc-parenthesis-stencils grob)
488   (let* ((font (ly:grob-default-font grob))
489          (lp (ly:font-get-glyph font "accidentals.leftparen"))
490          (rp (ly:font-get-glyph font "accidentals.rightparen")))
491
492     (list lp rp)))
493
494
495 (define-public (grob-interpret-markup grob text)
496   (let*
497       ((layout (ly:grob-layout grob))
498        (defs (ly:output-def-lookup layout 'text-font-defaults))
499        (props (ly:grob-alist-chain grob defs)))
500
501     (ly:text-interface::interpret-markup
502      layout props text)))
503
504 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
505   (let* (
506          (font (ly:grob-default-font grob))
507          (lp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-interpret-markup grob (ly:wide-char->utf-8 #x2329))
508                                                            Y CENTER)  X RIGHT))
509          (rp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-interpret-markup grob (ly:wide-char->utf-8 #x232A))
510                                                            Y CENTER) X LEFT))
511          )
512
513     (list (stencil-whiteout lp)
514           (stencil-whiteout rp))))
515
516 (define (parenthesize-elements grob . rest)
517   (let*
518       ((refp (if (null? rest)
519                  grob
520                  (car rest)))
521        (elts (ly:grob-object grob 'elements))
522        (x-ext (ly:relative-group-extent elts refp X))
523        (stencils (ly:grob-property grob 'stencils))
524        (lp (car stencils))
525        (rp (cadr stencils))
526        (padding (ly:grob-property grob 'padding 0.1)))
527     
528     (ly:stencil-add
529      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
530      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
531   ))
532
533
534 (define (parentheses-item::print me)
535   (let*
536       ((elts (ly:grob-object me 'elements))
537        (y-ref (ly:grob-common-refpoint-of-array me elts Y))
538        (x-ref (ly:grob-common-refpoint-of-array me elts X))
539        (stencil (parenthesize-elements me x-ref))
540        (elt-y-ext  (ly:relative-group-extent elts y-ref Y))
541        (y-center (interval-center elt-y-ext)))
542
543     (ly:stencil-translate
544      stencil
545      (cons
546       (-
547        (ly:grob-relative-coordinate me x-ref X))
548       (-
549        y-center
550        (ly:grob-relative-coordinate me y-ref Y))))
551     ))
552
553
554
555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
556 ;; 
557
558 (define-public (chain-grob-member-functions grob value . funcs)
559   (for-each
560    (lambda (func)
561      (set! value (func grob value)))
562    funcs)
563
564   value)
565
566 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
567 ;; falls/doits
568
569 (define-public (bend::print spanner)
570   (define (close  a b)
571     (< (abs (- a b)) 0.01))
572   
573   (let*
574       ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
575        (left-span (ly:spanner-bound spanner LEFT))
576        (dots (if (and (grob::has-interface left-span 'note-head-interface)
577                       (ly:grob? (ly:grob-object left-span 'dot)))
578                  (ly:grob-object left-span 'dot) #f))
579                  
580        (right-span (ly:spanner-bound spanner RIGHT))
581        (thickness (* (ly:grob-property spanner 'thickness)
582                      (ly:output-def-lookup (ly:grob-layout spanner)
583                                            'line-thickness)))
584        (padding (ly:grob-property spanner 'padding 0.5))
585        (common (ly:grob-common-refpoint right-span
586                                         (ly:grob-common-refpoint spanner
587                                                                  left-span X)
588                                         X))
589        (common-y (ly:grob-common-refpoint spanner left-span Y))
590        (minimum-length (ly:grob-property spanner 'minimum-length 0.5))
591
592        (left-x (+ padding
593                   (max (interval-end (ly:grob-robust-relative-extent
594                                       left-span common X))
595                         (if (and
596                              dots
597                              (close (ly:grob-relative-coordinate dots common-y Y)
598                                         (ly:grob-relative-coordinate spanner common-y Y)))
599                             (interval-end (ly:grob-robust-relative-extent dots common X))
600                             -10000) ;; TODO: use real infinity constant.
601                         )))
602        (right-x (max (- (interval-start (ly:grob-robust-relative-extent right-span common X))
603                         padding)
604                      (+ left-x minimum-length)))
605        (self-x (ly:grob-relative-coordinate spanner common X))
606        (dx (- right-x left-x))
607        (exp (list 'path thickness 
608                   `(quote
609                     (rmoveto
610                      ,(- left-x self-x) 0
611
612                      rcurveto                
613                      ,(/ dx 3)
614                      0
615                      ,dx ,(* 0.66 delta-y)
616                      ,dx ,delta-y
617                      )))))
618
619     (ly:make-stencil
620      exp
621      (cons (- left-x self-x) (- right-x self-x))
622      (cons (min 0 delta-y)
623            (max 0 delta-y)))))
624
625 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
626 ;; grace spacing
627
628
629 (define-public (grace-spacing::calc-shortest-duration grob)
630   (let*
631      ((cols (ly:grob-object grob 'columns))
632       (get-difference
633        (lambda (idx)
634          (ly:moment-sub (ly:grob-property
635                          (ly:grob-array-ref cols (1+ idx)) 'when)
636                         (ly:grob-property
637                          (ly:grob-array-ref cols idx) 'when))))
638       
639       (moment-min (lambda (x y)
640                     (cond
641                      ((and x y)
642                       (if (ly:moment<? x y)
643                             x
644                             y))
645                      (x x)
646                      (y y)))))
647                      
648     (fold moment-min #f (map get-difference
649                              (iota (1- (ly:grob-array-length cols)))))))
650
651
652
653 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
654 ;; fingering
655
656 (define-public (fingering::calc-text grob)
657   (let*
658       ((event (event-cause grob))
659        (digit (ly:event-property event 'digit)))
660     
661     (if (> digit 5)
662         (ly:input-message (ly:event-property event 'origin)
663                           "Warning: Fingering notation for finger number ~a" digit))
664
665     (number->string digit 10)
666   ))
667
668 (define-public (string-number::calc-text grob)
669   (let*
670       ((digit (ly:event-property (event-cause  grob) 'string-number)))
671     
672     (number->string digit 10)
673   ))
674
675
676 (define-public (stroke-finger::calc-text grob)
677   (let*
678       ((digit (ly:event-property (event-cause grob) 'digit))
679        (text (ly:event-property (event-cause grob) 'text)))
680
681     (if (string? text)
682         text
683         (vector-ref (ly:grob-property grob 'digit-names)  (1- (max (min 5 digit) 1))))))
684
685 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
686 ;; dynamics
687 (define-public (hairpin::calc-grow-direction grob)
688   (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
689       START
690       STOP
691       ))
692
693 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
694 ;; lyrics
695
696 (define-public (lyric-text::print grob)
697   "Allow interpretation of tildes as lyric tieing marks."
698   
699   (let*
700       ((text (ly:grob-property grob 'text)))
701
702     (grob-interpret-markup grob 
703                (if (string? text)
704                    (make-tied-lyric-markup text)
705                    text))))
706
707 (define-public ((grob::calc-property-by-copy prop) grob)
708   (ly:event-property (event-cause grob) prop))
709
710 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
711 ;; fret boards
712
713 (define-public (fret-board::calc-stencil grob)
714     (grob-interpret-markup 
715       grob
716       (make-fret-diagram-verbose-markup
717         (ly:grob-property grob 'dot-placement-list))))
718
719 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
720 ;; scripts
721
722 (define-public (script-interface::calc-x-offset grob)
723   (ly:grob-property grob 'positioning-done)
724   (let* ((shift (ly:grob-property grob 'toward-stem-shift 0.0))
725          (note-head-location (ly:self-alignment-interface::centered-on-x-parent grob))
726          (note-head-grob (ly:grob-parent grob X))
727          (stem-grob (ly:grob-object note-head-grob 'stem)))
728     (+ note-head-location
729        ;; If the property 'toward-stem-shift is defined and the script has the
730        ;; same direction as the stem, move the script accordingly. Since scripts can
731        ;; also be over skips, we need to check whether the grob has a stem at all.
732        (if (ly:grob? stem-grob)
733            (let ((dir1 (ly:grob-property grob 'direction))
734                  (dir2 (ly:grob-property stem-grob 'direction)))
735              (if (equal? dir1 dir2)
736                  (let* ((common-refp (ly:grob-common-refpoint grob stem-grob X))
737                         (stem-location (ly:grob-relative-coordinate stem-grob common-refp X)))
738                    (* shift (- stem-location
739                                note-head-location)))
740                  0.0))
741            0.0))))