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