]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Implements padding at right-broken hairpins that come up against span bars.
[lilypond.git] / scm / output-lib.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 1998--2011 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 ;; general
22
23 (define-public (grob::has-interface grob iface)
24   (memq iface (ly:grob-interfaces grob)))
25
26 (define-public (grob::is-live? grob)
27   (pair? (ly:grob-basic-properties grob)))
28
29 (define-public (grob::x-parent-width grob)
30   (ly:grob-property (ly:grob-parent grob X) 'X-extent))
31
32 (define-public (make-stencil-boxer thickness padding callback)
33   "Return function that adds a box around the grob passed as argument."
34   (lambda (grob)
35     (box-stencil (callback grob) thickness padding)))
36
37 (define-public (make-stencil-circler thickness padding callback)
38   "Return function that adds a circle around the grob passed as argument."
39   (lambda (grob)
40     (circle-stencil (callback grob) thickness padding)))
41
42 (define-public (print-circled-text-callback grob)
43   (grob-interpret-markup grob (make-circle-markup
44                                (ly:grob-property grob 'text))))
45
46 (define-public (event-cause grob)
47   (let ((cause (ly:grob-property  grob 'cause)))
48
49     (cond
50      ((ly:stream-event? cause) cause)
51      ((ly:grob? cause) (event-cause cause))
52      (else #f))))
53
54 (define-public (grob-interpret-markup grob text)
55   (let* ((layout (ly:grob-layout grob))
56          (defs (ly:output-def-lookup layout 'text-font-defaults))
57          (props (ly:grob-alist-chain grob defs)))
58
59     (ly:text-interface::interpret-markup layout props text)))
60
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62 ;; beam slope
63
64 ;; calculates each slope of a broken beam individually
65 (define-public (beam::place-broken-parts-individually grob)
66   (ly:beam::quanting grob '(+inf.0 . -inf.0) #f))
67
68 ;; calculates the slope of a beam as a single unit,
69 ;; even if it is broken.  this assures that the beam
70 ;; will pick up where it left off after a line break
71 (define-public (beam::align-with-broken-parts grob)
72   (ly:beam::quanting grob '(+inf.0 . -inf.0) #t))
73
74 ;; uses the broken beam style from edition peters combines the
75 ;; values of place-broken-parts-individually and align-with-broken-parts above,
76 ;; favoring place-broken-parts-individually when the beam naturally has a steeper
77 ;; incline and align-with-broken-parts when the beam is flat
78 (define-public (beam::slope-like-broken-parts grob)
79   (define (slope y x)
80     (/ (- (cdr y) (car y)) (- (cdr x) (car x))))
81   (let* ((quant1 (ly:beam::quanting grob '(+inf.0 . -inf.0) #t))
82          (original (ly:grob-original grob))
83          (siblings (if (ly:grob? original)
84                        (ly:spanner-broken-into original)
85                        '())))
86     (if (null? siblings)
87         quant1
88         (let* ((quant2 (ly:beam::quanting grob '(+inf.0 . -inf.0) #f))
89                (x-span (ly:grob-property grob 'X-positions))
90                (slope1 (slope quant1 x-span))
91                (slope2 (slope quant2 x-span))
92                (quant2 (if (not (= (sign slope1) (sign slope2)))
93                            '(0 . 0)
94                            quant2))
95                (factor (/ (atan (abs slope1)) PI-OVER-TWO))
96                (base (cons-map
97                        (lambda (x)
98                          (+ (* (x quant1) (- 1 factor))
99                             (* (x quant2) factor)))
100                        (cons car cdr))))
101           (ly:beam::quanting grob base #f)))))
102
103 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
104 ;; cross-staff stuff
105
106 (define-public (script-or-side-position-cross-staff g)
107   (or
108    (ly:script-interface::calc-cross-staff g)
109    (ly:side-position-interface::calc-cross-staff g)))
110
111
112 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
113 ;; note heads
114
115 (define-public (stem::calc-duration-log grob)
116   (ly:duration-log
117    (ly:event-property (event-cause grob) 'duration)))
118
119 (define-public (stem::length grob)
120   (let* ((ss (ly:staff-symbol-staff-space grob))
121          (beg (ly:grob-property grob 'stem-begin-position))
122          (beam (ly:grob-object grob 'beam)))
123     (if (null? beam)
124         (abs (- (ly:stem::calc-stem-end-position grob) beg))
125         (begin
126           (ly:programming-error
127             "stem::length called but will not be used for beamed stem.")
128           0.0))))
129
130 (define-public (stem::pure-length grob beg end)
131   (let* ((ss (ly:staff-symbol-staff-space grob))
132          (beg (ly:grob-pure-property grob 'stem-begin-position 0 1000)))
133     (abs (- (ly:stem::pure-calc-stem-end-position grob 0 2147483646) beg))))
134
135 (define-public (note-head::calc-duration-log grob)
136   (min 2
137        (ly:duration-log
138         (ly:event-property (event-cause grob) 'duration))))
139
140 (define-public (dots::calc-dot-count grob)
141   (ly:duration-dot-count
142    (ly:event-property (event-cause grob) 'duration)))
143
144 (define-public (dots::calc-staff-position grob)
145   (let* ((head (ly:grob-parent grob Y))
146          (log (ly:grob-property head 'duration-log)))
147
148     (cond
149      ((or (not (grob::has-interface head 'rest-interface))
150           (not (integer? log))) 0)
151      ((= log 7) 4)
152      ((> log 4) 3)
153      ((= log 0) -1)
154      ((= log 1) 1)
155      ((= log -1) 1)
156      (else 0))))
157
158 ;; Kept separate from note-head::calc-glyph-name to allow use by
159 ;; markup commands \note and \note-by-number
160 (define-public (select-head-glyph style log)
161   "Select a note head glyph string based on note head style @var{style}
162 and duration-log @var{log}."
163   (case style
164     ;; "default" style is directly handled in note-head.cc as a
165     ;; special case (HW says, mainly for performance reasons).
166     ;; Therefore, style "default" does not appear in this case
167     ;; statement.  -- jr
168     ((xcircle) "2xcircle")
169     ((harmonic) "0harmonic")
170     ((harmonic-black) "2harmonic")
171     ((harmonic-mixed) (if (<= log 1) "0harmonic"
172                           "2harmonic"))
173     ((baroque)
174      ;; Oops, I actually would not call this "baroque", but, for
175      ;; backwards compatibility to 1.4, this is supposed to take
176      ;; brevis, longa and maxima from the neo-mensural font and all
177      ;; other note heads from the default font.  -- jr
178      (if (< log 0)
179          (string-append (number->string log) "neomensural")
180          (number->string log)))
181     ((altdefault)
182      ;; Like default, but brevis is drawn with double vertical lines
183      (if (= log -1)
184          (string-append (number->string log) "double")
185          (number->string log)))
186     ((mensural)
187      (string-append (number->string log) (symbol->string style)))
188     ((petrucci)
189      (if (< log 0)
190          (string-append (number->string log) "mensural")
191          (string-append (number->string log) (symbol->string style))))
192     ((blackpetrucci)
193      (if (< log 0)
194          (string-append (number->string log) "blackmensural")
195          (string-append (number->string log) (symbol->string style))))
196     ((semipetrucci)
197      (if (< log 0)
198          (string-append (number->string log) "semimensural")
199          (string-append (number->string log) "petrucci")))
200     ((neomensural)
201      (string-append (number->string log) (symbol->string style)))
202     (else
203      (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
204          (symbol->string style)
205          (string-append (number->string (max 0 log))
206                         (symbol->string style))))))
207
208 (define-public (note-head::calc-glyph-name grob)
209   (let ((style (ly:grob-property grob 'style))
210         (log (min 2 (ly:grob-property grob 'duration-log))))
211
212     (select-head-glyph style log)))
213
214 (define-public (note-head::brew-ez-stencil grob)
215   (let* ((log (ly:grob-property grob 'duration-log))
216          (pitch (ly:event-property (event-cause grob) 'pitch))
217          (pitch-index (ly:pitch-notename pitch))
218          (note-names (ly:grob-property grob 'note-names))
219          (pitch-string (if (and (vector? note-names)
220                                 (> (vector-length note-names) pitch-index))
221                            (vector-ref note-names pitch-index)
222                            (string
223                             (integer->char
224                              (+ (modulo (+ pitch-index 2) 7)
225                                 (char->integer #\A))))))
226          (staff-space (ly:staff-symbol-staff-space grob))
227          (line-thickness (ly:staff-symbol-line-thickness grob))
228          (stem (ly:grob-object grob 'stem))
229          (stem-thickness (* (if (ly:grob? stem)
230                                 (ly:grob-property stem 'thickness)
231                                 1.3)
232                             line-thickness))
233          (radius (/ (+ staff-space line-thickness) 2))
234          (letter (markup #:center-align #:vcenter pitch-string))
235          (filled-circle (markup #:draw-circle radius 0 #t)))
236
237     (ly:stencil-translate-axis
238      (grob-interpret-markup
239       grob
240       (if (>= log 2)
241           (make-combine-markup
242            filled-circle
243            (make-with-color-markup white letter))
244           (make-combine-markup
245            (make-combine-markup
246             filled-circle
247             (make-with-color-markup white (make-draw-circle-markup
248                                            (- radius stem-thickness) 0 #t)))
249            letter)))
250      radius X)))
251
252 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
253 ;; clipping
254
255 (define-public (make-rhythmic-location bar-num num den)
256   (cons
257    bar-num (ly:make-moment num den)))
258
259 (define-public (rhythmic-location? a)
260   (and (pair? a)
261        (integer? (car a))
262        (ly:moment? (cdr a))))
263
264 (define-public (make-graceless-rhythmic-location loc)
265   (make-rhythmic-location
266    (car loc)
267    (ly:moment-main-numerator (rhythmic-location-measure-position loc))
268    (ly:moment-main-denominator (rhythmic-location-measure-position loc))))
269
270 (define-public rhythmic-location-measure-position cdr)
271 (define-public rhythmic-location-bar-number car)
272
273 (define-public (rhythmic-location<? a b)
274   (cond
275    ((< (car a) (car b)) #t)
276    ((> (car a) (car b)) #f)
277    (else
278     (ly:moment<? (cdr a) (cdr b)))))
279
280 (define-public (rhythmic-location<=? a b)
281   (not (rhythmic-location<? b a)))
282 (define-public (rhythmic-location>=? a b)
283   (rhythmic-location<? a b))
284 (define-public (rhythmic-location>? a b)
285   (rhythmic-location<? b a))
286
287 (define-public (rhythmic-location=? a b)
288   (and (rhythmic-location<=? a b)
289        (rhythmic-location<=? b a)))
290
291 (define-public (rhythmic-location->file-string a)
292   (ly:format "~a.~a.~a"
293              (car a)
294              (ly:moment-main-numerator (cdr a))
295              (ly:moment-main-denominator (cdr a))))
296
297 (define-public (rhythmic-location->string a)
298   (ly:format "bar ~a ~a"
299              (car a)
300              (ly:moment->string (cdr a))))
301
302 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
303 ;; break visibility
304
305 (define-public all-visible             #(#t #t #t))
306 (define-public begin-of-line-invisible #(#t #t #f))
307 (define-public center-invisible        #(#t #f #t))
308 (define-public end-of-line-invisible   #(#f #t #t))
309 (define-public begin-of-line-visible   #(#f #f #t))
310 (define-public center-visible          #(#f #t #f))
311 (define-public end-of-line-visible     #(#t #f #f))
312 (define-public all-invisible           #(#f #f #f))
313 (define-public (inherit-x-parent-visibility grob)
314   (let ((parent (ly:grob-parent grob X)))
315     (ly:grob-property parent 'break-visibility all-invisible)))
316 (define-public (inherit-y-parent-visibility grob)
317   (let ((parent (ly:grob-parent grob X)))
318     (ly:grob-property parent 'break-visibility)))
319
320
321 (define-public spanbar-begin-of-line-invisible #(#t #f #f))
322
323
324 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
325 ;; Bar lines.
326
327 ;;
328 ;; How should a  bar line behave at a break?
329 (define bar-glyph-alist
330   '((":|:" . (":|" . "|:"))
331     (":|.|:" . (":|" . "|:"))
332     (":|.:" . (":|" . "|:"))
333     ("||:" . ("||" . "|:"))
334     ("dashed" . ("dashed" . '()))
335     ("|" . ("|" . ()))
336     ("||:" . ("||" . "|:"))
337     ("|s" . (() . "|"))
338     ("|:" . ("|" . "|:"))
339     ("|." . ("|." . ()))
340
341     ;; hmm... should we end with a bar line here?
342     (".|" . ("|" . ".|"))
343     (":|" . (":|" . ()))
344     ("||" . ("||" . ()))
345     (".|." . (".|." . ()))
346     ("|.|" . ("|.|" . ()))
347     ("" . ("" . ""))
348     (":" . (":" . ""))
349     ("." . ("." . ()))
350     ("'" . ("'" . ()))
351     ("empty" . (() . ()))
352     ("brace" . (() . "brace"))
353     ("bracket" . (() . "bracket"))
354
355     ;; segno bar lines
356     ("S" . ("||" . "S"))
357     ("|S" . ("|" . "S"))
358     ("S|" . ("S" . ()))
359     (":|S" . (":|" . "S"))
360     (":|S." . (":|S" . ()))
361     ("S|:" . ("S" . "|:"))
362     (".S|:" . ("|" . "S|:"))
363     (":|S|:" . (":|" . "S|:"))
364     (":|S.|:" . (":|S" . "|:"))))
365
366 (define-public (bar-line::calc-glyph-name grob)
367   (let* ((glyph (ly:grob-property grob 'glyph))
368          (dir (ly:item-break-dir grob))
369          (result (assoc-get glyph bar-glyph-alist))
370          (glyph-name (if (= dir CENTER)
371                          glyph
372                          (if (and result
373                                   (string? (index-cell result dir)))
374                              (index-cell result dir)
375                              #f))))
376     glyph-name))
377
378 (define-public (bar-line::calc-break-visibility grob)
379   (let* ((glyph (ly:grob-property grob 'glyph))
380          (result (assoc-get glyph bar-glyph-alist)))
381
382     (if result
383         (vector (string? (car result)) #t (string? (cdr result)))
384         all-invisible)))
385
386 (define-public (shift-right-at-line-begin g)
387   "Shift an item to the right, but only at the start of the line."
388   (if (and (ly:item? g)
389            (equal? (ly:item-break-dir g) RIGHT))
390       (ly:grob-translate-axis! g 3.5 X)))
391
392 (define-public (pure-from-neighbor-interface::extra-spacing-height-at-beginning-of-line grob)
393   (if (= 1 (ly:item-break-dir grob))
394       (pure-from-neighbor-interface::extra-spacing-height grob)
395       (cons -0.1 0.1)))
396
397 (define-public (pure-from-neighbor-interface::extra-spacing-height grob)
398   (let* ((height (ly:grob::stencil-height grob))
399          (from-neighbors (interval-union
400                             height
401                             (ly:axis-group-interface::pure-height
402                               grob
403                               0
404                               10000000))))
405     (coord-operation - from-neighbors height)))
406
407 (define-public (pure-from-neighbor-interface::account-for-span-bar grob)
408   (let* ((esh (pure-from-neighbor-interface::extra-spacing-height grob))
409          (hsb (ly:grob-property grob 'has-span-bar))
410          (ii (interval-intersection esh (cons -1.01 1.01))))
411     (if (pair? hsb)
412         (cons (car (if (and (car hsb)
413                        (ly:grob-property grob 'allow-span-bar))
414                        esh ii))
415               (cdr (if (cdr hsb) esh ii)))
416         ii)))
417
418 (define-public (pure-from-neighbor-interface::extra-spacing-height-including-staff grob)
419   (let ((esh (pure-from-neighbor-interface::extra-spacing-height grob))
420         (to-staff (coord-operation -
421                                    (interval-widen
422                                      '(0 . 0)
423                                      (ly:staff-symbol-staff-radius grob))
424                                    (ly:grob::stencil-height grob))))
425     (interval-union esh to-staff)))
426
427
428 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429 ;; Tuplets
430
431 (define-public (tuplet-number::calc-direction grob)
432   (ly:tuplet-bracket::calc-direction (ly:grob-object grob 'bracket)))
433
434 (define-public (tuplet-number::calc-denominator-text grob)
435   (number->string (ly:event-property (event-cause grob) 'denominator)))
436
437 (define-public (tuplet-number::calc-fraction-text grob)
438   (let ((ev (event-cause grob)))
439
440     (format #f "~a:~a"
441             (ly:event-property ev 'denominator)
442             (ly:event-property ev 'numerator))))
443
444 ;; a formatter function, which is simply a wrapper around an existing
445 ;; tuplet formatter function. It takes the value returned by the given
446 ;; function and appends a note of given length.
447 (define-public ((tuplet-number::append-note-wrapper function note) grob)
448   (let ((txt (if function (function grob) #f)))
449
450     (if txt
451         (markup txt #:fontsize -5 #:note note UP)
452         (markup #:fontsize -5 #:note note UP))))
453
454 ;; Print a tuplet denominator with a different number than the one derived from
455 ;; the actual tuplet fraction
456 (define-public ((tuplet-number::non-default-tuplet-denominator-text denominator)
457                 grob)
458   (number->string (if denominator
459                       denominator
460                       (ly:event-property (event-cause grob) 'denominator))))
461
462 ;; Print a tuplet fraction with different numbers than the ones derived from
463 ;; the actual tuplet fraction
464 (define-public ((tuplet-number::non-default-tuplet-fraction-text
465                  denominator numerator) grob)
466   (let* ((ev (event-cause grob))
467          (den (if denominator denominator (ly:event-property ev 'denominator)))
468          (num (if numerator numerator (ly:event-property ev 'numerator))))
469
470     (format #f "~a:~a" den num)))
471
472 ;; Print a tuplet fraction with note durations appended to the numerator and the
473 ;; denominator
474 (define-public ((tuplet-number::fraction-with-notes
475                  denominatornote numeratornote) grob)
476   (let* ((ev (event-cause grob))
477          (denominator (ly:event-property ev 'denominator))
478          (numerator (ly:event-property ev 'numerator)))
479
480     ((tuplet-number::non-default-fraction-with-notes
481       denominator denominatornote numerator numeratornote) grob)))
482
483 ;; Print a tuplet fraction with note durations appended to the numerator and the
484 ;; denominator
485 (define-public ((tuplet-number::non-default-fraction-with-notes
486                  denominator denominatornote numerator numeratornote) grob)
487   (let* ((ev (event-cause grob))
488          (den (if denominator denominator (ly:event-property ev 'denominator)))
489          (num (if numerator numerator (ly:event-property ev 'numerator))))
490
491     (make-concat-markup (list
492                          (make-simple-markup (format #f "~a" den))
493                          (markup #:fontsize -5 #:note denominatornote UP)
494                          (make-simple-markup " : ")
495                          (make-simple-markup (format #f "~a" num))
496                          (markup #:fontsize -5 #:note numeratornote UP)))))
497
498
499 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
500 ;; Color
501
502 (define-public (color? x)
503   (and (list? x)
504        (= 3 (length x))
505        (apply eq? #t (map number? x))
506        (apply eq? #t (map (lambda (y) (<= 0 y 1)) x))))
507
508 (define-public (rgb-color r g b) (list r g b))
509
510 ; predefined colors
511 (define-public black       '(0.0 0.0 0.0))
512 (define-public white       '(1.0 1.0 1.0))
513 (define-public red         '(1.0 0.0 0.0))
514 (define-public green       '(0.0 1.0 0.0))
515 (define-public blue        '(0.0 0.0 1.0))
516 (define-public cyan        '(0.0 1.0 1.0))
517 (define-public magenta     '(1.0 0.0 1.0))
518 (define-public yellow      '(1.0 1.0 0.0))
519
520 (define-public grey        '(0.5 0.5 0.5))
521 (define-public darkred     '(0.5 0.0 0.0))
522 (define-public darkgreen   '(0.0 0.5 0.0))
523 (define-public darkblue    '(0.0 0.0 0.5))
524 (define-public darkcyan    '(0.0 0.5 0.5))
525 (define-public darkmagenta '(0.5 0.0 0.5))
526 (define-public darkyellow  '(0.5 0.5 0.0))
527
528
529 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
530 ;; key signature
531
532 (define-public (key-signature-interface::alteration-position step alter
533                                                              c0-position)
534   ;; TODO: memoize - this is mostly constant.
535
536   ;; fes, ges, as and bes typeset in lower octave
537   (define FLAT_TOP_PITCH 2)
538
539   ;; ais and bis typeset in lower octave
540   (define SHARP_TOP_PITCH 4)
541
542   (if (pair? step)
543       (+ (cdr step) (* (car step) 7) c0-position)
544       (let* ((from-bottom-pos (modulo (+ 4 49 c0-position) 7))
545              (p step)
546              (c0 (- from-bottom-pos 4)))
547
548         (if
549          (or (and (< alter 0)
550                   (or (> p FLAT_TOP_PITCH) (> (+ p c0) 4)) (> (+ p c0) 1))
551              (and (> alter 0)
552                   (or (> p SHARP_TOP_PITCH) (> (+ p c0) 5)) (> (+ p c0) 2)))
553
554          ;; Typeset below c_position
555          (set! p (- p 7)))
556
557         ;; Provide for the four cases in which there's a glitch
558         ;; it's a hack, but probably not worth
559         ;; the effort of finding a nicer solution.
560         ;; --dl.
561         (cond
562          ((and (= c0 2) (= p 3) (> alter 0))
563           (set! p (- p 7)))
564          ((and (= c0 -3) (= p -1) (> alter 0))
565           (set! p (+ p 7)))
566          ((and (= c0 -4) (= p -1) (< alter 0))
567           (set! p (+ p 7)))
568          ((and (= c0 -2) (= p -3) (< alter 0))
569           (set! p (+ p 7))))
570
571         (+ c0 p))))
572
573
574 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
575 ;; annotations
576
577 (define-public (numbered-footnotes int)
578   (markup #:tiny (number->string (+ 1 int))))
579
580 (define-public (symbol-footnotes int)
581   (define (helper symbols out idx n)
582     (if (< n 1)
583         out
584         (helper symbols
585                 (string-append out (list-ref symbols idx))
586                 idx
587                 (- n 1))))
588   (markup #:tiny (helper '("*" "†" "‡" "§" "¶")
589                           ""
590                           (remainder int 5)
591                           (+ 1 (quotient int 5)))))
592
593 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
594 ;; accidentals
595
596 (define-public (accidental-interface::calc-alteration grob)
597   (ly:pitch-alteration (ly:event-property (event-cause grob) 'pitch)))
598
599 (define-public cancellation-glyph-name-alist
600   '((0 . "accidentals.natural")))
601
602 (define-public standard-alteration-glyph-name-alist
603   '(
604     ;; ordered for optimal performance.
605     (0 . "accidentals.natural")
606     (-1/2 . "accidentals.flat")
607     (1/2 . "accidentals.sharp")
608
609     (1 . "accidentals.doublesharp")
610     (-1 . "accidentals.flatflat")
611
612     (3/4 . "accidentals.sharp.slashslash.stemstemstem")
613     (1/4 . "accidentals.sharp.slashslash.stem")
614     (-1/4 . "accidentals.mirroredflat")
615     (-3/4 . "accidentals.mirroredflat.flat")))
616
617 ;; FIXME: standard vs default, alteration-FOO vs FOO-alteration
618 (define-public alteration-default-glyph-name-alist
619   standard-alteration-glyph-name-alist)
620
621 (define-public makam-alteration-glyph-name-alist
622   '((1 . "accidentals.doublesharp")
623     (8/9 . "accidentals.sharp.slashslashslash.stemstem")
624     (5/9 . "accidentals.sharp.slashslashslash.stem")
625     (4/9 . "accidentals.sharp")
626     (1/9 . "accidentals.sharp.slashslash.stem")
627     (0 . "accidentals.natural")
628     (-1/9 . "accidentals.mirroredflat")
629     (-4/9 . "accidentals.flat.slash")
630     (-5/9 . "accidentals.flat")
631     (-8/9 . "accidentals.flat.slashslash")
632     (-1 . "accidentals.flatflat")))
633
634 (define-public alteration-hufnagel-glyph-name-alist
635   '((-1/2 . "accidentals.hufnagelM1")
636     (0 . "accidentals.vaticana0")
637     (1/2 . "accidentals.mensural1")))
638
639 (define-public alteration-medicaea-glyph-name-alist
640   '((-1/2 . "accidentals.medicaeaM1")
641     (0 . "accidentals.vaticana0")
642     (1/2 . "accidentals.mensural1")))
643
644 (define-public alteration-vaticana-glyph-name-alist
645   '((-1/2 . "accidentals.vaticanaM1")
646     (0 . "accidentals.vaticana0")
647     (1/2 . "accidentals.mensural1")))
648
649 (define-public alteration-mensural-glyph-name-alist
650   '((-1/2 . "accidentals.mensuralM1")
651     (0 . "accidentals.vaticana0")
652     (1/2 . "accidentals.mensural1")))
653
654
655 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
656 ;; * Pitch Trill Heads
657 ;; * Parentheses
658
659 (define-public (parentheses-item::calc-parenthesis-stencils grob)
660   (let* ((font (ly:grob-default-font grob))
661          (lp (ly:font-get-glyph font "accidentals.leftparen"))
662          (rp (ly:font-get-glyph font "accidentals.rightparen")))
663
664     (list lp rp)))
665
666 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
667   (let* ((parent (ly:grob-parent grob Y))
668          (y-extent (ly:grob-extent parent parent Y))
669          (half-thickness 0.05) ; should it be a property?
670          (width 0.5) ; should it be a property?
671          (angularity 1.5)  ; makes angle brackets
672          (white-padding 0.1) ; should it be a property?
673          (lp (ly:stencil-aligned-to
674                  (ly:stencil-aligned-to
675                    (make-parenthesis-stencil y-extent
676                                              half-thickness
677                                              (- width)
678                                              angularity)
679                    Y CENTER)
680                  X RIGHT))
681          (lp-x-extent
682            (interval-widen (ly:stencil-extent lp X) white-padding))
683          (rp (ly:stencil-aligned-to
684                  (ly:stencil-aligned-to
685                    (make-parenthesis-stencil y-extent
686                                              half-thickness
687                                              width
688                                              angularity)
689                    Y CENTER)
690                  X LEFT))
691           (rp-x-extent
692             (interval-widen (ly:stencil-extent rp X) white-padding)))
693     (set! lp (ly:make-stencil (ly:stencil-expr lp)
694                               lp-x-extent
695                               (ly:stencil-extent lp Y)))
696     (set! rp (ly:make-stencil (ly:stencil-expr rp)
697                               rp-x-extent
698                               (ly:stencil-extent rp Y)))
699     (list (stencil-whiteout lp)
700           (stencil-whiteout rp))))
701
702 (define (parenthesize-elements grob . rest)
703   (let* ((refp (if (null? rest)
704                    grob
705                    (car rest)))
706          (elts (ly:grob-object grob 'elements))
707          (x-ext (ly:relative-group-extent elts refp X))
708          (stencils (ly:grob-property grob 'stencils))
709          (lp (car stencils))
710          (rp (cadr stencils))
711          (padding (ly:grob-property grob 'padding 0.1)))
712
713     (ly:stencil-add
714      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
715      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))))
716
717
718 (define-public (parentheses-item::print me)
719   (let* ((elts (ly:grob-object me 'elements))
720          (y-ref (ly:grob-common-refpoint-of-array me elts Y))
721          (x-ref (ly:grob-common-refpoint-of-array me elts X))
722          (stencil (parenthesize-elements me x-ref))
723          (elt-y-ext (ly:relative-group-extent elts y-ref Y))
724          (y-center (interval-center elt-y-ext)))
725
726     (ly:stencil-translate
727      stencil
728      (cons
729       (- (ly:grob-relative-coordinate me x-ref X))
730       (- y-center (ly:grob-relative-coordinate me y-ref Y))))))
731
732
733
734 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
735 ;;
736
737 (define-public (chain-grob-member-functions grob value . funcs)
738   (for-each
739    (lambda (func)
740      (set! value (func grob value)))
741    funcs)
742
743   value)
744
745
746 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
747 ;; falls/doits
748
749 (define-public (bend::print spanner)
750   (define (close  a b)
751     (< (abs (- a b)) 0.01))
752
753   (let* ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
754          (left-span (ly:spanner-bound spanner LEFT))
755          (dots (if (and (grob::has-interface left-span 'note-head-interface)
756                         (ly:grob? (ly:grob-object left-span 'dot)))
757                    (ly:grob-object left-span 'dot) #f))
758
759          (right-span (ly:spanner-bound spanner RIGHT))
760          (thickness (* (ly:grob-property spanner 'thickness)
761                        (ly:output-def-lookup (ly:grob-layout spanner)
762                                              'line-thickness)))
763          (padding (ly:grob-property spanner 'padding 0.5))
764          (common (ly:grob-common-refpoint right-span
765                                           (ly:grob-common-refpoint spanner
766                                                                    left-span X)
767                                           X))
768          (common-y (ly:grob-common-refpoint spanner left-span Y))
769          (minimum-length (ly:grob-property spanner 'minimum-length 0.5))
770
771          (left-x (+ padding
772                     (max
773                      (interval-end (ly:grob-robust-relative-extent
774                                     left-span common X))
775                      (if
776                       (and dots
777                            (close
778                             (ly:grob-relative-coordinate dots common-y Y)
779                             (ly:grob-relative-coordinate spanner common-y Y)))
780                       (interval-end
781                        (ly:grob-robust-relative-extent dots common X))
782                       ;; TODO: use real infinity constant.
783                       -10000))))
784          (right-x (max (- (interval-start
785                            (ly:grob-robust-relative-extent right-span common X))
786                           padding)
787                        (+ left-x minimum-length)))
788          (self-x (ly:grob-relative-coordinate spanner common X))
789          (dx (- right-x left-x))
790          (exp (list 'path thickness
791                     `(quote
792                       (rmoveto
793                        ,(- left-x self-x) 0
794
795                        rcurveto
796                        ,(/ dx 3)
797                        0
798                        ,dx ,(* 0.66 delta-y)
799                        ,dx ,delta-y)))))
800
801     (ly:make-stencil
802      exp
803      (cons (- left-x self-x) (- right-x self-x))
804      (cons (min 0 delta-y)
805            (max 0 delta-y)))))
806
807
808 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
809 ;; grace spacing
810
811 (define-public (grace-spacing::calc-shortest-duration grob)
812   (let* ((cols (ly:grob-object grob 'columns))
813          (get-difference
814           (lambda (idx)
815             (ly:moment-sub (ly:grob-property
816                             (ly:grob-array-ref cols (1+ idx)) 'when)
817                            (ly:grob-property
818                             (ly:grob-array-ref cols idx) 'when))))
819
820          (moment-min (lambda (x y)
821                        (cond
822                         ((and x y)
823                          (if (ly:moment<? x y)
824                              x
825                              y))
826                         (x x)
827                         (y y)))))
828
829     (fold moment-min #f (map get-difference
830                              (iota (1- (ly:grob-array-length cols)))))))
831
832
833 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
834 ;; fingering
835
836 (define-public (fingering::calc-text grob)
837   (let* ((event (event-cause grob))
838          (digit (ly:event-property event 'digit)))
839
840     (if (> digit 5)
841         (ly:input-message (ly:event-property event 'origin)
842                           "Warning: Fingering notation for finger number ~a"
843                           digit))
844
845     (number->string digit 10)))
846
847 (define-public (string-number::calc-text grob)
848   (let ((digit (ly:event-property (event-cause grob) 'string-number)))
849
850     (number->string digit 10)))
851
852 (define-public (stroke-finger::calc-text grob)
853   (let* ((digit (ly:event-property (event-cause grob) 'digit))
854          (text (ly:event-property (event-cause grob) 'text)))
855
856     (if (string? text)
857         text
858         (vector-ref (ly:grob-property grob 'digit-names)
859                     (1- (max (min 5 digit) 1))))))
860
861
862 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
863 ;; dynamics
864
865 (define-public (hairpin::calc-grow-direction grob)
866   (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
867       START
868       STOP))
869
870 (define-public (dynamic-text-spanner::before-line-breaking grob)
871   "Monitor left bound of @code{DynamicTextSpanner} for absolute dynamics.
872 If found, ensure @code{DynamicText} does not collide with spanner text by
873 changing @code{'attach-dir} and @code{'padding}.  Reads the
874 @code{'right-padding} property of @code{DynamicText} to fine tune space
875 between the two text elements."
876   (let ((left-bound (ly:spanner-bound grob LEFT)))
877     (if (grob::has-interface left-bound 'dynamic-text-interface)
878         (let* ((details (ly:grob-property grob 'bound-details))
879                (left-details (ly:assoc-get 'left details))
880                (my-padding (ly:assoc-get 'padding left-details))
881                (script-padding (ly:grob-property left-bound 'right-padding 0)))
882
883           (and (number? my-padding)
884                (ly:grob-set-nested-property! grob
885                                              '(bound-details left attach-dir)
886                                              RIGHT)
887                (ly:grob-set-nested-property! grob
888                                              '(bound-details left padding)
889                                              (+ my-padding script-padding)))))))
890
891
892 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
893 ;; lyrics
894
895 (define-public (lyric-text::print grob)
896   "Allow interpretation of tildes as lyric tieing marks."
897
898   (let ((text (ly:grob-property grob 'text)))
899
900     (grob-interpret-markup grob (if (string? text)
901                                     (make-tied-lyric-markup text)
902                                     text))))
903
904 (define-public ((grob::calc-property-by-copy prop) grob)
905   (ly:event-property (event-cause grob) prop))
906
907
908 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
909 ;; fret boards
910
911 (define-public (fret-board::calc-stencil grob)
912   (grob-interpret-markup
913    grob
914    (make-fret-diagram-verbose-markup
915     (ly:grob-property grob 'dot-placement-list))))
916
917
918 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
919 ;; scripts
920
921 (define-public (script-interface::calc-x-offset grob)
922   (ly:grob-property grob 'positioning-done)
923   (let* ((shift (ly:grob-property grob 'toward-stem-shift 0.0))
924          (note-head-location
925           (ly:self-alignment-interface::centered-on-x-parent grob))
926          (note-head-grob (ly:grob-parent grob X))
927          (stem-grob (ly:grob-object note-head-grob 'stem)))
928
929     (+ note-head-location
930        ;; If the property 'toward-stem-shift is defined and the script
931        ;; has the same direction as the stem, move the script accordingly.
932        ;; Since scripts can also be over skips, we need to check whether
933        ;; the grob has a stem at all.
934        (if (ly:grob? stem-grob)
935            (let ((dir1 (ly:grob-property grob 'direction))
936                  (dir2 (ly:grob-property stem-grob 'direction)))
937              (if (equal? dir1 dir2)
938                  (let* ((common-refp (ly:grob-common-refpoint grob stem-grob X))
939                         (stem-location
940                          (ly:grob-relative-coordinate stem-grob common-refp X)))
941                    (* shift (- stem-location note-head-location)))
942                  0.0))
943            0.0))))
944
945
946 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
947 ;; instrument names
948
949 (define-public (system-start-text::print grob)
950   (let* ((left-bound (ly:spanner-bound grob LEFT))
951          (left-mom (ly:grob-property left-bound 'when))
952          (name (if (moment<=? left-mom ZERO-MOMENT)
953                    (ly:grob-property grob 'long-text)
954                    (ly:grob-property grob 'text))))
955
956     (if (and (markup? name)
957              (!= (ly:item-break-dir left-bound) CENTER))
958
959         (grob-interpret-markup grob name)
960         (ly:grob-suicide! grob))))
961
962 (define-public (system-start-text::calc-x-offset grob)
963   (let* ((left-bound (ly:spanner-bound grob LEFT))
964          (left-mom (ly:grob-property left-bound 'when))
965          (layout (ly:grob-layout grob))
966          (indent (ly:output-def-lookup layout
967                                        (if (moment<=? left-mom ZERO-MOMENT)
968                                            'indent
969                                            'short-indent)
970                                        0.0))
971          (system (ly:grob-system grob))
972          (my-extent (ly:grob-extent grob system X))
973          (elements (ly:grob-object system 'elements))
974          (common (ly:grob-common-refpoint-of-array system elements X))
975          (total-ext empty-interval)
976          (align-x (ly:grob-property grob 'self-alignment-X 0))
977          (padding (min 0 (- (interval-length my-extent) indent)))
978          (right-padding (- padding
979                            (/ (* padding (1+ align-x)) 2))))
980
981     ;; compensate for the variation in delimiter extents by
982     ;; calculating an X-offset correction based on united extents
983     ;; of all delimiters in this system
984     (let unite-delims ((l (ly:grob-array-length elements)))
985       (if (> l 0)
986           (let ((elt (ly:grob-array-ref elements (1- l))))
987
988             (if (grob::has-interface elt 'system-start-delimiter-interface)
989                 (let ((dims (ly:grob-extent elt common X)))
990                   (if (interval-sane? dims)
991                       (set! total-ext (interval-union total-ext dims)))))
992             (unite-delims (1- l)))))
993
994     (+
995      (ly:side-position-interface::x-aligned-side grob)
996      right-padding
997      (- (interval-length total-ext)))))
998
999 (define-public (system-start-text::calc-y-offset grob)
1000
1001   (define (live-elements-list me)
1002     (let ((elements (ly:grob-object me 'elements)))
1003
1004       (filter! grob::is-live?
1005                (ly:grob-array->list elements))))
1006
1007   (let* ((left-bound (ly:spanner-bound grob LEFT))
1008          (live-elts (live-elements-list grob))
1009          (system (ly:grob-system grob))
1010          (extent empty-interval))
1011
1012     (if (and (pair? live-elts)
1013              (interval-sane? (ly:grob-extent grob system Y)))
1014         (let get-extent ((lst live-elts))
1015           (if (pair? lst)
1016               (let ((axis-group (car lst)))
1017
1018                 (if (and (ly:spanner? axis-group)
1019                          (equal? (ly:spanner-bound axis-group LEFT)
1020                                  left-bound))
1021                     (set! extent (add-point extent
1022                                             (ly:grob-relative-coordinate
1023                                              axis-group system Y))))
1024                 (get-extent (cdr lst)))))
1025         ;; no live axis group(s) for this instrument name -> remove from system
1026         (ly:grob-suicide! grob))
1027
1028     (+
1029      (ly:self-alignment-interface::y-aligned-on-self grob)
1030      (interval-center extent))))
1031
1032
1033 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1034 ;; ambitus
1035
1036 (define-public (ambitus::print grob)
1037   (let ((heads (ly:grob-object grob 'note-heads)))
1038
1039     (if (and (ly:grob-array? heads)
1040              (= (ly:grob-array-length heads) 2))
1041         (let* ((common (ly:grob-common-refpoint-of-array grob heads Y))
1042                (head-down (ly:grob-array-ref heads 0))
1043                (head-up (ly:grob-array-ref heads 1))
1044                (gap (ly:grob-property grob 'gap 0.35))
1045                (point-min (+ (interval-end (ly:grob-extent head-down common Y))
1046                              gap))
1047                (point-max (- (interval-start (ly:grob-extent head-up common Y))
1048                              gap)))
1049
1050           (if (< point-min point-max)
1051               (let* ((layout (ly:grob-layout grob))
1052                      (line-thick (ly:output-def-lookup layout 'line-thickness))
1053                      (blot (ly:output-def-lookup layout 'blot-diameter))
1054                      (grob-thick (ly:grob-property grob 'thickness 2))
1055                      (width (* line-thick grob-thick))
1056                      (x-ext (symmetric-interval (/ width 2)))
1057                      (y-ext (cons point-min point-max))
1058                      (line (ly:round-filled-box x-ext y-ext blot))
1059                      (y-coord (ly:grob-relative-coordinate grob common Y)))
1060
1061                 (ly:stencil-translate-axis line (- y-coord) Y))
1062               empty-stencil))
1063         (begin
1064           (ly:grob-suicide! grob)
1065           (list)))))
1066
1067 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1068 ;;  laissez-vibrer tie
1069 ;;
1070 ;;  needed so we can make laissez-vibrer a pure print
1071 ;;
1072 (define-public (laissez-vibrer::print grob)
1073  (ly:tie::print grob))
1074