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