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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 (define-public (make-stencil-boxer thickness padding callback)
16 "Return function that adds a box around the grob passed as argument."
19 (box-stencil (callback grob) thickness padding)))
21 (define-public (make-stencil-circler thickness padding callback)
22 "Return function that adds a circle around the grob passed as argument."
24 (lambda (grob) (circle-stencil (callback grob) thickness padding)))
26 (define-public (print-circled-text-callback grob)
27 (let* ((text (ly:grob-property grob 'text))
29 (layout (ly:grob-layout grob))
30 (defs (ly:output-def-lookup layout 'text-font-defaults))
31 (props (ly:grob-alist-chain grob defs))
32 (circle (ly:text-interface::interpret-markup
33 layout props (make-circle-markup text))))
36 (define-public (music-cause grob)
38 ((event (event-cause grob)))
40 (if (ly:stream-event? event)
41 (ly:event-property event 'music-cause)
44 (define-public (event-cause grob)
46 ((cause (ly:grob-property grob 'cause)))
49 ((ly:stream-event? cause) cause)
50 ((ly:grob? cause) (event-cause cause))
53 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56 ;; The TabNoteHead tablatureFormat callback.
57 ;; Compute the text grob-property
58 (define-public (fret-number-tablature-format string tuning pitch)
62 (- (ly:pitch-semitones pitch)
64 ;; remove 1 because list index starts at 0 and guitar string at 1.
67 ;; The 5-string banjo has got a extra string, the fifth (duh), wich
68 ;; starts at the fifth fret on the neck. Frets on the fifth string
69 ;; are referred to relative to the other frets:
70 ;; the "first fret" on the fifth string is really the sixth fret
72 ;; We solve this by defining a new fret-number-tablature function:
73 (define-public (fret-number-tablature-format-banjo string tuning pitch)
76 (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (- string 1)))))
78 ((and (> fret 0) (= string 5))
83 ; default tunings for common string instruments
84 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
85 (define-public guitar-open-g-tuning '(2 -1 -5 -10 -17 -22))
86 (define-public bass-tuning '(-17 -22 -27 -32))
87 (define-public mandolin-tuning '(16 9 2 -5))
89 ;; tunings for 5-string banjo
90 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
91 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
92 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
93 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
94 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
95 ;; convert 5-string banjo tuning to 4-string by removing the 5th string
96 (define-public (four-string-banjo tuning)
97 (reverse (cdr (reverse tuning))))
99 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
103 (define-public (stem::calc-duration-log grob)
105 (ly:event-property (event-cause grob) 'duration)))
107 (define-public (note-head::calc-duration-log grob)
110 (ly:event-property (event-cause grob) 'duration))))
112 (define-public (dots::calc-dot-count grob)
113 (ly:duration-dot-count
114 (ly:event-property (event-cause grob) 'duration)))
116 (define (note-head::calc-tablature-stem-attachment grob)
122 (define-public (note-head::calc-glyph-name grob)
124 ((style (ly:grob-property grob 'style))
125 (log (min 2 (ly:grob-property grob 'duration-log))))
128 ;; "default" style is directly handled in note-head.cc as a
129 ;; special case (HW says, mainly for performance reasons).
130 ;; Therefore, style "default" does not appear in this case
132 ((xcircle) "2xcircle")
133 ((harmonic) "0harmonic")
135 ;; Oops, I actually would not call this "baroque", but, for
136 ;; backwards compatibility to 1.4, this is supposed to take
137 ;; brevis, longa and maxima from the neo-mensural font and all
138 ;; other note heads from the default font. -- jr
140 (string-append (number->string log) "neomensural")
141 (number->string log)))
143 (string-append (number->string log) (symbol->string style)))
146 (string-append (number->string log) "mensural")
147 (string-append (number->string log) (symbol->string style))))
149 (string-append (number->string log) (symbol->string style)))
151 (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
152 (symbol->string style)
153 (string-append (number->string (max 0 log))
154 (symbol->string style)))))))
156 ;; TODO junk completely?
157 (define (note-head-style->attachment-coordinates grob axis)
158 "Return pair (X . Y), containing multipliers for the note head
159 bounding box, where to attach the stem. e.g.: X==0 means horizontally
160 centered, X==1 is at the right, X == -1 is at the left."
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
168 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
170 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
172 (define-public ((set-bar-number-visibility n) tr)
173 (let* ((bn (ly:context-property tr 'currentBarNumber)))
174 (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
176 (define-public (first-bar-number-invisible barnum) (> barnum 1))
178 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181 (define-public begin-of-line-visible
183 (define-public end-of-line-visible
185 (define-public end-of-line-invisible
187 (define-public spanbar-begin-of-line-invisible
189 (define-public all-visible #(#t #t #t))
190 (define-public all-invisible #(#f #f #f))
191 (define-public begin-of-line-invisible
193 (define-public center-invisible #(#t #f #t))
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199 ;; How should a bar line behave at a break?
201 ;; Why prepend `default-' to every scm identifier?
202 (define-public (bar-line::calc-glyph-name grob)
204 (glyph (ly:grob-property grob 'glyph))
205 (dir (ly:item-break-dir grob))
207 '((":|:" . (":|" . "|:"))
208 ("||:" . ("||" . "|:"))
209 ("dashed" . ("dashed" . '()))
211 ("||:" . ("||" . "|:"))
213 ("|:" . ("|" . "|:"))
216 ;; hmm... should we end with a bar line here?
217 (".|" . ("|" . ".|"))
220 (".|." . (".|." . ()))
224 ("empty" . (() . ()))
225 ("brace" . (() . "brace"))
226 ("bracket" . (() . "bracket")) )))
227 (glyph-name (if (= dir CENTER)
229 (if (and result (string? (index-cell (cdr result) dir)))
230 (index-cell (cdr result) dir)
235 (ly:grob-suicide! grob))
240 (define-public (shift-right-at-line-begin g)
241 "Shift an item to the right, but only at the start of the line."
242 (if (and (ly:item? g)
243 (equal? (ly:item-break-dir g) RIGHT))
244 (ly:grob-translate-axis! g 3.5 X)))
247 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250 (define-public (tuplet-number::calc-denominator-text grob)
251 (number->string (ly:event-property (event-cause grob) 'denominator)))
253 (define-public (tuplet-number::calc-fraction-text grob)
255 ((ev (event-cause grob)))
258 (ly:event-property ev 'denominator)
259 (ly:event-property ev 'numerator))))
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
264 (define-public color? list?)
267 (define-public black '(0.0 0.0 0.0))
268 (define-public white '(1.0 1.0 1.0))
269 (define-public red '(1.0 0.0 0.0))
270 (define-public green '(0.0 1.0 0.0))
271 (define-public blue '(0.0 0.0 1.0))
272 (define-public cyan '(0.0 1.0 1.0))
273 (define-public magenta '(1.0 0.0 1.0))
274 (define-public yellow '(1.0 1.0 0.0))
276 (define-public grey '(0.5 0.5 0.5))
277 (define-public darkred '(0.5 0.0 0.0))
278 (define-public darkgreen '(0.0 0.5 0.0))
279 (define-public darkblue '(0.0 0.0 0.5))
280 (define-public darkcyan '(0.0 0.5 0.5))
281 (define-public darkmagenta '(0.5 0.0 0.5))
282 (define-public darkyellow '(0.5 0.5 0.0))
284 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
285 ;; * Pitch Trill Heads
288 (define (parenthesize-elements grob . rest)
290 ((refp (if (null? rest)
293 (elts (ly:grob-object grob 'elements))
294 (x-ext (ly:relative-group-extent elts refp X))
296 (font (ly:grob-default-font grob))
297 (lp (ly:font-get-glyph font "accidentals.leftparen"))
298 (rp (ly:font-get-glyph font "accidentals.rightparen"))
299 (padding (ly:grob-property grob 'padding 0.1)))
302 (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
303 (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
307 (define (parentheses-item::print me)
309 ((elts (ly:grob-object me 'elements))
310 (y-ref (ly:grob-common-refpoint-of-array me elts Y))
311 (x-ref (ly:grob-common-refpoint-of-array me elts X))
312 (stencil (parenthesize-elements me x-ref))
313 (elt-y-ext (ly:relative-group-extent elts y-ref Y))
314 (y-center (interval-center elt-y-ext)))
316 (ly:stencil-translate
320 (ly:grob-relative-coordinate me x-ref X))
323 (ly:grob-relative-coordinate me y-ref Y))))
327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
330 (define-public (chain-grob-member-functions grob value . funcs)
333 (set! value (func grob value)))
338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 (define-public (fall::print spanner)
343 ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
344 (left-span (ly:spanner-bound spanner LEFT))
345 (right-span (ly:spanner-bound spanner RIGHT))
346 (thickness (* (ly:grob-property spanner 'thickness)
347 (ly:output-def-lookup (ly:grob-layout spanner)
349 (padding (ly:grob-property spanner 'padding 0.5))
350 (common (ly:grob-common-refpoint right-span
351 (ly:grob-common-refpoint spanner
355 (interval-end (ly:grob-robust-relative-extent
356 left-span common X))))
357 (right-x (- (interval-start
358 (ly:grob-robust-relative-extent right-span common X))
360 (self-x (ly:grob-relative-coordinate spanner common X))
361 (dx (- right-x left-x))
362 (exp (list 'path thickness
370 ,dx ,(* 0.66 delta-y)
378 (cons (min 0 delta-y)
381 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
385 (define-public (grace-spacing::calc-shortest-duration grob)
387 ((cols (ly:grob-object grob 'columns))
390 (ly:moment-sub (ly:grob-property
391 (ly:grob-array-ref cols (1+ idx)) 'when)
393 (ly:grob-array-ref cols idx) 'when))))
395 (moment-min (lambda (x y)
398 (if (ly:moment<? x y)
404 (fold moment-min #f (map get-difference
405 (iota (1- (ly:grob-array-length cols)))))))
409 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
412 (define-public (fingering::calc-text grob)
414 ((event (event-cause grob))
415 (digit (ly:event-property event 'digit)))
418 (ly:input-message (ly:event-property event 'origin)
419 "Music for the martians"))
421 (number->string digit 10)
424 (define-public (string-number::calc-text grob)
426 ((digit (ly:event-property (event-cause grob) 'string-number)))
428 (number->string digit 10)
432 (define-public (stroke-finger::calc-text grob)
434 ((digit (ly:event-property (event-cause grob) 'digit))
435 (text (ly:event-property (event-cause grob) 'text)))
439 (vector-ref (ly:grob-property grob 'digit-names) (1- (max (min 5 digit) 1))))))
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
443 (define-public (hairpin::calc-grow-direction grob)
444 (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
452 (define-public (lyric-text::print grob)
453 "Allow interpretation of tildes as lyric tieing marks."
456 ((text (ly:grob-property grob 'text))
457 (layout (ly:grob-layout grob))
458 (defs (ly:output-def-lookup layout 'text-font-defaults))
459 (props (ly:grob-alist-chain grob defs)))
461 (ly:text-interface::interpret-markup layout
464 (make-tied-lyric-markup text)
467 (define-public ((grob::calc-property-by-copy prop) grob)
468 (ly:event-property (event-cause grob) prop))
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
473 (define (string-frets->description string-frets string-count)
476 (map (lambda (x) (list 'mute (1+ x)))
477 (iota string-count)))))
479 (for-each (lambda (sf)
486 (vector-set! desc (1- string)
490 (list 'place-fret string fret finger)
491 (list 'place-fret string fret))
498 (vector->list desc)))
500 (define-public (fret-board::calc-stencil grob)
501 (let* ((string-frets (ly:grob-property grob 'string-fret-finger-combinations))
502 (string-count (ly:grob-property grob 'string-count))
503 (layout (ly:grob-layout grob))
504 (defs (ly:output-def-lookup layout 'text-font-defaults))
505 (props (ly:grob-alist-chain grob defs)))
507 (make-fret-diagram layout props
508 (string-frets->description string-frets 6))))