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