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