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