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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
102 (define-public (note-head::calc-duration-log grob)
104 (ly:event-property (event-cause grob) 'duration)))
106 (define-public (dots::calc-dot-count grob)
107 (ly:duration-dot-count
108 (ly:event-property (event-cause grob) 'duration)))
110 (define (note-head::calc-tablature-stem-attachment grob)
116 (define-public (note-head::calc-glyph-name grob)
118 ((style (ly:grob-property grob 'style))
119 (log (min 2 (ly:grob-property grob 'duration-log))))
122 ;; "default" style is directly handled in note-head.cc as a
123 ;; special case (HW says, mainly for performance reasons).
124 ;; Therefore, style "default" does not appear in this case
126 ((xcircle) "2xcircle")
127 ((harmonic) "0harmonic")
129 ;; Oops, I actually would not call this "baroque", but, for
130 ;; backwards compatibility to 1.4, this is supposed to take
131 ;; brevis, longa and maxima from the neo-mensural font and all
132 ;; other note heads from the default font. -- jr
134 (string-append (number->string log) "neomensural")
135 (number->string log)))
137 (string-append (number->string log) (symbol->string style)))
140 (string-append (number->string log) "mensural")
141 (string-append (number->string log) (symbol->string style))))
143 (string-append (number->string log) (symbol->string style)))
145 (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
146 (symbol->string style)
147 (string-append (number->string (max 0 log))
148 (symbol->string style)))))))
150 ;; TODO junk completely?
151 (define (note-head-style->attachment-coordinates grob axis)
152 "Return pair (X . Y), containing multipliers for the note head
153 bounding box, where to attach the stem. e.g.: X==0 means horizontally
154 centered, X==1 is at the right, X == -1 is at the left."
159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
162 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
164 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
166 (define-public ((set-bar-number-visibility n) tr)
167 (let* ((bn (ly:context-property tr 'currentBarNumber)))
168 (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
170 (define-public (first-bar-number-invisible barnum) (> barnum 1))
172 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175 (define-public begin-of-line-visible
177 (define-public end-of-line-visible
179 (define-public end-of-line-invisible
181 (define-public spanbar-begin-of-line-invisible
183 (define-public all-visible #(#t #t #t))
184 (define-public all-invisible #(#f #f #f))
185 (define-public begin-of-line-invisible
187 (define-public center-invisible #(#t #f #t))
189 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 ;; How should a bar line behave at a break?
195 ;; Why prepend `default-' to every scm identifier?
196 (define-public (bar-line::calc-glyph-name grob)
198 (glyph (ly:grob-property grob 'glyph))
199 (dir (ly:item-break-dir grob))
201 '((":|:" . (":|" . "|:"))
202 ("||:" . ("||" . "|:"))
203 ("dashed" . ("dashed" . '()))
205 ("||:" . ("||" . "|:"))
207 ("|:" . ("|" . "|:"))
210 ;; hmm... should we end with a bar line here?
211 (".|" . ("|" . ".|"))
214 (".|." . (".|." . ()))
218 ("empty" . (() . ()))
219 ("brace" . (() . "brace"))
220 ("bracket" . (() . "bracket")) )))
221 (glyph-name (if (= dir CENTER)
223 (if (and result (string? (index-cell (cdr result) dir)))
224 (index-cell (cdr result) dir)
229 (ly:grob-suicide! grob))
234 (define-public (shift-right-at-line-begin g)
235 "Shift an item to the right, but only at the start of the line."
236 (if (and (ly:item? g)
237 (equal? (ly:item-break-dir g) RIGHT))
238 (ly:grob-translate-axis! g 3.5 X)))
241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
244 (define-public (tuplet-number::calc-denominator-text grob)
245 (number->string (ly:event-property (event-cause grob) 'denominator)))
247 (define-public (tuplet-number::calc-fraction-text grob)
249 ((ev (event-cause grob)))
252 (ly:event-property ev 'denominator)
253 (ly:event-property ev 'numerator))))
255 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
258 (define-public color? list?)
261 (define-public black '(0.0 0.0 0.0))
262 (define-public white '(1.0 1.0 1.0))
263 (define-public red '(1.0 0.0 0.0))
264 (define-public green '(0.0 1.0 0.0))
265 (define-public blue '(0.0 0.0 1.0))
266 (define-public cyan '(0.0 1.0 1.0))
267 (define-public magenta '(1.0 0.0 1.0))
268 (define-public yellow '(1.0 1.0 0.0))
270 (define-public grey '(0.5 0.5 0.5))
271 (define-public darkred '(0.5 0.0 0.0))
272 (define-public darkgreen '(0.0 0.5 0.0))
273 (define-public darkblue '(0.0 0.0 0.5))
274 (define-public darkcyan '(0.0 0.5 0.5))
275 (define-public darkmagenta '(0.5 0.0 0.5))
276 (define-public darkyellow '(0.5 0.5 0.0))
278 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
279 ;; * Pitch Trill Heads
282 (define (parenthesize-elements grob . rest)
284 ((refp (if (null? rest)
287 (elts (ly:grob-object grob 'elements))
288 (x-ext (ly:relative-group-extent elts refp X))
290 (font (ly:grob-default-font grob))
291 (lp (ly:font-get-glyph font "accidentals.leftparen"))
292 (rp (ly:font-get-glyph font "accidentals.rightparen"))
293 (padding (ly:grob-property grob 'padding 0.1)))
296 (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
297 (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
301 (define (parentheses-item::print me)
303 ((elts (ly:grob-object me 'elements))
304 (y-ref (ly:grob-common-refpoint-of-array me elts Y))
305 (x-ref (ly:grob-common-refpoint-of-array me elts X))
306 (stencil (parenthesize-elements me x-ref))
307 (elt-y-ext (ly:relative-group-extent elts y-ref Y))
308 (y-center (interval-center elt-y-ext)))
310 (ly:stencil-translate
314 (ly:grob-relative-coordinate me x-ref X))
317 (ly:grob-relative-coordinate me y-ref Y))))
321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 (define-public (chain-grob-member-functions grob value . funcs)
327 (set! value (func grob value)))
332 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
335 (define-public (fall::print spanner)
337 ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
338 (left-span (ly:spanner-bound spanner LEFT))
339 (right-span (ly:spanner-bound spanner RIGHT))
340 (thickness (* (ly:grob-property spanner 'thickness)
341 (ly:output-def-lookup (ly:grob-layout spanner)
343 (padding (ly:grob-property spanner 'padding 0.5))
344 (common (ly:grob-common-refpoint right-span
345 (ly:grob-common-refpoint spanner
349 (interval-end (ly:grob-robust-relative-extent
350 left-span common X))))
351 (right-x (- (interval-start
352 (ly:grob-robust-relative-extent right-span common X))
354 (self-x (ly:grob-relative-coordinate spanner common X))
355 (dx (- right-x left-x))
356 (exp (list 'path thickness
364 ,dx ,(* 0.66 delta-y)
372 (cons (min 0 delta-y)
375 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 (define-public (grace-spacing::calc-shortest-duration grob)
381 ((cols (ly:grob-object grob 'columns))
384 (ly:moment-sub (ly:grob-property
385 (ly:grob-array-ref cols (1+ idx)) 'when)
387 (ly:grob-array-ref cols idx) 'when))))
389 (moment-min (lambda (x y)
392 (if (ly:moment<? x y)
398 (fold moment-min #f (map get-difference
399 (iota (1- (ly:grob-array-length cols)))))))
403 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
406 (define-public (fingering::calc-text grob)
408 ((event (event-cause grob))
409 (digit (ly:event-property event 'digit)))
412 (ly:input-message (ly:event-property event 'origin)
413 "Music for the martians"))
415 (number->string digit 10)
418 (define-public (string-number::calc-text grob)
420 ((digit (ly:event-property (event-cause grob) 'string-number)))
422 (number->string digit 10)
426 (define-public (stroke-finger::calc-text grob)
428 ((digit (ly:event-property (event-cause grob) 'digit))
429 (text (ly:event-property (event-cause grob) 'text)))
433 (vector-ref (ly:grob-property grob 'digit-names) (1- (max (min 5 digit) 1))))))
435 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
437 (define-public (hairpin::calc-grow-direction grob)
438 (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
443 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
446 (define-public (lyric-text::print grob)
447 "Allow interpretation of tildes as lyric tieing marks."
450 ((text (ly:grob-property grob 'text))
451 (layout (ly:grob-layout grob))
452 (defs (ly:output-def-lookup layout 'text-font-defaults))
453 (props (ly:grob-alist-chain grob defs)))
455 (ly:text-interface::interpret-markup layout
458 (make-tied-lyric-markup text)
461 (define-public (lyric-text::calc-text grob)
462 (ly:event-property (event-cause grob) 'text))
464 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
467 (define (string-frets->description string-frets string-count)
470 (map (lambda (x) (list 'mute (1+ x)))
471 (iota string-count)))))
473 (for-each (lambda (sf)
479 (vector-set! desc (1- string)
482 (list 'place-fret string fret)))
485 (vector->list desc)))
487 (define-public (fret-board::calc-stencil grob)
488 (let* ((string-frets (ly:grob-property grob 'string-frets))
489 (string-count (ly:grob-property grob 'string-count))
490 (layout (ly:grob-layout grob))
491 (defs (ly:output-def-lookup layout 'text-font-defaults))
492 (props (ly:grob-alist-chain grob defs)))
494 (make-fret-diagram layout props
495 (string-frets->description string-frets 6))))