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