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