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