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