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 ; default tunings for common string instruments
93 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
94 (define-public guitar-open-g-tuning '(2 -1 -5 -10 -17 -22))
95 (define-public bass-tuning '(-17 -22 -27 -32))
96 (define-public mandolin-tuning '(16 9 2 -5))
98 ;; tunings for 5-string banjo
99 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
100 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
101 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
102 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
103 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
104 ;; convert 5-string banjo tuning to 4-string by removing the 5th string
105 (define-public (four-string-banjo tuning)
106 (reverse (cdr (reverse tuning))))
108 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
112 (define-public (stem::calc-duration-log grob)
114 (ly:event-property (event-cause grob) 'duration)))
116 (define-public (note-head::calc-duration-log grob)
119 (ly:event-property (event-cause grob) 'duration))))
121 (define-public (dots::calc-dot-count grob)
122 (ly:duration-dot-count
123 (ly:event-property (event-cause grob) 'duration)))
125 (define-public (dots::calc-staff-position grob)
127 ((head (ly:grob-parent grob Y))
128 (log (ly:grob-property head 'duration-log)))
131 ((or (not (grob::has-interface head 'rest-interface))
132 (not (integer? log))) 0)
140 (define (note-head::calc-tablature-stem-attachment grob)
146 (define-public (note-head::calc-glyph-name grob)
148 ((style (ly:grob-property grob 'style))
149 (log (min 2 (ly:grob-property grob 'duration-log))))
152 ;; "default" style is directly handled in note-head.cc as a
153 ;; special case (HW says, mainly for performance reasons).
154 ;; Therefore, style "default" does not appear in this case
156 ((xcircle) "2xcircle")
157 ((harmonic) "0harmonic")
158 ((harmonic-black) "2harmonic")
159 ((harmonic-mixed) (if (<= log 1) "0harmonic"
162 ;; Oops, I actually would not call this "baroque", but, for
163 ;; backwards compatibility to 1.4, this is supposed to take
164 ;; brevis, longa and maxima from the neo-mensural font and all
165 ;; other note heads from the default font. -- jr
167 (string-append (number->string log) "neomensural")
168 (number->string log)))
170 (string-append (number->string log) (symbol->string style)))
173 (string-append (number->string log) "mensural")
174 (string-append (number->string log) (symbol->string style))))
176 (string-append (number->string log) (symbol->string style)))
178 (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
179 (symbol->string style)
180 (string-append (number->string (max 0 log))
181 (symbol->string style)))))))
183 ;; TODO junk completely?
184 (define (note-head-style->attachment-coordinates grob axis)
185 "Return pair (X . Y), containing multipliers for the note head
186 bounding box, where to attach the stem. e.g.: X==0 means horizontally
187 centered, X==1 is at the right, X == -1 is at the left."
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
197 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
199 (define-public ((set-bar-number-visibility n) tr)
200 (let* ((bn (ly:context-property tr 'currentBarNumber)))
201 (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
203 (define-public (first-bar-number-invisible barnum) (> barnum 1))
205 (define-public (all-bar-numbers-visible barnum) #t)
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 ;; percent repeat counters
210 (define-public ((every-nth-repeat-count-visible n) count context) (= 0 (modulo count n)))
212 (define-public (all-repeat-counts-visible count context) #t)
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
217 (define-public all-visible #(#t #t #t))
218 (define-public begin-of-line-invisible #(#t #t #f))
219 (define-public center-invisible #(#t #f #t))
220 (define-public end-of-line-invisible #(#f #t #t))
221 (define-public begin-of-line-visible #(#f #f #t))
222 (define-public center-visible #(#f #t #f))
223 (define-public end-of-line-visible #(#t #f #f))
224 (define-public all-invisible #(#f #f #f))
226 (define-public spanbar-begin-of-line-invisible
229 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 ;; How should a bar line behave at a break?
234 (define bar-glyph-alist
235 '((":|:" . (":|" . "|:"))
236 (":|.|:" . (":|" . "|:"))
237 (":|.:" . (":|" . "|:"))
238 ("||:" . ("||" . "|:"))
239 ("dashed" . ("dashed" . '()))
241 ("||:" . ("||" . "|:"))
243 ("|:" . ("|" . "|:"))
246 ;; hmm... should we end with a bar line here?
247 (".|" . ("|" . ".|"))
250 (".|." . (".|." . ()))
251 ("|.|" . ("|.|" . ()))
256 ("empty" . (() . ()))
257 ("brace" . (() . "brace"))
258 ("bracket" . (() . "bracket"))
261 (define-public (bar-line::calc-glyph-name grob)
263 (glyph (ly:grob-property grob 'glyph))
264 (dir (ly:item-break-dir grob))
265 (result (assoc glyph bar-glyph-alist))
266 (glyph-name (if (= dir CENTER)
268 (if (and result (string? (index-cell (cdr result) dir)))
269 (index-cell (cdr result) dir)
274 (define-public (bar-line::calc-break-visibility grob)
275 (let* ((glyph (ly:grob-property grob 'glyph))
276 (result (assoc glyph bar-glyph-alist)))
278 (vector (string? (cadr result)) #t (string? (cddr result)))
282 (define-public (shift-right-at-line-begin g)
283 "Shift an item to the right, but only at the start of the line."
284 (if (and (ly:item? g)
285 (equal? (ly:item-break-dir g) RIGHT))
286 (ly:grob-translate-axis! g 3.5 X)))
289 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
292 (define-public (tuplet-number::calc-denominator-text grob)
293 (number->string (ly:event-property (event-cause grob) 'denominator)))
295 (define-public (tuplet-number::calc-fraction-text grob)
297 ((ev (event-cause grob)))
300 (ly:event-property ev 'denominator)
301 (ly:event-property ev 'numerator))))
303 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
306 (define-public color? list?)
307 (define-public (rgb-color r g b) (list r g b))
310 (define-public black '(0.0 0.0 0.0))
311 (define-public white '(1.0 1.0 1.0))
312 (define-public red '(1.0 0.0 0.0))
313 (define-public green '(0.0 1.0 0.0))
314 (define-public blue '(0.0 0.0 1.0))
315 (define-public cyan '(0.0 1.0 1.0))
316 (define-public magenta '(1.0 0.0 1.0))
317 (define-public yellow '(1.0 1.0 0.0))
319 (define-public grey '(0.5 0.5 0.5))
320 (define-public darkred '(0.5 0.0 0.0))
321 (define-public darkgreen '(0.0 0.5 0.0))
322 (define-public darkblue '(0.0 0.0 0.5))
323 (define-public darkcyan '(0.0 0.5 0.5))
324 (define-public darkmagenta '(0.5 0.0 0.5))
325 (define-public darkyellow '(0.5 0.5 0.0))
327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
331 (define-public (key-signature-interface::alteration-position step alter c0-position)
332 ;; TODO: memoize - this is mostly constant.
334 ;; fes, ges, as and bes typeset in lower octave
335 (define FLAT_TOP_PITCH 2)
337 ;; ais and bis typeset in lower octave
338 (define SHARP_TOP_PITCH 4)
341 (+ (cdr step) (* (car step) 7) c0-position)
343 ((from-bottom-pos (modulo (+ 4 49 c0-position) 7))
345 (c0 (- from-bottom-pos 4)))
348 (or (and (< alter 0) (or (> p FLAT_TOP_PITCH) (> (+ p c0) 4)) (> (+ p c0) 1))
349 (and (> alter 0) (or (> p SHARP_TOP_PITCH) (> (+ p c0) 5)) (> (+ p c0) 2))
352 ;; Typeset below c_position
355 ;; Provide for the four cases in which there's a glitch
356 ;; it's a hack, but probably not worth
357 ;; the effort of finding a nicer solution.
360 ((and (= c0 2) (= p 3) (> alter 0))
362 ((and (= c0 -3) (= p -1) (> alter 0))
364 ((and (= c0 -4) (= p -1) (< alter 0))
366 ((and (= c0 -2) (= p -3) (< alter 0))
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
375 (define-public (accidental-interface::calc-alteration grob)
376 (ly:pitch-alteration (ly:event-property (event-cause grob) 'pitch)))
379 (define-public cancellation-glyph-name-alist
380 '((0 . "accidentals.natural")))
382 (define-public standard-alteration-glyph-name-alist
384 ;; ordered for optimal performance.
385 (0 . "accidentals.natural")
386 (-1/2 . "accidentals.flat")
387 (1/2 . "accidentals.sharp")
389 (1 . "accidentals.doublesharp")
390 (-1 . "accidentals.flatflat")
392 (3/4 . "accidentals.sharp.slashslash.stemstemstem")
393 (1/4 . "accidentals.sharp.slashslash.stem")
394 (-1/4 . "accidentals.mirroredflat")
395 (-3/4 . "accidentals.mirroredflat.flat")
398 ;; FIXME: standard vs default, alteration-FOO vs FOO-alteration
399 (define-public alteration-default-glyph-name-alist standard-alteration-glyph-name-alist)
401 (define-public makam-alteration-glyph-name-alist
402 '((1 . "accidentals.doublesharp")
403 (8/9 . "accidentals.sharp.slashslashslash.stemstem")
404 (5/9 . "accidentals.sharp.slashslashslash.stem")
405 (4/9 . "accidentals.sharp")
406 (1/9 . "accidentals.sharp.slashslash.stem")
407 (0 . "accidentals.natural")
408 (-1/9 . "accidentals.mirroredflat")
409 (-4/9 . "accidentals.flat.slash")
410 (-5/9 . "accidentals.flat")
411 (-8/9 . "accidentals.flat.slashslash")
412 (-1 . "accidentals.flatflat")
415 (define-public alteration-hufnagel-glyph-name-alist
416 '((-1/2 . "accidentals.hufnagelM1")
417 (0 . "accidentals.vaticana0")
418 (1/2 . "accidentals.mensural1")))
420 (define-public alteration-medicaea-glyph-name-alist
421 '((-1/2 . "accidentals.medicaeaM1")
422 (0 . "accidentals.vaticana0")
423 (1/2 . "accidentals.mensural1")))
425 (define-public alteration-vaticana-glyph-name-alist
426 '((-1/2 . "accidentals.vaticanaM1")
427 (0 . "accidentals.vaticana0")
428 (1/2 . "accidentals.mensural1")))
430 (define-public alteration-mensural-glyph-name-alist
431 '((-1/2 . "accidentals.mensuralM1")
432 (0 . "accidentals.vaticana0")
433 (1/2 . "accidentals.mensural1")))
436 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
437 ;; * Pitch Trill Heads
440 (define-public (parentheses-item::calc-parenthesis-stencils grob)
441 (let* ((font (ly:grob-default-font grob))
442 (lp (ly:font-get-glyph font "accidentals.leftparen"))
443 (rp (ly:font-get-glyph font "accidentals.rightparen")))
448 (define-public (grob-interpret-markup grob text)
450 ((layout (ly:grob-layout grob))
451 (defs (ly:output-def-lookup layout 'text-font-defaults))
452 (props (ly:grob-alist-chain grob defs)))
454 (ly:text-interface::interpret-markup
457 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
459 (font (ly:grob-default-font grob))
460 (lp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-interpret-markup grob (ly:wide-char->utf-8 #x2329))
462 (rp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-interpret-markup grob (ly:wide-char->utf-8 #x232A))
466 (list (stencil-whiteout lp)
467 (stencil-whiteout rp))))
469 (define (parenthesize-elements grob . rest)
471 ((refp (if (null? rest)
474 (elts (ly:grob-object grob 'elements))
475 (x-ext (ly:relative-group-extent elts refp X))
476 (stencils (ly:grob-property grob 'stencils))
479 (padding (ly:grob-property grob 'padding 0.1)))
482 (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
483 (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
487 (define (parentheses-item::print me)
489 ((elts (ly:grob-object me 'elements))
490 (y-ref (ly:grob-common-refpoint-of-array me elts Y))
491 (x-ref (ly:grob-common-refpoint-of-array me elts X))
492 (stencil (parenthesize-elements me x-ref))
493 (elt-y-ext (ly:relative-group-extent elts y-ref Y))
494 (y-center (interval-center elt-y-ext)))
496 (ly:stencil-translate
500 (ly:grob-relative-coordinate me x-ref X))
503 (ly:grob-relative-coordinate me y-ref Y))))
508 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
511 (define-public (chain-grob-member-functions grob value . funcs)
514 (set! value (func grob value)))
519 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
522 (define-public (bend::print spanner)
524 (< (abs (- a b)) 0.01))
527 ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
528 (left-span (ly:spanner-bound spanner LEFT))
529 (dots (if (and (grob::has-interface left-span 'note-head-interface)
530 (ly:grob? (ly:grob-object left-span 'dot)))
531 (ly:grob-object left-span 'dot) #f))
533 (right-span (ly:spanner-bound spanner RIGHT))
534 (thickness (* (ly:grob-property spanner 'thickness)
535 (ly:output-def-lookup (ly:grob-layout spanner)
537 (padding (ly:grob-property spanner 'padding 0.5))
538 (common (ly:grob-common-refpoint right-span
539 (ly:grob-common-refpoint spanner
542 (common-y (ly:grob-common-refpoint spanner left-span Y))
543 (minimum-length (ly:grob-property spanner 'minimum-length 0.5))
546 (max (interval-end (ly:grob-robust-relative-extent
550 (close (ly:grob-relative-coordinate dots common-y Y)
551 (ly:grob-relative-coordinate spanner common-y Y)))
552 (interval-end (ly:grob-robust-relative-extent dots common X))
553 -10000) ;; TODO: use real infinity constant.
555 (right-x (max (- (interval-start (ly:grob-robust-relative-extent right-span common X))
557 (+ left-x minimum-length)))
558 (self-x (ly:grob-relative-coordinate spanner common X))
559 (dx (- right-x left-x))
560 (exp (list 'path thickness
568 ,dx ,(* 0.66 delta-y)
574 (cons (- left-x self-x) (- right-x self-x))
575 (cons (min 0 delta-y)
578 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
582 (define-public (grace-spacing::calc-shortest-duration grob)
584 ((cols (ly:grob-object grob 'columns))
587 (ly:moment-sub (ly:grob-property
588 (ly:grob-array-ref cols (1+ idx)) 'when)
590 (ly:grob-array-ref cols idx) 'when))))
592 (moment-min (lambda (x y)
595 (if (ly:moment<? x y)
601 (fold moment-min #f (map get-difference
602 (iota (1- (ly:grob-array-length cols)))))))
606 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
609 (define-public (fingering::calc-text grob)
611 ((event (event-cause grob))
612 (digit (ly:event-property event 'digit)))
615 (ly:input-message (ly:event-property event 'origin)
616 "Warning: Fingering notation for finger number ~a" digit))
618 (number->string digit 10)
621 (define-public (string-number::calc-text grob)
623 ((digit (ly:event-property (event-cause grob) 'string-number)))
625 (number->string digit 10)
629 (define-public (stroke-finger::calc-text grob)
631 ((digit (ly:event-property (event-cause grob) 'digit))
632 (text (ly:event-property (event-cause grob) 'text)))
636 (vector-ref (ly:grob-property grob 'digit-names) (1- (max (min 5 digit) 1))))))
638 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
640 (define-public (hairpin::calc-grow-direction grob)
641 (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
646 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
649 (define-public (lyric-text::print grob)
650 "Allow interpretation of tildes as lyric tieing marks."
653 ((text (ly:grob-property grob 'text)))
655 (grob-interpret-markup grob
657 (make-tied-lyric-markup text)
660 (define-public ((grob::calc-property-by-copy prop) grob)
661 (ly:event-property (event-cause grob) prop))
663 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
666 (define-public (fret-board::calc-stencil grob)
667 (grob-interpret-markup
669 (make-fret-diagram-verbose-markup
670 (ly:grob-property grob 'dot-placement-list))))
672 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
675 (define-public (script-interface::calc-x-offset grob)
676 (ly:grob-property grob 'positioning-done)
677 (let* ((shift (ly:grob-property grob 'toward-stem-shift 0.0))
678 (note-head-location (ly:self-alignment-interface::centered-on-x-parent grob))
679 (note-head-grob (ly:grob-parent grob X))
680 (stem-grob (ly:grob-object note-head-grob 'stem)))
681 (+ note-head-location
682 ;; If the property 'toward-stem-shift is defined and the script has the
683 ;; same direction as the stem, move the script accordingly. Since scripts can
684 ;; also be over skips, we need to check whether the grob has a stem at all.
685 (if (ly:grob? stem-grob)
686 (let ((dir1 (ly:grob-property grob 'direction))
687 (dir2 (ly:grob-property stem-grob 'direction)))
688 (if (equal? dir1 dir2)
689 (let* ((common-refp (ly:grob-common-refpoint grob stem-grob X))
690 (stem-location (ly:grob-relative-coordinate stem-grob common-refp X)))
691 (* shift (- stem-location
692 note-head-location)))