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 (note-head::calc-tablature-stem-attachment grob)
144 (define-public (note-head::calc-glyph-name grob)
146 ((style (ly:grob-property grob 'style))
147 (log (min 2 (ly:grob-property grob 'duration-log))))
150 ;; "default" style is directly handled in note-head.cc as a
151 ;; special case (HW says, mainly for performance reasons).
152 ;; Therefore, style "default" does not appear in this case
154 ((xcircle) "2xcircle")
155 ((harmonic) "0harmonic")
157 ;; Oops, I actually would not call this "baroque", but, for
158 ;; backwards compatibility to 1.4, this is supposed to take
159 ;; brevis, longa and maxima from the neo-mensural font and all
160 ;; other note heads from the default font. -- jr
162 (string-append (number->string log) "neomensural")
163 (number->string log)))
165 (string-append (number->string log) (symbol->string style)))
168 (string-append (number->string log) "mensural")
169 (string-append (number->string log) (symbol->string style))))
171 (string-append (number->string log) (symbol->string style)))
173 (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
174 (symbol->string style)
175 (string-append (number->string (max 0 log))
176 (symbol->string style)))))))
178 ;; TODO junk completely?
179 (define (note-head-style->attachment-coordinates grob axis)
180 "Return pair (X . Y), containing multipliers for the note head
181 bounding box, where to attach the stem. e.g.: X==0 means horizontally
182 centered, X==1 is at the right, X == -1 is at the left."
187 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
192 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
194 (define-public ((set-bar-number-visibility n) tr)
195 (let* ((bn (ly:context-property tr 'currentBarNumber)))
196 (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
198 (define-public (first-bar-number-invisible barnum) (> barnum 1))
200 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
203 (define-public begin-of-line-visible
205 (define-public end-of-line-visible
207 (define-public end-of-line-invisible
209 (define-public spanbar-begin-of-line-invisible
211 (define-public all-visible #(#t #t #t))
212 (define-public all-invisible #(#f #f #f))
213 (define-public begin-of-line-invisible
215 (define-public center-invisible #(#t #f #t))
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 ;; How should a bar line behave at a break?
223 ;; Why prepend `default-' to every scm identifier?
224 (define-public (bar-line::calc-glyph-name grob)
226 (glyph (ly:grob-property grob 'glyph))
227 (dir (ly:item-break-dir grob))
229 '((":|:" . (":|" . "|:"))
230 ("||:" . ("||" . "|:"))
231 ("dashed" . ("dashed" . '()))
233 ("||:" . ("||" . "|:"))
235 ("|:" . ("|" . "|:"))
238 ;; hmm... should we end with a bar line here?
239 (".|" . ("|" . ".|"))
242 (".|." . (".|." . ()))
246 ("empty" . (() . ()))
247 ("brace" . (() . "brace"))
248 ("bracket" . (() . "bracket")) )))
249 (glyph-name (if (= dir CENTER)
251 (if (and result (string? (index-cell (cdr result) dir)))
252 (index-cell (cdr result) dir)
257 (ly:grob-suicide! grob))
262 (define-public (shift-right-at-line-begin g)
263 "Shift an item to the right, but only at the start of the line."
264 (if (and (ly:item? g)
265 (equal? (ly:item-break-dir g) RIGHT))
266 (ly:grob-translate-axis! g 3.5 X)))
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
272 (define-public (tuplet-number::calc-denominator-text grob)
273 (number->string (ly:event-property (event-cause grob) 'denominator)))
275 (define-public (tuplet-number::calc-fraction-text grob)
277 ((ev (event-cause grob)))
280 (ly:event-property ev 'denominator)
281 (ly:event-property ev 'numerator))))
283 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 (define-public color? list?)
289 (define-public black '(0.0 0.0 0.0))
290 (define-public white '(1.0 1.0 1.0))
291 (define-public red '(1.0 0.0 0.0))
292 (define-public green '(0.0 1.0 0.0))
293 (define-public blue '(0.0 0.0 1.0))
294 (define-public cyan '(0.0 1.0 1.0))
295 (define-public magenta '(1.0 0.0 1.0))
296 (define-public yellow '(1.0 1.0 0.0))
298 (define-public grey '(0.5 0.5 0.5))
299 (define-public darkred '(0.5 0.0 0.0))
300 (define-public darkgreen '(0.0 0.5 0.0))
301 (define-public darkblue '(0.0 0.0 0.5))
302 (define-public darkcyan '(0.0 0.5 0.5))
303 (define-public darkmagenta '(0.5 0.0 0.5))
304 (define-public darkyellow '(0.5 0.5 0.0))
306 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
307 ;; * Pitch Trill Heads
310 (define-public (parentheses-item::calc-parenthesis-stencils grob)
312 (font (ly:grob-default-font grob))
313 (lp (ly:font-get-glyph font "accidentals.leftparen"))
314 (rp (ly:font-get-glyph font "accidentals.rightparen"))
320 (define (grob-text grob text)
322 ((layout (ly:grob-layout grob))
323 (defs (ly:output-def-lookup layout 'text-font-defaults))
324 (props (ly:grob-alist-chain grob defs)))
326 (ly:text-interface::interpret-markup
329 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
331 (font (ly:grob-default-font grob))
332 (lp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-text grob (ly:wide-char->utf-8 #x2329))
334 (rp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-text grob (ly:wide-char->utf-8 #x232A))
338 (list (stencil-whiteout lp)
339 (stencil-whiteout rp))))
341 (define (parenthesize-elements grob . rest)
343 ((refp (if (null? rest)
346 (elts (ly:grob-object grob 'elements))
347 (x-ext (ly:relative-group-extent elts refp X))
348 (stencils (ly:grob-property grob 'stencils))
351 (padding (ly:grob-property grob 'padding 0.1)))
354 (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
355 (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
359 (define (parentheses-item::print me)
361 ((elts (ly:grob-object me 'elements))
362 (y-ref (ly:grob-common-refpoint-of-array me elts Y))
363 (x-ref (ly:grob-common-refpoint-of-array me elts X))
364 (stencil (parenthesize-elements me x-ref))
365 (elt-y-ext (ly:relative-group-extent elts y-ref Y))
366 (y-center (interval-center elt-y-ext)))
368 (ly:stencil-translate
372 (ly:grob-relative-coordinate me x-ref X))
375 (ly:grob-relative-coordinate me y-ref Y))))
380 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
383 (define-public (chain-grob-member-functions grob value . funcs)
386 (set! value (func grob value)))
391 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
394 (define-public (bend::print spanner)
396 (< (abs (- a b)) 0.01))
399 ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
400 (left-span (ly:spanner-bound spanner LEFT))
401 (dots (if (and (grob::has-interface left-span 'note-head-interface)
402 (ly:grob? (ly:grob-object left-span 'dot)))
403 (ly:grob-object left-span 'dot) #f))
405 (right-span (ly:spanner-bound spanner RIGHT))
406 (thickness (* (ly:grob-property spanner 'thickness)
407 (ly:output-def-lookup (ly:grob-layout spanner)
409 (padding (ly:grob-property spanner 'padding 0.5))
410 (common (ly:grob-common-refpoint right-span
411 (ly:grob-common-refpoint spanner
414 (common-y (ly:grob-common-refpoint spanner left-span Y))
416 (max (interval-end (ly:grob-robust-relative-extent
420 (close (ly:grob-relative-coordinate dots common-y Y)
421 (ly:grob-relative-coordinate spanner common-y Y)))
422 (interval-end (ly:grob-robust-relative-extent dots common X))
423 -10000) ;; TODO: use real infinity constant.
425 (x (display (grob::has-interface left-span 'note-head-interface)))
427 (right-x (- (interval-start
428 (ly:grob-robust-relative-extent right-span common X))
430 (self-x (ly:grob-relative-coordinate spanner common X))
431 (dx (- right-x left-x))
432 (exp (list 'path thickness
440 ,dx ,(* 0.66 delta-y)
448 (cons (- left-x self-x) (- right-x self-x))
449 (cons (min 0 delta-y)
452 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
456 (define-public (grace-spacing::calc-shortest-duration grob)
458 ((cols (ly:grob-object grob 'columns))
461 (ly:moment-sub (ly:grob-property
462 (ly:grob-array-ref cols (1+ idx)) 'when)
464 (ly:grob-array-ref cols idx) 'when))))
466 (moment-min (lambda (x y)
469 (if (ly:moment<? x y)
475 (fold moment-min #f (map get-difference
476 (iota (1- (ly:grob-array-length cols)))))))
480 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
483 (define-public (fingering::calc-text grob)
485 ((event (event-cause grob))
486 (digit (ly:event-property event 'digit)))
489 (ly:input-message (ly:event-property event 'origin)
490 "Warning: Fingering notation for finger number ~a" digit))
492 (number->string digit 10)
495 (define-public (string-number::calc-text grob)
497 ((digit (ly:event-property (event-cause grob) 'string-number)))
499 (number->string digit 10)
503 (define-public (stroke-finger::calc-text grob)
505 ((digit (ly:event-property (event-cause grob) 'digit))
506 (text (ly:event-property (event-cause grob) 'text)))
510 (vector-ref (ly:grob-property grob 'digit-names) (1- (max (min 5 digit) 1))))))
512 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
514 (define-public (hairpin::calc-grow-direction grob)
515 (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
520 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
523 (define-public (lyric-text::print grob)
524 "Allow interpretation of tildes as lyric tieing marks."
527 ((text (ly:grob-property grob 'text))
528 (layout (ly:grob-layout grob))
529 (defs (ly:output-def-lookup layout 'text-font-defaults))
530 (props (ly:grob-alist-chain grob defs)))
532 (ly:text-interface::interpret-markup layout
535 (make-tied-lyric-markup text)
538 (define-public ((grob::calc-property-by-copy prop) grob)
539 (ly:event-property (event-cause grob) prop))
541 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
544 (define (string-frets->description string-frets string-count)
547 (map (lambda (x) (list 'mute (1+ x)))
548 (iota string-count)))))
550 (for-each (lambda (sf)
557 (vector-set! desc (1- string)
561 (list 'place-fret string fret finger)
562 (list 'place-fret string fret))
569 (vector->list desc)))
571 (define-public (fret-board::calc-stencil grob)
572 (let* ((string-frets (ly:grob-property grob 'string-fret-finger-combinations))
573 (string-count (ly:grob-property grob 'string-count))
574 (layout (ly:grob-layout grob))
575 (defs (ly:output-def-lookup layout 'text-font-defaults))
576 (props (ly:grob-alist-chain grob defs)))
578 (make-fret-diagram layout props
579 (string-frets->description string-frets 6))))