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