]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Remove tied accidentals after line-breaking
[lilypond.git] / scm / output-lib.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 1998--2014 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 (make-stencil-boxer thickness padding callback)
30   "Return function that adds a box around the grob passed as argument."
31   (lambda (grob)
32     (box-stencil (callback grob) thickness padding)))
33
34 (define-public (make-stencil-circler thickness padding callback)
35   "Return function that adds a circle around the grob passed as argument."
36   (lambda (grob)
37     (circle-stencil (callback grob) thickness padding)))
38
39 (define-public (print-circled-text-callback grob)
40   (grob-interpret-markup grob (make-circle-markup
41                                (ly:grob-property grob 'text))))
42
43 (define-public (event-cause grob)
44   (let ((cause (ly:grob-property  grob 'cause)))
45
46     (cond
47      ((ly:stream-event? cause) cause)
48      ((ly:grob? cause) (event-cause cause))
49      (else #f))))
50
51 (define-public (grob-interpret-markup grob text)
52   (let* ((layout (ly:grob-layout grob))
53          (defs (ly:output-def-lookup layout 'text-font-defaults))
54          (props (ly:grob-alist-chain grob defs)))
55
56     (ly:text-interface::interpret-markup layout props text)))
57
58 (define-public (grob::unpure-Y-extent-from-stencil pure-function)
59   "The unpure height will come from a stencil whereas the pure
60    height will come from @code{pure-function}."
61   (ly:make-unpure-pure-container ly:grob::stencil-height pure-function))
62
63 (define-public grob::unpure-horizontal-skylines-from-stencil
64   (ly:make-unpure-pure-container
65    ly:grob::horizontal-skylines-from-stencil
66    ly:grob::pure-simple-horizontal-skylines-from-extents))
67
68 (define-public grob::always-horizontal-skylines-from-stencil
69   (ly:make-unpure-pure-container
70    ly:grob::horizontal-skylines-from-stencil))
71
72 (define-public grob::unpure-vertical-skylines-from-stencil
73   (ly:make-unpure-pure-container
74    ly:grob::vertical-skylines-from-stencil
75    ly:grob::pure-simple-vertical-skylines-from-extents))
76
77 (define-public grob::always-vertical-skylines-from-stencil
78   (ly:make-unpure-pure-container
79    ly:grob::vertical-skylines-from-stencil))
80
81 (define-public grob::always-vertical-skylines-from-element-stencils
82   (ly:make-unpure-pure-container
83    ly:grob::vertical-skylines-from-element-stencils
84    ly:grob::pure-vertical-skylines-from-element-stencils))
85
86 (define-public grob::always-horizontal-skylines-from-element-stencils
87   (ly:make-unpure-pure-container
88    ly:grob::horizontal-skylines-from-element-stencils
89    ly:grob::pure-horizontal-skylines-from-element-stencils))
90
91 ;; Using this as a callback for a grob's Y-extent promises
92 ;; that the grob's stencil does not depend on line-spacing.
93 ;; We use this promise to figure the space required by Clefs
94 ;; and such at the note-spacing stage.
95
96 (define-public grob::always-Y-extent-from-stencil
97   (ly:make-unpure-pure-container ly:grob::stencil-height))
98
99 (define-public (layout-line-thickness grob)
100   "Get the line thickness of the @var{grob}'s corresponding layout."
101   (let* ((layout (ly:grob-layout grob))
102          (line-thickness (ly:output-def-lookup layout 'line-thickness)))
103
104     line-thickness))
105
106 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
107 ;; beam slope
108
109 ;; even though kievan noteheads do not have stems, their
110 ;; invisible stems help with beam placement
111 ;; this assures that invisible stems for kievan notes are aligned
112 ;; to the center of kievan noteheads. that is thus where the beams'
113 ;; x extrema will fall
114 (define-public (stem::kievan-offset-callback grob)
115   (let* ((note-heads (ly:grob-object grob 'note-heads))
116          (note-heads-grobs (if (not (null? note-heads))
117                                (ly:grob-array->list note-heads)
118                                '()))
119          (first-note-head (if (not (null? note-heads-grobs))
120                               (car note-heads-grobs)
121                               '()))
122          (note-head-w (if (not (null? first-note-head))
123                           (ly:grob-extent first-note-head first-note-head X)
124                           '(0 . 0))))
125     (interval-center note-head-w)))
126
127
128 ;; sets position of beams for Kievan notation
129 (define-public (beam::get-kievan-positions grob)
130   (let* ((stems (ly:grob-object grob 'stems))
131          (stems-grobs (if (not (null? stems))
132                           (ly:grob-array->list stems)
133                           '()))
134          (first-stem (if (not (null? stems-grobs))
135                          (car stems-grobs)
136                          '()))
137          (note-heads (if (not (null? first-stem))
138                          (ly:grob-object first-stem 'note-heads)
139                          '()))
140          (note-heads-grobs (if (not (null? note-heads))
141                                (ly:grob-array->list note-heads)
142                                '()))
143          (first-note-head (if (not (null? note-heads-grobs))
144                               (car note-heads-grobs)
145                               '()))
146          (next-stem (if (not (null? stems))
147                         (cadr stems-grobs)
148                         '()))
149          (next-note-heads (if (not (null? next-stem))
150                               (ly:grob-object next-stem 'note-heads)
151                               '()))
152          (next-note-heads-grobs (if (not (null? next-note-heads))
153                                     (ly:grob-array->list next-note-heads)
154                                     '()))
155          (next-note-head (if (not (null? next-note-heads-grobs))
156                              (car next-note-heads-grobs)
157                              '()))
158          (left-pos (ly:grob-property first-note-head 'Y-offset))
159          (right-pos (ly:grob-property next-note-head 'Y-offset))
160          (direction (ly:grob-property grob 'direction))
161          (first-nh-height (ly:grob::stencil-height first-note-head))
162          (next-nh-height (ly:grob::stencil-height next-note-head))
163          (left-height (if (= direction DOWN)
164                           (+ (car first-nh-height) 0.75)
165                           (- (cdr first-nh-height) 0.75)))
166          (right-height (if (= direction DOWN)
167                            (+ (car next-nh-height) 0.75)
168                            (- (cdr next-nh-height) 0.75))))
169     (cons (+ left-pos left-height) (+ right-pos right-height))))
170
171 (define-public (beam::get-kievan-quantized-positions grob)
172   (let* ((pos (ly:grob-property grob 'positions))
173          (stems (ly:grob-object grob 'stems))
174          (stems-grobs (if (not (null? stems))
175                           (ly:grob-array->list stems)
176                           '())))
177     (for-each
178      (lambda (g)
179        (ly:grob-set-property! g 'stem-begin-position 0)
180        (ly:grob-set-property! g 'length 0))
181      stems-grobs)
182     pos))
183
184 ;; calculates each slope of a broken beam individually
185 (define-public (beam::place-broken-parts-individually grob)
186   (ly:beam::quanting grob '(+inf.0 . -inf.0) #f))
187
188 ;; calculates the slope of a beam as a single unit,
189 ;; even if it is broken.  this assures that the beam
190 ;; will pick up where it left off after a line break
191 (define-public (beam::align-with-broken-parts grob)
192   (ly:beam::quanting grob '(+inf.0 . -inf.0) #t))
193
194 ;; uses the broken beam style from edition peters combines the
195 ;; values of place-broken-parts-individually and align-with-broken-parts above,
196 ;; favoring place-broken-parts-individually when the beam naturally has a steeper
197 ;; incline and align-with-broken-parts when the beam is flat
198 (define-public (beam::slope-like-broken-parts grob)
199   (define (slope y x)
200     (/ (- (cdr y) (car y)) (- (cdr x) (car x))))
201   (let* ((quant1 (ly:beam::quanting grob '(+inf.0 . -inf.0) #t))
202          (original (ly:grob-original grob))
203          (siblings (if (ly:grob? original)
204                        (ly:spanner-broken-into original)
205                        '())))
206     (if (null? siblings)
207         quant1
208         (let* ((quant2 (ly:beam::quanting grob '(+inf.0 . -inf.0) #f))
209                (x-span (ly:grob-property grob 'X-positions))
210                (slope1 (slope quant1 x-span))
211                (slope2 (slope quant2 x-span))
212                (quant2 (if (not (= (sign slope1) (sign slope2)))
213                            '(0 . 0)
214                            quant2))
215                (factor (/ (atan (abs slope1)) PI-OVER-TWO))
216                (base (cons-map
217                       (lambda (x)
218                         (+ (* (x quant1) (- 1 factor))
219                            (* (x quant2) factor)))
220                       (cons car cdr))))
221           (ly:beam::quanting grob base #f)))))
222
223 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
224 ;; cross-staff stuff
225
226 (define-public (script-or-side-position-cross-staff g)
227   (or
228    (ly:script-interface::calc-cross-staff g)
229    (ly:side-position-interface::calc-cross-staff g)))
230
231
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 ;; side-position stuff
234
235 (define-public (only-if-beamed g)
236   (any (lambda (x) (ly:grob? (ly:grob-object x 'beam)))
237        (ly:grob-array->list (ly:grob-object g 'side-support-elements))))
238
239 (define-public side-position-interface::y-aligned-side
240   (ly:make-unpure-pure-container
241    ly:side-position-interface::y-aligned-side
242    ly:side-position-interface::pure-y-aligned-side))
243
244 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
245 ;; self-alignment stuff
246
247 (define-public self-alignment-interface::y-aligned-on-self
248   (ly:make-unpure-pure-container
249    ly:self-alignment-interface::y-aligned-on-self
250    ly:self-alignment-interface::pure-y-aligned-on-self))
251
252 (define-public (self-alignment-interface::self-aligned-on-breakable grob)
253   "Return the @code{X-offset} that places @var{grob} according to its
254    @code{self-alignment-X} over the reference point defined by the
255    @code{break-align-anchor-alignment} of a @code{break-aligned} item
256    such as a @code{Clef}."
257   (+ (ly:break-alignable-interface::self-align-callback grob)
258      (ly:self-alignment-interface::x-aligned-on-self grob)))
259
260 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
261 ;; staff symbol
262
263 (define staff-symbol-referencer::callback
264   (ly:make-unpure-pure-container ly:staff-symbol-referencer::callback))
265
266 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
267 ;; note heads
268
269 (define-public (stem::calc-duration-log grob)
270   (ly:duration-log
271    (ly:event-property (event-cause grob) 'duration)))
272
273 (define (stem-stub::do-calculations grob)
274   (and (ly:grob-property (ly:grob-parent grob X) 'cross-staff)
275        (not (ly:grob-property (ly:grob-parent grob X) 'transparent))))
276
277 (define-public (stem-stub::pure-height grob beg end)
278   (if (stem-stub::do-calculations grob)
279       '(0 . 0)
280       '(+inf.0 . -inf.0)))
281
282 (define-public (stem-stub::width grob)
283   (if (stem-stub::do-calculations grob)
284       (grob::x-parent-width grob)
285       '(+inf.0 . -inf.0)))
286
287 (define-public (stem-stub::extra-spacing-height grob)
288   (if (stem-stub::do-calculations grob)
289       (let* ((dad (ly:grob-parent grob X))
290              (refp (ly:grob-common-refpoint grob dad Y))
291              (stem_ph (ly:grob-pure-height dad refp 0 INFINITY-INT))
292              (my_ph (ly:grob-pure-height grob refp 0 INFINITY-INT))
293              ;; only account for distance if stem is on different staff than stub
294              (dist (if (grob::has-interface refp 'hara-kiri-group-spanner-interface)
295                        0
296                        (- (car my_ph) (car stem_ph)))))
297         (if (interval-empty? (interval-intersection stem_ph my_ph)) #f (coord-translate stem_ph dist)))
298       #f))
299
300 (define-public (note-head::calc-kievan-duration-log grob)
301   (min 3
302        (ly:duration-log
303         (ly:event-property (event-cause grob) 'duration))))
304
305 (define-public (note-head::calc-duration-log grob)
306   (min 2
307        (ly:duration-log
308         (ly:event-property (event-cause grob) 'duration))))
309
310 (define-public (dots::calc-dot-count grob)
311   (ly:duration-dot-count
312    (ly:event-property (event-cause grob) 'duration)))
313
314 (define-public (dots::calc-staff-position grob)
315   (let* ((head (ly:grob-parent grob Y))
316          (log (ly:grob-property head 'duration-log)))
317
318     (cond
319      ((or (not (grob::has-interface head 'rest-interface))
320           (not (integer? log))) 0)
321      ((= log 7) 4)
322      ((> log 4) 3)
323      ((= log 0) -1)
324      ((= log 1) 1)
325      ((= log -1) 1)
326      (else 0))))
327
328 ;; Kept separate from note-head::calc-glyph-name to allow use by
329 ;; markup commands \note and \note-by-number
330 (define-public (select-head-glyph style log)
331   "Select a note head glyph string based on note head style @var{style}
332 and duration-log @var{log}."
333   (case style
334     ;; "default" style is directly handled in note-head.cc as a
335     ;; special case (HW says, mainly for performance reasons).
336     ;; Therefore, style "default" does not appear in this case
337     ;; statement.  -- jr
338     ((xcircle) "2xcircle")
339     ((harmonic) "0harmonic")
340     ((harmonic-black) "2harmonic")
341     ((harmonic-mixed) (if (<= log 1) "0harmonic"
342                           "2harmonic"))
343     ((baroque)
344      ;; Oops, I actually would not call this "baroque", but, for
345      ;; backwards compatibility to 1.4, this is supposed to take
346      ;; brevis, longa and maxima from the neo-mensural font and all
347      ;; other note heads from the default font.  -- jr
348      (if (< log 0)
349          (string-append (number->string log) "neomensural")
350          (number->string log)))
351     ((altdefault)
352      ;; Like default, but brevis is drawn with double vertical lines
353      (if (= log -1)
354          (string-append (number->string log) "double")
355          (number->string log)))
356     ((mensural)
357      (string-append (number->string log) (symbol->string style)))
358     ((petrucci)
359      (if (< log 0)
360          (string-append (number->string log) "mensural")
361          (string-append (number->string log) (symbol->string style))))
362     ((blackpetrucci)
363      (if (< log 0)
364          (string-append (number->string log) "blackmensural")
365          (string-append (number->string log) (symbol->string style))))
366     ((semipetrucci)
367      (if (< log 0)
368          (string-append (number->string log) "semimensural")
369          (string-append (number->string log) "petrucci")))
370     ((neomensural)
371      (string-append (number->string log) (symbol->string style)))
372     ((kievan)
373      (string-append (number->string log) "kievan"))
374     (else
375      (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
376          (symbol->string style)
377          (string-append (number->string (max 0 log))
378                         (symbol->string style))))))
379
380 (define-public (note-head::calc-glyph-name grob)
381   (let* ((style (ly:grob-property grob 'style))
382          (log (if (string-match "kievan*" (symbol->string style))
383                   (min 3 (ly:grob-property grob 'duration-log))
384                   (min 2 (ly:grob-property grob 'duration-log)))))
385     (select-head-glyph style log)))
386
387 (define-public (note-head::brew-ez-stencil grob)
388   (let* ((log (ly:grob-property grob 'duration-log))
389          (pitch (ly:event-property (event-cause grob) 'pitch))
390          (pitch-index (ly:pitch-notename pitch))
391          (note-names (ly:grob-property grob 'note-names))
392          (pitch-string (if (and (vector? note-names)
393                                 (> (vector-length note-names) pitch-index))
394                            (vector-ref note-names pitch-index)
395                            (string
396                             (integer->char
397                              (+ (modulo (+ pitch-index 2) 7)
398                                 (char->integer #\A))))))
399          (staff-space (ly:staff-symbol-staff-space grob))
400          (line-thickness (ly:staff-symbol-line-thickness grob))
401          (stem (ly:grob-object grob 'stem))
402          (stem-thickness (* (if (ly:grob? stem)
403                                 (ly:grob-property stem 'thickness)
404                                 1.3)
405                             line-thickness))
406          (radius (/ (+ staff-space line-thickness) 2))
407          (letter (markup #:center-align #:vcenter pitch-string))
408          (filled-circle (markup #:draw-circle radius 0 #t)))
409
410     (ly:stencil-translate-axis
411      (grob-interpret-markup
412       grob
413       (if (>= log 2)
414           (make-combine-markup
415            filled-circle
416            (make-with-color-markup white letter))
417           (make-combine-markup
418            (make-combine-markup
419             filled-circle
420             (make-with-color-markup white (make-draw-circle-markup
421                                            (- radius stem-thickness) 0 #t)))
422            letter)))
423      radius X)))
424
425 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
426 ;; clipping
427
428 (define-public (make-rhythmic-location bar-num num den)
429   (cons
430    bar-num (ly:make-moment num den)))
431
432 (define-public (rhythmic-location? a)
433   (and (pair? a)
434        (integer? (car a))
435        (ly:moment? (cdr a))))
436
437 (define-public (make-graceless-rhythmic-location loc)
438   (make-rhythmic-location
439    (car loc)
440    (ly:moment-main-numerator (rhythmic-location-measure-position loc))
441    (ly:moment-main-denominator (rhythmic-location-measure-position loc))))
442
443 (define-public rhythmic-location-measure-position cdr)
444 (define-public rhythmic-location-bar-number car)
445
446 (define-public (rhythmic-location<? a b)
447   (cond
448    ((< (car a) (car b)) #t)
449    ((> (car a) (car b)) #f)
450    (else
451     (ly:moment<? (cdr a) (cdr b)))))
452
453 (define-public (rhythmic-location<=? a b)
454   (not (rhythmic-location<? b a)))
455 (define-public (rhythmic-location>=? a b)
456   (not (rhythmic-location<? a b)))
457 (define-public (rhythmic-location>? a b)
458   (rhythmic-location<? b a))
459
460 (define-public (rhythmic-location=? a b)
461   (and (rhythmic-location<=? a b)
462        (rhythmic-location<=? b a)))
463
464 (define-public (rhythmic-location->file-string a)
465   (ly:format "~a.~a.~a"
466              (car a)
467              (ly:moment-main-numerator (cdr a))
468              (ly:moment-main-denominator (cdr a))))
469
470 (define-public (rhythmic-location->string a)
471   (ly:format "bar ~a ~a"
472              (car a)
473              (ly:moment->string (cdr a))))
474
475 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
476 ;; break visibility
477
478 (define-public all-visible             #(#t #t #t))
479 (define-public begin-of-line-invisible #(#t #t #f))
480 (define-public center-invisible        #(#t #f #t))
481 (define-public end-of-line-invisible   #(#f #t #t))
482 (define-public begin-of-line-visible   #(#f #f #t))
483 (define-public center-visible          #(#f #t #f))
484 (define-public end-of-line-visible     #(#t #f #f))
485 (define-public all-invisible           #(#f #f #f))
486
487 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
488 ;; neighbor-interface routines
489
490
491 (define-public (shift-right-at-line-begin g)
492   "Shift an item to the right, but only at the start of the line."
493   (if (and (ly:item? g)
494            (equal? (ly:item-break-dir g) RIGHT))
495       (ly:grob-translate-axis! g 3.5 X)))
496
497 (define-public (pure-from-neighbor-interface::extra-spacing-height-at-beginning-of-line grob)
498   (if (= 1 (ly:item-break-dir grob))
499       (pure-from-neighbor-interface::extra-spacing-height grob)
500       (cons -0.1 0.1)))
501
502 (define-public (pure-from-neighbor-interface::extra-spacing-height grob)
503   (let* ((height (ly:grob-pure-height grob grob 0 INFINITY-INT))
504          (from-neighbors (interval-union
505                           height
506                           (ly:axis-group-interface::pure-height
507                            grob
508                            0
509                            INFINITY-INT))))
510     (coord-operation - from-neighbors height)))
511
512 ;; If there are neighbors, we place the height at their midpoint
513 ;; to avoid protrusion of this pure height out of the vertical
514 ;; axis group on either side.  This will minimize the impact of the
515 ;; grob on pure minimum translations.
516
517 ;; TODO - there is a double call to axis-group-interface::pure-height
518 ;; here and then in the extra-spacing-height function above. Can/should this
519 ;; be rolled into one?
520 (define-public (pure-from-neighbor-interface::pure-height grob beg end)
521   (let* ((height (ly:axis-group-interface::pure-height
522                   grob
523                   0
524                   INFINITY-INT))
525          (c (interval-center height)))
526     (if (interval-empty? height) empty-interval (cons c c))))
527
528 ;; Minimizes the impact of the height on vertical spacing while allowing
529 ;; it to appear in horizontal skylines of paper columns if necessary.
530 (define-public pure-from-neighbor-interface::height-if-pure
531   (ly:make-unpure-pure-container #f pure-from-neighbor-interface::pure-height))
532
533 (define-public (pure-from-neighbor-interface::account-for-span-bar grob)
534   (let* ((esh (pure-from-neighbor-interface::extra-spacing-height grob))
535          (hsb (ly:grob-property grob 'has-span-bar))
536          (ii (interval-intersection esh (cons -1.01 1.01))))
537     (if (pair? hsb)
538         (cons (car (if (and (car hsb)
539                             (ly:grob-property grob 'allow-span-bar))
540                        esh ii))
541               (cdr (if (cdr hsb) esh ii)))
542         ii)))
543
544 (define-public (pure-from-neighbor-interface::extra-spacing-height-including-staff grob)
545   (let ((esh (pure-from-neighbor-interface::extra-spacing-height grob))
546         (to-staff (coord-operation -
547                                    (interval-widen
548                                     '(0 . 0)
549                                     (ly:staff-symbol-staff-radius grob))
550                                    (ly:grob::stencil-height grob))))
551     (interval-union esh to-staff)))
552
553
554 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
555 ;; Tuplets
556
557 (define-public (tuplet-number::calc-direction grob)
558   (ly:tuplet-bracket::calc-direction (ly:grob-object grob 'bracket)))
559
560 (define-public (tuplet-number::calc-denominator-text grob)
561   (number->string (ly:event-property (event-cause grob) 'denominator)))
562
563 (define-public (tuplet-number::calc-fraction-text grob)
564   (let ((ev (event-cause grob)))
565
566     (format #f "~a:~a"
567             (ly:event-property ev 'denominator)
568             (ly:event-property ev 'numerator))))
569
570 ;; a formatter function, which is simply a wrapper around an existing
571 ;; tuplet formatter function. It takes the value returned by the given
572 ;; function and appends a note of given length.
573 (define ((tuplet-number::append-note-wrapper function note) grob)
574   (let ((txt (if function (function grob) #f)))
575
576     (if txt
577         (markup txt #:fontsize -5 #:note note UP)
578         (markup #:fontsize -5 #:note note UP))))
579 (export tuplet-number::append-note-wrapper)
580
581 ;; Print a tuplet denominator with a different number than the one derived from
582 ;; the actual tuplet fraction
583 (define ((tuplet-number::non-default-tuplet-denominator-text denominator)
584                 grob)
585   (number->string (if denominator
586                       denominator
587                       (ly:event-property (event-cause grob) 'denominator))))
588 (export tuplet-number::non-default-tuplet-denominator-text)
589
590 ;; Print a tuplet fraction with different numbers than the ones derived from
591 ;; the actual tuplet fraction
592 (define ((tuplet-number::non-default-tuplet-fraction-text
593                  denominator numerator) grob)
594   (let* ((ev (event-cause grob))
595          (den (if denominator denominator (ly:event-property ev 'denominator)))
596          (num (if numerator numerator (ly:event-property ev 'numerator))))
597
598     (format #f "~a:~a" den num)))
599 (export tuplet-number::non-default-tuplet-fraction-text)
600
601 ;; Print a tuplet fraction with note durations appended to the numerator and the
602 ;; denominator
603 (define ((tuplet-number::fraction-with-notes
604                  denominatornote numeratornote) grob)
605   (let* ((ev (event-cause grob))
606          (denominator (ly:event-property ev 'denominator))
607          (numerator (ly:event-property ev 'numerator)))
608
609     ((tuplet-number::non-default-fraction-with-notes
610       denominator denominatornote numerator numeratornote) grob)))
611 (export tuplet-number::fraction-with-notes)
612
613 ;; Print a tuplet fraction with note durations appended to the numerator and the
614 ;; denominator
615 (define ((tuplet-number::non-default-fraction-with-notes
616                  denominator denominatornote numerator numeratornote) grob)
617   (let* ((ev (event-cause grob))
618          (den (if denominator denominator (ly:event-property ev 'denominator)))
619          (num (if numerator numerator (ly:event-property ev 'numerator))))
620
621     (make-concat-markup (list
622                          (make-simple-markup (format #f "~a" den))
623                          (markup #:fontsize -5 #:note denominatornote UP)
624                          (make-simple-markup " : ")
625                          (make-simple-markup (format #f "~a" num))
626                          (markup #:fontsize -5 #:note numeratornote UP)))))
627 (export tuplet-number::non-default-fraction-with-notes)
628
629
630 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
631 ;; Color
632
633 (define-public (color? x)
634   (and (list? x)
635        (= 3 (length x))
636        (every number? x)
637        (every (lambda (y) (<= 0 y 1)) x)))
638
639 (define-public (rgb-color r g b) (list r g b))
640
641 ;; predefined colors
642 (define-public black       '(0.0 0.0 0.0))
643 (define-public white       '(1.0 1.0 1.0))
644 (define-public red         '(1.0 0.0 0.0))
645 (define-public green       '(0.0 1.0 0.0))
646 (define-public blue        '(0.0 0.0 1.0))
647 (define-public cyan        '(0.0 1.0 1.0))
648 (define-public magenta     '(1.0 0.0 1.0))
649 (define-public yellow      '(1.0 1.0 0.0))
650
651 (define-public grey        '(0.5 0.5 0.5))
652 (define-public darkred     '(0.5 0.0 0.0))
653 (define-public darkgreen   '(0.0 0.5 0.0))
654 (define-public darkblue    '(0.0 0.0 0.5))
655 (define-public darkcyan    '(0.0 0.5 0.5))
656 (define-public darkmagenta '(0.5 0.0 0.5))
657 (define-public darkyellow  '(0.5 0.5 0.0))
658
659
660 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
661 ;; key signature
662
663 (define-public (key-signature-interface::alteration-positions
664                 entry c0-position grob)
665   (let ((step (car entry))
666         (alter (cdr entry)))
667     (if (pair? step)
668         (list (+ (cdr step) (* (car step) 7) c0-position))
669         (let* ((c-position (modulo c0-position 7))
670                (positions
671                 (if (< alter 0)
672                     ;; See (flat|sharp)-positions in define-grob-properties.scm
673                     (ly:grob-property grob 'flat-positions '(3))
674                     (ly:grob-property grob 'sharp-positions '(3))))
675                (p (list-ref positions
676                             (if (< c-position (length positions))
677                                 c-position 0)))
678                (max-position (if (pair? p) (cdr p) p))
679                (min-position (if (pair? p) (car p) (- max-position 6)))
680                (first-position (+ (modulo (- (+ c-position step)
681                                              min-position)
682                                           7)
683                                   min-position)))
684           (define (prepend x l) (if (> x max-position)
685                                     l
686                                     (prepend (+ x 7) (cons x l))))
687           (prepend first-position '())))))
688
689 (define-public (key-signature-interface::alteration-position
690                 step alter c0-position)
691 ;; Deprecated.  Not a documented interface, and no longer used in LilyPond,
692 ;; but needed for a popular file, LilyJAZZ.ily for version 2.16
693   (if (pair? step)
694     (+ (cdr step) (* (car step) 7) c0-position)
695     (let* ((c-pos (modulo c0-position 7))
696            (hi (list-ref
697                  (if (< alter 0)
698                    '(2 3 4 2 1 2 1) ; position of highest flat
699                    '(4 5 4 2 3 2 3)); position of highest sharp
700                  c-pos)))
701       (- hi (modulo (- hi (+ c-pos step)) 7)))))
702
703 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
704 ;; annotations
705
706 (define-public (numbered-footnotes int)
707   (markup #:tiny (number->string (+ 1 int))))
708
709 (define-public (symbol-footnotes int)
710   (define (helper symbols out idx n)
711     (if (< n 1)
712         out
713         (helper symbols
714                 (string-append out (list-ref symbols idx))
715                 idx
716                 (- n 1))))
717   (markup #:tiny (helper '("*" "†" "‡" "§" "¶")
718                          ""
719                          (remainder int 5)
720                          (+ 1 (quotient int 5)))))
721
722 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
723 ;; accidentals
724
725 (define-public (accidental-interface::calc-alteration grob)
726   (ly:pitch-alteration (ly:event-property (event-cause grob) 'pitch)))
727
728 (define-public (accidental-interface::glyph-name grob)
729   (assoc-get (ly:grob-property grob 'alteration)
730              standard-alteration-glyph-name-alist))
731
732 (define-public accidental-interface::height
733   (ly:make-unpure-pure-container
734    ly:accidental-interface::height))
735
736 (define-public cancellation-glyph-name-alist
737   '((0 . "accidentals.natural")))
738
739 (define-public standard-alteration-glyph-name-alist
740   '(
741     ;; ordered for optimal performance.
742     (0 . "accidentals.natural")
743     (-1/2 . "accidentals.flat")
744     (1/2 . "accidentals.sharp")
745
746     (1 . "accidentals.doublesharp")
747     (-1 . "accidentals.flatflat")
748
749     (3/4 . "accidentals.sharp.slashslash.stemstemstem")
750     (1/4 . "accidentals.sharp.slashslash.stem")
751     (-1/4 . "accidentals.mirroredflat")
752     (-3/4 . "accidentals.mirroredflat.flat")))
753
754 ;; FIXME: standard vs default, alteration-FOO vs FOO-alteration
755 (define-public alteration-default-glyph-name-alist
756   standard-alteration-glyph-name-alist)
757
758 (define-public makam-alteration-glyph-name-alist
759   '((1 . "accidentals.doublesharp")
760     (8/9 . "accidentals.sharp.slashslashslash.stemstem")
761     (5/9 . "accidentals.sharp.slashslashslash.stem")
762     (4/9 . "accidentals.sharp")
763     (1/9 . "accidentals.sharp.slashslash.stem")
764     (0 . "accidentals.natural")
765     (-1/9 . "accidentals.mirroredflat")
766     (-4/9 . "accidentals.flat.slash")
767     (-5/9 . "accidentals.flat")
768     (-8/9 . "accidentals.flat.slashslash")
769     (-1 . "accidentals.flatflat")))
770
771 (define-public alteration-hufnagel-glyph-name-alist
772   '((-1/2 . "accidentals.hufnagelM1")
773     (0 . "accidentals.vaticana0")
774     (1/2 . "accidentals.mensural1")))
775
776 (define-public alteration-medicaea-glyph-name-alist
777   '((-1/2 . "accidentals.medicaeaM1")
778     (0 . "accidentals.vaticana0")
779     (1/2 . "accidentals.mensural1")))
780
781 (define-public alteration-vaticana-glyph-name-alist
782   '((-1/2 . "accidentals.vaticanaM1")
783     (0 . "accidentals.vaticana0")
784     (1/2 . "accidentals.mensural1")))
785
786 (define-public alteration-mensural-glyph-name-alist
787   '((-1/2 . "accidentals.mensuralM1")
788     (0 . "accidentals.vaticana0")
789     (1/2 . "accidentals.mensural1")))
790
791 (define-public alteration-kievan-glyph-name-alist
792   '((-1/2 . "accidentals.kievanM1")
793     (1/2 . "accidentals.kievan1")))
794
795 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
796 ;; * Pitch Trill Heads
797 ;; * Parentheses
798
799 (define-public (parentheses-item::calc-parenthesis-stencils grob)
800   (let* ((font (ly:grob-default-font grob))
801          (lp (ly:font-get-glyph font "accidentals.leftparen"))
802          (rp (ly:font-get-glyph font "accidentals.rightparen")))
803
804     (list lp rp)))
805
806 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
807   (let* ((parent (ly:grob-parent grob Y))
808          (y-extent (ly:grob-extent parent parent Y))
809          (half-thickness 0.05) ; should it be a property?
810          (width 0.5) ; should it be a property?
811          (angularity 1.5)  ; makes angle brackets
812          (white-padding 0.1) ; should it be a property?
813          (lp (ly:stencil-aligned-to
814               (ly:stencil-aligned-to
815                (make-parenthesis-stencil y-extent
816                                          half-thickness
817                                          (- width)
818                                          angularity)
819                Y CENTER)
820               X RIGHT))
821          (lp-x-extent
822           (interval-widen (ly:stencil-extent lp X) white-padding))
823          (rp (ly:stencil-aligned-to
824               (ly:stencil-aligned-to
825                (make-parenthesis-stencil y-extent
826                                          half-thickness
827                                          width
828                                          angularity)
829                Y CENTER)
830               X LEFT))
831          (rp-x-extent
832           (interval-widen (ly:stencil-extent rp X) white-padding)))
833     (set! lp (ly:make-stencil (ly:stencil-expr lp)
834                               lp-x-extent
835                               (ly:stencil-extent lp Y)))
836     (set! rp (ly:make-stencil (ly:stencil-expr rp)
837                               rp-x-extent
838                               (ly:stencil-extent rp Y)))
839     (list (stencil-whiteout lp)
840           (stencil-whiteout rp))))
841
842 (define (parenthesize-elements grob . rest)
843   (let* ((refp (if (null? rest)
844                    grob
845                    (car rest)))
846          (elts (ly:grob-object grob 'elements))
847          (x-ext (ly:relative-group-extent elts refp X))
848          (stencils (ly:grob-property grob 'stencils))
849          (lp (car stencils))
850          (rp (cadr stencils))
851          (padding (ly:grob-property grob 'padding 0.1)))
852
853     (ly:stencil-add
854      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
855      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))))
856
857
858 (define-public (parentheses-item::print me)
859   (let* ((elts (ly:grob-object me 'elements))
860          (y-ref (ly:grob-common-refpoint-of-array me elts Y))
861          (x-ref (ly:grob-common-refpoint-of-array me elts X))
862          (stencil (parenthesize-elements me x-ref))
863          (elt-y-ext (ly:relative-group-extent elts y-ref Y))
864          (y-center (interval-center elt-y-ext)))
865
866     (ly:stencil-translate
867      stencil
868      (cons
869       (- (ly:grob-relative-coordinate me x-ref X))
870       (- y-center (ly:grob-relative-coordinate me y-ref Y))))))
871
872
873 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
874 ;; offset callbacks
875
876 (define-public (pure-chain-offset-callback grob start end prev-offset)
877   "Sometimes, a chained offset callback is unpure and there is
878    no way to write a pure function that estimates its behavior.
879    In this case, we use a pure equivalent that will simply pass
880    the previous calculated offset value."
881   prev-offset)
882
883 (define-public (scale-by-font-size x)
884   (ly:make-unpure-pure-container
885     (lambda (grob)
886       (* x (magstep (ly:grob-property grob 'font-size 0))))))
887
888 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
889 ;;
890
891 (define-public (chain-grob-member-functions grob value . funcs)
892   (for-each
893    (lambda (func)
894      (set! value (func grob value)))
895    funcs)
896
897   value)
898
899
900 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
901 ;; falls/doits
902
903 (define-public (bend::print spanner)
904   (define (close  a b)
905     (< (abs (- a b)) 0.01))
906
907   (let* ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
908          (left-span (ly:spanner-bound spanner LEFT))
909          (dots (if (and (grob::has-interface left-span 'note-head-interface)
910                         (ly:grob? (ly:grob-object left-span 'dot)))
911                    (ly:grob-object left-span 'dot) #f))
912
913          (right-span (ly:spanner-bound spanner RIGHT))
914          (thickness (* (ly:grob-property spanner 'thickness)
915                        (ly:output-def-lookup (ly:grob-layout spanner)
916                                              'line-thickness)))
917          (padding (ly:grob-property spanner 'padding 0.5))
918          (common (ly:grob-common-refpoint right-span
919                                           (ly:grob-common-refpoint spanner
920                                                                    left-span X)
921                                           X))
922          (common-y (ly:grob-common-refpoint spanner left-span Y))
923          (minimum-length (ly:grob-property spanner 'minimum-length 0.5))
924
925          (left-x (+ padding
926                     (max
927                      (interval-end (ly:generic-bound-extent
928                                     left-span common))
929                      (if
930                       (and dots
931                            (close
932                             (ly:grob-relative-coordinate dots common-y Y)
933                             (ly:grob-relative-coordinate spanner common-y Y)))
934                       (interval-end
935                        (ly:grob-robust-relative-extent dots common X))
936                       (- INFINITY-INT)))))
937          (right-x (max (- (interval-start
938                            (ly:generic-bound-extent right-span common))
939                           padding)
940                        (+ left-x minimum-length)))
941          (self-x (ly:grob-relative-coordinate spanner common X))
942          (dx (- right-x left-x))
943          (exp (list 'path thickness
944                     `(quote
945                       (rmoveto
946                        ,(- left-x self-x) 0
947
948                        rcurveto
949                        ,(/ dx 3)
950                        0
951                        ,dx ,(* 0.66 delta-y)
952                        ,dx ,delta-y)))))
953
954     (ly:make-stencil
955      exp
956      (cons (- left-x self-x) (- right-x self-x))
957      (cons (min 0 delta-y)
958            (max 0 delta-y)))))
959
960
961 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
962 ;; grace spacing
963
964 (define-public (grace-spacing::calc-shortest-duration grob)
965   (let* ((cols (ly:grob-object grob 'columns))
966          (get-difference
967           (lambda (idx)
968             (ly:moment-sub (ly:grob-property
969                             (ly:grob-array-ref cols (1+ idx)) 'when)
970                            (ly:grob-property
971                             (ly:grob-array-ref cols idx) 'when))))
972
973          (moment-min (lambda (x y)
974                        (cond
975                         ((and x y)
976                          (if (ly:moment<? x y)
977                              x
978                              y))
979                         (x x)
980                         (y y)))))
981
982     (fold moment-min #f (map get-difference
983                              (iota (1- (ly:grob-array-length cols)))))))
984
985
986 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
987 ;; fingering
988
989 (define-public (fingering::calc-text grob)
990   (let ((event (event-cause grob)))
991     (or (ly:event-property event 'text #f)
992         (number->string (ly:event-property event 'digit) 10))))
993
994 (define-public (string-number::calc-text grob)
995   (let ((event (event-cause grob)))
996     (or (ly:event-property event 'text #f)
997         (number->string (ly:event-property event 'string-number) 10))))
998
999 (define-public (stroke-finger::calc-text grob)
1000   (let ((event (event-cause grob)))
1001     (or (ly:event-property event 'text #f)
1002         (vector-ref (ly:grob-property grob 'digit-names)
1003                     (1- (max 1
1004                              (min 5 (ly:event-property event 'digit))))))))
1005
1006
1007 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1008 ;; dynamics
1009
1010 (define-public (hairpin::calc-grow-direction grob)
1011   (if (ly:in-event-class? (event-cause grob) 'decrescendo-event)
1012       START
1013       STOP))
1014
1015 (define-public (dynamic-text-spanner::before-line-breaking grob)
1016   "Monitor left bound of @code{DynamicTextSpanner} for absolute dynamics.
1017 If found, ensure @code{DynamicText} does not collide with spanner text by
1018 changing @code{'attach-dir} and @code{'padding}.  Reads the
1019 @code{'right-padding} property of @code{DynamicText} to fine tune space
1020 between the two text elements."
1021   (let ((left-bound (ly:spanner-bound grob LEFT)))
1022     (if (grob::has-interface left-bound 'dynamic-text-interface)
1023         (let* ((details (ly:grob-property grob 'bound-details))
1024                (left-details (ly:assoc-get 'left details))
1025                (my-padding (ly:assoc-get 'padding left-details))
1026                (script-padding (ly:grob-property left-bound 'right-padding 0)))
1027
1028           (and (number? my-padding)
1029                (ly:grob-set-nested-property! grob
1030                                              '(bound-details left attach-dir)
1031                                              RIGHT)
1032                (ly:grob-set-nested-property! grob
1033                                              '(bound-details left padding)
1034                                              (+ my-padding script-padding)))))))
1035
1036 (define ((elbowed-hairpin coords mirrored?) grob)
1037   "Create hairpin based on a list of @var{coords} in @code{(cons x y)}
1038 form.  @code{x} is the portion of the width consumed for a given line
1039 and @code{y} is the portion of the height.  For example,
1040 @code{'((0.3 . 0.7) (0.8 . 0.9) (1.0 . 1.0))} means that at the point
1041 where the hairpin has consumed 30% of its width, it must
1042 be at 70% of its height.  Once it is to 80% width, it
1043 must be at 90% height.  It finishes at
1044 100% width and 100% height.  @var{mirrored?} indicates if the hairpin
1045 is mirrored over the Y-axis or if just the upper part is drawn.
1046 Returns a function that accepts a hairpin grob as an argument
1047 and draws the stencil based on its coordinates.
1048 @lilypond[verbatim,quote]
1049 #(define simple-hairpin
1050   (elbowed-hairpin '((1.0 . 1.0)) #t))
1051
1052 \\relative c' {
1053   \\override Hairpin #'stencil = #simple-hairpin
1054   a\\p\\< a a a\\f
1055 }
1056 @end lilypond
1057 "
1058   (define (pair-to-list pair)
1059     (list (car pair) (cdr pair)))
1060   (define (normalize-coords goods x y)
1061     (map
1062      (lambda (coord)
1063        (cons (* x (car coord)) (* y (cdr coord))))
1064      goods))
1065   (define (my-c-p-s points thick decresc?)
1066     (make-connected-path-stencil
1067      points
1068      thick
1069      (if decresc? -1.0 1.0)
1070      1.0
1071      #f
1072      #f))
1073   ;; outer let to trigger suicide
1074   (let ((sten (ly:hairpin::print grob)))
1075     (if (grob::is-live? grob)
1076         (let* ((decresc? (eq? (ly:grob-property grob 'grow-direction) LEFT))
1077                (thick (ly:grob-property grob 'thickness 0.1))
1078                (thick (* thick (layout-line-thickness grob)))
1079                (xex (ly:stencil-extent sten X))
1080                (lenx (interval-length xex))
1081                (yex (ly:stencil-extent sten Y))
1082                (leny (interval-length yex))
1083                (xtrans (+ (car xex) (if decresc? lenx 0)))
1084                (ytrans (car yex))
1085                (uplist (map pair-to-list
1086                             (normalize-coords coords lenx (/ leny 2))))
1087                (downlist (map pair-to-list
1088                               (normalize-coords coords lenx (/ leny -2)))))
1089           (ly:stencil-translate
1090            (ly:stencil-add
1091             (my-c-p-s uplist thick decresc?)
1092             (if mirrored? (my-c-p-s downlist thick decresc?) empty-stencil))
1093            (cons xtrans ytrans)))
1094         '())))
1095 (export elbowed-hairpin)
1096
1097 (define-public flared-hairpin
1098   (elbowed-hairpin '((0.95 . 0.4) (1.0 . 1.0)) #t))
1099
1100 (define-public constante-hairpin
1101   (elbowed-hairpin '((1.0 . 0.0) (1.0 . 1.0)) #f))
1102
1103 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1104 ;; lyrics
1105
1106 (define-public (lyric-text::print grob)
1107   "Allow interpretation of tildes as lyric tieing marks."
1108
1109   (let ((text (ly:grob-property grob 'text)))
1110
1111     (grob-interpret-markup grob (if (string? text)
1112                                     (make-tied-lyric-markup text)
1113                                     text))))
1114
1115 (define ((grob::calc-property-by-copy prop) grob)
1116   (ly:event-property (event-cause grob) prop))
1117 (export grob::calc-property-by-copy)
1118
1119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1120 ;; general inheritance
1121
1122 (define ((grob::inherit-parent-property axis property . default) grob)
1123   "@var{grob} callback generator for inheriting a @var{property} from
1124 an @var{axis} parent, defaulting to @var{default} if there is no
1125 parent or the parent has no setting."
1126   (let ((parent (ly:grob-parent grob axis)))
1127     (cond
1128      ((ly:grob? parent)
1129       (apply ly:grob-property parent property default))
1130      ((pair? default) (car default))
1131      (else '()))))
1132 (export grob::inherit-parent-property)
1133
1134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1135 ;; fret boards
1136
1137 (define-public (fret-board::calc-stencil grob)
1138   (grob-interpret-markup
1139    grob
1140    (make-fret-diagram-verbose-markup
1141     (ly:grob-property grob 'dot-placement-list))))
1142
1143
1144 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1145 ;; slurs
1146
1147 (define-public slur::height
1148   (ly:make-unpure-pure-container
1149    ly:slur::height
1150    ly:slur::pure-height))
1151
1152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1153 ;; scripts
1154
1155 (define-public (script-interface::calc-x-offset grob)
1156   (ly:grob-property grob 'positioning-done)
1157   (let* ((shift (ly:grob-property grob 'toward-stem-shift 0.0))
1158          (note-head-location
1159           (ly:self-alignment-interface::aligned-on-x-parent grob))
1160          (note-head-grob (ly:grob-parent grob X))
1161          (stem-grob (ly:grob-object note-head-grob 'stem)))
1162
1163     (+ note-head-location
1164        ;; If the property 'toward-stem-shift is defined and the script
1165        ;; has the same direction as the stem, move the script accordingly.
1166        ;; Since scripts can also be over skips, we need to check whether
1167        ;; the grob has a stem at all.
1168        (if (ly:grob? stem-grob)
1169            (let ((dir1 (ly:grob-property grob 'direction))
1170                  (dir2 (ly:grob-property stem-grob 'direction)))
1171              (if (equal? dir1 dir2)
1172                  (let* ((common-refp (ly:grob-common-refpoint grob stem-grob X))
1173                         (stem-location
1174                          (ly:grob-relative-coordinate stem-grob common-refp X)))
1175                    (* shift (- stem-location note-head-location)))
1176                  0.0))
1177            0.0))))
1178
1179
1180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1181 ;; instrument names
1182
1183 (define-public (system-start-text::print grob)
1184   (let* ((left-bound (ly:spanner-bound grob LEFT))
1185          (left-mom (ly:grob-property left-bound 'when))
1186          (name (if (moment<=? left-mom ZERO-MOMENT)
1187                    (ly:grob-property grob 'long-text)
1188                    (ly:grob-property grob 'text))))
1189
1190     (if (and (markup? name)
1191              (!= (ly:item-break-dir left-bound) CENTER))
1192
1193         (grob-interpret-markup grob name)
1194         (ly:grob-suicide! grob))))
1195
1196 (define-public (system-start-text::calc-x-offset grob)
1197   (let* ((left-bound (ly:spanner-bound grob LEFT))
1198          (left-mom (ly:grob-property left-bound 'when))
1199          (layout (ly:grob-layout grob))
1200          (indent (ly:output-def-lookup layout
1201                                        (if (moment<=? left-mom ZERO-MOMENT)
1202                                            'indent
1203                                            'short-indent)
1204                                        0.0))
1205          (system (ly:grob-system grob))
1206          (my-extent (ly:grob-extent grob system X))
1207          (elements (ly:grob-object system 'elements))
1208          (common (ly:grob-common-refpoint-of-array system elements X))
1209          (total-ext empty-interval)
1210          (align-x (ly:grob-property grob 'self-alignment-X 0))
1211          (padding (min 0 (- (interval-length my-extent) indent)))
1212          (right-padding (- padding
1213                            (/ (* padding (1+ align-x)) 2))))
1214
1215     ;; compensate for the variation in delimiter extents by
1216     ;; calculating an X-offset correction based on united extents
1217     ;; of all delimiters in this system
1218     (let unite-delims ((l (ly:grob-array-length elements)))
1219       (if (> l 0)
1220           (let ((elt (ly:grob-array-ref elements (1- l))))
1221
1222             (if (grob::has-interface elt 'system-start-delimiter-interface)
1223                 (let ((dims (ly:grob-extent elt common X)))
1224                   (if (interval-sane? dims)
1225                       (set! total-ext (interval-union total-ext dims)))))
1226             (unite-delims (1- l)))))
1227
1228     (+
1229      (ly:side-position-interface::x-aligned-side grob)
1230      right-padding
1231      (- (interval-length total-ext)))))
1232
1233 (define-public (system-start-text::calc-y-offset grob)
1234
1235   (define (live-elements-list me)
1236     (let ((elements (ly:grob-object me 'elements)))
1237
1238       (filter! grob::is-live?
1239                (ly:grob-array->list elements))))
1240
1241   (let* ((left-bound (ly:spanner-bound grob LEFT))
1242          (live-elts (live-elements-list grob))
1243          (system (ly:grob-system grob))
1244          (extent empty-interval))
1245
1246     (if (and (pair? live-elts)
1247              (interval-sane? (ly:grob-extent grob system Y)))
1248         (let get-extent ((lst live-elts))
1249           (if (pair? lst)
1250               (let ((axis-group (car lst)))
1251
1252                 (if (and (ly:spanner? axis-group)
1253                          (equal? (ly:spanner-bound axis-group LEFT)
1254                                  left-bound))
1255                     (set! extent (add-point extent
1256                                             (ly:grob-relative-coordinate
1257                                              axis-group system Y))))
1258                 (get-extent (cdr lst)))))
1259         ;; no live axis group(s) for this instrument name -> remove from system
1260         (ly:grob-suicide! grob))
1261
1262     (+
1263      (ly:self-alignment-interface::y-aligned-on-self grob)
1264      (interval-center extent))))
1265
1266
1267 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1268 ;; axis group interface
1269
1270 (define-public axis-group-interface::height
1271   (ly:make-unpure-pure-container
1272    ly:axis-group-interface::height
1273    ly:axis-group-interface::pure-height))
1274
1275 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1276 ;; ambitus
1277
1278 ;; Calculate the gaps between ambitus heads and ends of ambitus line.
1279 ;; Start by determining desired length of the ambitus line (based on
1280 ;; length-fraction property), calc gap from that and make sure that
1281 ;; it doesn't exceed maximum allowed value.
1282
1283 (define-public (ambitus-line::calc-gap grob)
1284   (let ((heads (ly:grob-object grob 'note-heads)))
1285
1286   (if (and (ly:grob-array? heads)
1287              (= (ly:grob-array-length heads) 2))
1288       (let* ((common (ly:grob-common-refpoint-of-array grob heads Y))
1289               (head-down (ly:grob-array-ref heads 0))
1290               (head-up (ly:grob-array-ref heads 1))
1291               (fraction (ly:grob-property grob 'length-fraction 0.7))
1292               (max-gap (ly:grob-property grob 'maximum-gap 0.45))
1293               ;; distance between noteheads:
1294               (distance (- (interval-start (ly:grob-extent head-up common Y))
1295                           (interval-end (ly:grob-extent head-down common Y))))
1296               (gap (* 0.5 distance (- 1 fraction))))
1297
1298          (min gap max-gap))
1299       0)))
1300
1301 ;; Print a line connecting ambitus heads:
1302
1303 (define-public (ambitus::print grob)
1304   (let ((heads (ly:grob-object grob 'note-heads)))
1305
1306     (if (and (ly:grob-array? heads)
1307              (= (ly:grob-array-length heads) 2))
1308         (let* ((common (ly:grob-common-refpoint-of-array grob heads Y))
1309                (head-down (ly:grob-array-ref heads 0))
1310                (head-up (ly:grob-array-ref heads 1))
1311                ;; The value used when 'gap' property cannot be read is small
1312                ;; to make sure that ambitus of a fifth will have a visible line.
1313                (gap (ly:grob-property grob 'gap 0.25))
1314                (point-min (+ (interval-end (ly:grob-extent head-down common Y))
1315                              gap))
1316                (point-max (- (interval-start (ly:grob-extent head-up common Y))
1317                              gap)))
1318
1319           (if (< (+ point-min 0.1) point-max) ; don't print lines shorter than 0.1ss
1320               (let* ((layout (ly:grob-layout grob))
1321                      (line-thick (ly:output-def-lookup layout 'line-thickness))
1322                      (blot (ly:output-def-lookup layout 'blot-diameter))
1323                      (grob-thick (ly:grob-property grob 'thickness 2))
1324                      (width (* line-thick grob-thick))
1325                      (x-ext (symmetric-interval (/ width 2)))
1326                      (y-ext (cons point-min point-max))
1327                      (line (ly:round-filled-box x-ext y-ext blot))
1328                      (y-coord (ly:grob-relative-coordinate grob common Y)))
1329
1330                 (ly:stencil-translate-axis line (- y-coord) Y))
1331               empty-stencil))
1332         (begin
1333           (ly:grob-suicide! grob)
1334           (list)))))
1335
1336 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1337 ;;  laissez-vibrer tie
1338 ;;
1339 ;;  needed so we can make laissez-vibrer a pure print
1340 ;;
1341 (define-public (laissez-vibrer::print grob)
1342   (ly:tie::print grob))
1343
1344 (define-public (semi-tie::calc-cross-staff grob)
1345   (let* ((note-head (ly:grob-object grob 'note-head))
1346          (stem (ly:grob-object note-head 'stem)))
1347     (and (ly:grob? stem)
1348          (ly:grob-property stem 'cross-staff #f))))
1349
1350 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1351 ;; volta-bracket
1352
1353 (define-public (volta-bracket-interface::pure-height grob start end)
1354   (let ((edge-height (ly:grob-property grob 'edge-height)))
1355     (if (number-pair? edge-height)
1356         (let ((smaller (min (car edge-height) (cdr edge-height)))
1357               (larger (max (car edge-height) (cdr edge-height))))
1358           (interval-union '(0 . 0) (cons smaller larger)))
1359         '(0 . 0))))