]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Tempo and Rehearsal marks horizontal positioning; issue 3279
[lilypond.git] / scm / output-lib.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 1998--2012 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 ;; general
22
23 (define-public (grob::has-interface grob iface)
24   (memq iface (ly:grob-interfaces grob)))
25
26 (define-public (grob::is-live? grob)
27   (pair? (ly:grob-basic-properties grob)))
28
29 (define-public (make-stencil-boxer thickness padding callback)
30   "Return function that adds a box around the grob passed as argument."
31   (lambda (grob)
32     (box-stencil (callback grob) thickness padding)))
33
34 (define-public (make-stencil-circler thickness padding callback)
35   "Return function that adds a circle around the grob passed as argument."
36   (lambda (grob)
37     (circle-stencil (callback grob) thickness padding)))
38
39 (define-public (print-circled-text-callback grob)
40   (grob-interpret-markup grob (make-circle-markup
41                                (ly:grob-property grob 'text))))
42
43 (define-public (event-cause grob)
44   (let ((cause (ly:grob-property  grob 'cause)))
45
46     (cond
47      ((ly:stream-event? cause) cause)
48      ((ly:grob? cause) (event-cause cause))
49      (else #f))))
50
51 (define-public (grob-interpret-markup grob text)
52   (let* ((layout (ly:grob-layout grob))
53          (defs (ly:output-def-lookup layout 'text-font-defaults))
54          (props (ly:grob-alist-chain grob defs)))
55
56     (ly:text-interface::interpret-markup layout props text)))
57
58 (define-public (grob::unpure-Y-extent-from-stencil pure-function)
59   "The unpure height will come from a stencil whereas the pure
60    height will come from @code{pure-function}."
61   (ly:make-unpure-pure-container ly:grob::stencil-height pure-function))
62
63 (define-public grob::unpure-horizontal-skylines-from-stencil
64   (ly:make-unpure-pure-container
65     ly:grob::horizontal-skylines-from-stencil
66     ly:grob::pure-simple-horizontal-skylines-from-extents))
67
68 (define-public grob::always-horizontal-skylines-from-stencil
69   (ly:make-unpure-pure-container
70     ly:grob::horizontal-skylines-from-stencil))
71
72 (define-public grob::unpure-vertical-skylines-from-stencil
73   (ly:make-unpure-pure-container
74     ly:grob::vertical-skylines-from-stencil
75     ly:grob::pure-simple-vertical-skylines-from-extents))
76
77 (define-public grob::always-vertical-skylines-from-stencil
78   (ly:make-unpure-pure-container
79     ly:grob::vertical-skylines-from-stencil))
80
81 (define-public grob::always-vertical-skylines-from-element-stencils
82   (ly:make-unpure-pure-container
83     ly:grob::vertical-skylines-from-element-stencils
84     ly:grob::pure-vertical-skylines-from-element-stencils))
85
86 (define-public grob::always-horizontal-skylines-from-element-stencils
87   (ly:make-unpure-pure-container
88     ly:grob::horizontal-skylines-from-element-stencils
89     ly:grob::pure-horizontal-skylines-from-element-stencils))
90
91 ;; Using this as a callback for a grob's Y-extent promises
92 ;; that the grob's stencil does not depend on line-spacing.
93 ;; We use this promise to figure the space required by Clefs
94 ;; and such at the note-spacing stage.
95
96 (define-public grob::always-Y-extent-from-stencil
97   (ly:make-unpure-pure-container ly:grob::stencil-height))
98
99 (define-public (layout-line-thickness grob)
100   "Get the line thickness of the @var{grob}'s corresponding layout."
101   (let* ((layout (ly:grob-layout grob))
102          (line-thickness (ly:output-def-lookup layout 'line-thickness)))
103
104         line-thickness))
105
106 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
107 ;; beam slope
108
109 ;; even though kievan noteheads do not have stems, their
110 ;; invisible stems help with beam placement
111 ;; this assures that invisible stems for kievan notes are aligned
112 ;; to the center of kievan noteheads. that is thus where the beams'
113 ;; x extrema will fall
114 (define-public (stem::kievan-offset-callback grob)
115   (let* ((note-heads (ly:grob-object grob 'note-heads))
116          (note-heads-grobs (if (not (null? note-heads))
117                                (ly:grob-array->list note-heads)
118                                '()))
119          (first-note-head (if (not (null? note-heads-grobs))
120                               (car note-heads-grobs)
121                               '()))
122          (note-head-w (if (not (null? first-note-head))
123                           (ly:grob-extent first-note-head first-note-head X)
124                           '(0 . 0))))
125     (interval-center note-head-w)))
126
127
128 ;; sets position of beams for Kievan notation
129 (define-public (beam::get-kievan-positions grob)
130   (let* ((stems (ly:grob-object grob 'stems))
131          (stems-grobs (if (not (null? stems))
132                           (ly:grob-array->list stems)
133                           '()))
134          (first-stem (if (not (null? stems-grobs))
135                          (car stems-grobs)
136                          '()))
137          (note-heads (if (not (null? first-stem))
138                          (ly:grob-object first-stem 'note-heads)
139                          '()))
140          (note-heads-grobs (if (not (null? note-heads))
141                                (ly:grob-array->list note-heads)
142                                '()))
143          (first-note-head (if (not (null? note-heads-grobs))
144                               (car note-heads-grobs)
145                               '()))
146          (next-stem (if (not (null? stems))
147                         (cadr stems-grobs)
148                         '()))
149          (next-note-heads (if (not (null? next-stem))
150                               (ly:grob-object next-stem 'note-heads)
151                               '()))
152          (next-note-heads-grobs (if (not (null? next-note-heads))
153                                     (ly:grob-array->list next-note-heads)
154                                     '()))
155          (next-note-head (if (not (null? next-note-heads-grobs))
156                              (car next-note-heads-grobs)
157                              '()))
158          (left-pos (ly:grob-property first-note-head 'Y-offset))
159          (right-pos (ly:grob-property next-note-head 'Y-offset))
160          (direction (ly:grob-property grob 'direction))
161          (first-nh-height (ly:grob::stencil-height first-note-head))
162          (next-nh-height (ly:grob::stencil-height next-note-head))
163          (left-height (if (= direction DOWN)
164                           (+ (car first-nh-height) 0.75)
165                           (- (cdr first-nh-height) 0.75)))
166          (right-height (if (= direction DOWN)
167                            (+ (car next-nh-height) 0.75)
168                            (- (cdr next-nh-height) 0.75))))
169     (cons (+ left-pos left-height) (+ right-pos right-height))))
170
171 (define-public (beam::get-kievan-quantized-positions grob)
172   (let* ((pos (ly:grob-property grob 'positions))
173          (stems (ly:grob-object grob 'stems))
174          (stems-grobs (if (not (null? stems))
175                           (ly:grob-array->list stems)
176                           '())))
177     (for-each
178       (lambda (g)
179         (ly:grob-set-property! g 'stem-begin-position 0)
180         (ly:grob-set-property! g 'length 0))
181       stems-grobs)
182     pos))
183
184 ;; calculates each slope of a broken beam individually
185 (define-public (beam::place-broken-parts-individually grob)
186   (ly:beam::quanting grob '(+inf.0 . -inf.0) #f))
187
188 ;; calculates the slope of a beam as a single unit,
189 ;; even if it is broken.  this assures that the beam
190 ;; will pick up where it left off after a line break
191 (define-public (beam::align-with-broken-parts grob)
192   (ly:beam::quanting grob '(+inf.0 . -inf.0) #t))
193
194 ;; uses the broken beam style from edition peters combines the
195 ;; values of place-broken-parts-individually and align-with-broken-parts above,
196 ;; favoring place-broken-parts-individually when the beam naturally has a steeper
197 ;; incline and align-with-broken-parts when the beam is flat
198 (define-public (beam::slope-like-broken-parts grob)
199   (define (slope y x)
200     (/ (- (cdr y) (car y)) (- (cdr x) (car x))))
201   (let* ((quant1 (ly:beam::quanting grob '(+inf.0 . -inf.0) #t))
202          (original (ly:grob-original grob))
203          (siblings (if (ly:grob? original)
204                        (ly:spanner-broken-into original)
205                        '())))
206     (if (null? siblings)
207         quant1
208         (let* ((quant2 (ly:beam::quanting grob '(+inf.0 . -inf.0) #f))
209                (x-span (ly:grob-property grob 'X-positions))
210                (slope1 (slope quant1 x-span))
211                (slope2 (slope quant2 x-span))
212                (quant2 (if (not (= (sign slope1) (sign slope2)))
213                            '(0 . 0)
214                            quant2))
215                (factor (/ (atan (abs slope1)) PI-OVER-TWO))
216                (base (cons-map
217                        (lambda (x)
218                          (+ (* (x quant1) (- 1 factor))
219                             (* (x quant2) factor)))
220                        (cons car cdr))))
221           (ly:beam::quanting grob base #f)))))
222
223 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
224 ;; cross-staff stuff
225
226 (define-public (script-or-side-position-cross-staff g)
227   (or
228    (ly:script-interface::calc-cross-staff g)
229    (ly:side-position-interface::calc-cross-staff g)))
230
231
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 ;; side-position stuff
234
235 (define-public (only-if-beamed g)
236   (any (lambda (x) (ly:grob? (ly:grob-object x 'beam)))
237        (ly:grob-array->list (ly:grob-object g 'side-support-elements))))
238
239 (define-public side-position-interface::y-aligned-side
240   (ly:make-unpure-pure-container
241     ly:side-position-interface::y-aligned-side
242     ly:side-position-interface::pure-y-aligned-side))
243
244 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
245 ;; self-alignment stuff
246
247 (define-public self-alignment-interface::y-aligned-on-self
248   (ly:make-unpure-pure-container
249     ly:self-alignment-interface::y-aligned-on-self
250     ly:self-alignment-interface::pure-y-aligned-on-self))
251
252 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
253 ;; staff symbol
254
255 (define staff-symbol-referencer::callback
256   (ly:make-unpure-pure-container ly:staff-symbol-referencer::callback))
257
258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
259 ;; note heads
260
261 (define-public (stem::calc-duration-log grob)
262   (ly:duration-log
263    (ly:event-property (event-cause grob) 'duration)))
264
265 (define (stem-stub::do-calculations grob)
266   (and (ly:grob-property (ly:grob-parent grob X) 'cross-staff)
267        (not (ly:grob-property (ly:grob-parent grob X) 'transparent))))
268
269 (define-public (stem-stub::pure-height grob beg end)
270   (if (stem-stub::do-calculations grob)
271       '(0 . 0)
272       '(+inf.0 . -inf.0)))
273
274 (define-public (stem-stub::width grob)
275   (if (stem-stub::do-calculations grob)
276       (grob::x-parent-width grob)
277       '(+inf.0 . -inf.0)))
278
279 (define-public (stem-stub::extra-spacing-height grob)
280   (if (stem-stub::do-calculations grob)
281       (let* ((dad (ly:grob-parent grob X))
282              (refp (ly:grob-common-refpoint grob dad Y))
283              (stem_ph (ly:grob-pure-height dad refp 0 INFINITY-INT))
284              (my_ph (ly:grob-pure-height grob refp 0 INFINITY-INT))
285              ;; only account for distance if stem is on different staff than stub
286              (dist (if (grob::has-interface refp 'hara-kiri-group-spanner-interface)
287                        0
288                        (- (car my_ph) (car stem_ph)))))
289         (if (interval-empty? (interval-intersection stem_ph my_ph)) #f (coord-translate stem_ph dist)))
290       #f))
291
292 (define-public (note-head::calc-kievan-duration-log grob)
293   (min 3
294        (ly:duration-log
295          (ly:event-property (event-cause grob) 'duration))))
296
297 (define-public (note-head::calc-duration-log grob)
298   (min 2
299        (ly:duration-log
300          (ly:event-property (event-cause grob) 'duration))))
301
302 (define-public (dots::calc-dot-count grob)
303   (ly:duration-dot-count
304    (ly:event-property (event-cause grob) 'duration)))
305
306 (define-public (dots::calc-staff-position grob)
307   (let* ((head (ly:grob-parent grob Y))
308          (log (ly:grob-property head 'duration-log)))
309
310     (cond
311      ((or (not (grob::has-interface head 'rest-interface))
312           (not (integer? log))) 0)
313      ((= log 7) 4)
314      ((> log 4) 3)
315      ((= log 0) -1)
316      ((= log 1) 1)
317      ((= log -1) 1)
318      (else 0))))
319
320 ;; Kept separate from note-head::calc-glyph-name to allow use by
321 ;; markup commands \note and \note-by-number
322 (define-public (select-head-glyph style log)
323   "Select a note head glyph string based on note head style @var{style}
324 and duration-log @var{log}."
325   (case style
326     ;; "default" style is directly handled in note-head.cc as a
327     ;; special case (HW says, mainly for performance reasons).
328     ;; Therefore, style "default" does not appear in this case
329     ;; statement.  -- jr
330     ((xcircle) "2xcircle")
331     ((harmonic) "0harmonic")
332     ((harmonic-black) "2harmonic")
333     ((harmonic-mixed) (if (<= log 1) "0harmonic"
334                           "2harmonic"))
335     ((baroque)
336      ;; Oops, I actually would not call this "baroque", but, for
337      ;; backwards compatibility to 1.4, this is supposed to take
338      ;; brevis, longa and maxima from the neo-mensural font and all
339      ;; other note heads from the default font.  -- jr
340      (if (< log 0)
341          (string-append (number->string log) "neomensural")
342          (number->string log)))
343     ((altdefault)
344      ;; Like default, but brevis is drawn with double vertical lines
345      (if (= log -1)
346          (string-append (number->string log) "double")
347          (number->string log)))
348     ((mensural)
349      (string-append (number->string log) (symbol->string style)))
350     ((petrucci)
351      (if (< log 0)
352          (string-append (number->string log) "mensural")
353          (string-append (number->string log) (symbol->string style))))
354     ((blackpetrucci)
355      (if (< log 0)
356          (string-append (number->string log) "blackmensural")
357          (string-append (number->string log) (symbol->string style))))
358     ((semipetrucci)
359      (if (< log 0)
360          (string-append (number->string log) "semimensural")
361          (string-append (number->string log) "petrucci")))
362     ((neomensural)
363      (string-append (number->string log) (symbol->string style)))
364     ((kievan)
365      (string-append (number->string log) "kievan"))
366     (else
367      (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
368          (symbol->string style)
369          (string-append (number->string (max 0 log))
370                         (symbol->string style))))))
371
372 (define-public (note-head::calc-glyph-name grob)
373   (let* ((style (ly:grob-property grob 'style))
374          (log (if (string-match "kievan*" (symbol->string style))
375                   (min 3 (ly:grob-property grob 'duration-log))
376                   (min 2 (ly:grob-property grob 'duration-log)))))
377     (select-head-glyph style log)))
378
379 (define-public (note-head::brew-ez-stencil grob)
380   (let* ((log (ly:grob-property grob 'duration-log))
381          (pitch (ly:event-property (event-cause grob) 'pitch))
382          (pitch-index (ly:pitch-notename pitch))
383          (note-names (ly:grob-property grob 'note-names))
384          (pitch-string (if (and (vector? note-names)
385                                 (> (vector-length note-names) pitch-index))
386                            (vector-ref note-names pitch-index)
387                            (string
388                             (integer->char
389                              (+ (modulo (+ pitch-index 2) 7)
390                                 (char->integer #\A))))))
391          (staff-space (ly:staff-symbol-staff-space grob))
392          (line-thickness (ly:staff-symbol-line-thickness grob))
393          (stem (ly:grob-object grob 'stem))
394          (stem-thickness (* (if (ly:grob? stem)
395                                 (ly:grob-property stem 'thickness)
396                                 1.3)
397                             line-thickness))
398          (radius (/ (+ staff-space line-thickness) 2))
399          (letter (markup #:center-align #:vcenter pitch-string))
400          (filled-circle (markup #:draw-circle radius 0 #t)))
401
402     (ly:stencil-translate-axis
403      (grob-interpret-markup
404       grob
405       (if (>= log 2)
406           (make-combine-markup
407            filled-circle
408            (make-with-color-markup white letter))
409           (make-combine-markup
410            (make-combine-markup
411             filled-circle
412             (make-with-color-markup white (make-draw-circle-markup
413                                            (- radius stem-thickness) 0 #t)))
414            letter)))
415      radius X)))
416
417 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
418 ;; clipping
419
420 (define-public (make-rhythmic-location bar-num num den)
421   (cons
422    bar-num (ly:make-moment num den)))
423
424 (define-public (rhythmic-location? a)
425   (and (pair? a)
426        (integer? (car a))
427        (ly:moment? (cdr a))))
428
429 (define-public (make-graceless-rhythmic-location loc)
430   (make-rhythmic-location
431    (car loc)
432    (ly:moment-main-numerator (rhythmic-location-measure-position loc))
433    (ly:moment-main-denominator (rhythmic-location-measure-position loc))))
434
435 (define-public rhythmic-location-measure-position cdr)
436 (define-public rhythmic-location-bar-number car)
437
438 (define-public (rhythmic-location<? a b)
439   (cond
440    ((< (car a) (car b)) #t)
441    ((> (car a) (car b)) #f)
442    (else
443     (ly:moment<? (cdr a) (cdr b)))))
444
445 (define-public (rhythmic-location<=? a b)
446   (not (rhythmic-location<? b a)))
447 (define-public (rhythmic-location>=? a b)
448   (not (rhythmic-location<? a b)))
449 (define-public (rhythmic-location>? a b)
450   (rhythmic-location<? b a))
451
452 (define-public (rhythmic-location=? a b)
453   (and (rhythmic-location<=? a b)
454        (rhythmic-location<=? b a)))
455
456 (define-public (rhythmic-location->file-string a)
457   (ly:format "~a.~a.~a"
458              (car a)
459              (ly:moment-main-numerator (cdr a))
460              (ly:moment-main-denominator (cdr a))))
461
462 (define-public (rhythmic-location->string a)
463   (ly:format "bar ~a ~a"
464              (car a)
465              (ly:moment->string (cdr a))))
466
467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
468 ;; break visibility
469
470 (define-public all-visible             #(#t #t #t))
471 (define-public begin-of-line-invisible #(#t #t #f))
472 (define-public center-invisible        #(#t #f #t))
473 (define-public end-of-line-invisible   #(#f #t #t))
474 (define-public begin-of-line-visible   #(#f #f #t))
475 (define-public center-visible          #(#f #t #f))
476 (define-public end-of-line-visible     #(#t #f #f))
477 (define-public all-invisible           #(#f #f #f))
478
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
480 ;; neighbor-interface routines
481
482
483 (define-public (shift-right-at-line-begin g)
484   "Shift an item to the right, but only at the start of the line."
485   (if (and (ly:item? g)
486            (equal? (ly:item-break-dir g) RIGHT))
487       (ly:grob-translate-axis! g 3.5 X)))
488
489 (define-public (pure-from-neighbor-interface::extra-spacing-height-at-beginning-of-line grob)
490   (if (= 1 (ly:item-break-dir grob))
491       (pure-from-neighbor-interface::extra-spacing-height grob)
492       (cons -0.1 0.1)))
493
494 (define-public (pure-from-neighbor-interface::extra-spacing-height grob)
495   (let* ((height (ly:grob-pure-height grob grob 0 INFINITY-INT))
496          (from-neighbors (interval-union
497                             height
498                             (ly:axis-group-interface::pure-height
499                               grob
500                               0
501                               INFINITY-INT))))
502     (coord-operation - from-neighbors height)))
503
504 ;; If there are neighbors, we place the height at their midpoint
505 ;; to avoid protrusion of this pure height out of the vertical
506 ;; axis group on either side.  This will minimize the impact of the
507 ;; grob on pure minimum translations.
508
509 ;; TODO - there is a double call to axis-group-interface::pure-height
510 ;; here and then in the extra-spacing-height function above. Can/should this
511 ;; be rolled into one?
512 (define-public (pure-from-neighbor-interface::pure-height grob beg end)
513   (let* ((height (ly:axis-group-interface::pure-height
514                   grob
515                   0
516                   INFINITY-INT))
517          (c (interval-center height)))
518     (if (interval-empty? height) empty-interval (cons c c))))
519
520 ;; Minimizes the impact of the height on vertical spacing while allowing
521 ;; it to appear in horizontal skylines of paper columns if necessary.
522 (define-public pure-from-neighbor-interface::height-if-pure
523   (ly:make-unpure-pure-container #f pure-from-neighbor-interface::pure-height))
524
525 (define-public (pure-from-neighbor-interface::account-for-span-bar grob)
526   (let* ((esh (pure-from-neighbor-interface::extra-spacing-height grob))
527          (hsb (ly:grob-property grob 'has-span-bar))
528          (ii (interval-intersection esh (cons -1.01 1.01))))
529     (if (pair? hsb)
530         (cons (car (if (and (car hsb)
531                        (ly:grob-property grob 'allow-span-bar))
532                        esh ii))
533               (cdr (if (cdr hsb) esh ii)))
534         ii)))
535
536 (define-public (pure-from-neighbor-interface::extra-spacing-height-including-staff grob)
537   (let ((esh (pure-from-neighbor-interface::extra-spacing-height grob))
538         (to-staff (coord-operation -
539                                    (interval-widen
540                                      '(0 . 0)
541                                      (ly:staff-symbol-staff-radius grob))
542                                    (ly:grob::stencil-height grob))))
543     (interval-union esh to-staff)))
544
545
546 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
547 ;; Tuplets
548
549 (define-public (tuplet-number::calc-direction grob)
550   (ly:tuplet-bracket::calc-direction (ly:grob-object grob 'bracket)))
551
552 (define-public (tuplet-number::calc-denominator-text grob)
553   (number->string (ly:event-property (event-cause grob) 'denominator)))
554
555 (define-public (tuplet-number::calc-fraction-text grob)
556   (let ((ev (event-cause grob)))
557
558     (format #f "~a:~a"
559             (ly:event-property ev 'denominator)
560             (ly:event-property ev 'numerator))))
561
562 ;; a formatter function, which is simply a wrapper around an existing
563 ;; tuplet formatter function. It takes the value returned by the given
564 ;; function and appends a note of given length.
565 (define-public ((tuplet-number::append-note-wrapper function note) grob)
566   (let ((txt (if function (function grob) #f)))
567
568     (if txt
569         (markup txt #:fontsize -5 #:note note UP)
570         (markup #:fontsize -5 #:note note UP))))
571
572 ;; Print a tuplet denominator with a different number than the one derived from
573 ;; the actual tuplet fraction
574 (define-public ((tuplet-number::non-default-tuplet-denominator-text denominator)
575                 grob)
576   (number->string (if denominator
577                       denominator
578                       (ly:event-property (event-cause grob) 'denominator))))
579
580 ;; Print a tuplet fraction with different numbers than the ones derived from
581 ;; the actual tuplet fraction
582 (define-public ((tuplet-number::non-default-tuplet-fraction-text
583                  denominator numerator) grob)
584   (let* ((ev (event-cause grob))
585          (den (if denominator denominator (ly:event-property ev 'denominator)))
586          (num (if numerator numerator (ly:event-property ev 'numerator))))
587
588     (format #f "~a:~a" den num)))
589
590 ;; Print a tuplet fraction with note durations appended to the numerator and the
591 ;; denominator
592 (define-public ((tuplet-number::fraction-with-notes
593                  denominatornote numeratornote) grob)
594   (let* ((ev (event-cause grob))
595          (denominator (ly:event-property ev 'denominator))
596          (numerator (ly:event-property ev 'numerator)))
597
598     ((tuplet-number::non-default-fraction-with-notes
599       denominator denominatornote numerator numeratornote) grob)))
600
601 ;; Print a tuplet fraction with note durations appended to the numerator and the
602 ;; denominator
603 (define-public ((tuplet-number::non-default-fraction-with-notes
604                  denominator denominatornote numerator numeratornote) grob)
605   (let* ((ev (event-cause grob))
606          (den (if denominator denominator (ly:event-property ev 'denominator)))
607          (num (if numerator numerator (ly:event-property ev 'numerator))))
608
609     (make-concat-markup (list
610                          (make-simple-markup (format #f "~a" den))
611                          (markup #:fontsize -5 #:note denominatornote UP)
612                          (make-simple-markup " : ")
613                          (make-simple-markup (format #f "~a" num))
614                          (markup #:fontsize -5 #:note numeratornote UP)))))
615
616
617 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
618 ;; Color
619
620 (define-public (color? x)
621   (and (list? x)
622        (= 3 (length x))
623        (apply eq? #t (map number? x))
624        (apply eq? #t (map (lambda (y) (<= 0 y 1)) x))))
625
626 (define-public (rgb-color r g b) (list r g b))
627
628 ; predefined colors
629 (define-public black       '(0.0 0.0 0.0))
630 (define-public white       '(1.0 1.0 1.0))
631 (define-public red         '(1.0 0.0 0.0))
632 (define-public green       '(0.0 1.0 0.0))
633 (define-public blue        '(0.0 0.0 1.0))
634 (define-public cyan        '(0.0 1.0 1.0))
635 (define-public magenta     '(1.0 0.0 1.0))
636 (define-public yellow      '(1.0 1.0 0.0))
637
638 (define-public grey        '(0.5 0.5 0.5))
639 (define-public darkred     '(0.5 0.0 0.0))
640 (define-public darkgreen   '(0.0 0.5 0.0))
641 (define-public darkblue    '(0.0 0.0 0.5))
642 (define-public darkcyan    '(0.0 0.5 0.5))
643 (define-public darkmagenta '(0.5 0.0 0.5))
644 (define-public darkyellow  '(0.5 0.5 0.0))
645
646
647 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
648 ;; key signature
649
650 (define-public (key-signature-interface::alteration-positions
651                 entry c0-position grob)
652   (let ((step (car entry))
653         (alter (cdr entry)))
654     (if (pair? step)
655         (list (+ (cdr step) (* (car step) 7) c0-position))
656         (let* ((c-position (modulo c0-position 7))
657                (positions
658                 (if (< alter 0)
659                     ;; See (flat|sharp)-positions in define-grob-properties.scm
660                     (ly:grob-property grob 'flat-positions '(3))
661                     (ly:grob-property grob 'sharp-positions '(3))))
662                (p (list-ref positions
663                             (if (< c-position (length positions))
664                                 c-position 0)))
665                (max-position (if (pair? p) (cdr p) p))
666                (min-position (if (pair? p) (car p) (- max-position 6)))
667                (first-position (+ (modulo (- (+ c-position step)
668                                              min-position)
669                                           7)
670                                   min-position)))
671           (define (prepend x l) (if (> x max-position)
672                                     l
673                                     (prepend (+ x 7) (cons x l))))
674           (prepend first-position '())))))
675
676 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
677 ;; annotations
678
679 (define-public (numbered-footnotes int)
680   (markup #:tiny (number->string (+ 1 int))))
681
682 (define-public (symbol-footnotes int)
683   (define (helper symbols out idx n)
684     (if (< n 1)
685         out
686         (helper symbols
687                 (string-append out (list-ref symbols idx))
688                 idx
689                 (- n 1))))
690   (markup #:tiny (helper '("*" "†" "‡" "§" "¶")
691                           ""
692                           (remainder int 5)
693                           (+ 1 (quotient int 5)))))
694
695 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
696 ;; accidentals
697
698 (define-public (accidental-interface::calc-alteration grob)
699   (ly:pitch-alteration (ly:event-property (event-cause grob) 'pitch)))
700
701 (define-public (accidental-interface::glyph-name grob)
702   (assoc-get (ly:grob-property grob 'alteration)
703              standard-alteration-glyph-name-alist))
704
705 (define-public accidental-interface::height
706   (ly:make-unpure-pure-container
707     ly:accidental-interface::height
708     ly:accidental-interface::pure-height))
709
710 (define-public cancellation-glyph-name-alist
711   '((0 . "accidentals.natural")))
712
713 (define-public standard-alteration-glyph-name-alist
714   '(
715     ;; ordered for optimal performance.
716     (0 . "accidentals.natural")
717     (-1/2 . "accidentals.flat")
718     (1/2 . "accidentals.sharp")
719
720     (1 . "accidentals.doublesharp")
721     (-1 . "accidentals.flatflat")
722
723     (3/4 . "accidentals.sharp.slashslash.stemstemstem")
724     (1/4 . "accidentals.sharp.slashslash.stem")
725     (-1/4 . "accidentals.mirroredflat")
726     (-3/4 . "accidentals.mirroredflat.flat")))
727
728 ;; FIXME: standard vs default, alteration-FOO vs FOO-alteration
729 (define-public alteration-default-glyph-name-alist
730   standard-alteration-glyph-name-alist)
731
732 (define-public makam-alteration-glyph-name-alist
733   '((1 . "accidentals.doublesharp")
734     (8/9 . "accidentals.sharp.slashslashslash.stemstem")
735     (5/9 . "accidentals.sharp.slashslashslash.stem")
736     (4/9 . "accidentals.sharp")
737     (1/9 . "accidentals.sharp.slashslash.stem")
738     (0 . "accidentals.natural")
739     (-1/9 . "accidentals.mirroredflat")
740     (-4/9 . "accidentals.flat.slash")
741     (-5/9 . "accidentals.flat")
742     (-8/9 . "accidentals.flat.slashslash")
743     (-1 . "accidentals.flatflat")))
744
745 (define-public alteration-hufnagel-glyph-name-alist
746   '((-1/2 . "accidentals.hufnagelM1")
747     (0 . "accidentals.vaticana0")
748     (1/2 . "accidentals.mensural1")))
749
750 (define-public alteration-medicaea-glyph-name-alist
751   '((-1/2 . "accidentals.medicaeaM1")
752     (0 . "accidentals.vaticana0")
753     (1/2 . "accidentals.mensural1")))
754
755 (define-public alteration-vaticana-glyph-name-alist
756   '((-1/2 . "accidentals.vaticanaM1")
757     (0 . "accidentals.vaticana0")
758     (1/2 . "accidentals.mensural1")))
759
760 (define-public alteration-mensural-glyph-name-alist
761   '((-1/2 . "accidentals.mensuralM1")
762     (0 . "accidentals.vaticana0")
763     (1/2 . "accidentals.mensural1")))
764
765 (define-public alteration-kievan-glyph-name-alist
766  '((-1/2 . "accidentals.kievanM1")
767    (1/2 . "accidentals.kievan1")))
768
769 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
770 ;; * Pitch Trill Heads
771 ;; * Parentheses
772
773 (define-public (parentheses-item::calc-parenthesis-stencils grob)
774   (let* ((font (ly:grob-default-font grob))
775          (lp (ly:font-get-glyph font "accidentals.leftparen"))
776          (rp (ly:font-get-glyph font "accidentals.rightparen")))
777
778     (list lp rp)))
779
780 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
781   (let* ((parent (ly:grob-parent grob Y))
782          (y-extent (ly:grob-extent parent parent Y))
783          (half-thickness 0.05) ; should it be a property?
784          (width 0.5) ; should it be a property?
785          (angularity 1.5)  ; makes angle brackets
786          (white-padding 0.1) ; should it be a property?
787          (lp (ly:stencil-aligned-to
788                  (ly:stencil-aligned-to
789                    (make-parenthesis-stencil y-extent
790                                              half-thickness
791                                              (- width)
792                                              angularity)
793                    Y CENTER)
794                  X RIGHT))
795          (lp-x-extent
796            (interval-widen (ly:stencil-extent lp X) white-padding))
797          (rp (ly:stencil-aligned-to
798                  (ly:stencil-aligned-to
799                    (make-parenthesis-stencil y-extent
800                                              half-thickness
801                                              width
802                                              angularity)
803                    Y CENTER)
804                  X LEFT))
805           (rp-x-extent
806             (interval-widen (ly:stencil-extent rp X) white-padding)))
807     (set! lp (ly:make-stencil (ly:stencil-expr lp)
808                               lp-x-extent
809                               (ly:stencil-extent lp Y)))
810     (set! rp (ly:make-stencil (ly:stencil-expr rp)
811                               rp-x-extent
812                               (ly:stencil-extent rp Y)))
813     (list (stencil-whiteout lp)
814           (stencil-whiteout rp))))
815
816 (define (parenthesize-elements grob . rest)
817   (let* ((refp (if (null? rest)
818                    grob
819                    (car rest)))
820          (elts (ly:grob-object grob 'elements))
821          (x-ext (ly:relative-group-extent elts refp X))
822          (stencils (ly:grob-property grob 'stencils))
823          (lp (car stencils))
824          (rp (cadr stencils))
825          (padding (ly:grob-property grob 'padding 0.1)))
826
827     (ly:stencil-add
828      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
829      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))))
830
831
832 (define-public (parentheses-item::print me)
833   (let* ((elts (ly:grob-object me 'elements))
834          (y-ref (ly:grob-common-refpoint-of-array me elts Y))
835          (x-ref (ly:grob-common-refpoint-of-array me elts X))
836          (stencil (parenthesize-elements me x-ref))
837          (elt-y-ext (ly:relative-group-extent elts y-ref Y))
838          (y-center (interval-center elt-y-ext)))
839
840     (ly:stencil-translate
841      stencil
842      (cons
843       (- (ly:grob-relative-coordinate me x-ref X))
844       (- y-center (ly:grob-relative-coordinate me y-ref Y))))))
845
846
847 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
848 ;; offset callbacks
849
850 (define-public (pure-chain-offset-callback grob start end prev-offset)
851   "Sometimes, a chained offset callback is unpure and there is
852    no way to write a pure function that estimates its behavior.
853    In this case, we use a pure equivalent that will simply pass
854    the previous calculated offset value."
855   prev-offset)
856
857 (define-public (outside-staff::pure-Y-offset grob start end)
858   "Initial vertical placement of items such as tempo and
859    rehearsal marks, for use in note-spacing."
860    (* (+ (ly:staff-symbol-staff-radius grob)
861          (ly:grob-property grob 'outside-staff-padding 0.0)
862          1.0)
863       (ly:grob-property grob 'direction CENTER)))
864
865 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
866 ;;
867
868 (define-public (chain-grob-member-functions grob value . funcs)
869   (for-each
870    (lambda (func)
871      (set! value (func grob value)))
872    funcs)
873
874   value)
875
876
877 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
878 ;; falls/doits
879
880 (define-public (bend::print spanner)
881   (define (close  a b)
882     (< (abs (- a b)) 0.01))
883
884   (let* ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
885          (left-span (ly:spanner-bound spanner LEFT))
886          (dots (if (and (grob::has-interface left-span 'note-head-interface)
887                         (ly:grob? (ly:grob-object left-span 'dot)))
888                    (ly:grob-object left-span 'dot) #f))
889
890          (right-span (ly:spanner-bound spanner RIGHT))
891          (thickness (* (ly:grob-property spanner 'thickness)
892                        (ly:output-def-lookup (ly:grob-layout spanner)
893                                              'line-thickness)))
894          (padding (ly:grob-property spanner 'padding 0.5))
895          (common (ly:grob-common-refpoint right-span
896                                           (ly:grob-common-refpoint spanner
897                                                                    left-span X)
898                                           X))
899          (common-y (ly:grob-common-refpoint spanner left-span Y))
900          (minimum-length (ly:grob-property spanner 'minimum-length 0.5))
901
902          (left-x (+ padding
903                     (max
904                      (interval-end (ly:grob-robust-relative-extent
905                                     left-span common X))
906                      (if
907                       (and dots
908                            (close
909                             (ly:grob-relative-coordinate dots common-y Y)
910                             (ly:grob-relative-coordinate spanner common-y Y)))
911                       (interval-end
912                        (ly:grob-robust-relative-extent dots common X))
913                       (- INFINITY-INT)))))
914          (right-x (max (- (interval-start
915                            (ly:grob-robust-relative-extent right-span common X))
916                           padding)
917                        (+ left-x minimum-length)))
918          (self-x (ly:grob-relative-coordinate spanner common X))
919          (dx (- right-x left-x))
920          (exp (list 'path thickness
921                     `(quote
922                       (rmoveto
923                        ,(- left-x self-x) 0
924
925                        rcurveto
926                        ,(/ dx 3)
927                        0
928                        ,dx ,(* 0.66 delta-y)
929                        ,dx ,delta-y)))))
930
931     (ly:make-stencil
932      exp
933      (cons (- left-x self-x) (- right-x self-x))
934      (cons (min 0 delta-y)
935            (max 0 delta-y)))))
936
937
938 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
939 ;; grace spacing
940
941 (define-public (grace-spacing::calc-shortest-duration grob)
942   (let* ((cols (ly:grob-object grob 'columns))
943          (get-difference
944           (lambda (idx)
945             (ly:moment-sub (ly:grob-property
946                             (ly:grob-array-ref cols (1+ idx)) 'when)
947                            (ly:grob-property
948                             (ly:grob-array-ref cols idx) 'when))))
949
950          (moment-min (lambda (x y)
951                        (cond
952                         ((and x y)
953                          (if (ly:moment<? x y)
954                              x
955                              y))
956                         (x x)
957                         (y y)))))
958
959     (fold moment-min #f (map get-difference
960                              (iota (1- (ly:grob-array-length cols)))))))
961
962
963 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
964 ;; fingering
965
966 (define-public (fingering::calc-text grob)
967   (let ((event (event-cause grob)))
968     (or (ly:event-property event 'text #f)
969         (number->string (ly:event-property event 'digit) 10))))
970
971 (define-public (string-number::calc-text grob)
972   (let ((event (event-cause grob)))
973     (or (ly:event-property event 'text #f)
974         (number->string (ly:event-property event 'string-number) 10))))
975
976 (define-public (stroke-finger::calc-text grob)
977   (let ((event (event-cause grob)))
978     (or (ly:event-property event 'text #f)
979         (vector-ref (ly:grob-property grob 'digit-names)
980                     (1- (max 1
981                              (min 5 (ly:event-property event 'digit))))))))
982
983
984 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
985 ;; dynamics
986
987 (define-public (hairpin::calc-grow-direction grob)
988   (if (ly:in-event-class? (event-cause grob) 'decrescendo-event)
989       START
990       STOP))
991
992 (define-public (dynamic-text-spanner::before-line-breaking grob)
993   "Monitor left bound of @code{DynamicTextSpanner} for absolute dynamics.
994 If found, ensure @code{DynamicText} does not collide with spanner text by
995 changing @code{'attach-dir} and @code{'padding}.  Reads the
996 @code{'right-padding} property of @code{DynamicText} to fine tune space
997 between the two text elements."
998   (let ((left-bound (ly:spanner-bound grob LEFT)))
999     (if (grob::has-interface left-bound 'dynamic-text-interface)
1000         (let* ((details (ly:grob-property grob 'bound-details))
1001                (left-details (ly:assoc-get 'left details))
1002                (my-padding (ly:assoc-get 'padding left-details))
1003                (script-padding (ly:grob-property left-bound 'right-padding 0)))
1004
1005           (and (number? my-padding)
1006                (ly:grob-set-nested-property! grob
1007                                              '(bound-details left attach-dir)
1008                                              RIGHT)
1009                (ly:grob-set-nested-property! grob
1010                                              '(bound-details left padding)
1011                                              (+ my-padding script-padding)))))))
1012
1013 (define-public ((elbowed-hairpin coords mirrored?) grob)
1014   "Create hairpin based on a list of @var{coords} in @code{(cons x y)}
1015 form.  @code{x} is the portion of the width consumed for a given line
1016 and @code{y} is the portion of the height.  For example,
1017 @code{'((0.3 . 0.7) (0.8 . 0.9) (1.0 . 1.0))} means that at the point
1018 where the hairpin has consumed 30% of its width, it must
1019 be at 70% of its height.  Once it is to 80% width, it
1020 must be at 90% height.  It finishes at
1021 100% width and 100% height.  @var{mirrored?} indicates if the hairpin
1022 is mirrored over the Y-axis or if just the upper part is drawn.
1023 Returns a function that accepts a hairpin grob as an argument
1024 and draws the stencil based on its coordinates.
1025 @lilypond[verbatim,quote]
1026 #(define simple-hairpin
1027   (elbowed-hairpin '((1.0 . 1.0)) #t))
1028
1029 \\relative c' {
1030   \\override Hairpin #'stencil = #simple-hairpin
1031   a\\p\\< a a a\\f
1032 }
1033 @end lilypond
1034 "
1035   (define (pair-to-list pair)
1036     (list (car pair) (cdr pair)))
1037   (define (normalize-coords goods x y)
1038     (map
1039       (lambda (coord)
1040         (cons (* x (car coord)) (* y (cdr coord))))
1041       goods))
1042   (define (my-c-p-s points thick decresc?)
1043     (make-connected-path-stencil
1044       points
1045       thick
1046       (if decresc? -1.0 1.0)
1047       1.0
1048       #f
1049       #f))
1050   ; outer let to trigger suicide
1051   (let ((sten (ly:hairpin::print grob)))
1052     (if (grob::is-live? grob)
1053       (let* ((decresc? (eq? (ly:grob-property grob 'grow-direction) LEFT))
1054              (thick (ly:grob-property grob 'thickness 0.1))
1055              (thick (* thick (layout-line-thickness grob)))
1056              (xex (ly:stencil-extent sten X))
1057              (lenx (interval-length xex))
1058              (yex (ly:stencil-extent sten Y))
1059              (leny (interval-length yex))
1060              (xtrans (+ (car xex) (if decresc? lenx 0)))
1061              (ytrans (car yex))
1062              (uplist (map pair-to-list
1063                           (normalize-coords coords lenx (/ leny 2))))
1064              (downlist (map pair-to-list
1065                             (normalize-coords coords lenx (/ leny -2)))))
1066       (ly:stencil-translate
1067         (ly:stencil-add
1068           (my-c-p-s uplist thick decresc?)
1069           (if mirrored? (my-c-p-s downlist thick decresc?) empty-stencil))
1070         (cons xtrans ytrans)))
1071       '())))
1072
1073 (define-public flared-hairpin
1074   (elbowed-hairpin '((0.95 . 0.4) (1.0 . 1.0)) #t))
1075
1076 (define-public constante-hairpin
1077   (elbowed-hairpin '((1.0 . 0.0) (1.0 . 1.0)) #f))
1078
1079 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1080 ;; lyrics
1081
1082 (define-public (lyric-text::print grob)
1083   "Allow interpretation of tildes as lyric tieing marks."
1084
1085   (let ((text (ly:grob-property grob 'text)))
1086
1087     (grob-interpret-markup grob (if (string? text)
1088                                     (make-tied-lyric-markup text)
1089                                     text))))
1090
1091 (define-public ((grob::calc-property-by-copy prop) grob)
1092   (ly:event-property (event-cause grob) prop))
1093
1094 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1095 ;; general inheritance
1096
1097 (define-public ((grob::inherit-parent-property axis property . default) grob)
1098   "@var{grob} callback generator for inheriting a @var{property} from
1099 an @var{axis} parent, defaulting to @var{default} if there is no
1100 parent or the parent has no setting."
1101   (let ((parent (ly:grob-parent grob axis)))
1102     (cond
1103      ((ly:grob? parent)
1104       (apply ly:grob-property parent property default))
1105      ((pair? default) (car default))
1106      (else '()))))
1107
1108 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1109 ;; fret boards
1110
1111 (define-public (fret-board::calc-stencil grob)
1112   (grob-interpret-markup
1113    grob
1114    (make-fret-diagram-verbose-markup
1115     (ly:grob-property grob 'dot-placement-list))))
1116
1117
1118 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1119 ;; slurs
1120
1121 (define-public slur::height
1122   (ly:make-unpure-pure-container
1123     ly:slur::height
1124     ly:slur::pure-height))
1125
1126 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1127 ;; scripts
1128
1129 (define-public (script-interface::calc-x-offset grob)
1130   (ly:grob-property grob 'positioning-done)
1131   (let* ((shift (ly:grob-property grob 'toward-stem-shift 0.0))
1132          (note-head-location
1133           (ly:self-alignment-interface::centered-on-x-parent grob))
1134          (note-head-grob (ly:grob-parent grob X))
1135          (stem-grob (ly:grob-object note-head-grob 'stem)))
1136
1137     (+ note-head-location
1138        ;; If the property 'toward-stem-shift is defined and the script
1139        ;; has the same direction as the stem, move the script accordingly.
1140        ;; Since scripts can also be over skips, we need to check whether
1141        ;; the grob has a stem at all.
1142        (if (ly:grob? stem-grob)
1143            (let ((dir1 (ly:grob-property grob 'direction))
1144                  (dir2 (ly:grob-property stem-grob 'direction)))
1145              (if (equal? dir1 dir2)
1146                  (let* ((common-refp (ly:grob-common-refpoint grob stem-grob X))
1147                         (stem-location
1148                          (ly:grob-relative-coordinate stem-grob common-refp X)))
1149                    (* shift (- stem-location note-head-location)))
1150                  0.0))
1151            0.0))))
1152
1153
1154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1155 ;; instrument names
1156
1157 (define-public (system-start-text::print grob)
1158   (let* ((left-bound (ly:spanner-bound grob LEFT))
1159          (left-mom (ly:grob-property left-bound 'when))
1160          (name (if (moment<=? left-mom ZERO-MOMENT)
1161                    (ly:grob-property grob 'long-text)
1162                    (ly:grob-property grob 'text))))
1163
1164     (if (and (markup? name)
1165              (!= (ly:item-break-dir left-bound) CENTER))
1166
1167         (grob-interpret-markup grob name)
1168         (ly:grob-suicide! grob))))
1169
1170 (define-public (system-start-text::calc-x-offset grob)
1171   (let* ((left-bound (ly:spanner-bound grob LEFT))
1172          (left-mom (ly:grob-property left-bound 'when))
1173          (layout (ly:grob-layout grob))
1174          (indent (ly:output-def-lookup layout
1175                                        (if (moment<=? left-mom ZERO-MOMENT)
1176                                            'indent
1177                                            'short-indent)
1178                                        0.0))
1179          (system (ly:grob-system grob))
1180          (my-extent (ly:grob-extent grob system X))
1181          (elements (ly:grob-object system 'elements))
1182          (common (ly:grob-common-refpoint-of-array system elements X))
1183          (total-ext empty-interval)
1184          (align-x (ly:grob-property grob 'self-alignment-X 0))
1185          (padding (min 0 (- (interval-length my-extent) indent)))
1186          (right-padding (- padding
1187                            (/ (* padding (1+ align-x)) 2))))
1188
1189     ;; compensate for the variation in delimiter extents by
1190     ;; calculating an X-offset correction based on united extents
1191     ;; of all delimiters in this system
1192     (let unite-delims ((l (ly:grob-array-length elements)))
1193       (if (> l 0)
1194           (let ((elt (ly:grob-array-ref elements (1- l))))
1195
1196             (if (grob::has-interface elt 'system-start-delimiter-interface)
1197                 (let ((dims (ly:grob-extent elt common X)))
1198                   (if (interval-sane? dims)
1199                       (set! total-ext (interval-union total-ext dims)))))
1200             (unite-delims (1- l)))))
1201
1202     (+
1203      (ly:side-position-interface::x-aligned-side grob)
1204      right-padding
1205      (- (interval-length total-ext)))))
1206
1207 (define-public (system-start-text::calc-y-offset grob)
1208
1209   (define (live-elements-list me)
1210     (let ((elements (ly:grob-object me 'elements)))
1211
1212       (filter! grob::is-live?
1213                (ly:grob-array->list elements))))
1214
1215   (let* ((left-bound (ly:spanner-bound grob LEFT))
1216          (live-elts (live-elements-list grob))
1217          (system (ly:grob-system grob))
1218          (extent empty-interval))
1219
1220     (if (and (pair? live-elts)
1221              (interval-sane? (ly:grob-extent grob system Y)))
1222         (let get-extent ((lst live-elts))
1223           (if (pair? lst)
1224               (let ((axis-group (car lst)))
1225
1226                 (if (and (ly:spanner? axis-group)
1227                          (equal? (ly:spanner-bound axis-group LEFT)
1228                                  left-bound))
1229                     (set! extent (add-point extent
1230                                             (ly:grob-relative-coordinate
1231                                              axis-group system Y))))
1232                 (get-extent (cdr lst)))))
1233         ;; no live axis group(s) for this instrument name -> remove from system
1234         (ly:grob-suicide! grob))
1235
1236     (+
1237      (ly:self-alignment-interface::y-aligned-on-self grob)
1238      (interval-center extent))))
1239
1240
1241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1242 ;; axis group interface
1243
1244 (define-public axis-group-interface::height
1245   (ly:make-unpure-pure-container
1246     ly:axis-group-interface::height
1247     ly:axis-group-interface::pure-height))
1248
1249 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1250 ;; ambitus
1251
1252 (define-public (ambitus::print grob)
1253   (let ((heads (ly:grob-object grob 'note-heads)))
1254
1255     (if (and (ly:grob-array? heads)
1256              (= (ly:grob-array-length heads) 2))
1257         (let* ((common (ly:grob-common-refpoint-of-array grob heads Y))
1258                (head-down (ly:grob-array-ref heads 0))
1259                (head-up (ly:grob-array-ref heads 1))
1260                (gap (ly:grob-property grob 'gap 0.35))
1261                (point-min (+ (interval-end (ly:grob-extent head-down common Y))
1262                              gap))
1263                (point-max (- (interval-start (ly:grob-extent head-up common Y))
1264                              gap)))
1265
1266           (if (< point-min point-max)
1267               (let* ((layout (ly:grob-layout grob))
1268                      (line-thick (ly:output-def-lookup layout 'line-thickness))
1269                      (blot (ly:output-def-lookup layout 'blot-diameter))
1270                      (grob-thick (ly:grob-property grob 'thickness 2))
1271                      (width (* line-thick grob-thick))
1272                      (x-ext (symmetric-interval (/ width 2)))
1273                      (y-ext (cons point-min point-max))
1274                      (line (ly:round-filled-box x-ext y-ext blot))
1275                      (y-coord (ly:grob-relative-coordinate grob common Y)))
1276
1277                 (ly:stencil-translate-axis line (- y-coord) Y))
1278               empty-stencil))
1279         (begin
1280           (ly:grob-suicide! grob)
1281           (list)))))
1282
1283 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1284 ;;  laissez-vibrer tie
1285 ;;
1286 ;;  needed so we can make laissez-vibrer a pure print
1287 ;;
1288 (define-public (laissez-vibrer::print grob)
1289  (ly:tie::print grob))
1290
1291 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1292 ;; volta-bracket
1293
1294 (define-public (volta-bracket-interface::pure-height grob start end)
1295   (let ((edge-height (ly:grob-property grob 'edge-height)))
1296     (if (number-pair? edge-height)
1297         (let ((smaller (min (car edge-height) (cdr edge-height)))
1298               (larger (max (car edge-height) (cdr edge-height))))
1299           (interval-union '(0 . 0) (cons smaller larger)))
1300         '(0 . 0))))