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