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