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