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