1 ;;;; output-lib.scm -- implement Scheme output helper functions
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2006 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 (let* ((text (ly:grob-property grob 'text))
31 (layout (ly:grob-layout grob))
32 (defs (ly:output-def-lookup layout 'text-font-defaults))
33 (props (ly:grob-alist-chain grob defs))
34 (circle (ly:text-interface::interpret-markup
35 layout props (make-circle-markup text))))
38 (define-public (music-cause grob)
40 ((event (event-cause grob)))
42 (if (ly:stream-event? event)
43 (ly:event-property event 'music-cause)
46 (define-public (event-cause grob)
48 ((cause (ly:grob-property grob 'cause)))
51 ((ly:stream-event? cause) cause)
52 ((ly:grob? cause) (event-cause cause))
55 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
58 ;; The TabNoteHead tablatureFormat callback.
59 ;; Compute the text grob-property
60 (define-public (fret-number-tablature-format string
63 ((tuning (ly:context-property context 'stringTunings))
64 (pitch (ly:event-property event 'pitch))
69 (eq? 'harmonic-event (ly:event-property ev 'class)))
70 (ly:event-property event 'articulations)))))
77 (- (ly:pitch-semitones pitch)
79 ;; remove 1 because list index starts at 0 and guitar string at 1.
83 ;; The 5-string banjo has got a extra string, the fifth (duh), wich
84 ;; starts at the fifth fret on the neck. Frets on the fifth string
85 ;; are referred to relative to the other frets:
86 ;; the "first fret" on the fifth string is really the sixth fret
88 ;; We solve this by defining a new fret-number-tablature function:
89 (define-public (fret-number-tablature-format-banjo string
92 ((tuning (ly:context-property context 'stringTunings))
93 (pitch (ly:event-property event 'pitch))
97 (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (- string 1)))))
99 ((and (> fret 0) (= string 5))
105 ; default tunings for common string instruments
106 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
107 (define-public guitar-open-g-tuning '(2 -1 -5 -10 -17 -22))
108 (define-public bass-tuning '(-17 -22 -27 -32))
109 (define-public mandolin-tuning '(16 9 2 -5))
111 ;; tunings for 5-string banjo
112 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
113 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
114 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
115 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
116 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
117 ;; convert 5-string banjo tuning to 4-string by removing the 5th string
118 (define-public (four-string-banjo tuning)
119 (reverse (cdr (reverse tuning))))
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 (define-public (stem::calc-duration-log grob)
127 (ly:event-property (event-cause grob) 'duration)))
129 (define-public (note-head::calc-duration-log grob)
132 (ly:event-property (event-cause grob) 'duration))))
134 (define-public (dots::calc-dot-count grob)
135 (ly:duration-dot-count
136 (ly:event-property (event-cause grob) 'duration)))
138 (define-public (dots::calc-staff-position grob)
140 ((head (ly:grob-parent grob Y))
141 (log (ly:grob-property head 'duration-log)))
144 ((or (not (grob::has-interface head 'rest-interface))
145 (not (integer? log))) 0)
153 (define (note-head::calc-tablature-stem-attachment grob)
159 (define-public (note-head::calc-glyph-name grob)
161 ((style (ly:grob-property grob 'style))
162 (log (min 2 (ly:grob-property grob 'duration-log))))
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
169 ((xcircle) "2xcircle")
170 ((harmonic) "0harmonic")
171 ((harmonic-black) "2harmonic")
172 ((harmonic-mixed) (if (<= log 1) "0harmonic"
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
180 (string-append (number->string log) "neomensural")
181 (number->string log)))
183 (string-append (number->string log) (symbol->string style)))
186 (string-append (number->string log) "mensural")
187 (string-append (number->string log) (symbol->string style))))
189 (string-append (number->string log) (symbol->string style)))
191 (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
192 (symbol->string style)
193 (string-append (number->string (max 0 log))
194 (symbol->string style)))))))
196 ;; TODO junk completely?
197 (define (note-head-style->attachment-coordinates grob axis)
198 "Return pair (X . Y), containing multipliers for the note head
199 bounding box, where to attach the stem. e.g.: X==0 means horizontally
200 centered, X==1 is at the right, X == -1 is at the left."
205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
210 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
212 (define-public ((set-bar-number-visibility n) tr)
213 (let* ((bn (ly:context-property tr 'currentBarNumber)))
214 (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
216 (define-public (first-bar-number-invisible barnum) (> barnum 1))
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 (define-public begin-of-line-visible
223 (define-public end-of-line-visible
225 (define-public end-of-line-invisible
227 (define-public spanbar-begin-of-line-invisible
229 (define-public all-visible #(#t #t #t))
230 (define-public all-invisible #(#f #f #f))
231 (define-public begin-of-line-invisible
233 (define-public center-invisible #(#t #f #t))
235 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
239 ;; How should a bar line behave at a break?
241 ;; Why prepend `default-' to every scm identifier?
242 (define-public (bar-line::calc-glyph-name grob)
244 (glyph (ly:grob-property grob 'glyph))
245 (dir (ly:item-break-dir grob))
247 '((":|:" . (":|" . "|:"))
248 ("||:" . ("||" . "|:"))
249 ("dashed" . ("dashed" . '()))
251 ("||:" . ("||" . "|:"))
253 ("|:" . ("|" . "|:"))
256 ;; hmm... should we end with a bar line here?
257 (".|" . ("|" . ".|"))
260 (".|." . (".|." . ()))
264 ("empty" . (() . ()))
265 ("brace" . (() . "brace"))
266 ("bracket" . (() . "bracket")) )))
267 (glyph-name (if (= dir CENTER)
269 (if (and result (string? (index-cell (cdr result) dir)))
270 (index-cell (cdr result) dir)
275 (ly:grob-suicide! grob))
280 (define-public (shift-right-at-line-begin g)
281 "Shift an item to the right, but only at the start of the line."
282 (if (and (ly:item? g)
283 (equal? (ly:item-break-dir g) RIGHT))
284 (ly:grob-translate-axis! g 3.5 X)))
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
290 (define-public (tuplet-number::calc-denominator-text grob)
291 (number->string (ly:event-property (event-cause grob) 'denominator)))
293 (define-public (tuplet-number::calc-fraction-text grob)
295 ((ev (event-cause grob)))
298 (ly:event-property ev 'denominator)
299 (ly:event-property ev 'numerator))))
301 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
304 (define-public color? list?)
307 (define-public black '(0.0 0.0 0.0))
308 (define-public white '(1.0 1.0 1.0))
309 (define-public red '(1.0 0.0 0.0))
310 (define-public green '(0.0 1.0 0.0))
311 (define-public blue '(0.0 0.0 1.0))
312 (define-public cyan '(0.0 1.0 1.0))
313 (define-public magenta '(1.0 0.0 1.0))
314 (define-public yellow '(1.0 1.0 0.0))
316 (define-public grey '(0.5 0.5 0.5))
317 (define-public darkred '(0.5 0.0 0.0))
318 (define-public darkgreen '(0.0 0.5 0.0))
319 (define-public darkblue '(0.0 0.0 0.5))
320 (define-public darkcyan '(0.0 0.5 0.5))
321 (define-public darkmagenta '(0.5 0.0 0.5))
322 (define-public darkyellow '(0.5 0.5 0.0))
324 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
328 (define-public (key-signature-interface::alteration-position step alter c0-position)
329 ;; TODO: memoize - this is mostly constant.
331 ;; fes, ges, as and bes typeset in lower octave
332 (define FLAT_TOP_PITCH 2)
334 ;; ais and bis typeset in lower octave
335 (define SHARP_TOP_PITCH 4)
338 (+ (cdr step) (* (car step) 7) c0-position)
340 ((from-bottom-pos (modulo (+ 4 49 c0-position) 7))
342 (c0 (- from-bottom-pos 4)))
345 (or (and (< alter 0) (or (> p FLAT_TOP_PITCH) (> (+ p c0) 4)) (> (+ p c0) 1))
346 (and (> alter 0) (or (> p SHARP_TOP_PITCH) (> (+ p c0) 5)) (> (+ p c0) 2))
349 ;; Typeset below c_position
352 ;; Provide for the four cases in which there's a glitch
353 ;; it's a hack, but probably not worth
354 ;; the effort of finding a nicer solution.
357 ((and (= c0 2) (= p 3) (> alter 0))
359 ((and (= c0 -3) (= p -1) (> alter 0))
361 ((and (= c0 -4) (= p -1) (< alter 0))
363 ((and (= c0 -2) (= p -3) (< alter 0))
369 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
372 (define-public (accidental-interface::calc-alteration grob)
373 (ly:pitch-alteration (ly:event-property (event-cause grob) 'pitch)))
376 (define-public cancellation-glyph-name-alist
377 '((0 . "accidentals.natural")))
379 (define-public standard-alteration-glyph-name-alist
381 ;; ordered for optimal performance.
382 (0 . "accidentals.natural")
383 (-1/2 . "accidentals.flat")
384 (1/2 . "accidentals.sharp")
386 (1 . "accidentals.doublesharp")
387 (-1 . "accidentals.flatflat")
389 (3/4 . "accidentals.sharp.slashslash.stemstemstem")
390 (1/4 . "accidentals.sharp.slashslash.stem")
391 (-1/4 . "accidentals.mirroredflat")
392 (-3/4 . "accidentals.mirroredflat.flat")
395 (define-public makam-alteration-glyph-name-alist
396 '((1 . "accidentals.doublesharp")
397 (8/9 . "accidentals.sharp.slashslashslash.stemstem")
398 (5/9 . "accidentals.sharp.slashslashslash.stem")
399 (4/9 . "accidentals.sharp")
400 (1/9 . "accidentals.sharp.slashslash.stem")
401 (0 . "accidentals.natural")
402 (-1/9 . "accidentals.mirroredflat")
403 (-4/9 . "accidentals.flat.slash")
404 (-5/9 . "accidentals.flat")
405 (-8/9 . "accidentals.flat.slashslash")
406 (-1 . "accidentals.flatflat")
409 (define-public alteration-hufnagel-glyph-name-alist
410 '((1/2 . "accidentals.hufnagel-1")
411 (0 . "accidentals.vaticana0")
412 (-1/2 . "accidentals.mensural1")))
414 (define-public alteration-medicaea-glyph-name-alist
415 '((1/2 . "accidentals.medicaea-1")
416 (0 . "accidentals.vaticana0")
417 (-1/2 . "accidentals.mensural1")))
419 (define-public alteration-vaticana-glyph-name-alist
420 '((1/2 . "accidentals.vaticana-1")
421 (0 . "accidentals.vaticana0")
422 (-1/2 . "accidentals.mensural1")))
424 (define-public alteration-mensural-glyph-name-alist
425 '((1/2 . "accidentals.mensural-1")
426 (0 . "accidentals.vaticana0")
427 (-1/2 . "accidentals.mensural1")))
430 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
431 ;; * Pitch Trill Heads
434 (define-public (parentheses-item::calc-parenthesis-stencils grob)
435 (let* ((font (ly:grob-default-font grob))
436 (lp (ly:font-get-glyph font "accidentals.leftparen"))
437 (rp (ly:font-get-glyph font "accidentals.rightparen")))
442 (define (grob-text grob text)
444 ((layout (ly:grob-layout grob))
445 (defs (ly:output-def-lookup layout 'text-font-defaults))
446 (props (ly:grob-alist-chain grob defs)))
448 (ly:text-interface::interpret-markup
451 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
453 (font (ly:grob-default-font grob))
454 (lp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-text grob (ly:wide-char->utf-8 #x2329))
456 (rp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-text grob (ly:wide-char->utf-8 #x232A))
460 (list (stencil-whiteout lp)
461 (stencil-whiteout rp))))
463 (define (parenthesize-elements grob . rest)
465 ((refp (if (null? rest)
468 (elts (ly:grob-object grob 'elements))
469 (x-ext (ly:relative-group-extent elts refp X))
470 (stencils (ly:grob-property grob 'stencils))
473 (padding (ly:grob-property grob 'padding 0.1)))
476 (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
477 (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
481 (define (parentheses-item::print me)
483 ((elts (ly:grob-object me 'elements))
484 (y-ref (ly:grob-common-refpoint-of-array me elts Y))
485 (x-ref (ly:grob-common-refpoint-of-array me elts X))
486 (stencil (parenthesize-elements me x-ref))
487 (elt-y-ext (ly:relative-group-extent elts y-ref Y))
488 (y-center (interval-center elt-y-ext)))
490 (ly:stencil-translate
494 (ly:grob-relative-coordinate me x-ref X))
497 (ly:grob-relative-coordinate me y-ref Y))))
502 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
505 (define-public (chain-grob-member-functions grob value . funcs)
508 (set! value (func grob value)))
513 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
516 (define-public (bend::print spanner)
518 (< (abs (- a b)) 0.01))
521 ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
522 (left-span (ly:spanner-bound spanner LEFT))
523 (dots (if (and (grob::has-interface left-span 'note-head-interface)
524 (ly:grob? (ly:grob-object left-span 'dot)))
525 (ly:grob-object left-span 'dot) #f))
527 (right-span (ly:spanner-bound spanner RIGHT))
528 (thickness (* (ly:grob-property spanner 'thickness)
529 (ly:output-def-lookup (ly:grob-layout spanner)
531 (padding (ly:grob-property spanner 'padding 0.5))
532 (common (ly:grob-common-refpoint right-span
533 (ly:grob-common-refpoint spanner
536 (common-y (ly:grob-common-refpoint spanner left-span Y))
538 (max (interval-end (ly:grob-robust-relative-extent
542 (close (ly:grob-relative-coordinate dots common-y Y)
543 (ly:grob-relative-coordinate spanner common-y Y)))
544 (interval-end (ly:grob-robust-relative-extent dots common X))
545 -10000) ;; TODO: use real infinity constant.
547 (right-x (- (interval-start
548 (ly:grob-robust-relative-extent right-span common X))
550 (self-x (ly:grob-relative-coordinate spanner common X))
551 (dx (- right-x left-x))
552 (exp (list 'path thickness
560 ,dx ,(* 0.66 delta-y)
566 (cons (- left-x self-x) (- right-x self-x))
567 (cons (min 0 delta-y)
570 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
574 (define-public (grace-spacing::calc-shortest-duration grob)
576 ((cols (ly:grob-object grob 'columns))
579 (ly:moment-sub (ly:grob-property
580 (ly:grob-array-ref cols (1+ idx)) 'when)
582 (ly:grob-array-ref cols idx) 'when))))
584 (moment-min (lambda (x y)
587 (if (ly:moment<? x y)
593 (fold moment-min #f (map get-difference
594 (iota (1- (ly:grob-array-length cols)))))))
598 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
601 (define-public (fingering::calc-text grob)
603 ((event (event-cause grob))
604 (digit (ly:event-property event 'digit)))
607 (ly:input-message (ly:event-property event 'origin)
608 "Warning: Fingering notation for finger number ~a" digit))
610 (number->string digit 10)
613 (define-public (string-number::calc-text grob)
615 ((digit (ly:event-property (event-cause grob) 'string-number)))
617 (number->string digit 10)
621 (define-public (stroke-finger::calc-text grob)
623 ((digit (ly:event-property (event-cause grob) 'digit))
624 (text (ly:event-property (event-cause grob) 'text)))
628 (vector-ref (ly:grob-property grob 'digit-names) (1- (max (min 5 digit) 1))))))
630 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
632 (define-public (hairpin::calc-grow-direction grob)
633 (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
638 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
641 (define-public (lyric-text::print grob)
642 "Allow interpretation of tildes as lyric tieing marks."
645 ((text (ly:grob-property grob 'text))
646 (layout (ly:grob-layout grob))
647 (defs (ly:output-def-lookup layout 'text-font-defaults))
648 (props (ly:grob-alist-chain grob defs)))
650 (ly:text-interface::interpret-markup layout
653 (make-tied-lyric-markup text)
656 (define-public ((grob::calc-property-by-copy prop) grob)
657 (ly:event-property (event-cause grob) prop))
659 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
662 (define (string-frets->description string-frets string-count)
665 (map (lambda (x) (list 'mute (1+ x)))
666 (iota string-count)))))
668 (for-each (lambda (sf)
675 (vector-set! desc (1- string)
679 (list 'place-fret string fret finger)
680 (list 'place-fret string fret))
687 (vector->list desc)))
689 (define-public (fret-board::calc-stencil grob)
690 (let* ((string-frets (ly:grob-property grob 'string-fret-finger-combinations))
691 (string-count (ly:grob-property grob 'string-count))
692 (layout (ly:grob-layout grob))
693 (defs (ly:output-def-lookup layout 'text-font-defaults))
694 (props (ly:grob-alist-chain grob defs)))
696 (make-fret-diagram layout props
697 (string-frets->description string-frets 6))))