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