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