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