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