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