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