1 ;;;; output-lib.scm -- implement Scheme output helper functions
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
11 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 (define-public (grob::has-interface grob iface)
14 (memq iface (ly:grob-interfaces grob)))
16 (define-public (make-stencil-boxer thickness padding callback)
18 "Return function that adds a box around the grob passed as argument."
21 (box-stencil (callback grob) thickness padding)))
23 (define-public (make-stencil-circler thickness padding callback)
24 "Return function that adds a circle around the grob passed as argument."
26 (lambda (grob) (circle-stencil (callback grob) thickness padding)))
28 (define-public (print-circled-text-callback grob)
29 (grob-interpret-markup grob (make-circle-markup
30 (ly:grob-property grob 'text))
33 (define-public (event-cause grob)
35 ((cause (ly:grob-property grob 'cause)))
38 ((ly:stream-event? cause) cause)
39 ((ly:grob? cause) (event-cause cause))
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;; The TabNoteHead tablatureFormat callback.
46 ;; Compute the text grob-property
47 (define-public (fret-number-tablature-format string
50 ((tuning (ly:context-property context 'stringTunings))
51 (pitch (ly:event-property event 'pitch))
56 (eq? 'harmonic-event (ly:event-property ev 'class)))
57 (ly:event-property event 'articulations)))))
64 (- (ly:pitch-semitones pitch)
66 ;; remove 1 because list index starts at 0 and guitar string at 1.
70 ;; The 5-string banjo has got a extra string, the fifth (duh), wich
71 ;; starts at the fifth fret on the neck. Frets on the fifth string
72 ;; are referred to relative to the other frets:
73 ;; the "first fret" on the fifth string is really the sixth fret
75 ;; We solve this by defining a new fret-number-tablature function:
76 (define-public (fret-number-tablature-format-banjo string
79 ((tuning (ly:context-property context 'stringTunings))
80 (pitch (ly:event-property event 'pitch))
84 (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (- string 1)))))
86 ((and (> fret 0) (= string 5))
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96 (define-public (stem::calc-duration-log grob)
98 (ly:event-property (event-cause grob) 'duration)))
100 (define-public (note-head::calc-duration-log grob)
103 (ly:event-property (event-cause grob) 'duration))))
105 (define-public (dots::calc-dot-count grob)
106 (ly:duration-dot-count
107 (ly:event-property (event-cause grob) 'duration)))
109 (define-public (dots::calc-staff-position grob)
111 ((head (ly:grob-parent grob Y))
112 (log (ly:grob-property head 'duration-log)))
115 ((or (not (grob::has-interface head 'rest-interface))
116 (not (integer? log))) 0)
124 (define (note-head::calc-tablature-stem-attachment grob)
130 (define-public (note-head::calc-glyph-name grob)
132 ((style (ly:grob-property grob 'style))
133 (log (min 2 (ly:grob-property grob 'duration-log))))
136 ;; "default" style is directly handled in note-head.cc as a
137 ;; special case (HW says, mainly for performance reasons).
138 ;; Therefore, style "default" does not appear in this case
140 ((xcircle) "2xcircle")
141 ((harmonic) "0harmonic")
142 ((harmonic-black) "2harmonic")
143 ((harmonic-mixed) (if (<= log 1) "0harmonic"
146 ;; Oops, I actually would not call this "baroque", but, for
147 ;; backwards compatibility to 1.4, this is supposed to take
148 ;; brevis, longa and maxima from the neo-mensural font and all
149 ;; other note heads from the default font. -- jr
151 (string-append (number->string log) "neomensural")
152 (number->string log)))
154 (string-append (number->string log) (symbol->string style)))
157 (string-append (number->string log) "mensural")
158 (string-append (number->string log) (symbol->string style))))
160 (string-append (number->string log) (symbol->string style)))
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)))))))
167 ;; TODO junk completely?
168 (define (note-head-style->attachment-coordinates grob axis)
169 "Return pair (X . Y), containing multipliers for the note head
170 bounding box, where to attach the stem. e.g.: X==0 means horizontally
171 centered, X==1 is at the right, X == -1 is at the left."
176 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
179 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
181 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
183 (define-public ((set-bar-number-visibility n) tr)
184 (let* ((bn (ly:context-property tr 'currentBarNumber)))
185 (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
187 (define-public (first-bar-number-invisible barnum) (> barnum 1))
189 (define-public (all-bar-numbers-visible barnum) #t)
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
192 ;; percent repeat counters
194 (define-public ((every-nth-repeat-count-visible n) count context) (= 0 (modulo count n)))
196 (define-public (all-repeat-counts-visible count context) #t)
198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
201 (define-public all-visible #(#t #t #t))
202 (define-public begin-of-line-invisible #(#t #t #f))
203 (define-public center-invisible #(#t #f #t))
204 (define-public end-of-line-invisible #(#f #t #t))
205 (define-public begin-of-line-visible #(#f #f #t))
206 (define-public center-visible #(#f #t #f))
207 (define-public end-of-line-visible #(#t #f #f))
208 (define-public all-invisible #(#f #f #f))
210 (define-public spanbar-begin-of-line-invisible
213 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
217 ;; How should a bar line behave at a break?
218 (define bar-glyph-alist
219 '((":|:" . (":|" . "|:"))
220 (":|.|:" . (":|" . "|:"))
221 (":|.:" . (":|" . "|:"))
222 ("||:" . ("||" . "|:"))
223 ("dashed" . ("dashed" . '()))
225 ("||:" . ("||" . "|:"))
227 ("|:" . ("|" . "|:"))
230 ;; hmm... should we end with a bar line here?
231 (".|" . ("|" . ".|"))
234 (".|." . (".|." . ()))
235 ("|.|" . ("|.|" . ()))
240 ("empty" . (() . ()))
241 ("brace" . (() . "brace"))
242 ("bracket" . (() . "bracket"))
245 (define-public (bar-line::calc-glyph-name grob)
247 (glyph (ly:grob-property grob 'glyph))
248 (dir (ly:item-break-dir grob))
249 (result (assoc glyph bar-glyph-alist))
250 (glyph-name (if (= dir CENTER)
252 (if (and result (string? (index-cell (cdr result) dir)))
253 (index-cell (cdr result) dir)
258 (define-public (bar-line::calc-break-visibility grob)
259 (let* ((glyph (ly:grob-property grob 'glyph))
260 (result (assoc glyph bar-glyph-alist)))
262 (vector (string? (cadr result)) #t (string? (cddr result)))
266 (define-public (shift-right-at-line-begin g)
267 "Shift an item to the right, but only at the start of the line."
268 (if (and (ly:item? g)
269 (equal? (ly:item-break-dir g) RIGHT))
270 (ly:grob-translate-axis! g 3.5 X)))
273 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
276 (define-public (tuplet-number::calc-denominator-text grob)
277 (number->string (ly:event-property (event-cause grob) 'denominator)))
279 (define-public (tuplet-number::calc-fraction-text grob)
281 ((ev (event-cause grob)))
284 (ly:event-property ev 'denominator)
285 (ly:event-property ev 'numerator))))
288 ; a formatter function, which is simply a wrapper around an existing
289 ; tuplet formatter function. It takes the value returned by the given
290 ; function and appends a note of given length.
291 (define-public ((tuplet-number::append-note-wrapper function note) grob)
292 (let* ((txt (if function (function grob) #f)))
294 (markup txt #:fontsize -5 #:note note UP)
295 (markup #:fontsize -5 #:note note UP))))
297 ; Print a tuplet denominator with a different number than the one derived from
298 ; the actual tuplet fraction
299 (define-public ((tuplet-number::non-default-tuplet-denominator-text denominator) grob)
300 (number->string (if denominator
302 (ly:event-property (event-cause grob) 'denominator))))
304 ; Print a tuplet fraction with different numbers than the ones derived from
305 ; the actual tuplet fraction
306 (define-public ((tuplet-number::non-default-tuplet-fraction-text denominator numerator) grob)
307 (let* ((ev (event-cause grob))
308 (den (if denominator denominator (ly:event-property ev 'denominator)))
309 (num (if numerator numerator (ly:event-property ev 'numerator))))
310 (format "~a:~a" den num)))
312 ; Print a tuplet fraction with note durations appended to the numerator and the
314 (define-public ((tuplet-number::fraction-with-notes denominatornote numeratornote) grob)
315 (let* ((ev (event-cause grob))
316 (denominator (ly:event-property ev 'denominator))
317 (numerator (ly:event-property ev 'numerator)))
318 ((tuplet-number::non-default-fraction-with-notes denominator denominatornote numerator numeratornote) grob)))
320 ; Print a tuplet fraction with note durations appended to the numerator and the
322 (define-public ((tuplet-number::non-default-fraction-with-notes denominator denominatornote numerator numeratornote) grob)
323 (let* ((ev (event-cause grob))
324 (den (if denominator denominator (ly:event-property ev 'denominator)))
325 (num (if numerator numerator (ly:event-property ev 'numerator))))
326 (make-concat-markup (list
327 (make-simple-markup (format "~a" den))
328 (markup #:fontsize -5 #:note denominatornote UP)
329 (make-simple-markup " : ")
330 (make-simple-markup (format "~a" num))
331 (markup #:fontsize -5 #:note numeratornote UP)))))
334 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
337 (define-public color? list?)
338 (define-public (rgb-color r g b) (list r g b))
341 (define-public black '(0.0 0.0 0.0))
342 (define-public white '(1.0 1.0 1.0))
343 (define-public red '(1.0 0.0 0.0))
344 (define-public green '(0.0 1.0 0.0))
345 (define-public blue '(0.0 0.0 1.0))
346 (define-public cyan '(0.0 1.0 1.0))
347 (define-public magenta '(1.0 0.0 1.0))
348 (define-public yellow '(1.0 1.0 0.0))
350 (define-public grey '(0.5 0.5 0.5))
351 (define-public darkred '(0.5 0.0 0.0))
352 (define-public darkgreen '(0.0 0.5 0.0))
353 (define-public darkblue '(0.0 0.0 0.5))
354 (define-public darkcyan '(0.0 0.5 0.5))
355 (define-public darkmagenta '(0.5 0.0 0.5))
356 (define-public darkyellow '(0.5 0.5 0.0))
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
362 (define-public (key-signature-interface::alteration-position step alter c0-position)
363 ;; TODO: memoize - this is mostly constant.
365 ;; fes, ges, as and bes typeset in lower octave
366 (define FLAT_TOP_PITCH 2)
368 ;; ais and bis typeset in lower octave
369 (define SHARP_TOP_PITCH 4)
372 (+ (cdr step) (* (car step) 7) c0-position)
374 ((from-bottom-pos (modulo (+ 4 49 c0-position) 7))
376 (c0 (- from-bottom-pos 4)))
379 (or (and (< alter 0) (or (> p FLAT_TOP_PITCH) (> (+ p c0) 4)) (> (+ p c0) 1))
380 (and (> alter 0) (or (> p SHARP_TOP_PITCH) (> (+ p c0) 5)) (> (+ p c0) 2))
383 ;; Typeset below c_position
386 ;; Provide for the four cases in which there's a glitch
387 ;; it's a hack, but probably not worth
388 ;; the effort of finding a nicer solution.
391 ((and (= c0 2) (= p 3) (> alter 0))
393 ((and (= c0 -3) (= p -1) (> alter 0))
395 ((and (= c0 -4) (= p -1) (< alter 0))
397 ((and (= c0 -2) (= p -3) (< alter 0))
403 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
406 (define-public (accidental-interface::calc-alteration grob)
407 (ly:pitch-alteration (ly:event-property (event-cause grob) 'pitch)))
410 (define-public cancellation-glyph-name-alist
411 '((0 . "accidentals.natural")))
413 (define-public standard-alteration-glyph-name-alist
415 ;; ordered for optimal performance.
416 (0 . "accidentals.natural")
417 (-1/2 . "accidentals.flat")
418 (1/2 . "accidentals.sharp")
420 (1 . "accidentals.doublesharp")
421 (-1 . "accidentals.flatflat")
423 (3/4 . "accidentals.sharp.slashslash.stemstemstem")
424 (1/4 . "accidentals.sharp.slashslash.stem")
425 (-1/4 . "accidentals.mirroredflat")
426 (-3/4 . "accidentals.mirroredflat.flat")
429 ;; FIXME: standard vs default, alteration-FOO vs FOO-alteration
430 (define-public alteration-default-glyph-name-alist standard-alteration-glyph-name-alist)
432 (define-public makam-alteration-glyph-name-alist
433 '((1 . "accidentals.doublesharp")
434 (8/9 . "accidentals.sharp.slashslashslash.stemstem")
435 (5/9 . "accidentals.sharp.slashslashslash.stem")
436 (4/9 . "accidentals.sharp")
437 (1/9 . "accidentals.sharp.slashslash.stem")
438 (0 . "accidentals.natural")
439 (-1/9 . "accidentals.mirroredflat")
440 (-4/9 . "accidentals.flat.slash")
441 (-5/9 . "accidentals.flat")
442 (-8/9 . "accidentals.flat.slashslash")
443 (-1 . "accidentals.flatflat")
446 (define-public alteration-hufnagel-glyph-name-alist
447 '((-1/2 . "accidentals.hufnagelM1")
448 (0 . "accidentals.vaticana0")
449 (1/2 . "accidentals.mensural1")))
451 (define-public alteration-medicaea-glyph-name-alist
452 '((-1/2 . "accidentals.medicaeaM1")
453 (0 . "accidentals.vaticana0")
454 (1/2 . "accidentals.mensural1")))
456 (define-public alteration-vaticana-glyph-name-alist
457 '((-1/2 . "accidentals.vaticanaM1")
458 (0 . "accidentals.vaticana0")
459 (1/2 . "accidentals.mensural1")))
461 (define-public alteration-mensural-glyph-name-alist
462 '((-1/2 . "accidentals.mensuralM1")
463 (0 . "accidentals.vaticana0")
464 (1/2 . "accidentals.mensural1")))
467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
468 ;; * Pitch Trill Heads
471 (define-public (parentheses-item::calc-parenthesis-stencils grob)
472 (let* ((font (ly:grob-default-font grob))
473 (lp (ly:font-get-glyph font "accidentals.leftparen"))
474 (rp (ly:font-get-glyph font "accidentals.rightparen")))
479 (define-public (grob-interpret-markup grob text)
481 ((layout (ly:grob-layout grob))
482 (defs (ly:output-def-lookup layout 'text-font-defaults))
483 (props (ly:grob-alist-chain grob defs)))
485 (ly:text-interface::interpret-markup
488 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
490 (font (ly:grob-default-font grob))
491 (lp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-interpret-markup grob (ly:wide-char->utf-8 #x2329))
493 (rp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-interpret-markup grob (ly:wide-char->utf-8 #x232A))
497 (list (stencil-whiteout lp)
498 (stencil-whiteout rp))))
500 (define (parenthesize-elements grob . rest)
502 ((refp (if (null? rest)
505 (elts (ly:grob-object grob 'elements))
506 (x-ext (ly:relative-group-extent elts refp X))
507 (stencils (ly:grob-property grob 'stencils))
510 (padding (ly:grob-property grob 'padding 0.1)))
513 (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
514 (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
518 (define (parentheses-item::print me)
520 ((elts (ly:grob-object me 'elements))
521 (y-ref (ly:grob-common-refpoint-of-array me elts Y))
522 (x-ref (ly:grob-common-refpoint-of-array me elts X))
523 (stencil (parenthesize-elements me x-ref))
524 (elt-y-ext (ly:relative-group-extent elts y-ref Y))
525 (y-center (interval-center elt-y-ext)))
527 (ly:stencil-translate
531 (ly:grob-relative-coordinate me x-ref X))
534 (ly:grob-relative-coordinate me y-ref Y))))
539 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
542 (define-public (chain-grob-member-functions grob value . funcs)
545 (set! value (func grob value)))
550 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
553 (define-public (bend::print spanner)
555 (< (abs (- a b)) 0.01))
558 ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
559 (left-span (ly:spanner-bound spanner LEFT))
560 (dots (if (and (grob::has-interface left-span 'note-head-interface)
561 (ly:grob? (ly:grob-object left-span 'dot)))
562 (ly:grob-object left-span 'dot) #f))
564 (right-span (ly:spanner-bound spanner RIGHT))
565 (thickness (* (ly:grob-property spanner 'thickness)
566 (ly:output-def-lookup (ly:grob-layout spanner)
568 (padding (ly:grob-property spanner 'padding 0.5))
569 (common (ly:grob-common-refpoint right-span
570 (ly:grob-common-refpoint spanner
573 (common-y (ly:grob-common-refpoint spanner left-span Y))
574 (minimum-length (ly:grob-property spanner 'minimum-length 0.5))
577 (max (interval-end (ly:grob-robust-relative-extent
581 (close (ly:grob-relative-coordinate dots common-y Y)
582 (ly:grob-relative-coordinate spanner common-y Y)))
583 (interval-end (ly:grob-robust-relative-extent dots common X))
584 -10000) ;; TODO: use real infinity constant.
586 (right-x (max (- (interval-start (ly:grob-robust-relative-extent right-span common X))
588 (+ left-x minimum-length)))
589 (self-x (ly:grob-relative-coordinate spanner common X))
590 (dx (- right-x left-x))
591 (exp (list 'path thickness
599 ,dx ,(* 0.66 delta-y)
605 (cons (- left-x self-x) (- right-x self-x))
606 (cons (min 0 delta-y)
609 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
613 (define-public (grace-spacing::calc-shortest-duration grob)
615 ((cols (ly:grob-object grob 'columns))
618 (ly:moment-sub (ly:grob-property
619 (ly:grob-array-ref cols (1+ idx)) 'when)
621 (ly:grob-array-ref cols idx) 'when))))
623 (moment-min (lambda (x y)
626 (if (ly:moment<? x y)
632 (fold moment-min #f (map get-difference
633 (iota (1- (ly:grob-array-length cols)))))))
637 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
640 (define-public (fingering::calc-text grob)
642 ((event (event-cause grob))
643 (digit (ly:event-property event 'digit)))
646 (ly:input-message (ly:event-property event 'origin)
647 "Warning: Fingering notation for finger number ~a" digit))
649 (number->string digit 10)
652 (define-public (string-number::calc-text grob)
654 ((digit (ly:event-property (event-cause grob) 'string-number)))
656 (number->string digit 10)
660 (define-public (stroke-finger::calc-text grob)
662 ((digit (ly:event-property (event-cause grob) 'digit))
663 (text (ly:event-property (event-cause grob) 'text)))
667 (vector-ref (ly:grob-property grob 'digit-names) (1- (max (min 5 digit) 1))))))
669 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
671 (define-public (hairpin::calc-grow-direction grob)
672 (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
677 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
680 (define-public (lyric-text::print grob)
681 "Allow interpretation of tildes as lyric tieing marks."
684 ((text (ly:grob-property grob 'text)))
686 (grob-interpret-markup grob
688 (make-tied-lyric-markup text)
691 (define-public ((grob::calc-property-by-copy prop) grob)
692 (ly:event-property (event-cause grob) prop))
694 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
697 (define-public (fret-board::calc-stencil grob)
698 (grob-interpret-markup
700 (make-fret-diagram-verbose-markup
701 (ly:grob-property grob 'dot-placement-list))))
703 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
706 (define-public (script-interface::calc-x-offset grob)
707 (ly:grob-property grob 'positioning-done)
708 (let* ((shift (ly:grob-property grob 'toward-stem-shift 0.0))
709 (note-head-location (ly:self-alignment-interface::centered-on-x-parent grob))
710 (note-head-grob (ly:grob-parent grob X))
711 (stem-grob (ly:grob-object note-head-grob 'stem)))
712 (+ note-head-location
713 ;; If the property 'toward-stem-shift is defined and the script has the
714 ;; same direction as the stem, move the script accordingly. Since scripts can
715 ;; also be over skips, we need to check whether the grob has a stem at all.
716 (if (ly:grob? stem-grob)
717 (let ((dir1 (ly:grob-property grob 'direction))
718 (dir2 (ly:grob-property stem-grob 'direction)))
719 (if (equal? dir1 dir2)
720 (let* ((common-refp (ly:grob-common-refpoint grob stem-grob X))
721 (stem-location (ly:grob-relative-coordinate stem-grob common-refp X)))
722 (* shift (- stem-location
723 note-head-location)))