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
61 ((tuning (ly:context-property context 'stringTunings))
62 (pitch (ly:event-property event 'pitch))
67 (eq? 'harmonic-event (ly:event-property ev 'class)))
68 (ly:event-property event 'articulations)))))
75 (- (ly:pitch-semitones pitch)
77 ;; remove 1 because list index starts at 0 and guitar string at 1.
81 ;; The 5-string banjo has got a extra string, the fifth (duh), wich
82 ;; starts at the fifth fret on the neck. Frets on the fifth string
83 ;; are referred to relative to the other frets:
84 ;; the "first fret" on the fifth string is really the sixth fret
86 ;; We solve this by defining a new fret-number-tablature function:
87 (define-public (fret-number-tablature-format-banjo string
90 ((tuning (ly:context-property context 'stringTunings))
91 (pitch (ly:event-property event 'pitch))
95 (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (- string 1)))))
97 ((and (> fret 0) (= string 5))
103 ; default tunings for common string instruments
104 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
105 (define-public guitar-open-g-tuning '(2 -1 -5 -10 -17 -22))
106 (define-public bass-tuning '(-17 -22 -27 -32))
107 (define-public mandolin-tuning '(16 9 2 -5))
109 ;; tunings for 5-string banjo
110 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
111 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
112 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
113 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
114 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
115 ;; convert 5-string banjo tuning to 4-string by removing the 5th string
116 (define-public (four-string-banjo tuning)
117 (reverse (cdr (reverse tuning))))
119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
123 (define-public (stem::calc-duration-log grob)
125 (ly:event-property (event-cause grob) 'duration)))
127 (define-public (note-head::calc-duration-log grob)
130 (ly:event-property (event-cause grob) 'duration))))
132 (define-public (dots::calc-dot-count grob)
133 (ly:duration-dot-count
134 (ly:event-property (event-cause grob) 'duration)))
136 (define (note-head::calc-tablature-stem-attachment grob)
142 (define-public (note-head::calc-glyph-name grob)
144 ((style (ly:grob-property grob 'style))
145 (log (min 2 (ly:grob-property grob 'duration-log))))
148 ;; "default" style is directly handled in note-head.cc as a
149 ;; special case (HW says, mainly for performance reasons).
150 ;; Therefore, style "default" does not appear in this case
152 ((xcircle) "2xcircle")
153 ((harmonic) "0harmonic")
155 ;; Oops, I actually would not call this "baroque", but, for
156 ;; backwards compatibility to 1.4, this is supposed to take
157 ;; brevis, longa and maxima from the neo-mensural font and all
158 ;; other note heads from the default font. -- jr
160 (string-append (number->string log) "neomensural")
161 (number->string log)))
163 (string-append (number->string log) (symbol->string style)))
166 (string-append (number->string log) "mensural")
167 (string-append (number->string log) (symbol->string style))))
169 (string-append (number->string log) (symbol->string style)))
171 (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
172 (symbol->string style)
173 (string-append (number->string (max 0 log))
174 (symbol->string style)))))))
176 ;; TODO junk completely?
177 (define (note-head-style->attachment-coordinates grob axis)
178 "Return pair (X . Y), containing multipliers for the note head
179 bounding box, where to attach the stem. e.g.: X==0 means horizontally
180 centered, X==1 is at the right, X == -1 is at the left."
185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
190 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
192 (define-public ((set-bar-number-visibility n) tr)
193 (let* ((bn (ly:context-property tr 'currentBarNumber)))
194 (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
196 (define-public (first-bar-number-invisible barnum) (> barnum 1))
198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
201 (define-public begin-of-line-visible
203 (define-public end-of-line-visible
205 (define-public end-of-line-invisible
207 (define-public spanbar-begin-of-line-invisible
209 (define-public all-visible #(#t #t #t))
210 (define-public all-invisible #(#f #f #f))
211 (define-public begin-of-line-invisible
213 (define-public center-invisible #(#t #f #t))
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 ;; How should a bar line behave at a break?
221 ;; Why prepend `default-' to every scm identifier?
222 (define-public (bar-line::calc-glyph-name grob)
224 (glyph (ly:grob-property grob 'glyph))
225 (dir (ly:item-break-dir grob))
227 '((":|:" . (":|" . "|:"))
228 ("||:" . ("||" . "|:"))
229 ("dashed" . ("dashed" . '()))
231 ("||:" . ("||" . "|:"))
233 ("|:" . ("|" . "|:"))
236 ;; hmm... should we end with a bar line here?
237 (".|" . ("|" . ".|"))
240 (".|." . (".|." . ()))
244 ("empty" . (() . ()))
245 ("brace" . (() . "brace"))
246 ("bracket" . (() . "bracket")) )))
247 (glyph-name (if (= dir CENTER)
249 (if (and result (string? (index-cell (cdr result) dir)))
250 (index-cell (cdr result) dir)
255 (ly:grob-suicide! grob))
260 (define-public (shift-right-at-line-begin g)
261 "Shift an item to the right, but only at the start of the line."
262 (if (and (ly:item? g)
263 (equal? (ly:item-break-dir g) RIGHT))
264 (ly:grob-translate-axis! g 3.5 X)))
267 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 (define-public (tuplet-number::calc-denominator-text grob)
271 (number->string (ly:event-property (event-cause grob) 'denominator)))
273 (define-public (tuplet-number::calc-fraction-text grob)
275 ((ev (event-cause grob)))
278 (ly:event-property ev 'denominator)
279 (ly:event-property ev 'numerator))))
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 (define-public color? list?)
287 (define-public black '(0.0 0.0 0.0))
288 (define-public white '(1.0 1.0 1.0))
289 (define-public red '(1.0 0.0 0.0))
290 (define-public green '(0.0 1.0 0.0))
291 (define-public blue '(0.0 0.0 1.0))
292 (define-public cyan '(0.0 1.0 1.0))
293 (define-public magenta '(1.0 0.0 1.0))
294 (define-public yellow '(1.0 1.0 0.0))
296 (define-public grey '(0.5 0.5 0.5))
297 (define-public darkred '(0.5 0.0 0.0))
298 (define-public darkgreen '(0.0 0.5 0.0))
299 (define-public darkblue '(0.0 0.0 0.5))
300 (define-public darkcyan '(0.0 0.5 0.5))
301 (define-public darkmagenta '(0.5 0.0 0.5))
302 (define-public darkyellow '(0.5 0.5 0.0))
304 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
305 ;; * Pitch Trill Heads
308 (define-public (parentheses-item::calc-parenthesis-stencils grob)
310 (font (ly:grob-default-font grob))
311 (lp (ly:font-get-glyph font "accidentals.leftparen"))
312 (rp (ly:font-get-glyph font "accidentals.rightparen"))
318 (define (grob-text grob text)
320 ((layout (ly:grob-layout grob))
321 (defs (ly:output-def-lookup layout 'text-font-defaults))
322 (props (ly:grob-alist-chain grob defs)))
324 (ly:text-interface::interpret-markup
327 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
329 (font (ly:grob-default-font grob))
330 (lp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-text grob (ly:wide-char->utf-8 #x2329))
332 (rp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-text grob (ly:wide-char->utf-8 #x232A))
336 (list (stencil-whiteout lp)
337 (stencil-whiteout rp))))
339 (define (parenthesize-elements grob . rest)
341 ((refp (if (null? rest)
344 (elts (ly:grob-object grob 'elements))
345 (x-ext (ly:relative-group-extent elts refp X))
346 (stencils (ly:grob-property grob 'stencils))
349 (padding (ly:grob-property grob 'padding 0.1)))
352 (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
353 (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
357 (define (parentheses-item::print me)
359 ((elts (ly:grob-object me 'elements))
360 (y-ref (ly:grob-common-refpoint-of-array me elts Y))
361 (x-ref (ly:grob-common-refpoint-of-array me elts X))
362 (stencil (parenthesize-elements me x-ref))
363 (elt-y-ext (ly:relative-group-extent elts y-ref Y))
364 (y-center (interval-center elt-y-ext)))
366 (ly:stencil-translate
370 (ly:grob-relative-coordinate me x-ref X))
373 (ly:grob-relative-coordinate me y-ref Y))))
378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
381 (define-public (chain-grob-member-functions grob value . funcs)
384 (set! value (func grob value)))
389 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
392 (define-public (fall::print spanner)
394 ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
395 (left-span (ly:spanner-bound spanner LEFT))
396 (right-span (ly:spanner-bound spanner RIGHT))
397 (thickness (* (ly:grob-property spanner 'thickness)
398 (ly:output-def-lookup (ly:grob-layout spanner)
400 (padding (ly:grob-property spanner 'padding 0.5))
401 (common (ly:grob-common-refpoint right-span
402 (ly:grob-common-refpoint spanner
406 (interval-end (ly:grob-robust-relative-extent
407 left-span common X))))
408 (right-x (- (interval-start
409 (ly:grob-robust-relative-extent right-span common X))
411 (self-x (ly:grob-relative-coordinate spanner common X))
412 (dx (- right-x left-x))
413 (exp (list 'path thickness
421 ,dx ,(* 0.66 delta-y)
429 (cons (min 0 delta-y)
432 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
436 (define-public (grace-spacing::calc-shortest-duration grob)
438 ((cols (ly:grob-object grob 'columns))
441 (ly:moment-sub (ly:grob-property
442 (ly:grob-array-ref cols (1+ idx)) 'when)
444 (ly:grob-array-ref cols idx) 'when))))
446 (moment-min (lambda (x y)
449 (if (ly:moment<? x y)
455 (fold moment-min #f (map get-difference
456 (iota (1- (ly:grob-array-length cols)))))))
460 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
463 (define-public (fingering::calc-text grob)
465 ((event (event-cause grob))
466 (digit (ly:event-property event 'digit)))
469 (ly:input-message (ly:event-property event 'origin)
470 "Warning: Fingering notation for finger number ~a" digit))
472 (number->string digit 10)
475 (define-public (string-number::calc-text grob)
477 ((digit (ly:event-property (event-cause grob) 'string-number)))
479 (number->string digit 10)
483 (define-public (stroke-finger::calc-text grob)
485 ((digit (ly:event-property (event-cause grob) 'digit))
486 (text (ly:event-property (event-cause grob) 'text)))
490 (vector-ref (ly:grob-property grob 'digit-names) (1- (max (min 5 digit) 1))))))
492 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
494 (define-public (hairpin::calc-grow-direction grob)
495 (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
500 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
503 (define-public (lyric-text::print grob)
504 "Allow interpretation of tildes as lyric tieing marks."
507 ((text (ly:grob-property grob 'text))
508 (layout (ly:grob-layout grob))
509 (defs (ly:output-def-lookup layout 'text-font-defaults))
510 (props (ly:grob-alist-chain grob defs)))
512 (ly:text-interface::interpret-markup layout
515 (make-tied-lyric-markup text)
518 (define-public ((grob::calc-property-by-copy prop) grob)
519 (ly:event-property (event-cause grob) prop))
521 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
524 (define (string-frets->description string-frets string-count)
527 (map (lambda (x) (list 'mute (1+ x)))
528 (iota string-count)))))
530 (for-each (lambda (sf)
537 (vector-set! desc (1- string)
541 (list 'place-fret string fret finger)
542 (list 'place-fret string fret))
549 (vector->list desc)))
551 (define-public (fret-board::calc-stencil grob)
552 (let* ((string-frets (ly:grob-property grob 'string-fret-finger-combinations))
553 (string-count (ly:grob-property grob 'string-count))
554 (layout (ly:grob-layout grob))
555 (defs (ly:output-def-lookup layout 'text-font-defaults))
556 (props (ly:grob-alist-chain grob defs)))
558 (make-fret-diagram layout props
559 (string-frets->description string-frets 6))))