]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Merge branch 'master' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / scm / output-lib.scm
1 ;;;; output-lib.scm -- implement Scheme output helper functions
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;;
5 ;;;; (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10 ;; general
11
12 (define-public (grob::has-interface grob iface)
13   (memq iface (ly:grob-interfaces grob)))
14
15 (define-public (grob::is-live? grob)
16   (pair? (ly:grob-basic-properties grob)))
17
18 (define-public (make-stencil-boxer thickness padding callback)
19
20   "Return function that adds a box around the grob passed as argument."
21   (lambda (grob)
22     (box-stencil (callback grob) thickness padding)))
23
24 (define-public (make-stencil-circler thickness padding callback)
25   "Return function that adds a circle around the grob passed as argument."
26
27   (lambda (grob) (circle-stencil (callback grob) thickness padding)))
28
29 (define-public (print-circled-text-callback grob)
30   (grob-interpret-markup grob (make-circle-markup
31                                (ly:grob-property grob 'text))))
32
33 (define-public (event-cause grob)
34   (let ((cause (ly:grob-property  grob 'cause)))
35
36     (cond
37      ((ly:stream-event? cause) cause)
38      ((ly:grob? cause) (event-cause cause))
39      (else #f))))
40
41 (define-public (grob-interpret-markup grob text)
42   (let* ((layout (ly:grob-layout grob))
43          (defs (ly:output-def-lookup layout 'text-font-defaults))
44          (props (ly:grob-alist-chain grob defs)))
45
46     (ly:text-interface::interpret-markup layout props text)))
47
48
49 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50 ;; tablature
51
52 ;; The TabNoteHead tablatureFormat callback.
53 ;; Compute the text grob-property
54 (define-public (fret-number-tablature-format string context event)
55   (let* ((tuning (ly:context-property context 'stringTunings))
56          (pitch (ly:event-property event 'pitch))
57          (is-harmonic (apply
58                        functional-or
59                        (map
60                         (lambda (ev)
61                           (eq? 'harmonic-event (ly:event-property ev 'class)))
62                         (ly:event-property event 'articulations)))))
63
64     (make-whiteout-markup
65      (make-vcenter-markup
66       (format
67        "~a"
68        (- (ly:pitch-semitones pitch)
69           (list-ref tuning
70                     ;; remove 1 because list index starts at 0
71                     ;;and guitar string at 1.
72                     (1- string))))))))
73
74 ;; The 5-string banjo has got a extra string, the fifth (duh), which
75 ;; starts at the fifth fret on the neck.  Frets on the fifth string
76 ;; are referred to relative to the other frets:
77 ;;   the "first fret" on the fifth string is really the sixth fret
78 ;;   on the banjo neck.
79 ;; We solve this by defining a new fret-number-tablature function:
80 (define-public (fret-number-tablature-format-banjo string context event)
81   (let* ((tuning (ly:context-property context 'stringTunings))
82          (pitch (ly:event-property event 'pitch)))
83
84     (make-whiteout-markup
85      (make-vcenter-markup
86       (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (1- string)))))
87         (number->string (cond
88                          ((and (> fret 0) (= string 5))
89                           (+ fret 5))
90                          (else fret))))))))
91
92
93 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 ;; note heads
95
96 (define-public (stem::calc-duration-log grob)
97   (ly:duration-log
98    (ly:event-property (event-cause grob) 'duration)))
99
100 (define-public (note-head::calc-duration-log grob)
101   (min 2
102        (ly:duration-log
103         (ly:event-property (event-cause grob) 'duration))))
104
105 (define-public (dots::calc-dot-count grob)
106   (ly:duration-dot-count
107    (ly:event-property (event-cause grob) 'duration)))
108
109 (define-public (dots::calc-staff-position grob)
110   (let* ((head (ly:grob-parent grob Y))
111          (log (ly:grob-property head 'duration-log)))
112
113     (cond
114      ((or (not (grob::has-interface head 'rest-interface))
115           (not (integer? log))) 0)
116      ((= log 7) 4)
117      ((> log 4) 3)
118      ((= log 0) -1)
119      ((= log 1) 1)
120      ((= log -1) 1)
121      (else 0))))
122
123 ;; silly, use alist?
124 (define-public (note-head::calc-glyph-name grob)
125   (let ((style (ly:grob-property grob 'style))
126         (log (min 2 (ly:grob-property grob 'duration-log))))
127
128     (case style
129       ;; "default" style is directly handled in note-head.cc as a
130       ;; special case (HW says, mainly for performance reasons).
131       ;; Therefore, style "default" does not appear in this case
132       ;; statement.  -- jr
133       ((xcircle) "2xcircle")
134       ((harmonic) "0harmonic")
135       ((harmonic-black) "2harmonic")
136       ((harmonic-mixed) (if (<= log 1) "0harmonic"
137                             "2harmonic"))
138       ((baroque)
139        ;; Oops, I actually would not call this "baroque", but, for
140        ;; backwards compatibility to 1.4, this is supposed to take
141        ;; brevis, longa and maxima from the neo-mensural font and all
142        ;; other note heads from the default font.  -- jr
143        (if (< log 0)
144            (string-append (number->string log) "neomensural")
145            (number->string log)))
146       ((mensural)
147        (string-append (number->string log) (symbol->string style)))
148       ((petrucci)
149        (if (< log 0)
150            (string-append (number->string log) "mensural")
151            (string-append (number->string log) (symbol->string style))))
152       ((neomensural)
153        (string-append (number->string log) (symbol->string style)))
154       (else
155        (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
156            (symbol->string style)
157            (string-append (number->string (max 0 log))
158                           (symbol->string style)))))))
159
160
161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 ;; bar numbers
163
164 (define-public ((every-nth-bar-number-visible n) barnum)
165   (= 0 (modulo barnum n)))
166
167 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
168
169 (define-public ((set-bar-number-visibility n) tr)
170   (let ((bn (ly:context-property tr 'currentBarNumber)))
171     (ly:context-set-property! tr 'barNumberVisibility
172                               (modulo-bar-number-visible n (modulo bn n)))))
173
174 (define-public (first-bar-number-invisible barnum) (> barnum 1))
175
176 (define-public (all-bar-numbers-visible barnum) #t)
177
178
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 ;; percent repeat counters
181
182 (define-public ((every-nth-repeat-count-visible n) count context)
183   (= 0 (modulo count n)))
184
185 (define-public (all-repeat-counts-visible count context) #t)
186
187
188 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
189 ;; break visibility
190
191 (define-public all-visible             #(#t #t #t))
192 (define-public begin-of-line-invisible #(#t #t #f))
193 (define-public center-invisible        #(#t #f #t))
194 (define-public end-of-line-invisible   #(#f #t #t))
195 (define-public begin-of-line-visible   #(#f #f #t))
196 (define-public center-visible          #(#f #t #f))
197 (define-public end-of-line-visible     #(#t #f #f))
198 (define-public all-invisible           #(#f #f #f))
199
200 (define-public spanbar-begin-of-line-invisible #(#t #f #f))
201
202
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204 ;; Bar lines.
205
206 ;;
207 ;; How should a  bar line behave at a break?
208 (define bar-glyph-alist
209   '((":|:" . (":|" . "|:"))
210     (":|.|:" . (":|" . "|:"))
211     (":|.:" . (":|" . "|:"))
212     ("||:" . ("||" . "|:"))
213     ("dashed" . ("dashed" . '()))
214     ("|" . ("|" . ()))
215     ("||:" . ("||" . "|:"))
216     ("|s" . (() . "|"))
217     ("|:" . ("|" . "|:"))
218     ("|." . ("|." . ()))
219
220     ;; hmm... should we end with a bar line here?
221     (".|" . ("|" . ".|"))
222     (":|" . (":|" . ()))
223     ("||" . ("||" . ()))
224     (".|." . (".|." . ()))
225     ("|.|" . ("|.|" . ()))
226     ("" . ("" . ""))
227     (":" . (":" . ""))
228     ("." . ("." . ()))
229     ("'" . ("'" . ()))
230     ("empty" . (() . ()))
231     ("brace" . (() . "brace"))
232     ("bracket" . (() . "bracket"))))
233
234 (define-public (bar-line::calc-glyph-name grob)
235   (let* ((glyph (ly:grob-property grob 'glyph))
236          (dir (ly:item-break-dir grob))
237          (result (assoc glyph  bar-glyph-alist))
238          (glyph-name (if (= dir CENTER)
239                          glyph
240                          (if (and result
241                                   (string? (index-cell (cdr result) dir)))
242                              (index-cell (cdr result) dir)
243                              #f))))
244     glyph-name))
245
246 (define-public (bar-line::calc-break-visibility grob)
247   (let* ((glyph (ly:grob-property grob 'glyph))
248          (result (assoc glyph bar-glyph-alist)))
249
250     (if result
251         (vector (string? (cadr result)) #t (string? (cddr result)))
252         all-invisible)))
253
254 (define-public (shift-right-at-line-begin g)
255   "Shift an item to the right, but only at the start of the line."
256   (if (and (ly:item? g)
257            (equal? (ly:item-break-dir g) RIGHT))
258       (ly:grob-translate-axis! g 3.5 X)))
259
260
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
262 ;; Tuplets
263
264 (define-public (tuplet-number::calc-denominator-text grob)
265   (number->string (ly:event-property (event-cause grob) 'denominator)))
266
267 (define-public (tuplet-number::calc-fraction-text grob)
268   (let ((ev (event-cause grob)))
269
270     (format "~a:~a"
271             (ly:event-property ev 'denominator)
272             (ly:event-property ev 'numerator))))
273
274 ;; a formatter function, which is simply a wrapper around an existing
275 ;; tuplet formatter function. It takes the value returned by the given
276 ;; function and appends a note of given length.
277 (define-public ((tuplet-number::append-note-wrapper function note) grob)
278   (let ((txt (if function (function grob) #f)))
279
280     (if txt
281         (markup txt #:fontsize -5 #:note note UP)
282         (markup #:fontsize -5 #:note note UP))))
283
284 ;; Print a tuplet denominator with a different number than the one derived from
285 ;; the actual tuplet fraction
286 (define-public ((tuplet-number::non-default-tuplet-denominator-text denominator)
287                 grob)
288   (number->string (if denominator
289                       denominator
290                       (ly:event-property (event-cause grob) 'denominator))))
291
292 ;; Print a tuplet fraction with different numbers than the ones derived from
293 ;; the actual tuplet fraction
294 (define-public ((tuplet-number::non-default-tuplet-fraction-text
295                  denominator numerator) grob)
296   (let* ((ev (event-cause grob))
297          (den (if denominator denominator (ly:event-property ev 'denominator)))
298          (num (if numerator numerator (ly:event-property ev 'numerator))))
299
300     (format "~a:~a" den num)))
301
302 ;; Print a tuplet fraction with note durations appended to the numerator and the
303 ;; denominator
304 (define-public ((tuplet-number::fraction-with-notes
305                  denominatornote numeratornote) grob)
306   (let* ((ev (event-cause grob))
307          (denominator (ly:event-property ev 'denominator))
308          (numerator (ly:event-property ev 'numerator)))
309
310     ((tuplet-number::non-default-fraction-with-notes
311       denominator denominatornote numerator numeratornote) grob)))
312
313 ;; Print a tuplet fraction with note durations appended to the numerator and the
314 ;; denominator
315 (define-public ((tuplet-number::non-default-fraction-with-notes
316                  denominator denominatornote numerator numeratornote) grob)
317   (let* ((ev (event-cause grob))
318          (den (if denominator denominator (ly:event-property ev 'denominator)))
319          (num (if numerator numerator (ly:event-property ev 'numerator))))
320
321     (make-concat-markup (list
322                          (make-simple-markup (format "~a" den))
323                          (markup #:fontsize -5 #:note denominatornote UP)
324                          (make-simple-markup " : ")
325                          (make-simple-markup (format "~a" num))
326                          (markup #:fontsize -5 #:note numeratornote UP)))))
327
328
329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
330 ;; Color
331
332 (define-public color? list?)
333 (define-public (rgb-color r g b) (list r g b))
334
335 ; predefined colors
336 (define-public black       '(0.0 0.0 0.0))
337 (define-public white       '(1.0 1.0 1.0))
338 (define-public red         '(1.0 0.0 0.0))
339 (define-public green       '(0.0 1.0 0.0))
340 (define-public blue        '(0.0 0.0 1.0))
341 (define-public cyan        '(0.0 1.0 1.0))
342 (define-public magenta     '(1.0 0.0 1.0))
343 (define-public yellow      '(1.0 1.0 0.0))
344
345 (define-public grey        '(0.5 0.5 0.5))
346 (define-public darkred     '(0.5 0.0 0.0))
347 (define-public darkgreen   '(0.0 0.5 0.0))
348 (define-public darkblue    '(0.0 0.0 0.5))
349 (define-public darkcyan    '(0.0 0.5 0.5))
350 (define-public darkmagenta '(0.5 0.0 0.5))
351 (define-public darkyellow  '(0.5 0.5 0.0))
352
353
354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
355 ;; key signature
356
357 (define-public (key-signature-interface::alteration-position step alter
358                                                              c0-position)
359   ;; TODO: memoize - this is mostly constant.
360
361   ;; fes, ges, as and bes typeset in lower octave
362   (define FLAT_TOP_PITCH 2)
363
364   ;; ais and bis typeset in lower octave
365   (define SHARP_TOP_PITCH 4)
366
367   (if (pair? step)
368       (+ (cdr step) (* (car step) 7) c0-position)
369       (let* ((from-bottom-pos (modulo (+ 4 49 c0-position) 7))
370              (p step)
371              (c0 (- from-bottom-pos 4)))
372
373         (if
374          (or (and (< alter 0)
375                   (or (> p FLAT_TOP_PITCH) (> (+ p c0) 4)) (> (+ p c0) 1))
376              (and (> alter 0)
377                   (or (> p SHARP_TOP_PITCH) (> (+ p c0) 5)) (> (+ p c0) 2)))
378
379          ;; Typeset below c_position
380          (set! p (- p 7)))
381
382         ;; Provide for the four cases in which there's a glitch
383         ;; it's a hack, but probably not worth
384         ;; the effort of finding a nicer solution.
385         ;; --dl.
386         (cond
387          ((and (= c0 2) (= p 3) (> alter 0))
388           (set! p (- p 7)))
389          ((and (= c0 -3) (= p -1) (> alter 0))
390           (set! p (+ p 7)))
391          ((and (= c0 -4) (= p -1) (< alter 0))
392           (set! p (+ p 7)))
393          ((and (= c0 -2) (= p -3) (< alter 0))
394           (set! p (+ p 7))))
395
396         (+ c0 p))))
397
398
399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
400 ;; accidentals
401
402 (define-public (accidental-interface::calc-alteration grob)
403   (ly:pitch-alteration (ly:event-property (event-cause grob) 'pitch)))
404
405 (define-public cancellation-glyph-name-alist
406   '((0 . "accidentals.natural")))
407
408 (define-public standard-alteration-glyph-name-alist
409   '(
410     ;; ordered for optimal performance.
411     (0 . "accidentals.natural")
412     (-1/2 . "accidentals.flat")
413     (1/2 . "accidentals.sharp")
414
415     (1 . "accidentals.doublesharp")
416     (-1 . "accidentals.flatflat")
417
418     (3/4 . "accidentals.sharp.slashslash.stemstemstem")
419     (1/4 . "accidentals.sharp.slashslash.stem")
420     (-1/4 . "accidentals.mirroredflat")
421     (-3/4 . "accidentals.mirroredflat.flat")))
422
423 ;; FIXME: standard vs default, alteration-FOO vs FOO-alteration
424 (define-public alteration-default-glyph-name-alist
425   standard-alteration-glyph-name-alist)
426
427 (define-public makam-alteration-glyph-name-alist
428   '((1 . "accidentals.doublesharp")
429     (8/9 . "accidentals.sharp.slashslashslash.stemstem")
430     (5/9 . "accidentals.sharp.slashslashslash.stem")
431     (4/9 . "accidentals.sharp")
432     (1/9 . "accidentals.sharp.slashslash.stem")
433     (0 . "accidentals.natural")
434     (-1/9 . "accidentals.mirroredflat")
435     (-4/9 . "accidentals.flat.slash")
436     (-5/9 . "accidentals.flat")
437     (-8/9 . "accidentals.flat.slashslash")
438     (-1 . "accidentals.flatflat")))
439
440 (define-public alteration-hufnagel-glyph-name-alist
441   '((-1/2 . "accidentals.hufnagelM1")
442     (0 . "accidentals.vaticana0")
443     (1/2 . "accidentals.mensural1")))
444
445 (define-public alteration-medicaea-glyph-name-alist
446   '((-1/2 . "accidentals.medicaeaM1")
447     (0 . "accidentals.vaticana0")
448     (1/2 . "accidentals.mensural1")))
449
450 (define-public alteration-vaticana-glyph-name-alist
451   '((-1/2 . "accidentals.vaticanaM1")
452     (0 . "accidentals.vaticana0")
453     (1/2 . "accidentals.mensural1")))
454
455 (define-public alteration-mensural-glyph-name-alist
456   '((-1/2 . "accidentals.mensuralM1")
457     (0 . "accidentals.vaticana0")
458     (1/2 . "accidentals.mensural1")))
459
460
461 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
462 ;; * Pitch Trill Heads
463 ;; * Parentheses
464
465 (define-public (parentheses-item::calc-parenthesis-stencils grob)
466   (let* ((font (ly:grob-default-font grob))
467          (lp (ly:font-get-glyph font "accidentals.leftparen"))
468          (rp (ly:font-get-glyph font "accidentals.rightparen")))
469
470     (list lp rp)))
471
472 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
473   (let* ((font (ly:grob-default-font grob))
474          (lp (ly:stencil-aligned-to (ly:stencil-aligned-to
475                                      (grob-interpret-markup
476                                       grob
477                                       (ly:wide-char->utf-8 #x2329))
478                                      Y CENTER)
479                                     X RIGHT))
480          (rp (ly:stencil-aligned-to (ly:stencil-aligned-to
481                                      (grob-interpret-markup
482                                       grob
483                                       (ly:wide-char->utf-8 #x232A))
484                                      Y CENTER)
485                                     X LEFT)))
486
487     (list (stencil-whiteout lp)
488           (stencil-whiteout rp))))
489
490 (define (parenthesize-elements grob . rest)
491   (let* ((refp (if (null? rest)
492                    grob
493                    (car rest)))
494          (elts (ly:grob-object grob 'elements))
495          (x-ext (ly:relative-group-extent elts refp X))
496          (stencils (ly:grob-property grob 'stencils))
497          (lp (car stencils))
498          (rp (cadr stencils))
499          (padding (ly:grob-property grob 'padding 0.1)))
500
501     (ly:stencil-add
502      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
503      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))))
504
505
506 (define (parentheses-item::print me)
507   (let* ((elts (ly:grob-object me 'elements))
508          (y-ref (ly:grob-common-refpoint-of-array me elts Y))
509          (x-ref (ly:grob-common-refpoint-of-array me elts X))
510          (stencil (parenthesize-elements me x-ref))
511          (elt-y-ext (ly:relative-group-extent elts y-ref Y))
512          (y-center (interval-center elt-y-ext)))
513
514     (ly:stencil-translate
515      stencil
516      (cons
517       (- (ly:grob-relative-coordinate me x-ref X))
518       (- y-center (ly:grob-relative-coordinate me y-ref Y))))))
519
520
521
522 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
523 ;;
524
525 (define-public (chain-grob-member-functions grob value . funcs)
526   (for-each
527    (lambda (func)
528      (set! value (func grob value)))
529    funcs)
530
531   value)
532
533
534 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
535 ;; falls/doits
536
537 (define-public (bend::print spanner)
538   (define (close  a b)
539     (< (abs (- a b)) 0.01))
540
541   (let* ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
542          (left-span (ly:spanner-bound spanner LEFT))
543          (dots (if (and (grob::has-interface left-span 'note-head-interface)
544                         (ly:grob? (ly:grob-object left-span 'dot)))
545                    (ly:grob-object left-span 'dot) #f))
546
547          (right-span (ly:spanner-bound spanner RIGHT))
548          (thickness (* (ly:grob-property spanner 'thickness)
549                        (ly:output-def-lookup (ly:grob-layout spanner)
550                                              'line-thickness)))
551          (padding (ly:grob-property spanner 'padding 0.5))
552          (common (ly:grob-common-refpoint right-span
553                                           (ly:grob-common-refpoint spanner
554                                                                    left-span X)
555                                           X))
556          (common-y (ly:grob-common-refpoint spanner left-span Y))
557          (minimum-length (ly:grob-property spanner 'minimum-length 0.5))
558
559          (left-x (+ padding
560                     (max
561                      (interval-end (ly:grob-robust-relative-extent
562                                     left-span common X))
563                      (if
564                       (and dots
565                            (close
566                             (ly:grob-relative-coordinate dots common-y Y)
567                             (ly:grob-relative-coordinate spanner common-y Y)))
568                       (interval-end
569                        (ly:grob-robust-relative-extent dots common X))
570                       ;; TODO: use real infinity constant.
571                       -10000))))
572          (right-x (max (- (interval-start
573                            (ly:grob-robust-relative-extent right-span common X))
574                           padding)
575                        (+ left-x minimum-length)))
576          (self-x (ly:grob-relative-coordinate spanner common X))
577          (dx (- right-x left-x))
578          (exp (list 'path thickness
579                     `(quote
580                       (rmoveto
581                        ,(- left-x self-x) 0
582
583                        rcurveto
584                        ,(/ dx 3)
585                        0
586                        ,dx ,(* 0.66 delta-y)
587                        ,dx ,delta-y)))))
588
589     (ly:make-stencil
590      exp
591      (cons (- left-x self-x) (- right-x self-x))
592      (cons (min 0 delta-y)
593            (max 0 delta-y)))))
594
595
596 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
597 ;; grace spacing
598
599 (define-public (grace-spacing::calc-shortest-duration grob)
600   (let* ((cols (ly:grob-object grob 'columns))
601          (get-difference
602           (lambda (idx)
603             (ly:moment-sub (ly:grob-property
604                             (ly:grob-array-ref cols (1+ idx)) 'when)
605                            (ly:grob-property
606                             (ly:grob-array-ref cols idx) 'when))))
607
608          (moment-min (lambda (x y)
609                        (cond
610                         ((and x y)
611                          (if (ly:moment<? x y)
612                              x
613                              y))
614                         (x x)
615                         (y y)))))
616
617     (fold moment-min #f (map get-difference
618                              (iota (1- (ly:grob-array-length cols)))))))
619
620
621 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
622 ;; fingering
623
624 (define-public (fingering::calc-text grob)
625   (let* ((event (event-cause grob))
626          (digit (ly:event-property event 'digit)))
627
628     (if (> digit 5)
629         (ly:input-message (ly:event-property event 'origin)
630                           "Warning: Fingering notation for finger number ~a"
631                           digit))
632
633     (number->string digit 10)))
634
635 (define-public (string-number::calc-text grob)
636   (let ((digit (ly:event-property (event-cause grob) 'string-number)))
637
638     (number->string digit 10)))
639
640 (define-public (stroke-finger::calc-text grob)
641   (let* ((digit (ly:event-property (event-cause grob) 'digit))
642          (text (ly:event-property (event-cause grob) 'text)))
643
644     (if (string? text)
645         text
646         (vector-ref (ly:grob-property grob 'digit-names)
647                     (1- (max (min 5 digit) 1))))))
648
649
650 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
651 ;; dynamics
652
653 (define-public (hairpin::calc-grow-direction grob)
654   (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
655       START
656       STOP))
657
658
659 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
660 ;; lyrics
661
662 (define-public (lyric-text::print grob)
663   "Allow interpretation of tildes as lyric tieing marks."
664
665   (let ((text (ly:grob-property grob 'text)))
666
667     (grob-interpret-markup grob (if (string? text)
668                                     (make-tied-lyric-markup text)
669                                     text))))
670
671 (define-public ((grob::calc-property-by-copy prop) grob)
672   (ly:event-property (event-cause grob) prop))
673
674
675 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
676 ;; fret boards
677
678 (define-public (fret-board::calc-stencil grob)
679   (grob-interpret-markup
680    grob
681    (make-fret-diagram-verbose-markup
682     (ly:grob-property grob 'dot-placement-list))))
683
684
685 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
686 ;; scripts
687
688 (define-public (script-interface::calc-x-offset grob)
689   (ly:grob-property grob 'positioning-done)
690   (let* ((shift (ly:grob-property grob 'toward-stem-shift 0.0))
691          (note-head-location
692           (ly:self-alignment-interface::centered-on-x-parent grob))
693          (note-head-grob (ly:grob-parent grob X))
694          (stem-grob (ly:grob-object note-head-grob 'stem)))
695
696     (+ note-head-location
697        ;; If the property 'toward-stem-shift is defined and the script
698        ;; has the same direction as the stem, move the script accordingly.
699        ;; Since scripts can also be over skips, we need to check whether
700        ;; the grob has a stem at all.
701        (if (ly:grob? stem-grob)
702            (let ((dir1 (ly:grob-property grob 'direction))
703                  (dir2 (ly:grob-property stem-grob 'direction)))
704              (if (equal? dir1 dir2)
705                  (let* ((common-refp (ly:grob-common-refpoint grob stem-grob X))
706                         (stem-location
707                          (ly:grob-relative-coordinate stem-grob common-refp X)))
708                    (* shift (- stem-location note-head-location)))
709                  0.0))
710            0.0))))
711
712
713 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
714 ;; instrument names
715
716 (define-public (system-start-text::print grob)
717   (let* ((left-bound (ly:spanner-bound grob LEFT))
718          (left-mom (ly:grob-property left-bound 'when))
719          (name (if (moment<=? left-mom ZERO-MOMENT)
720                    (ly:grob-property grob 'long-text)
721                    (ly:grob-property grob 'text))))
722
723     (if (and (markup? name)
724              (!= (ly:item-break-dir left-bound) CENTER))
725
726         (grob-interpret-markup grob name)
727         (ly:grob-suicide! grob))))
728
729 (define-public (system-start-text::calc-x-offset grob)
730   (let* ((left-bound (ly:spanner-bound grob LEFT))
731          (left-mom (ly:grob-property left-bound 'when))
732          (layout (ly:grob-layout grob))
733          (indent (ly:output-def-lookup layout
734                                        (if (moment<=? left-mom ZERO-MOMENT)
735                                            'indent
736                                            'short-indent)
737                                        0.0))
738          (system (ly:grob-system grob))
739          (my-extent (ly:grob-extent grob system X))
740          (elements (ly:grob-object system 'elements))
741          (common (ly:grob-common-refpoint-of-array system elements X))
742          (total-ext empty-interval)
743          (align-x (ly:grob-property grob 'self-alignment-X 0))
744          (padding (min 0 (- (interval-length my-extent) indent)))
745          (right-padding (- padding
746                            (/ (* padding (1+ align-x)) 2))))
747
748     ;; compensate for the variation in delimiter extents by
749     ;; calculating an X-offset correction based on united extents
750     ;; of all delimiters in this system
751     (let unite-delims ((l (ly:grob-array-length elements)))
752       (if (> l 0)
753           (let ((elt (ly:grob-array-ref elements (1- l))))
754
755             (if (grob::has-interface elt 'system-start-delimiter-interface)
756                 (let ((dims (ly:grob-extent elt common X)))
757                   (if (interval-sane? dims)
758                       (set! total-ext (interval-union total-ext dims)))))
759             (unite-delims (1- l)))))
760
761     (+
762      (ly:side-position-interface::x-aligned-side grob)
763      right-padding
764      (- (interval-length total-ext)))))
765
766 (define-public (system-start-text::calc-y-offset grob)
767
768   (define (live-elements-list me)
769     (let* ((elements (ly:grob-object me 'elements))
770            (elts-length (ly:grob-array-length elements))
771            (live-elements '()))
772
773       (let get-live ((len elts-length))
774         (if (> len 0)
775             (let ((elt (ly:grob-array-ref elements (1- len))))
776
777               (if (grob::is-live? elt)
778                   (set! live-elements (cons elt live-elements)))
779               (get-live (1- len)))))
780       live-elements))
781
782   (let* ((left-bound (ly:spanner-bound grob LEFT))
783          (live-elts (live-elements-list grob))
784          (system (ly:grob-system grob))
785          (extent empty-interval))
786
787     (if (and (pair? live-elts)
788              (interval-sane? (ly:grob-extent grob system Y)))
789         (let get-extent ((lst live-elts))
790           (if (pair? lst)
791               (let ((axis-group (car lst)))
792
793                 (if (and (ly:spanner? axis-group)
794                          (equal? (ly:spanner-bound axis-group LEFT)
795                                  left-bound))
796                     (set! extent (add-point extent
797                                             (ly:grob-relative-coordinate
798                                              axis-group system Y))))
799                 (get-extent (cdr lst)))))
800         ;; no live axis group(s) for this instrument name -> remove from system
801         (ly:grob-suicide! grob))
802
803     (+
804      (ly:self-alignment-interface::y-aligned-on-self grob)
805      (interval-center extent))))