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