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