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