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