]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
rewrite balloon-text support. This is now a separate grob, with its
[lilypond.git] / scm / output-lib.scm
1 ;;;; output-lib.scm -- implement Scheme output helper functions
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8
9
10
11 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12 ;; general
13
14 (define-public (make-stencil-boxer thickness padding callback)
15
16   "Return function that adds a box around the grob passed as argument."
17   (lambda (grob)
18     
19     (box-stencil (callback grob) thickness padding)))
20
21 (define-public (make-stencil-circler thickness padding callback)
22   "Return function that adds a circle around the grob passed as argument."
23   
24   (lambda (grob) (circle-stencil (callback grob) thickness padding)))
25
26 (define-public (print-circled-text-callback grob)
27   (let* ((text (ly:grob-property grob 'text))
28          
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))))
34     circle))
35
36 (define-public (music-cause grob)
37   (let*
38       ((event (event-cause grob)))
39
40     (if (ly:stream-event? event)
41         (ly:event-property event 'music-cause)
42         #f)))
43
44 (define-public (event-cause grob)
45   (let*
46       ((cause (ly:grob-property  grob 'cause)))
47     
48     (cond
49      ((ly:stream-event? cause) cause)
50      ((ly:grob? cause) (event-cause cause))
51      (else #f))))
52
53 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
54 ;; tablature
55
56 ;; The TabNoteHead tablatureFormat callback.
57 ;; Compute the text grob-property
58 (define-public (fret-number-tablature-format string tuning pitch)
59   (make-whiteout-markup
60    (make-vcenter-markup  
61     (number->string
62      (- (ly:pitch-semitones pitch)
63         (list-ref tuning
64                   ;; remove 1 because list index starts at 0 and guitar string at 1. 
65                   (- string 1)))))))
66
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
71 ;;   on the banjo neck.
72 ;; We solve this by defining a new fret-number-tablature function:
73 (define-public (fret-number-tablature-format-banjo string tuning pitch)
74   (make-whiteout-markup
75    (make-vcenter-markup  
76     (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (- string 1)))))
77       (number->string (cond
78                        ((and (> fret 0) (= string 5))
79                         (+ fret 5))
80                        (else fret)))))))
81
82
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))
88
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))))
98
99 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
100 ;; note heads
101
102 (define-public (note-head::calc-duration-log grob)
103   (ly:duration-log
104    (ly:event-property (event-cause grob) 'duration)))
105
106 (define-public (dots::calc-dot-count grob)
107   (ly:duration-dot-count
108    (ly:event-property (event-cause grob) 'duration)))
109
110 (define (note-head::calc-tablature-stem-attachment grob)
111   (cons 0.0 1.35))
112
113
114
115 ;; silly, use alist? 
116 (define-public (note-head::calc-glyph-name grob)
117   (let*
118       ((style (ly:grob-property grob 'style))
119        (log (min 2 (ly:grob-property grob 'duration-log))))
120     
121     (case style
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
125       ;; statement.  -- jr
126       ((xcircle) "2xcircle")
127       ((harmonic) "0harmonic")
128       ((baroque) 
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
133        (if (< log 0)
134            (string-append (number->string log) "neomensural")
135            (number->string log)))
136       ((mensural)
137        (string-append (number->string log) (symbol->string style)))
138       ((petrucci)
139        (if (< log 0)
140            (string-append (number->string log) "mensural")
141            (string-append (number->string log) (symbol->string style))))
142       ((neomensural)
143        (string-append (number->string log) (symbol->string style)))
144       (else
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)))))))
149
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."
155
156   '(1.0 . 0.0))
157
158
159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
160 ;; bar numbers
161
162 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
163
164 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
165
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)))))
169
170 (define-public (first-bar-number-invisible barnum) (> barnum 1))
171
172 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
173 ;; break visibility
174
175 (define-public begin-of-line-visible
176   #(#f #f #t))
177 (define-public end-of-line-visible
178   #(#t #f #f))
179 (define-public end-of-line-invisible
180   #(#f #t #t))
181 (define-public spanbar-begin-of-line-invisible
182   #(#t #f #f))
183 (define-public all-visible #(#t #t #t))
184 (define-public all-invisible #(#f #f #f))
185 (define-public begin-of-line-invisible
186   #(#t #t #f))
187 (define-public center-invisible #(#t #f #t))
188
189 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 ;; Bar lines.
191
192 ;;
193 ;; How should a  bar line behave at a break? 
194 ;;
195 ;; Why prepend `default-' to every scm identifier?
196 (define-public (bar-line::calc-glyph-name grob)
197   (let* (
198          (glyph (ly:grob-property grob 'glyph))
199          (dir (ly:item-break-dir grob))
200          (result (assoc glyph 
201                        '((":|:" . (":|" . "|:"))
202                          ("||:" . ("||" . "|:"))
203                          ("dashed" . ("dashed" . '())) 
204                          ("|" . ("|" . ()))
205                          ("||:" . ("||" . "|:"))
206                          ("|s" . (() . "|"))
207                          ("|:" . ("|" . "|:"))
208                          ("|." . ("|." . ()))
209
210                          ;; hmm... should we end with a bar line here?
211                          (".|" . ("|" . ".|"))
212                          (":|" . (":|" . ()))
213                          ("||" . ("||" . ()))
214                          (".|." . (".|." . ()))
215                          ("" . ("" . ""))
216                          (":" . (":" . ""))
217                          ("." . ("." . ()))
218                          ("empty" . (() . ()))
219                          ("brace" . (() . "brace"))
220                          ("bracket" . (() . "bracket"))  )))
221          (glyph-name (if (= dir CENTER)
222                          glyph
223                          (if (and result (string? (index-cell (cdr result) dir)))
224                              (index-cell (cdr result) dir)
225                              #f)))
226          )
227
228     (if (not glyph-name)
229         (ly:grob-suicide! grob))
230
231     glyph-name))
232
233
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)))
239
240
241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
242 ;; Tuplets
243
244 (define-public (tuplet-number::calc-denominator-text grob)
245   (number->string (ly:event-property (event-cause grob) 'denominator)))
246
247 (define-public (tuplet-number::calc-fraction-text grob)
248   (let*
249       ((ev (event-cause grob)))
250
251     (format "~a:~a" 
252             (ly:event-property ev 'denominator)
253             (ly:event-property ev 'numerator))))
254
255 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
256 ;; Color
257
258 (define-public color? list?)
259
260 ; predefined colors
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))
269
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))
277
278 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
279 ;; * Pitch Trill Heads
280 ;; * Parentheses
281
282 (define (parenthesize-elements grob . rest)
283   (let*
284       ((refp (if (null? rest)
285                  grob
286                  (car rest)))
287        (elts (ly:grob-object grob 'elements))
288        (x-ext (ly:relative-group-extent elts refp X))
289
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)))
294     
295     (ly:stencil-add
296      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
297      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
298   ))
299
300
301 (define (parentheses-item::print me)
302   (let*
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)))
309
310     (ly:stencil-translate
311      stencil
312      (cons
313       (-
314        (ly:grob-relative-coordinate me x-ref X))
315       (-
316        y-center
317        (ly:grob-relative-coordinate me y-ref Y))))
318     ))
319
320
321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
322 ;; 
323
324 (define-public (chain-grob-member-functions grob value . funcs)
325   (for-each
326    (lambda (func)
327      (set! value (func grob value)))
328    funcs)
329
330   value)
331
332 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
333 ;; falls
334
335 (define-public (fall::print spanner)
336   (let*
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)
342                                            'line-thickness)))
343        (padding (ly:grob-property spanner 'padding 0.5))
344        (common (ly:grob-common-refpoint right-span
345                                         (ly:grob-common-refpoint spanner
346                                                                  left-span X)
347                                         X))
348        (left-x (+ padding
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))
353                    padding))
354        (self-x (ly:grob-relative-coordinate spanner common X))
355        (dx (- right-x left-x))
356        (exp (list 'path thickness 
357                   `(quote
358                     (rmoveto
359                      ,(- left-x self-x) 0
360
361                      rcurveto                
362                      ,(/ dx 3)
363                      0
364                      ,dx ,(* 0.66 delta-y)
365                      ,dx ,delta-y
366                      ))))
367        )
368
369     (ly:make-stencil
370      exp
371      (cons 0 dx)
372      (cons (min 0 delta-y)
373            (max 0 delta-y)))))
374
375 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
376 ;; grace spacing
377
378
379 (define-public (grace-spacing::calc-shortest-duration grob)
380   (let*
381      ((cols (ly:grob-object grob 'columns))
382       (get-difference
383        (lambda (idx)
384          (ly:moment-sub (ly:grob-property
385                          (ly:grob-array-ref cols (1+ idx)) 'when)
386                         (ly:grob-property
387                          (ly:grob-array-ref cols idx) 'when))))
388       
389       (moment-min (lambda (x y)
390                     (cond
391                      ((and x y)
392                       (if (ly:moment<? x y)
393                             x
394                             y))
395                      (x x)
396                      (y y)))))
397                      
398     (fold moment-min #f (map get-difference
399                              (iota (1- (ly:grob-array-length cols)))))))
400
401
402
403 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
404 ;; fingering
405
406 (define-public (fingering::calc-text grob)
407   (let*
408       ((event (event-cause grob))
409        (digit (ly:event-property event 'digit)))
410     
411     (if (> digit 5)
412         (ly:input-message (ly:event-property event 'origin)
413                           "Music for the martians"))
414
415     (number->string digit 10)
416   ))
417
418 (define-public (string-number::calc-text grob)
419   (let*
420       ((digit (ly:event-property (event-cause  grob) 'string-number)))
421     
422     (number->string digit 10)
423   ))
424
425
426 (define-public (stroke-finger::calc-text grob)
427   (let*
428       ((digit (ly:event-property (event-cause grob) 'digit))
429        (text (ly:event-property (event-cause grob) 'text)))
430
431     (if (string? text)
432         text
433         (vector-ref (ly:grob-property grob 'digit-names)  (1- (max (min 5 digit) 1))))))
434
435 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
436 ;; dynamics
437 (define-public (hairpin::calc-grow-direction grob)
438   (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
439       START
440       STOP
441       ))
442
443 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
444 ;; lyrics
445
446 (define-public (lyric-text::print grob)
447   "Allow interpretation of tildes as lyric tieing marks."
448   
449   (let*
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)))
454
455     (ly:text-interface::interpret-markup layout
456                                          props
457                                          (if (string? text)
458                                              (make-tied-lyric-markup text)
459                                              text))))
460
461 (define-public ((grob::calc-property-by-copy prop) grob)
462   (ly:event-property (event-cause grob) prop))
463
464 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465 ;; fret boards
466
467 (define (string-frets->description string-frets string-count)
468   (let*
469       ((desc (list->vector
470               (map (lambda (x) (list 'mute  (1+ x)))
471                    (iota string-count)))))
472        
473        (for-each (lambda (sf)
474                    (let*
475                        ((string (car sf))
476                         (fret (cadr sf))
477                         (finger (caddr sf)))
478
479                      
480                      (vector-set! desc (1- string)
481                                   (if (= 0 fret)
482                                       (list 'open string)
483                                       (if finger
484                                           (list 'place-fret string fret finger) 
485                                           (list 'place-fret string fret))
486                                           
487
488                                       ))
489                      ))
490                  string-frets)
491
492        (vector->list desc)))
493
494 (define-public (fret-board::calc-stencil grob)
495   (let* ((string-frets (ly:grob-property grob 'string-fret-finger-combinations))
496          (string-count (ly:grob-property grob 'string-count))
497          (layout (ly:grob-layout grob))
498          (defs (ly:output-def-lookup layout 'text-font-defaults))
499          (props (ly:grob-alist-chain grob defs)))
500
501     (make-fret-diagram layout props
502                        (string-frets->description string-frets 6))))