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