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