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