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