]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Doc & scm/: Music-functions and type-predicates.
[lilypond.git] / scm / output-lib.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 1998--2010 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 ;; general
22
23 (define-public (grob::has-interface grob iface)
24   (memq iface (ly:grob-interfaces grob)))
25
26 (define-public (grob::is-live? grob)
27   (pair? (ly:grob-basic-properties grob)))
28
29 (define-public (make-stencil-boxer thickness padding callback)
30
31   "Return function that adds a box around the grob passed as argument."
32   (lambda (grob)
33     (box-stencil (callback grob) thickness padding)))
34
35 (define-public (make-stencil-circler thickness padding callback)
36   "Return function that adds a circle around the grob passed as argument."
37
38   (lambda (grob) (circle-stencil (callback grob) thickness padding)))
39
40 (define-public (print-circled-text-callback grob)
41   (grob-interpret-markup grob (make-circle-markup
42                                (ly:grob-property grob 'text))))
43
44 (define-public (event-cause grob)
45   (let ((cause (ly:grob-property  grob 'cause)))
46
47     (cond
48      ((ly:stream-event? cause) cause)
49      ((ly:grob? cause) (event-cause cause))
50      (else #f))))
51
52 (define-public (grob-interpret-markup grob text)
53   (let* ((layout (ly:grob-layout grob))
54          (defs (ly:output-def-lookup layout 'text-font-defaults))
55          (props (ly:grob-alist-chain grob defs)))
56
57     (ly:text-interface::interpret-markup layout props text)))
58
59
60 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 ;; note heads
62
63 (define-public (stem::calc-duration-log grob)
64   (ly:duration-log
65    (ly:event-property (event-cause grob) 'duration)))
66
67 (define-public (note-head::calc-duration-log grob)
68   (min 2
69        (ly:duration-log
70         (ly:event-property (event-cause grob) 'duration))))
71
72 (define-public (dots::calc-dot-count grob)
73   (ly:duration-dot-count
74    (ly:event-property (event-cause grob) 'duration)))
75
76 (define-public (dots::calc-staff-position grob)
77   (let* ((head (ly:grob-parent grob Y))
78          (log (ly:grob-property head 'duration-log)))
79
80     (cond
81      ((or (not (grob::has-interface head 'rest-interface))
82           (not (integer? log))) 0)
83      ((= log 7) 4)
84      ((> log 4) 3)
85      ((= log 0) -1)
86      ((= log 1) 1)
87      ((= log -1) 1)
88      (else 0))))
89
90 ;; Kept separate from note-head::calc-glyph-name to allow use by
91 ;; markup commands \note and \note-by-number
92 (define-public (select-head-glyph style log)
93   "Select a note head glyph string based on note head style @var{style}
94 and duration-log @var{log}."
95   (case style
96     ;; "default" style is directly handled in note-head.cc as a
97     ;; special case (HW says, mainly for performance reasons).
98     ;; Therefore, style "default" does not appear in this case
99     ;; statement.  -- jr
100     ((xcircle) "2xcircle")
101     ((harmonic) "0harmonic")
102     ((harmonic-black) "2harmonic")
103     ((harmonic-mixed) (if (<= log 1) "0harmonic"
104                           "2harmonic"))
105     ((baroque)
106      ;; Oops, I actually would not call this "baroque", but, for
107      ;; backwards compatibility to 1.4, this is supposed to take
108      ;; brevis, longa and maxima from the neo-mensural font and all
109      ;; other note heads from the default font.  -- jr
110      (if (< log 0)
111          (string-append (number->string log) "neomensural")
112          (number->string log)))
113     ((altdefault)
114      ;; Like default, but brevis is drawn with double vertical lines
115      (if (= log -1)
116          (string-append (number->string log) "double")
117          (number->string log)))
118     ((mensural)
119      (string-append (number->string log) (symbol->string style)))
120     ((petrucci)
121      (if (< log 0)
122          (string-append (number->string log) "mensural")
123          (string-append (number->string log) (symbol->string style))))
124     ((neomensural)
125      (string-append (number->string log) (symbol->string style)))
126     (else
127      (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
128          (symbol->string style)
129          (string-append (number->string (max 0 log))
130                         (symbol->string style))))))
131
132 (define-public (note-head::calc-glyph-name grob)
133   (let ((style (ly:grob-property grob 'style))
134         (log (min 2 (ly:grob-property grob 'duration-log))))
135
136     (select-head-glyph style log)))
137
138 (define-public (note-head::brew-ez-stencil grob)
139   (let* ((log (ly:grob-property grob 'duration-log))
140          (pitch (ly:event-property (event-cause grob) 'pitch))
141          (pitch-index (ly:pitch-notename pitch))
142          (note-names (ly:grob-property grob 'note-names))
143          (pitch-string (if (and (vector? note-names)
144                                 (> (vector-length note-names) pitch-index))
145                            (vector-ref note-names pitch-index)
146                            (string
147                             (integer->char
148                              (+ (modulo (+ pitch-index 2) 7)
149                                 (char->integer #\A))))))
150          (staff-space (ly:staff-symbol-staff-space grob))
151          (line-thickness (ly:staff-symbol-line-thickness grob))
152          (stem (ly:grob-object grob 'stem))
153          (stem-thickness (* (if (ly:grob? stem)
154                                 (ly:grob-property stem 'thickness)
155                                 1.3)
156                             line-thickness))
157          (radius (/ (+ staff-space line-thickness) 2))
158          (letter (markup #:center-align #:vcenter pitch-string))
159          (filled-circle (markup #:draw-circle radius 0 #t)))
160
161     (ly:stencil-translate-axis
162      (grob-interpret-markup
163       grob
164       (if (>= log 2)
165           (make-combine-markup
166            filled-circle
167            (make-with-color-markup white letter))
168           (make-combine-markup
169            (make-combine-markup
170             filled-circle
171             (make-with-color-markup white (make-draw-circle-markup
172                                            (- radius stem-thickness) 0 #t)))
173            letter)))
174      radius X)))
175
176
177 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178 ;; break visibility
179
180 (define-public all-visible             #(#t #t #t))
181 (define-public begin-of-line-invisible #(#t #t #f))
182 (define-public center-invisible        #(#t #f #t))
183 (define-public end-of-line-invisible   #(#f #t #t))
184 (define-public begin-of-line-visible   #(#f #f #t))
185 (define-public center-visible          #(#f #t #f))
186 (define-public end-of-line-visible     #(#t #f #f))
187 (define-public all-invisible           #(#f #f #f))
188
189 (define-public spanbar-begin-of-line-invisible #(#t #f #f))
190
191
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 ;; Bar lines.
194
195 ;;
196 ;; How should a  bar line behave at a break?
197 (define bar-glyph-alist
198   '((":|:" . (":|" . "|:"))
199     (":|.|:" . (":|" . "|:"))
200     (":|.:" . (":|" . "|:"))
201     ("||:" . ("||" . "|:"))
202     ("dashed" . ("dashed" . '()))
203     ("|" . ("|" . ()))
204     ("||:" . ("||" . "|:"))
205     ("|s" . (() . "|"))
206     ("|:" . ("|" . "|:"))
207     ("|." . ("|." . ()))
208
209     ;; hmm... should we end with a bar line here?
210     (".|" . ("|" . ".|"))
211     (":|" . (":|" . ()))
212     ("||" . ("||" . ()))
213     (".|." . (".|." . ()))
214     ("|.|" . ("|.|" . ()))
215     ("" . ("" . ""))
216     (":" . (":" . ""))
217     ("." . ("." . ()))
218     ("'" . ("'" . ()))
219     ("empty" . (() . ()))
220     ("brace" . (() . "brace"))
221     ("bracket" . (() . "bracket"))
222
223     ;; segno bar lines
224     ("S" . ("||" . "S"))
225     ("|S" . ("|" . "S"))
226     ("S|" . ("S" . ()))
227     (":|S" . (":|" . "S"))
228     (":|S." . (":|S" . ()))
229     ("S|:" . ("S" . "|:"))
230     (".S|:" . ("|" . "S|:"))
231     (":|S|:" . (":|" . "S|:"))
232     (":|S.|:" . (":|S" . "|:"))))
233
234 (define-public (bar-line::calc-glyph-name grob)
235   (let* ((glyph (ly:grob-property grob 'glyph))
236          (dir (ly:item-break-dir grob))
237          (result (assoc-get glyph bar-glyph-alist))
238          (glyph-name (if (= dir CENTER)
239                          glyph
240                          (if (and result
241                                   (string? (index-cell result dir)))
242                              (index-cell result dir)
243                              #f))))
244     glyph-name))
245
246 (define-public (bar-line::calc-break-visibility grob)
247   (let* ((glyph (ly:grob-property grob 'glyph))
248          (result (assoc-get glyph bar-glyph-alist)))
249
250     (if result
251         (vector (string? (car result)) #t (string? (cdr result)))
252         all-invisible)))
253
254 (define-public (shift-right-at-line-begin g)
255   "Shift an item to the right, but only at the start of the line."
256   (if (and (ly:item? g)
257            (equal? (ly:item-break-dir g) RIGHT))
258       (ly:grob-translate-axis! g 3.5 X)))
259
260
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
262 ;; Tuplets
263
264 (define-public (tuplet-number::calc-denominator-text grob)
265   (number->string (ly:event-property (event-cause grob) 'denominator)))
266
267 (define-public (tuplet-number::calc-fraction-text grob)
268   (let ((ev (event-cause grob)))
269
270     (format "~a:~a"
271             (ly:event-property ev 'denominator)
272             (ly:event-property ev 'numerator))))
273
274 ;; a formatter function, which is simply a wrapper around an existing
275 ;; tuplet formatter function. It takes the value returned by the given
276 ;; function and appends a note of given length.
277 (define-public ((tuplet-number::append-note-wrapper function note) grob)
278   (let ((txt (if function (function grob) #f)))
279
280     (if txt
281         (markup txt #:fontsize -5 #:note note UP)
282         (markup #:fontsize -5 #:note note UP))))
283
284 ;; Print a tuplet denominator with a different number than the one derived from
285 ;; the actual tuplet fraction
286 (define-public ((tuplet-number::non-default-tuplet-denominator-text denominator)
287                 grob)
288   (number->string (if denominator
289                       denominator
290                       (ly:event-property (event-cause grob) 'denominator))))
291
292 ;; Print a tuplet fraction with different numbers than the ones derived from
293 ;; the actual tuplet fraction
294 (define-public ((tuplet-number::non-default-tuplet-fraction-text
295                  denominator numerator) grob)
296   (let* ((ev (event-cause grob))
297          (den (if denominator denominator (ly:event-property ev 'denominator)))
298          (num (if numerator numerator (ly:event-property ev 'numerator))))
299
300     (format "~a:~a" den num)))
301
302 ;; Print a tuplet fraction with note durations appended to the numerator and the
303 ;; denominator
304 (define-public ((tuplet-number::fraction-with-notes
305                  denominatornote numeratornote) grob)
306   (let* ((ev (event-cause grob))
307          (denominator (ly:event-property ev 'denominator))
308          (numerator (ly:event-property ev 'numerator)))
309
310     ((tuplet-number::non-default-fraction-with-notes
311       denominator denominatornote numerator numeratornote) grob)))
312
313 ;; Print a tuplet fraction with note durations appended to the numerator and the
314 ;; denominator
315 (define-public ((tuplet-number::non-default-fraction-with-notes
316                  denominator denominatornote numerator numeratornote) grob)
317   (let* ((ev (event-cause grob))
318          (den (if denominator denominator (ly:event-property ev 'denominator)))
319          (num (if numerator numerator (ly:event-property ev 'numerator))))
320
321     (make-concat-markup (list
322                          (make-simple-markup (format "~a" den))
323                          (markup #:fontsize -5 #:note denominatornote UP)
324                          (make-simple-markup " : ")
325                          (make-simple-markup (format "~a" num))
326                          (markup #:fontsize -5 #:note numeratornote UP)))))
327
328
329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
330 ;; Color
331
332 (define-public (color? x)
333   (and (list? x)
334        (= 3 (length x))
335        (apply eq? #t (map number? x))
336        (apply eq? #t (map (lambda (y) (<= 0 y 1)) x))))
337
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
360 ;; key signature
361
362 (define-public (key-signature-interface::alteration-position step alter
363                                                              c0-position)
364   ;; TODO: memoize - this is mostly constant.
365
366   ;; fes, ges, as and bes typeset in lower octave
367   (define FLAT_TOP_PITCH 2)
368
369   ;; ais and bis typeset in lower octave
370   (define SHARP_TOP_PITCH 4)
371
372   (if (pair? step)
373       (+ (cdr step) (* (car step) 7) c0-position)
374       (let* ((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)
380                   (or (> p FLAT_TOP_PITCH) (> (+ p c0) 4)) (> (+ p c0) 1))
381              (and (> alter 0)
382                   (or (> p SHARP_TOP_PITCH) (> (+ p c0) 5)) (> (+ p c0) 2)))
383
384          ;; Typeset below c_position
385          (set! p (- p 7)))
386
387         ;; Provide for the four cases in which there's a glitch
388         ;; it's a hack, but probably not worth
389         ;; the effort of finding a nicer solution.
390         ;; --dl.
391         (cond
392          ((and (= c0 2) (= p 3) (> alter 0))
393           (set! p (- p 7)))
394          ((and (= c0 -3) (= p -1) (> alter 0))
395           (set! p (+ p 7)))
396          ((and (= c0 -4) (= p -1) (< alter 0))
397           (set! p (+ p 7)))
398          ((and (= c0 -2) (= p -3) (< alter 0))
399           (set! p (+ p 7))))
400
401         (+ c0 p))))
402
403
404 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
405 ;; accidentals
406
407 (define-public (accidental-interface::calc-alteration grob)
408   (ly:pitch-alteration (ly:event-property (event-cause grob) 'pitch)))
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 ;; FIXME: standard vs default, alteration-FOO vs FOO-alteration
429 (define-public alteration-default-glyph-name-alist
430   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 (define-public alteration-hufnagel-glyph-name-alist
446   '((-1/2 . "accidentals.hufnagelM1")
447     (0 . "accidentals.vaticana0")
448     (1/2 . "accidentals.mensural1")))
449
450 (define-public alteration-medicaea-glyph-name-alist
451   '((-1/2 . "accidentals.medicaeaM1")
452     (0 . "accidentals.vaticana0")
453     (1/2 . "accidentals.mensural1")))
454
455 (define-public alteration-vaticana-glyph-name-alist
456   '((-1/2 . "accidentals.vaticanaM1")
457     (0 . "accidentals.vaticana0")
458     (1/2 . "accidentals.mensural1")))
459
460 (define-public alteration-mensural-glyph-name-alist
461   '((-1/2 . "accidentals.mensuralM1")
462     (0 . "accidentals.vaticana0")
463     (1/2 . "accidentals.mensural1")))
464
465
466 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
467 ;; * Pitch Trill Heads
468 ;; * Parentheses
469
470 (define-public (parentheses-item::calc-parenthesis-stencils grob)
471   (let* ((font (ly:grob-default-font grob))
472          (lp (ly:font-get-glyph font "accidentals.leftparen"))
473          (rp (ly:font-get-glyph font "accidentals.rightparen")))
474
475     (list lp rp)))
476
477 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
478   (let* ((parent (ly:grob-parent grob Y))
479          (y-extent (ly:grob-extent parent parent Y))
480          (half-thickness 0.05) ; should it be a property?
481          (width 0.5) ; should it be a property?
482          (angularity 1.5)  ; makes angle brackets
483          (white-padding 0.1) ; should it be a property?
484          (lp (ly:stencil-aligned-to
485                  (ly:stencil-aligned-to
486                    (make-parenthesis-stencil y-extent
487                                              half-thickness
488                                              (- width)
489                                              angularity)
490                    Y CENTER)
491                  X RIGHT))
492          (lp-x-extent
493            (interval-widen (ly:stencil-extent lp X) white-padding))
494          (rp (ly:stencil-aligned-to
495                  (ly:stencil-aligned-to
496                    (make-parenthesis-stencil y-extent
497                                              half-thickness
498                                              width
499                                              angularity)
500                    Y CENTER)
501                  X LEFT))
502           (rp-x-extent
503             (interval-widen (ly:stencil-extent rp X) white-padding)))
504     (set! lp (ly:make-stencil (ly:stencil-expr lp)
505                               lp-x-extent
506                               (ly:stencil-extent lp Y)))
507     (set! rp (ly:make-stencil (ly:stencil-expr rp)
508                               rp-x-extent
509                               (ly:stencil-extent rp Y)))
510     (list (stencil-whiteout lp)
511           (stencil-whiteout rp))))
512
513 (define (parenthesize-elements grob . rest)
514   (let* ((refp (if (null? rest)
515                    grob
516                    (car rest)))
517          (elts (ly:grob-object grob 'elements))
518          (x-ext (ly:relative-group-extent elts refp X))
519          (stencils (ly:grob-property grob 'stencils))
520          (lp (car stencils))
521          (rp (cadr stencils))
522          (padding (ly:grob-property grob 'padding 0.1)))
523
524     (ly:stencil-add
525      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
526      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))))
527
528
529 (define (parentheses-item::print me)
530   (let* ((elts (ly:grob-object me 'elements))
531          (y-ref (ly:grob-common-refpoint-of-array me elts Y))
532          (x-ref (ly:grob-common-refpoint-of-array me elts X))
533          (stencil (parenthesize-elements me x-ref))
534          (elt-y-ext (ly:relative-group-extent elts y-ref Y))
535          (y-center (interval-center elt-y-ext)))
536
537     (ly:stencil-translate
538      stencil
539      (cons
540       (- (ly:grob-relative-coordinate me x-ref X))
541       (- y-center (ly:grob-relative-coordinate me y-ref Y))))))
542
543
544
545 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
546 ;;
547
548 (define-public (chain-grob-member-functions grob value . funcs)
549   (for-each
550    (lambda (func)
551      (set! value (func grob value)))
552    funcs)
553
554   value)
555
556
557 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
558 ;; falls/doits
559
560 (define-public (bend::print spanner)
561   (define (close  a b)
562     (< (abs (- a b)) 0.01))
563
564   (let* ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
565          (left-span (ly:spanner-bound spanner LEFT))
566          (dots (if (and (grob::has-interface left-span 'note-head-interface)
567                         (ly:grob? (ly:grob-object left-span 'dot)))
568                    (ly:grob-object left-span 'dot) #f))
569
570          (right-span (ly:spanner-bound spanner RIGHT))
571          (thickness (* (ly:grob-property spanner 'thickness)
572                        (ly:output-def-lookup (ly:grob-layout spanner)
573                                              'line-thickness)))
574          (padding (ly:grob-property spanner 'padding 0.5))
575          (common (ly:grob-common-refpoint right-span
576                                           (ly:grob-common-refpoint spanner
577                                                                    left-span X)
578                                           X))
579          (common-y (ly:grob-common-refpoint spanner left-span Y))
580          (minimum-length (ly:grob-property spanner 'minimum-length 0.5))
581
582          (left-x (+ padding
583                     (max
584                      (interval-end (ly:grob-robust-relative-extent
585                                     left-span common X))
586                      (if
587                       (and dots
588                            (close
589                             (ly:grob-relative-coordinate dots common-y Y)
590                             (ly:grob-relative-coordinate spanner common-y Y)))
591                       (interval-end
592                        (ly:grob-robust-relative-extent dots common X))
593                       ;; TODO: use real infinity constant.
594                       -10000))))
595          (right-x (max (- (interval-start
596                            (ly:grob-robust-relative-extent right-span common X))
597                           padding)
598                        (+ left-x minimum-length)))
599          (self-x (ly:grob-relative-coordinate spanner common X))
600          (dx (- right-x left-x))
601          (exp (list 'path thickness
602                     `(quote
603                       (rmoveto
604                        ,(- left-x self-x) 0
605
606                        rcurveto
607                        ,(/ dx 3)
608                        0
609                        ,dx ,(* 0.66 delta-y)
610                        ,dx ,delta-y)))))
611
612     (ly:make-stencil
613      exp
614      (cons (- left-x self-x) (- right-x self-x))
615      (cons (min 0 delta-y)
616            (max 0 delta-y)))))
617
618
619 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
620 ;; grace spacing
621
622 (define-public (grace-spacing::calc-shortest-duration grob)
623   (let* ((cols (ly:grob-object grob 'columns))
624          (get-difference
625           (lambda (idx)
626             (ly:moment-sub (ly:grob-property
627                             (ly:grob-array-ref cols (1+ idx)) 'when)
628                            (ly:grob-property
629                             (ly:grob-array-ref cols idx) 'when))))
630
631          (moment-min (lambda (x y)
632                        (cond
633                         ((and x y)
634                          (if (ly:moment<? x y)
635                              x
636                              y))
637                         (x x)
638                         (y y)))))
639
640     (fold moment-min #f (map get-difference
641                              (iota (1- (ly:grob-array-length cols)))))))
642
643
644 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
645 ;; fingering
646
647 (define-public (fingering::calc-text grob)
648   (let* ((event (event-cause grob))
649          (digit (ly:event-property event 'digit)))
650
651     (if (> digit 5)
652         (ly:input-message (ly:event-property event 'origin)
653                           "Warning: Fingering notation for finger number ~a"
654                           digit))
655
656     (number->string digit 10)))
657
658 (define-public (string-number::calc-text grob)
659   (let ((digit (ly:event-property (event-cause grob) 'string-number)))
660
661     (number->string digit 10)))
662
663 (define-public (stroke-finger::calc-text grob)
664   (let* ((digit (ly:event-property (event-cause grob) 'digit))
665          (text (ly:event-property (event-cause grob) 'text)))
666
667     (if (string? text)
668         text
669         (vector-ref (ly:grob-property grob 'digit-names)
670                     (1- (max (min 5 digit) 1))))))
671
672
673 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
674 ;; dynamics
675
676 (define-public (hairpin::calc-grow-direction grob)
677   (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
678       START
679       STOP))
680
681 (define-public (dynamic-text-spanner::before-line-breaking grob)
682   "Monitor left bound of @code{DynamicTextSpanner} for absolute dynamics.
683 If found, ensure @code{DynamicText} does not collide with spanner text by
684 changing @code{'attach-dir} and @code{'padding}.  Reads the
685 @code{'right-padding} property of @code{DynamicText} to fine tune space
686 between the two text elements."
687   (let ((left-bound (ly:spanner-bound grob LEFT)))
688     (if (grob::has-interface left-bound 'dynamic-text-interface)
689         (let* ((details (ly:grob-property grob 'bound-details))
690                (left-details (ly:assoc-get 'left details))
691                (my-padding (ly:assoc-get 'padding left-details))
692                (script-padding (ly:grob-property left-bound 'right-padding 0)))
693
694           (and (number? my-padding)
695                (ly:grob-set-nested-property! grob
696                                              '(bound-details left attach-dir)
697                                              RIGHT)
698                (ly:grob-set-nested-property! grob
699                                              '(bound-details left padding)
700                                              (+ my-padding script-padding)))))))
701
702
703 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
704 ;; lyrics
705
706 (define-public (lyric-text::print grob)
707   "Allow interpretation of tildes as lyric tieing marks."
708
709   (let ((text (ly:grob-property grob 'text)))
710
711     (grob-interpret-markup grob (if (string? text)
712                                     (make-tied-lyric-markup text)
713                                     text))))
714
715 (define-public ((grob::calc-property-by-copy prop) grob)
716   (ly:event-property (event-cause grob) prop))
717
718
719 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
720 ;; fret boards
721
722 (define-public (fret-board::calc-stencil grob)
723   (grob-interpret-markup
724    grob
725    (make-fret-diagram-verbose-markup
726     (ly:grob-property grob 'dot-placement-list))))
727
728
729 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
730 ;; scripts
731
732 (define-public (script-interface::calc-x-offset grob)
733   (ly:grob-property grob 'positioning-done)
734   (let* ((shift (ly:grob-property grob 'toward-stem-shift 0.0))
735          (note-head-location
736           (ly:self-alignment-interface::centered-on-x-parent grob))
737          (note-head-grob (ly:grob-parent grob X))
738          (stem-grob (ly:grob-object note-head-grob 'stem)))
739
740     (+ note-head-location
741        ;; If the property 'toward-stem-shift is defined and the script
742        ;; has the same direction as the stem, move the script accordingly.
743        ;; Since scripts can also be over skips, we need to check whether
744        ;; the grob has a stem at all.
745        (if (ly:grob? stem-grob)
746            (let ((dir1 (ly:grob-property grob 'direction))
747                  (dir2 (ly:grob-property stem-grob 'direction)))
748              (if (equal? dir1 dir2)
749                  (let* ((common-refp (ly:grob-common-refpoint grob stem-grob X))
750                         (stem-location
751                          (ly:grob-relative-coordinate stem-grob common-refp X)))
752                    (* shift (- stem-location note-head-location)))
753                  0.0))
754            0.0))))
755
756
757 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
758 ;; instrument names
759
760 (define-public (system-start-text::print grob)
761   (let* ((left-bound (ly:spanner-bound grob LEFT))
762          (left-mom (ly:grob-property left-bound 'when))
763          (name (if (moment<=? left-mom ZERO-MOMENT)
764                    (ly:grob-property grob 'long-text)
765                    (ly:grob-property grob 'text))))
766
767     (if (and (markup? name)
768              (!= (ly:item-break-dir left-bound) CENTER))
769
770         (grob-interpret-markup grob name)
771         (ly:grob-suicide! grob))))
772
773 (define-public (system-start-text::calc-x-offset grob)
774   (let* ((left-bound (ly:spanner-bound grob LEFT))
775          (left-mom (ly:grob-property left-bound 'when))
776          (layout (ly:grob-layout grob))
777          (indent (ly:output-def-lookup layout
778                                        (if (moment<=? left-mom ZERO-MOMENT)
779                                            'indent
780                                            'short-indent)
781                                        0.0))
782          (system (ly:grob-system grob))
783          (my-extent (ly:grob-extent grob system X))
784          (elements (ly:grob-object system 'elements))
785          (common (ly:grob-common-refpoint-of-array system elements X))
786          (total-ext empty-interval)
787          (align-x (ly:grob-property grob 'self-alignment-X 0))
788          (padding (min 0 (- (interval-length my-extent) indent)))
789          (right-padding (- padding
790                            (/ (* padding (1+ align-x)) 2))))
791
792     ;; compensate for the variation in delimiter extents by
793     ;; calculating an X-offset correction based on united extents
794     ;; of all delimiters in this system
795     (let unite-delims ((l (ly:grob-array-length elements)))
796       (if (> l 0)
797           (let ((elt (ly:grob-array-ref elements (1- l))))
798
799             (if (grob::has-interface elt 'system-start-delimiter-interface)
800                 (let ((dims (ly:grob-extent elt common X)))
801                   (if (interval-sane? dims)
802                       (set! total-ext (interval-union total-ext dims)))))
803             (unite-delims (1- l)))))
804
805     (+
806      (ly:side-position-interface::x-aligned-side grob)
807      right-padding
808      (- (interval-length total-ext)))))
809
810 (define-public (system-start-text::calc-y-offset grob)
811
812   (define (live-elements-list me)
813     (let* ((elements (ly:grob-object me 'elements))
814            (elts-length (ly:grob-array-length elements))
815            (live-elements '()))
816
817       (let get-live ((len elts-length))
818         (if (> len 0)
819             (let ((elt (ly:grob-array-ref elements (1- len))))
820
821               (if (grob::is-live? elt)
822                   (set! live-elements (cons elt live-elements)))
823               (get-live (1- len)))))
824       live-elements))
825
826   (let* ((left-bound (ly:spanner-bound grob LEFT))
827          (live-elts (live-elements-list grob))
828          (system (ly:grob-system grob))
829          (extent empty-interval))
830
831     (if (and (pair? live-elts)
832              (interval-sane? (ly:grob-extent grob system Y)))
833         (let get-extent ((lst live-elts))
834           (if (pair? lst)
835               (let ((axis-group (car lst)))
836
837                 (if (and (ly:spanner? axis-group)
838                          (equal? (ly:spanner-bound axis-group LEFT)
839                                  left-bound))
840                     (set! extent (add-point extent
841                                             (ly:grob-relative-coordinate
842                                              axis-group system Y))))
843                 (get-extent (cdr lst)))))
844         ;; no live axis group(s) for this instrument name -> remove from system
845         (ly:grob-suicide! grob))
846
847     (+
848      (ly:self-alignment-interface::y-aligned-on-self grob)
849      (interval-center extent))))
850
851
852 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
853 ;; ambitus
854
855 (define-public (ambitus::print grob)
856   (let ((heads (ly:grob-object grob 'note-heads)))
857
858     (if (and (ly:grob-array? heads)
859              (= (ly:grob-array-length heads) 2))
860         (let* ((common (ly:grob-common-refpoint-of-array grob heads Y))
861                (head-down (ly:grob-array-ref heads 0))
862                (head-up (ly:grob-array-ref heads 1))
863                (gap (ly:grob-property grob 'gap 0.35))
864                (point-min (+ (interval-end (ly:grob-extent head-down common Y))
865                              gap))
866                (point-max (- (interval-start (ly:grob-extent head-up common Y))
867                              gap)))
868
869           (if (< point-min point-max)
870               (let* ((layout (ly:grob-layout grob))
871                      (line-thick (ly:output-def-lookup layout 'line-thickness))
872                      (blot (ly:output-def-lookup layout 'blot-diameter))
873                      (grob-thick (ly:grob-property grob 'thickness 2))
874                      (width (* line-thick grob-thick))
875                      (x-ext (symmetric-interval (/ width 2)))
876                      (y-ext (cons point-min point-max))
877                      (line (ly:round-filled-box x-ext y-ext blot))
878                      (y-coord (ly:grob-relative-coordinate grob common Y)))
879
880                 (ly:stencil-translate-axis line (- y-coord) Y))
881               empty-stencil))
882         (begin
883           (ly:grob-suicide! grob)
884           (list)))))