]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Merge branch 'master' of git://git.savannah.gnu.org/lilypond.git
[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 (define-public (inherit-y-parent-visibility grob)
248   (let ((parent (ly:grob-parent grob X)))
249     (ly:grob-property parent 'break-visibility)))
250
251
252 (define-public spanbar-begin-of-line-invisible #(#t #f #f))
253
254
255 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
256 ;; Bar lines.
257
258 ;;
259 ;; How should a  bar line behave at a break?
260 (define bar-glyph-alist
261   '((":|:" . (":|" . "|:"))
262     (":|.|:" . (":|" . "|:"))
263     (":|.:" . (":|" . "|:"))
264     ("||:" . ("||" . "|:"))
265     ("dashed" . ("dashed" . '()))
266     ("|" . ("|" . ()))
267     ("||:" . ("||" . "|:"))
268     ("|s" . (() . "|"))
269     ("|:" . ("|" . "|:"))
270     ("|." . ("|." . ()))
271
272     ;; hmm... should we end with a bar line here?
273     (".|" . ("|" . ".|"))
274     (":|" . (":|" . ()))
275     ("||" . ("||" . ()))
276     (".|." . (".|." . ()))
277     ("|.|" . ("|.|" . ()))
278     ("" . ("" . ""))
279     (":" . (":" . ""))
280     ("." . ("." . ()))
281     ("'" . ("'" . ()))
282     ("empty" . (() . ()))
283     ("brace" . (() . "brace"))
284     ("bracket" . (() . "bracket"))
285
286     ;; segno bar lines
287     ("S" . ("||" . "S"))
288     ("|S" . ("|" . "S"))
289     ("S|" . ("S" . ()))
290     (":|S" . (":|" . "S"))
291     (":|S." . (":|S" . ()))
292     ("S|:" . ("S" . "|:"))
293     (".S|:" . ("|" . "S|:"))
294     (":|S|:" . (":|" . "S|:"))
295     (":|S.|:" . (":|S" . "|:"))))
296
297 (define-public (bar-line::calc-glyph-name grob)
298   (let* ((glyph (ly:grob-property grob 'glyph))
299          (dir (ly:item-break-dir grob))
300          (result (assoc-get glyph bar-glyph-alist))
301          (glyph-name (if (= dir CENTER)
302                          glyph
303                          (if (and result
304                                   (string? (index-cell result dir)))
305                              (index-cell result dir)
306                              #f))))
307     glyph-name))
308
309 (define-public (bar-line::calc-break-visibility grob)
310   (let* ((glyph (ly:grob-property grob 'glyph))
311          (result (assoc-get glyph bar-glyph-alist)))
312
313     (if result
314         (vector (string? (car result)) #t (string? (cdr result)))
315         all-invisible)))
316
317 (define-public (shift-right-at-line-begin g)
318   "Shift an item to the right, but only at the start of the line."
319   (if (and (ly:item? g)
320            (equal? (ly:item-break-dir g) RIGHT))
321       (ly:grob-translate-axis! g 3.5 X)))
322
323
324 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
325 ;; Tuplets
326
327 (define-public (tuplet-number::calc-direction grob)
328   (ly:tuplet-bracket::calc-direction (ly:grob-object grob 'bracket)))
329
330 (define-public (tuplet-number::calc-denominator-text grob)
331   (number->string (ly:event-property (event-cause grob) 'denominator)))
332
333 (define-public (tuplet-number::calc-fraction-text grob)
334   (let ((ev (event-cause grob)))
335
336     (format #f "~a:~a"
337             (ly:event-property ev 'denominator)
338             (ly:event-property ev 'numerator))))
339
340 ;; a formatter function, which is simply a wrapper around an existing
341 ;; tuplet formatter function. It takes the value returned by the given
342 ;; function and appends a note of given length.
343 (define-public ((tuplet-number::append-note-wrapper function note) grob)
344   (let ((txt (if function (function grob) #f)))
345
346     (if txt
347         (markup txt #:fontsize -5 #:note note UP)
348         (markup #:fontsize -5 #:note note UP))))
349
350 ;; Print a tuplet denominator with a different number than the one derived from
351 ;; the actual tuplet fraction
352 (define-public ((tuplet-number::non-default-tuplet-denominator-text denominator)
353                 grob)
354   (number->string (if denominator
355                       denominator
356                       (ly:event-property (event-cause grob) 'denominator))))
357
358 ;; Print a tuplet fraction with different numbers than the ones derived from
359 ;; the actual tuplet fraction
360 (define-public ((tuplet-number::non-default-tuplet-fraction-text
361                  denominator numerator) grob)
362   (let* ((ev (event-cause grob))
363          (den (if denominator denominator (ly:event-property ev 'denominator)))
364          (num (if numerator numerator (ly:event-property ev 'numerator))))
365
366     (format #f "~a:~a" den num)))
367
368 ;; Print a tuplet fraction with note durations appended to the numerator and the
369 ;; denominator
370 (define-public ((tuplet-number::fraction-with-notes
371                  denominatornote numeratornote) grob)
372   (let* ((ev (event-cause grob))
373          (denominator (ly:event-property ev 'denominator))
374          (numerator (ly:event-property ev 'numerator)))
375
376     ((tuplet-number::non-default-fraction-with-notes
377       denominator denominatornote numerator numeratornote) grob)))
378
379 ;; Print a tuplet fraction with note durations appended to the numerator and the
380 ;; denominator
381 (define-public ((tuplet-number::non-default-fraction-with-notes
382                  denominator denominatornote numerator numeratornote) grob)
383   (let* ((ev (event-cause grob))
384          (den (if denominator denominator (ly:event-property ev 'denominator)))
385          (num (if numerator numerator (ly:event-property ev 'numerator))))
386
387     (make-concat-markup (list
388                          (make-simple-markup (format #f "~a" den))
389                          (markup #:fontsize -5 #:note denominatornote UP)
390                          (make-simple-markup " : ")
391                          (make-simple-markup (format #f "~a" num))
392                          (markup #:fontsize -5 #:note numeratornote UP)))))
393
394
395 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
396 ;; Color
397
398 (define-public (color? x)
399   (and (list? x)
400        (= 3 (length x))
401        (apply eq? #t (map number? x))
402        (apply eq? #t (map (lambda (y) (<= 0 y 1)) x))))
403
404 (define-public (rgb-color r g b) (list r g b))
405
406 ; predefined colors
407 (define-public black       '(0.0 0.0 0.0))
408 (define-public white       '(1.0 1.0 1.0))
409 (define-public red         '(1.0 0.0 0.0))
410 (define-public green       '(0.0 1.0 0.0))
411 (define-public blue        '(0.0 0.0 1.0))
412 (define-public cyan        '(0.0 1.0 1.0))
413 (define-public magenta     '(1.0 0.0 1.0))
414 (define-public yellow      '(1.0 1.0 0.0))
415
416 (define-public grey        '(0.5 0.5 0.5))
417 (define-public darkred     '(0.5 0.0 0.0))
418 (define-public darkgreen   '(0.0 0.5 0.0))
419 (define-public darkblue    '(0.0 0.0 0.5))
420 (define-public darkcyan    '(0.0 0.5 0.5))
421 (define-public darkmagenta '(0.5 0.0 0.5))
422 (define-public darkyellow  '(0.5 0.5 0.0))
423
424
425 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
426 ;; key signature
427
428 (define-public (key-signature-interface::alteration-position step alter
429                                                              c0-position)
430   ;; TODO: memoize - this is mostly constant.
431
432   ;; fes, ges, as and bes typeset in lower octave
433   (define FLAT_TOP_PITCH 2)
434
435   ;; ais and bis typeset in lower octave
436   (define SHARP_TOP_PITCH 4)
437
438   (if (pair? step)
439       (+ (cdr step) (* (car step) 7) c0-position)
440       (let* ((from-bottom-pos (modulo (+ 4 49 c0-position) 7))
441              (p step)
442              (c0 (- from-bottom-pos 4)))
443
444         (if
445          (or (and (< alter 0)
446                   (or (> p FLAT_TOP_PITCH) (> (+ p c0) 4)) (> (+ p c0) 1))
447              (and (> alter 0)
448                   (or (> p SHARP_TOP_PITCH) (> (+ p c0) 5)) (> (+ p c0) 2)))
449
450          ;; Typeset below c_position
451          (set! p (- p 7)))
452
453         ;; Provide for the four cases in which there's a glitch
454         ;; it's a hack, but probably not worth
455         ;; the effort of finding a nicer solution.
456         ;; --dl.
457         (cond
458          ((and (= c0 2) (= p 3) (> alter 0))
459           (set! p (- p 7)))
460          ((and (= c0 -3) (= p -1) (> alter 0))
461           (set! p (+ p 7)))
462          ((and (= c0 -4) (= p -1) (< alter 0))
463           (set! p (+ p 7)))
464          ((and (= c0 -2) (= p -3) (< alter 0))
465           (set! p (+ p 7))))
466
467         (+ c0 p))))
468
469
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 ;; annotations
472
473 (define-public (numbered-footnotes int)
474   (markup #:tiny (number->string (+ 1 int))))
475
476 (define-public (symbol-footnotes int)
477   (define (helper symbols out idx n)
478     (if (< n 1)
479         out
480         (helper symbols
481                 (string-append out (list-ref symbols idx))
482                 idx
483                 (- n 1))))
484   (markup #:tiny (helper '("*" "†" "‡" "§" "¶")
485                           ""
486                           (remainder int 5)
487                           (+ 1 (quotient int 5)))))
488
489 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
490 ;; accidentals
491
492 (define-public (accidental-interface::calc-alteration grob)
493   (ly:pitch-alteration (ly:event-property (event-cause grob) 'pitch)))
494
495 (define-public cancellation-glyph-name-alist
496   '((0 . "accidentals.natural")))
497
498 (define-public standard-alteration-glyph-name-alist
499   '(
500     ;; ordered for optimal performance.
501     (0 . "accidentals.natural")
502     (-1/2 . "accidentals.flat")
503     (1/2 . "accidentals.sharp")
504
505     (1 . "accidentals.doublesharp")
506     (-1 . "accidentals.flatflat")
507
508     (3/4 . "accidentals.sharp.slashslash.stemstemstem")
509     (1/4 . "accidentals.sharp.slashslash.stem")
510     (-1/4 . "accidentals.mirroredflat")
511     (-3/4 . "accidentals.mirroredflat.flat")))
512
513 ;; FIXME: standard vs default, alteration-FOO vs FOO-alteration
514 (define-public alteration-default-glyph-name-alist
515   standard-alteration-glyph-name-alist)
516
517 (define-public makam-alteration-glyph-name-alist
518   '((1 . "accidentals.doublesharp")
519     (8/9 . "accidentals.sharp.slashslashslash.stemstem")
520     (5/9 . "accidentals.sharp.slashslashslash.stem")
521     (4/9 . "accidentals.sharp")
522     (1/9 . "accidentals.sharp.slashslash.stem")
523     (0 . "accidentals.natural")
524     (-1/9 . "accidentals.mirroredflat")
525     (-4/9 . "accidentals.flat.slash")
526     (-5/9 . "accidentals.flat")
527     (-8/9 . "accidentals.flat.slashslash")
528     (-1 . "accidentals.flatflat")))
529
530 (define-public alteration-hufnagel-glyph-name-alist
531   '((-1/2 . "accidentals.hufnagelM1")
532     (0 . "accidentals.vaticana0")
533     (1/2 . "accidentals.mensural1")))
534
535 (define-public alteration-medicaea-glyph-name-alist
536   '((-1/2 . "accidentals.medicaeaM1")
537     (0 . "accidentals.vaticana0")
538     (1/2 . "accidentals.mensural1")))
539
540 (define-public alteration-vaticana-glyph-name-alist
541   '((-1/2 . "accidentals.vaticanaM1")
542     (0 . "accidentals.vaticana0")
543     (1/2 . "accidentals.mensural1")))
544
545 (define-public alteration-mensural-glyph-name-alist
546   '((-1/2 . "accidentals.mensuralM1")
547     (0 . "accidentals.vaticana0")
548     (1/2 . "accidentals.mensural1")))
549
550
551 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
552 ;; * Pitch Trill Heads
553 ;; * Parentheses
554
555 (define-public (parentheses-item::calc-parenthesis-stencils grob)
556   (let* ((font (ly:grob-default-font grob))
557          (lp (ly:font-get-glyph font "accidentals.leftparen"))
558          (rp (ly:font-get-glyph font "accidentals.rightparen")))
559
560     (list lp rp)))
561
562 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
563   (let* ((parent (ly:grob-parent grob Y))
564          (y-extent (ly:grob-extent parent parent Y))
565          (half-thickness 0.05) ; should it be a property?
566          (width 0.5) ; should it be a property?
567          (angularity 1.5)  ; makes angle brackets
568          (white-padding 0.1) ; should it be a property?
569          (lp (ly:stencil-aligned-to
570                  (ly:stencil-aligned-to
571                    (make-parenthesis-stencil y-extent
572                                              half-thickness
573                                              (- width)
574                                              angularity)
575                    Y CENTER)
576                  X RIGHT))
577          (lp-x-extent
578            (interval-widen (ly:stencil-extent lp X) white-padding))
579          (rp (ly:stencil-aligned-to
580                  (ly:stencil-aligned-to
581                    (make-parenthesis-stencil y-extent
582                                              half-thickness
583                                              width
584                                              angularity)
585                    Y CENTER)
586                  X LEFT))
587           (rp-x-extent
588             (interval-widen (ly:stencil-extent rp X) white-padding)))
589     (set! lp (ly:make-stencil (ly:stencil-expr lp)
590                               lp-x-extent
591                               (ly:stencil-extent lp Y)))
592     (set! rp (ly:make-stencil (ly:stencil-expr rp)
593                               rp-x-extent
594                               (ly:stencil-extent rp Y)))
595     (list (stencil-whiteout lp)
596           (stencil-whiteout rp))))
597
598 (define (parenthesize-elements grob . rest)
599   (let* ((refp (if (null? rest)
600                    grob
601                    (car rest)))
602          (elts (ly:grob-object grob 'elements))
603          (x-ext (ly:relative-group-extent elts refp X))
604          (stencils (ly:grob-property grob 'stencils))
605          (lp (car stencils))
606          (rp (cadr stencils))
607          (padding (ly:grob-property grob 'padding 0.1)))
608
609     (ly:stencil-add
610      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
611      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))))
612
613
614 (define-public (parentheses-item::print me)
615   (let* ((elts (ly:grob-object me 'elements))
616          (y-ref (ly:grob-common-refpoint-of-array me elts Y))
617          (x-ref (ly:grob-common-refpoint-of-array me elts X))
618          (stencil (parenthesize-elements me x-ref))
619          (elt-y-ext (ly:relative-group-extent elts y-ref Y))
620          (y-center (interval-center elt-y-ext)))
621
622     (ly:stencil-translate
623      stencil
624      (cons
625       (- (ly:grob-relative-coordinate me x-ref X))
626       (- y-center (ly:grob-relative-coordinate me y-ref Y))))))
627
628
629
630 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
631 ;;
632
633 (define-public (chain-grob-member-functions grob value . funcs)
634   (for-each
635    (lambda (func)
636      (set! value (func grob value)))
637    funcs)
638
639   value)
640
641
642 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
643 ;; falls/doits
644
645 (define-public (bend::print spanner)
646   (define (close  a b)
647     (< (abs (- a b)) 0.01))
648
649   (let* ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
650          (left-span (ly:spanner-bound spanner LEFT))
651          (dots (if (and (grob::has-interface left-span 'note-head-interface)
652                         (ly:grob? (ly:grob-object left-span 'dot)))
653                    (ly:grob-object left-span 'dot) #f))
654
655          (right-span (ly:spanner-bound spanner RIGHT))
656          (thickness (* (ly:grob-property spanner 'thickness)
657                        (ly:output-def-lookup (ly:grob-layout spanner)
658                                              'line-thickness)))
659          (padding (ly:grob-property spanner 'padding 0.5))
660          (common (ly:grob-common-refpoint right-span
661                                           (ly:grob-common-refpoint spanner
662                                                                    left-span X)
663                                           X))
664          (common-y (ly:grob-common-refpoint spanner left-span Y))
665          (minimum-length (ly:grob-property spanner 'minimum-length 0.5))
666
667          (left-x (+ padding
668                     (max
669                      (interval-end (ly:grob-robust-relative-extent
670                                     left-span common X))
671                      (if
672                       (and dots
673                            (close
674                             (ly:grob-relative-coordinate dots common-y Y)
675                             (ly:grob-relative-coordinate spanner common-y Y)))
676                       (interval-end
677                        (ly:grob-robust-relative-extent dots common X))
678                       ;; TODO: use real infinity constant.
679                       -10000))))
680          (right-x (max (- (interval-start
681                            (ly:grob-robust-relative-extent right-span common X))
682                           padding)
683                        (+ left-x minimum-length)))
684          (self-x (ly:grob-relative-coordinate spanner common X))
685          (dx (- right-x left-x))
686          (exp (list 'path thickness
687                     `(quote
688                       (rmoveto
689                        ,(- left-x self-x) 0
690
691                        rcurveto
692                        ,(/ dx 3)
693                        0
694                        ,dx ,(* 0.66 delta-y)
695                        ,dx ,delta-y)))))
696
697     (ly:make-stencil
698      exp
699      (cons (- left-x self-x) (- right-x self-x))
700      (cons (min 0 delta-y)
701            (max 0 delta-y)))))
702
703
704 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
705 ;; grace spacing
706
707 (define-public (grace-spacing::calc-shortest-duration grob)
708   (let* ((cols (ly:grob-object grob 'columns))
709          (get-difference
710           (lambda (idx)
711             (ly:moment-sub (ly:grob-property
712                             (ly:grob-array-ref cols (1+ idx)) 'when)
713                            (ly:grob-property
714                             (ly:grob-array-ref cols idx) 'when))))
715
716          (moment-min (lambda (x y)
717                        (cond
718                         ((and x y)
719                          (if (ly:moment<? x y)
720                              x
721                              y))
722                         (x x)
723                         (y y)))))
724
725     (fold moment-min #f (map get-difference
726                              (iota (1- (ly:grob-array-length cols)))))))
727
728
729 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
730 ;; fingering
731
732 (define-public (fingering::calc-text grob)
733   (let* ((event (event-cause grob))
734          (digit (ly:event-property event 'digit)))
735
736     (if (> digit 5)
737         (ly:input-message (ly:event-property event 'origin)
738                           "Warning: Fingering notation for finger number ~a"
739                           digit))
740
741     (number->string digit 10)))
742
743 (define-public (string-number::calc-text grob)
744   (let ((digit (ly:event-property (event-cause grob) 'string-number)))
745
746     (number->string digit 10)))
747
748 (define-public (stroke-finger::calc-text grob)
749   (let* ((digit (ly:event-property (event-cause grob) 'digit))
750          (text (ly:event-property (event-cause grob) 'text)))
751
752     (if (string? text)
753         text
754         (vector-ref (ly:grob-property grob 'digit-names)
755                     (1- (max (min 5 digit) 1))))))
756
757
758 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
759 ;; dynamics
760
761 (define-public (hairpin::calc-grow-direction grob)
762   (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
763       START
764       STOP))
765
766 (define-public (dynamic-text-spanner::before-line-breaking grob)
767   "Monitor left bound of @code{DynamicTextSpanner} for absolute dynamics.
768 If found, ensure @code{DynamicText} does not collide with spanner text by
769 changing @code{'attach-dir} and @code{'padding}.  Reads the
770 @code{'right-padding} property of @code{DynamicText} to fine tune space
771 between the two text elements."
772   (let ((left-bound (ly:spanner-bound grob LEFT)))
773     (if (grob::has-interface left-bound 'dynamic-text-interface)
774         (let* ((details (ly:grob-property grob 'bound-details))
775                (left-details (ly:assoc-get 'left details))
776                (my-padding (ly:assoc-get 'padding left-details))
777                (script-padding (ly:grob-property left-bound 'right-padding 0)))
778
779           (and (number? my-padding)
780                (ly:grob-set-nested-property! grob
781                                              '(bound-details left attach-dir)
782                                              RIGHT)
783                (ly:grob-set-nested-property! grob
784                                              '(bound-details left padding)
785                                              (+ my-padding script-padding)))))))
786
787
788 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
789 ;; lyrics
790
791 (define-public (lyric-text::print grob)
792   "Allow interpretation of tildes as lyric tieing marks."
793
794   (let ((text (ly:grob-property grob 'text)))
795
796     (grob-interpret-markup grob (if (string? text)
797                                     (make-tied-lyric-markup text)
798                                     text))))
799
800 (define-public ((grob::calc-property-by-copy prop) grob)
801   (ly:event-property (event-cause grob) prop))
802
803
804 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
805 ;; fret boards
806
807 (define-public (fret-board::calc-stencil grob)
808   (grob-interpret-markup
809    grob
810    (make-fret-diagram-verbose-markup
811     (ly:grob-property grob 'dot-placement-list))))
812
813
814 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
815 ;; scripts
816
817 (define-public (script-interface::calc-x-offset grob)
818   (ly:grob-property grob 'positioning-done)
819   (let* ((shift (ly:grob-property grob 'toward-stem-shift 0.0))
820          (note-head-location
821           (ly:self-alignment-interface::centered-on-x-parent grob))
822          (note-head-grob (ly:grob-parent grob X))
823          (stem-grob (ly:grob-object note-head-grob 'stem)))
824
825     (+ note-head-location
826        ;; If the property 'toward-stem-shift is defined and the script
827        ;; has the same direction as the stem, move the script accordingly.
828        ;; Since scripts can also be over skips, we need to check whether
829        ;; the grob has a stem at all.
830        (if (ly:grob? stem-grob)
831            (let ((dir1 (ly:grob-property grob 'direction))
832                  (dir2 (ly:grob-property stem-grob 'direction)))
833              (if (equal? dir1 dir2)
834                  (let* ((common-refp (ly:grob-common-refpoint grob stem-grob X))
835                         (stem-location
836                          (ly:grob-relative-coordinate stem-grob common-refp X)))
837                    (* shift (- stem-location note-head-location)))
838                  0.0))
839            0.0))))
840
841
842 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
843 ;; instrument names
844
845 (define-public (system-start-text::print grob)
846   (let* ((left-bound (ly:spanner-bound grob LEFT))
847          (left-mom (ly:grob-property left-bound 'when))
848          (name (if (moment<=? left-mom ZERO-MOMENT)
849                    (ly:grob-property grob 'long-text)
850                    (ly:grob-property grob 'text))))
851
852     (if (and (markup? name)
853              (!= (ly:item-break-dir left-bound) CENTER))
854
855         (grob-interpret-markup grob name)
856         (ly:grob-suicide! grob))))
857
858 (define-public (system-start-text::calc-x-offset grob)
859   (let* ((left-bound (ly:spanner-bound grob LEFT))
860          (left-mom (ly:grob-property left-bound 'when))
861          (layout (ly:grob-layout grob))
862          (indent (ly:output-def-lookup layout
863                                        (if (moment<=? left-mom ZERO-MOMENT)
864                                            'indent
865                                            'short-indent)
866                                        0.0))
867          (system (ly:grob-system grob))
868          (my-extent (ly:grob-extent grob system X))
869          (elements (ly:grob-object system 'elements))
870          (common (ly:grob-common-refpoint-of-array system elements X))
871          (total-ext empty-interval)
872          (align-x (ly:grob-property grob 'self-alignment-X 0))
873          (padding (min 0 (- (interval-length my-extent) indent)))
874          (right-padding (- padding
875                            (/ (* padding (1+ align-x)) 2))))
876
877     ;; compensate for the variation in delimiter extents by
878     ;; calculating an X-offset correction based on united extents
879     ;; of all delimiters in this system
880     (let unite-delims ((l (ly:grob-array-length elements)))
881       (if (> l 0)
882           (let ((elt (ly:grob-array-ref elements (1- l))))
883
884             (if (grob::has-interface elt 'system-start-delimiter-interface)
885                 (let ((dims (ly:grob-extent elt common X)))
886                   (if (interval-sane? dims)
887                       (set! total-ext (interval-union total-ext dims)))))
888             (unite-delims (1- l)))))
889
890     (+
891      (ly:side-position-interface::x-aligned-side grob)
892      right-padding
893      (- (interval-length total-ext)))))
894
895 (define-public (system-start-text::calc-y-offset grob)
896
897   (define (live-elements-list me)
898     (let ((elements (ly:grob-object me 'elements)))
899
900       (filter! grob::is-live?
901                (ly:grob-array->list elements))))
902
903   (let* ((left-bound (ly:spanner-bound grob LEFT))
904          (live-elts (live-elements-list grob))
905          (system (ly:grob-system grob))
906          (extent empty-interval))
907
908     (if (and (pair? live-elts)
909              (interval-sane? (ly:grob-extent grob system Y)))
910         (let get-extent ((lst live-elts))
911           (if (pair? lst)
912               (let ((axis-group (car lst)))
913
914                 (if (and (ly:spanner? axis-group)
915                          (equal? (ly:spanner-bound axis-group LEFT)
916                                  left-bound))
917                     (set! extent (add-point extent
918                                             (ly:grob-relative-coordinate
919                                              axis-group system Y))))
920                 (get-extent (cdr lst)))))
921         ;; no live axis group(s) for this instrument name -> remove from system
922         (ly:grob-suicide! grob))
923
924     (+
925      (ly:self-alignment-interface::y-aligned-on-self grob)
926      (interval-center extent))))
927
928
929 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
930 ;; ambitus
931
932 (define-public (ambitus::print grob)
933   (let ((heads (ly:grob-object grob 'note-heads)))
934
935     (if (and (ly:grob-array? heads)
936              (= (ly:grob-array-length heads) 2))
937         (let* ((common (ly:grob-common-refpoint-of-array grob heads Y))
938                (head-down (ly:grob-array-ref heads 0))
939                (head-up (ly:grob-array-ref heads 1))
940                (gap (ly:grob-property grob 'gap 0.35))
941                (point-min (+ (interval-end (ly:grob-extent head-down common Y))
942                              gap))
943                (point-max (- (interval-start (ly:grob-extent head-up common Y))
944                              gap)))
945
946           (if (< point-min point-max)
947               (let* ((layout (ly:grob-layout grob))
948                      (line-thick (ly:output-def-lookup layout 'line-thickness))
949                      (blot (ly:output-def-lookup layout 'blot-diameter))
950                      (grob-thick (ly:grob-property grob 'thickness 2))
951                      (width (* line-thick grob-thick))
952                      (x-ext (symmetric-interval (/ width 2)))
953                      (y-ext (cons point-min point-max))
954                      (line (ly:round-filled-box x-ext y-ext blot))
955                      (y-coord (ly:grob-relative-coordinate grob common Y)))
956
957                 (ly:stencil-translate-axis line (- y-coord) Y))
958               empty-stencil))
959         (begin
960           (ly:grob-suicide! grob)
961           (list)))))
962
963 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
964 ;;  laissez-vibrer tie
965 ;;
966 ;;  needed so we can make laissez-vibrer a pure print
967 ;;
968 (define-public (laissez-vibrer::print grob)
969  (ly:tie::print grob))
970