]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Remove the last vestiges of next-space.
[lilypond.git] / scm / output-lib.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 1998--2010 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178 ;; break visibility
179
180 (define-public all-visible             #(#t #t #t))
181 (define-public begin-of-line-invisible #(#t #t #f))
182 (define-public center-invisible        #(#t #f #t))
183 (define-public end-of-line-invisible   #(#f #t #t))
184 (define-public begin-of-line-visible   #(#f #f #t))
185 (define-public center-visible          #(#f #t #f))
186 (define-public end-of-line-visible     #(#t #f #f))
187 (define-public all-invisible           #(#f #f #f))
188
189 (define-public spanbar-begin-of-line-invisible #(#t #f #f))
190
191
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 ;; Bar lines.
194
195 ;;
196 ;; How should a  bar line behave at a break?
197 (define bar-glyph-alist
198   '((":|:" . (":|" . "|:"))
199     (":|.|:" . (":|" . "|:"))
200     (":|.:" . (":|" . "|:"))
201     ("||:" . ("||" . "|:"))
202     ("dashed" . ("dashed" . '()))
203     ("|" . ("|" . ()))
204     ("||:" . ("||" . "|:"))
205     ("|s" . (() . "|"))
206     ("|:" . ("|" . "|:"))
207     ("|." . ("|." . ()))
208
209     ;; hmm... should we end with a bar line here?
210     (".|" . ("|" . ".|"))
211     (":|" . (":|" . ()))
212     ("||" . ("||" . ()))
213     (".|." . (".|." . ()))
214     ("|.|" . ("|.|" . ()))
215     ("" . ("" . ""))
216     (":" . (":" . ""))
217     ("." . ("." . ()))
218     ("'" . ("'" . ()))
219     ("empty" . (() . ()))
220     ("brace" . (() . "brace"))
221     ("bracket" . (() . "bracket"))))
222
223 (define-public (bar-line::calc-glyph-name grob)
224   (let* ((glyph (ly:grob-property grob 'glyph))
225          (dir (ly:item-break-dir grob))
226          (result (assoc-get glyph bar-glyph-alist))
227          (glyph-name (if (= dir CENTER)
228                          glyph
229                          (if (and result
230                                   (string? (index-cell result dir)))
231                              (index-cell result dir)
232                              #f))))
233     glyph-name))
234
235 (define-public (bar-line::calc-break-visibility grob)
236   (let* ((glyph (ly:grob-property grob 'glyph))
237          (result (assoc-get glyph bar-glyph-alist)))
238
239     (if result
240         (vector (string? (car result)) #t (string? (cdr result)))
241         all-invisible)))
242
243 (define-public (shift-right-at-line-begin g)
244   "Shift an item to the right, but only at the start of the line."
245   (if (and (ly:item? g)
246            (equal? (ly:item-break-dir g) RIGHT))
247       (ly:grob-translate-axis! g 3.5 X)))
248
249
250 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
251 ;; Tuplets
252
253 (define-public (tuplet-number::calc-denominator-text grob)
254   (number->string (ly:event-property (event-cause grob) 'denominator)))
255
256 (define-public (tuplet-number::calc-fraction-text grob)
257   (let ((ev (event-cause grob)))
258
259     (format "~a:~a"
260             (ly:event-property ev 'denominator)
261             (ly:event-property ev 'numerator))))
262
263 ;; a formatter function, which is simply a wrapper around an existing
264 ;; tuplet formatter function. It takes the value returned by the given
265 ;; function and appends a note of given length.
266 (define-public ((tuplet-number::append-note-wrapper function note) grob)
267   (let ((txt (if function (function grob) #f)))
268
269     (if txt
270         (markup txt #:fontsize -5 #:note note UP)
271         (markup #:fontsize -5 #:note note UP))))
272
273 ;; Print a tuplet denominator with a different number than the one derived from
274 ;; the actual tuplet fraction
275 (define-public ((tuplet-number::non-default-tuplet-denominator-text denominator)
276                 grob)
277   (number->string (if denominator
278                       denominator
279                       (ly:event-property (event-cause grob) 'denominator))))
280
281 ;; Print a tuplet fraction with different numbers than the ones derived from
282 ;; the actual tuplet fraction
283 (define-public ((tuplet-number::non-default-tuplet-fraction-text
284                  denominator numerator) grob)
285   (let* ((ev (event-cause grob))
286          (den (if denominator denominator (ly:event-property ev 'denominator)))
287          (num (if numerator numerator (ly:event-property ev 'numerator))))
288
289     (format "~a:~a" den num)))
290
291 ;; Print a tuplet fraction with note durations appended to the numerator and the
292 ;; denominator
293 (define-public ((tuplet-number::fraction-with-notes
294                  denominatornote numeratornote) grob)
295   (let* ((ev (event-cause grob))
296          (denominator (ly:event-property ev 'denominator))
297          (numerator (ly:event-property ev 'numerator)))
298
299     ((tuplet-number::non-default-fraction-with-notes
300       denominator denominatornote numerator numeratornote) grob)))
301
302 ;; Print a tuplet fraction with note durations appended to the numerator and the
303 ;; denominator
304 (define-public ((tuplet-number::non-default-fraction-with-notes
305                  denominator denominatornote numerator numeratornote) grob)
306   (let* ((ev (event-cause grob))
307          (den (if denominator denominator (ly:event-property ev 'denominator)))
308          (num (if numerator numerator (ly:event-property ev 'numerator))))
309
310     (make-concat-markup (list
311                          (make-simple-markup (format "~a" den))
312                          (markup #:fontsize -5 #:note denominatornote UP)
313                          (make-simple-markup " : ")
314                          (make-simple-markup (format "~a" num))
315                          (markup #:fontsize -5 #:note numeratornote UP)))))
316
317
318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
319 ;; Color
320
321 (define-public color? list?)
322 (define-public (rgb-color r g b) (list r g b))
323
324 ; predefined colors
325 (define-public black       '(0.0 0.0 0.0))
326 (define-public white       '(1.0 1.0 1.0))
327 (define-public red         '(1.0 0.0 0.0))
328 (define-public green       '(0.0 1.0 0.0))
329 (define-public blue        '(0.0 0.0 1.0))
330 (define-public cyan        '(0.0 1.0 1.0))
331 (define-public magenta     '(1.0 0.0 1.0))
332 (define-public yellow      '(1.0 1.0 0.0))
333
334 (define-public grey        '(0.5 0.5 0.5))
335 (define-public darkred     '(0.5 0.0 0.0))
336 (define-public darkgreen   '(0.0 0.5 0.0))
337 (define-public darkblue    '(0.0 0.0 0.5))
338 (define-public darkcyan    '(0.0 0.5 0.5))
339 (define-public darkmagenta '(0.5 0.0 0.5))
340 (define-public darkyellow  '(0.5 0.5 0.0))
341
342
343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
344 ;; key signature
345
346 (define-public (key-signature-interface::alteration-position step alter
347                                                              c0-position)
348   ;; TODO: memoize - this is mostly constant.
349
350   ;; fes, ges, as and bes typeset in lower octave
351   (define FLAT_TOP_PITCH 2)
352
353   ;; ais and bis typeset in lower octave
354   (define SHARP_TOP_PITCH 4)
355
356   (if (pair? step)
357       (+ (cdr step) (* (car step) 7) c0-position)
358       (let* ((from-bottom-pos (modulo (+ 4 49 c0-position) 7))
359              (p step)
360              (c0 (- from-bottom-pos 4)))
361
362         (if
363          (or (and (< alter 0)
364                   (or (> p FLAT_TOP_PITCH) (> (+ p c0) 4)) (> (+ p c0) 1))
365              (and (> alter 0)
366                   (or (> p SHARP_TOP_PITCH) (> (+ p c0) 5)) (> (+ p c0) 2)))
367
368          ;; Typeset below c_position
369          (set! p (- p 7)))
370
371         ;; Provide for the four cases in which there's a glitch
372         ;; it's a hack, but probably not worth
373         ;; the effort of finding a nicer solution.
374         ;; --dl.
375         (cond
376          ((and (= c0 2) (= p 3) (> alter 0))
377           (set! p (- p 7)))
378          ((and (= c0 -3) (= p -1) (> alter 0))
379           (set! p (+ p 7)))
380          ((and (= c0 -4) (= p -1) (< alter 0))
381           (set! p (+ p 7)))
382          ((and (= c0 -2) (= p -3) (< alter 0))
383           (set! p (+ p 7))))
384
385         (+ c0 p))))
386
387
388 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
389 ;; accidentals
390
391 (define-public (accidental-interface::calc-alteration grob)
392   (ly:pitch-alteration (ly:event-property (event-cause grob) 'pitch)))
393
394 (define-public cancellation-glyph-name-alist
395   '((0 . "accidentals.natural")))
396
397 (define-public standard-alteration-glyph-name-alist
398   '(
399     ;; ordered for optimal performance.
400     (0 . "accidentals.natural")
401     (-1/2 . "accidentals.flat")
402     (1/2 . "accidentals.sharp")
403
404     (1 . "accidentals.doublesharp")
405     (-1 . "accidentals.flatflat")
406
407     (3/4 . "accidentals.sharp.slashslash.stemstemstem")
408     (1/4 . "accidentals.sharp.slashslash.stem")
409     (-1/4 . "accidentals.mirroredflat")
410     (-3/4 . "accidentals.mirroredflat.flat")))
411
412 ;; FIXME: standard vs default, alteration-FOO vs FOO-alteration
413 (define-public alteration-default-glyph-name-alist
414   standard-alteration-glyph-name-alist)
415
416 (define-public makam-alteration-glyph-name-alist
417   '((1 . "accidentals.doublesharp")
418     (8/9 . "accidentals.sharp.slashslashslash.stemstem")
419     (5/9 . "accidentals.sharp.slashslashslash.stem")
420     (4/9 . "accidentals.sharp")
421     (1/9 . "accidentals.sharp.slashslash.stem")
422     (0 . "accidentals.natural")
423     (-1/9 . "accidentals.mirroredflat")
424     (-4/9 . "accidentals.flat.slash")
425     (-5/9 . "accidentals.flat")
426     (-8/9 . "accidentals.flat.slashslash")
427     (-1 . "accidentals.flatflat")))
428
429 (define-public alteration-hufnagel-glyph-name-alist
430   '((-1/2 . "accidentals.hufnagelM1")
431     (0 . "accidentals.vaticana0")
432     (1/2 . "accidentals.mensural1")))
433
434 (define-public alteration-medicaea-glyph-name-alist
435   '((-1/2 . "accidentals.medicaeaM1")
436     (0 . "accidentals.vaticana0")
437     (1/2 . "accidentals.mensural1")))
438
439 (define-public alteration-vaticana-glyph-name-alist
440   '((-1/2 . "accidentals.vaticanaM1")
441     (0 . "accidentals.vaticana0")
442     (1/2 . "accidentals.mensural1")))
443
444 (define-public alteration-mensural-glyph-name-alist
445   '((-1/2 . "accidentals.mensuralM1")
446     (0 . "accidentals.vaticana0")
447     (1/2 . "accidentals.mensural1")))
448
449
450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
451 ;; * Pitch Trill Heads
452 ;; * Parentheses
453
454 (define-public (parentheses-item::calc-parenthesis-stencils grob)
455   (let* ((font (ly:grob-default-font grob))
456          (lp (ly:font-get-glyph font "accidentals.leftparen"))
457          (rp (ly:font-get-glyph font "accidentals.rightparen")))
458
459     (list lp rp)))
460
461 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
462   (let* ((font (ly:grob-default-font grob))
463          (lp (ly:stencil-aligned-to (ly:stencil-aligned-to
464                                      (grob-interpret-markup
465                                       grob
466                                       (ly:wide-char->utf-8 #x2329))
467                                      Y CENTER)
468                                     X RIGHT))
469          (rp (ly:stencil-aligned-to (ly:stencil-aligned-to
470                                      (grob-interpret-markup
471                                       grob
472                                       (ly:wide-char->utf-8 #x232A))
473                                      Y CENTER)
474                                     X LEFT)))
475
476     (list (stencil-whiteout lp)
477           (stencil-whiteout rp))))
478
479 (define (parenthesize-elements grob . rest)
480   (let* ((refp (if (null? rest)
481                    grob
482                    (car rest)))
483          (elts (ly:grob-object grob 'elements))
484          (x-ext (ly:relative-group-extent elts refp X))
485          (stencils (ly:grob-property grob 'stencils))
486          (lp (car stencils))
487          (rp (cadr stencils))
488          (padding (ly:grob-property grob 'padding 0.1)))
489
490     (ly:stencil-add
491      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
492      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))))
493
494
495 (define (parentheses-item::print me)
496   (let* ((elts (ly:grob-object me 'elements))
497          (y-ref (ly:grob-common-refpoint-of-array me elts Y))
498          (x-ref (ly:grob-common-refpoint-of-array me elts X))
499          (stencil (parenthesize-elements me x-ref))
500          (elt-y-ext (ly:relative-group-extent elts y-ref Y))
501          (y-center (interval-center elt-y-ext)))
502
503     (ly:stencil-translate
504      stencil
505      (cons
506       (- (ly:grob-relative-coordinate me x-ref X))
507       (- y-center (ly:grob-relative-coordinate me y-ref Y))))))
508
509
510
511 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
512 ;;
513
514 (define-public (chain-grob-member-functions grob value . funcs)
515   (for-each
516    (lambda (func)
517      (set! value (func grob value)))
518    funcs)
519
520   value)
521
522
523 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
524 ;; falls/doits
525
526 (define-public (bend::print spanner)
527   (define (close  a b)
528     (< (abs (- a b)) 0.01))
529
530   (let* ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
531          (left-span (ly:spanner-bound spanner LEFT))
532          (dots (if (and (grob::has-interface left-span 'note-head-interface)
533                         (ly:grob? (ly:grob-object left-span 'dot)))
534                    (ly:grob-object left-span 'dot) #f))
535
536          (right-span (ly:spanner-bound spanner RIGHT))
537          (thickness (* (ly:grob-property spanner 'thickness)
538                        (ly:output-def-lookup (ly:grob-layout spanner)
539                                              'line-thickness)))
540          (padding (ly:grob-property spanner 'padding 0.5))
541          (common (ly:grob-common-refpoint right-span
542                                           (ly:grob-common-refpoint spanner
543                                                                    left-span X)
544                                           X))
545          (common-y (ly:grob-common-refpoint spanner left-span Y))
546          (minimum-length (ly:grob-property spanner 'minimum-length 0.5))
547
548          (left-x (+ padding
549                     (max
550                      (interval-end (ly:grob-robust-relative-extent
551                                     left-span common X))
552                      (if
553                       (and dots
554                            (close
555                             (ly:grob-relative-coordinate dots common-y Y)
556                             (ly:grob-relative-coordinate spanner common-y Y)))
557                       (interval-end
558                        (ly:grob-robust-relative-extent dots common X))
559                       ;; TODO: use real infinity constant.
560                       -10000))))
561          (right-x (max (- (interval-start
562                            (ly:grob-robust-relative-extent right-span common X))
563                           padding)
564                        (+ left-x minimum-length)))
565          (self-x (ly:grob-relative-coordinate spanner common X))
566          (dx (- right-x left-x))
567          (exp (list 'path thickness
568                     `(quote
569                       (rmoveto
570                        ,(- left-x self-x) 0
571
572                        rcurveto
573                        ,(/ dx 3)
574                        0
575                        ,dx ,(* 0.66 delta-y)
576                        ,dx ,delta-y)))))
577
578     (ly:make-stencil
579      exp
580      (cons (- left-x self-x) (- right-x self-x))
581      (cons (min 0 delta-y)
582            (max 0 delta-y)))))
583
584
585 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
586 ;; grace spacing
587
588 (define-public (grace-spacing::calc-shortest-duration grob)
589   (let* ((cols (ly:grob-object grob 'columns))
590          (get-difference
591           (lambda (idx)
592             (ly:moment-sub (ly:grob-property
593                             (ly:grob-array-ref cols (1+ idx)) 'when)
594                            (ly:grob-property
595                             (ly:grob-array-ref cols idx) 'when))))
596
597          (moment-min (lambda (x y)
598                        (cond
599                         ((and x y)
600                          (if (ly:moment<? x y)
601                              x
602                              y))
603                         (x x)
604                         (y y)))))
605
606     (fold moment-min #f (map get-difference
607                              (iota (1- (ly:grob-array-length cols)))))))
608
609
610 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
611 ;; fingering
612
613 (define-public (fingering::calc-text grob)
614   (let* ((event (event-cause grob))
615          (digit (ly:event-property event 'digit)))
616
617     (if (> digit 5)
618         (ly:input-message (ly:event-property event 'origin)
619                           "Warning: Fingering notation for finger number ~a"
620                           digit))
621
622     (number->string digit 10)))
623
624 (define-public (string-number::calc-text grob)
625   (let ((digit (ly:event-property (event-cause grob) 'string-number)))
626
627     (number->string digit 10)))
628
629 (define-public (stroke-finger::calc-text grob)
630   (let* ((digit (ly:event-property (event-cause grob) 'digit))
631          (text (ly:event-property (event-cause grob) 'text)))
632
633     (if (string? text)
634         text
635         (vector-ref (ly:grob-property grob 'digit-names)
636                     (1- (max (min 5 digit) 1))))))
637
638
639 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
640 ;; dynamics
641
642 (define-public (hairpin::calc-grow-direction grob)
643   (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
644       START
645       STOP))
646
647 (define-public (dynamic-text-spanner::before-line-breaking grob)
648   "Monitor left bound of @code{DynamicTextSpanner} for absolute dynamics.
649 If found, ensure @code{DynamicText} does not collide with spanner text by
650 changing @code{'attach-dir} and @code{'padding}.  Reads the
651 @code{'right-padding} property of @code{DynamicText} to fine tune space
652 between the two text elements."
653   (let ((left-bound (ly:spanner-bound grob LEFT)))
654     (if (grob::has-interface left-bound 'dynamic-text-interface)
655         (let* ((details (ly:grob-property grob 'bound-details))
656                (left-details (ly:assoc-get 'left details))
657                (my-padding (ly:assoc-get 'padding left-details))
658                (script-padding (ly:grob-property left-bound 'right-padding 0)))
659
660           (and (number? my-padding)
661                (ly:grob-set-nested-property! grob
662                                              '(bound-details left attach-dir)
663                                              RIGHT)
664                (ly:grob-set-nested-property! grob
665                                              '(bound-details left padding)
666                                              (+ my-padding script-padding)))))))
667
668
669 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
670 ;; lyrics
671
672 (define-public (lyric-text::print grob)
673   "Allow interpretation of tildes as lyric tieing marks."
674
675   (let ((text (ly:grob-property grob 'text)))
676
677     (grob-interpret-markup grob (if (string? text)
678                                     (make-tied-lyric-markup text)
679                                     text))))
680
681 (define-public ((grob::calc-property-by-copy prop) grob)
682   (ly:event-property (event-cause grob) prop))
683
684
685 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
686 ;; fret boards
687
688 (define-public (fret-board::calc-stencil grob)
689   (grob-interpret-markup
690    grob
691    (make-fret-diagram-verbose-markup
692     (ly:grob-property grob 'dot-placement-list))))
693
694
695 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
696 ;; scripts
697
698 (define-public (script-interface::calc-x-offset grob)
699   (ly:grob-property grob 'positioning-done)
700   (let* ((shift (ly:grob-property grob 'toward-stem-shift 0.0))
701          (note-head-location
702           (ly:self-alignment-interface::centered-on-x-parent grob))
703          (note-head-grob (ly:grob-parent grob X))
704          (stem-grob (ly:grob-object note-head-grob 'stem)))
705
706     (+ note-head-location
707        ;; If the property 'toward-stem-shift is defined and the script
708        ;; has the same direction as the stem, move the script accordingly.
709        ;; Since scripts can also be over skips, we need to check whether
710        ;; the grob has a stem at all.
711        (if (ly:grob? stem-grob)
712            (let ((dir1 (ly:grob-property grob 'direction))
713                  (dir2 (ly:grob-property stem-grob 'direction)))
714              (if (equal? dir1 dir2)
715                  (let* ((common-refp (ly:grob-common-refpoint grob stem-grob X))
716                         (stem-location
717                          (ly:grob-relative-coordinate stem-grob common-refp X)))
718                    (* shift (- stem-location note-head-location)))
719                  0.0))
720            0.0))))
721
722
723 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
724 ;; instrument names
725
726 (define-public (system-start-text::print grob)
727   (let* ((left-bound (ly:spanner-bound grob LEFT))
728          (left-mom (ly:grob-property left-bound 'when))
729          (name (if (moment<=? left-mom ZERO-MOMENT)
730                    (ly:grob-property grob 'long-text)
731                    (ly:grob-property grob 'text))))
732
733     (if (and (markup? name)
734              (!= (ly:item-break-dir left-bound) CENTER))
735
736         (grob-interpret-markup grob name)
737         (ly:grob-suicide! grob))))
738
739 (define-public (system-start-text::calc-x-offset grob)
740   (let* ((left-bound (ly:spanner-bound grob LEFT))
741          (left-mom (ly:grob-property left-bound 'when))
742          (layout (ly:grob-layout grob))
743          (indent (ly:output-def-lookup layout
744                                        (if (moment<=? left-mom ZERO-MOMENT)
745                                            'indent
746                                            'short-indent)
747                                        0.0))
748          (system (ly:grob-system grob))
749          (my-extent (ly:grob-extent grob system X))
750          (elements (ly:grob-object system 'elements))
751          (common (ly:grob-common-refpoint-of-array system elements X))
752          (total-ext empty-interval)
753          (align-x (ly:grob-property grob 'self-alignment-X 0))
754          (padding (min 0 (- (interval-length my-extent) indent)))
755          (right-padding (- padding
756                            (/ (* padding (1+ align-x)) 2))))
757
758     ;; compensate for the variation in delimiter extents by
759     ;; calculating an X-offset correction based on united extents
760     ;; of all delimiters in this system
761     (let unite-delims ((l (ly:grob-array-length elements)))
762       (if (> l 0)
763           (let ((elt (ly:grob-array-ref elements (1- l))))
764
765             (if (grob::has-interface elt 'system-start-delimiter-interface)
766                 (let ((dims (ly:grob-extent elt common X)))
767                   (if (interval-sane? dims)
768                       (set! total-ext (interval-union total-ext dims)))))
769             (unite-delims (1- l)))))
770
771     (+
772      (ly:side-position-interface::x-aligned-side grob)
773      right-padding
774      (- (interval-length total-ext)))))
775
776 (define-public (system-start-text::calc-y-offset grob)
777
778   (define (live-elements-list me)
779     (let* ((elements (ly:grob-object me 'elements))
780            (elts-length (ly:grob-array-length elements))
781            (live-elements '()))
782
783       (let get-live ((len elts-length))
784         (if (> len 0)
785             (let ((elt (ly:grob-array-ref elements (1- len))))
786
787               (if (grob::is-live? elt)
788                   (set! live-elements (cons elt live-elements)))
789               (get-live (1- len)))))
790       live-elements))
791
792   (let* ((left-bound (ly:spanner-bound grob LEFT))
793          (live-elts (live-elements-list grob))
794          (system (ly:grob-system grob))
795          (extent empty-interval))
796
797     (if (and (pair? live-elts)
798              (interval-sane? (ly:grob-extent grob system Y)))
799         (let get-extent ((lst live-elts))
800           (if (pair? lst)
801               (let ((axis-group (car lst)))
802
803                 (if (and (ly:spanner? axis-group)
804                          (equal? (ly:spanner-bound axis-group LEFT)
805                                  left-bound))
806                     (set! extent (add-point extent
807                                             (ly:grob-relative-coordinate
808                                              axis-group system Y))))
809                 (get-extent (cdr lst)))))
810         ;; no live axis group(s) for this instrument name -> remove from system
811         (ly:grob-suicide! grob))
812
813     (+
814      (ly:self-alignment-interface::y-aligned-on-self grob)
815      (interval-center extent))))
816
817
818 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
819 ;; ambitus
820
821 (define-public (ambitus::print grob)
822   (let ((heads (ly:grob-object grob 'note-heads)))
823
824     (if (and (ly:grob-array? heads)
825              (= (ly:grob-array-length heads) 2))
826         (let* ((common (ly:grob-common-refpoint-of-array grob heads Y))
827                (head-down (ly:grob-array-ref heads 0))
828                (head-up (ly:grob-array-ref heads 1))
829                (gap (ly:grob-property grob 'gap 0.35))
830                (point-min (+ (interval-end (ly:grob-extent head-down common Y))
831                              gap))
832                (point-max (- (interval-start (ly:grob-extent head-up common Y))
833                              gap)))
834
835           (if (< point-min point-max)
836               (let* ((layout (ly:grob-layout grob))
837                      (line-thick (ly:output-def-lookup layout 'line-thickness))
838                      (blot (ly:output-def-lookup layout 'blot-diameter))
839                      (grob-thick (ly:grob-property grob 'thickness 2))
840                      (width (* line-thick grob-thick))
841                      (x-ext (symmetric-interval (/ width 2)))
842                      (y-ext (cons point-min point-max))
843                      (line (ly:round-filled-box x-ext y-ext blot))
844                      (y-coord (ly:grob-relative-coordinate grob common Y)))
845
846                 (ly:stencil-translate-axis line (- y-coord) Y))
847               empty-stencil))
848         (begin
849           (ly:grob-suicide! grob)
850           (list)))))