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