]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
* scm/backend-library.scm: update email address.
[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
37 (define-public (event-cause grob)
38   (let*
39       ((cause (ly:grob-property  grob 'cause)))
40     
41     (cond
42      ((ly:stream-event? cause) cause)
43      ((ly:grob? cause) (event-cause cause))
44      (else #f))))
45
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 ;; tablature
48
49 ;; The TabNoteHead tablatureFormat callback.
50 ;; Compute the text grob-property
51 (define-public (fret-number-tablature-format string tuning pitch)
52   (make-whiteout-markup
53    (make-vcenter-markup  
54     (number->string
55      (- (ly:pitch-semitones pitch)
56         (list-ref tuning
57                   ;; remove 1 because list index starts at 0 and guitar string at 1. 
58                   (- string 1)))))))
59
60 ;; The 5-string banjo has got a extra string, the fifth (duh), wich
61 ;; starts at the fifth fret on the neck. Frets on the fifth string
62 ;; are referred to relative to the other frets:
63 ;;   the "first fret" on the fifth string is really the sixth fret
64 ;;   on the banjo neck.
65 ;; We solve this by defining a new fret-number-tablature function:
66 (define-public (fret-number-tablature-format-banjo string tuning pitch)
67   (make-whiteout-markup
68    (make-vcenter-markup  
69     (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (- string 1)))))
70       (number->string (cond
71                        ((and (> fret 0) (= string 5))
72                         (+ fret 5))
73                        (else fret)))))))
74
75
76 ; default tunings for common string instruments
77 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
78 (define-public guitar-open-g-tuning '(2 -1 -5 -10 -17 -22))
79 (define-public bass-tuning '(-17 -22 -27 -32))
80 (define-public mandolin-tuning '(16 9 2 -5))
81
82 ;; tunings for 5-string banjo
83 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
84 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
85 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
86 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
87 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
88 ;; convert 5-string banjo tuning to 4-string by removing the 5th string
89 (define-public (four-string-banjo tuning)
90   (reverse (cdr (reverse tuning))))
91
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 ;; note heads
94
95 (define-public (note-head::calc-duration-log grob)
96   (ly:duration-log
97    (ly:event-property (event-cause grob) 'duration)))
98
99 (define-public (dots::calc-dot-count grob)
100   (ly:duration-dot-count
101    (ly:event-property (event-cause grob) 'duration)))
102
103 (define (note-head::calc-tablature-stem-attachment grob)
104   (cons 0.0 1.35))
105
106
107
108 ;; silly, use alist? 
109 (define-public (note-head::calc-glyph-name grob)
110   (let*
111       ((style (ly:grob-property grob 'style))
112        (log (min 2 (ly:grob-property grob 'duration-log))))
113     
114     (case style
115       ((xcircle) "2xcircle")
116       ((harmonic) "0harmonic")
117       ((baroque) 
118        ;; Oops, I actually would not call this "baroque", but, for
119        ;; backwards compatibility to 1.4, this is supposed to take
120        ;; brevis, longa and maxima from the neo-mensural font and all
121        ;; other note heads from the default font.  -- jr
122        (if (< log 0)
123            (string-append (number->string log) "neomensural")
124            (number->string log)))
125       ((mensural)
126        (string-append (number->string log) (symbol->string style)))
127       ((petrucci)
128        (if (< log 0)
129            (string-append (number->string log) "mensural")
130            (string-append (number->string log) (symbol->string style))))
131       ((neomensural)
132        (string-append (number->string log) (symbol->string style)))
133       ((default)
134        ;; The default font in mf/feta-bolletjes.mf defines a brevis, but
135        ;; neither a longa nor a maxima.  Hence let us, for the moment,
136        ;; take these from the neo-mensural font.  TODO: mf/feta-bolletjes
137        ;; should define at least a longa for the default font.  The longa
138        ;; should look exactly like the brevis of the default font, but
139        ;; with a stem exactly like that of the quarter note. -- jr
140        (if (< log -1)
141            (string-append (number->string log) "neomensural")
142            (number->string log)))
143       (else
144        (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
145            (symbol->string style)
146            (string-append (number->string (max 0 log))
147                           (symbol->string style)))))))
148
149 ;; TODO junk completely?
150 (define (note-head-style->attachment-coordinates grob axis)
151   "Return pair (X . Y), containing multipliers for the note head
152 bounding box, where to attach the stem. e.g.: X==0 means horizontally
153 centered, X==1 is at the right, X == -1 is at the left."
154
155   '(1.0 . 0.0))
156
157
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 ;; bar numbers
160
161 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
162
163 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
164
165 (define-public ((set-bar-number-visibility n) tr)
166   (let* ((bn (ly:context-property tr 'currentBarNumber)))
167     (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
168
169 (define-public (first-bar-number-invisible barnum) (> barnum 1))
170
171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 ;; break visibility
173
174 (define-public begin-of-line-visible
175   #(#f #f #t))
176 (define-public end-of-line-visible
177   #(#t #f #f))
178 (define-public end-of-line-invisible
179   #(#f #t #t))
180 (define-public spanbar-begin-of-line-invisible
181   #(#t #f #f))
182 (define-public all-visible #(#t #t #t))
183 (define-public all-invisible #(#f #f #f))
184 (define-public begin-of-line-invisible
185   #(#t #t #f))
186 (define-public center-invisible #(#t #f #t))
187
188 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
189 ;; Bar lines.
190
191 ;;
192 ;; How should a  bar line behave at a break? 
193 ;;
194 ;; Why prepend `default-' to every scm identifier?
195 (define-public (bar-line::calc-glyph-name grob)
196   (let* (
197          (glyph (ly:grob-property grob 'glyph))
198          (dir (ly:item-break-dir grob))
199          (result (assoc glyph 
200                        '((":|:" . (":|" . "|:"))
201                          ("||:" . ("||" . "|:"))
202                          ("dashed" . ("dashed" . '())) 
203                          ("|" . ("|" . ()))
204                          ("||:" . ("||" . "|:"))
205                          ("|s" . (() . "|"))
206                          ("|:" . ("|" . "|:"))
207                          ("|." . ("|." . ()))
208
209                          ;; hmm... should we end with a bar line here?
210                          (".|" . ("|" . ".|"))
211                          (":|" . (":|" . ()))
212                          ("||" . ("||" . ()))
213                          (".|." . (".|." . ()))
214                          ("" . ("" . ""))
215                          (":" . (":" . ""))
216                          ("." . ("." . ()))
217                          ("empty" . (() . ()))
218                          ("brace" . (() . "brace"))
219                          ("bracket" . (() . "bracket"))  )))
220          (glyph-name (if (= dir CENTER)
221                          glyph
222                          (if (and result (string? (index-cell (cdr result) dir)))
223                              (index-cell (cdr result) dir)
224                              #f)))
225          )
226
227     (if (not glyph-name)
228         (ly:grob-suicide! grob))
229
230     glyph-name))
231
232
233 (define-public (shift-right-at-line-begin g)
234   "Shift an item to the right, but only at the start of the line."
235   (if (and (ly:item? g)
236            (equal? (ly:item-break-dir g) RIGHT))
237       (ly:grob-translate-axis! g 3.5 X)))
238
239
240 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
241 ;; Tuplets
242
243 (define-public (tuplet-number::calc-denominator-text grob)
244   (let*
245       ((ev (ly:grob-property grob 'cause)))
246     
247     (number->string (ly:event-property ev 'denominator))))
248
249
250 (define-public (tuplet-number::calc-fraction-text grob)
251   (let*
252       ((ev (ly:grob-property grob 'cause)))
253     (format "~a:~a" 
254       (ly:event-property ev 'denominator)
255       (ly:event-property ev 'numerator))))
256
257 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
258 ;; Color
259
260 (define-public color? list?)
261
262 ; predefined colors
263 (define-public black       '(0.0 0.0 0.0))
264 (define-public white       '(1.0 1.0 1.0))
265 (define-public red         '(1.0 0.0 0.0))
266 (define-public green       '(0.0 1.0 0.0))
267 (define-public blue        '(0.0 0.0 1.0))
268 (define-public cyan        '(0.0 1.0 1.0))
269 (define-public magenta     '(1.0 0.0 1.0))
270 (define-public yellow      '(1.0 1.0 0.0))
271
272 (define-public grey        '(0.5 0.5 0.5))
273 (define-public darkred     '(0.5 0.0 0.0))
274 (define-public darkgreen   '(0.0 0.5 0.0))
275 (define-public darkblue    '(0.0 0.0 0.5))
276 (define-public darkcyan    '(0.0 0.5 0.5))
277 (define-public darkmagenta '(0.5 0.0 0.5))
278 (define-public darkyellow  '(0.5 0.5 0.0))
279
280 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
281 ;; * Pitch Trill Heads
282 ;; * Parentheses
283
284 (define (parenthesize-elements grob . rest)
285   (let*
286       ((refp (if (null? rest)
287                  grob
288                  (car rest)))
289        (elts (ly:grob-object grob 'elements))
290        (x-ext (ly:relative-group-extent elts refp X))
291
292        (font (ly:grob-default-font grob))
293        (lp (ly:font-get-glyph font "accidentals.leftparen"))
294        (rp (ly:font-get-glyph font "accidentals.rightparen"))
295        (padding (ly:grob-property grob 'padding 0.1)))
296     
297     (ly:stencil-add
298      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
299      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
300   ))
301
302
303 (define (parentheses-item::print me)
304   (let*
305       ((elts (ly:grob-object me 'elements))
306        (y-ref (ly:grob-common-refpoint-of-array me elts Y))
307        (x-ref (ly:grob-common-refpoint-of-array me elts X))
308        (stencil (parenthesize-elements me x-ref))
309        (elt-y-ext  (ly:relative-group-extent elts y-ref Y))
310        (y-center (interval-center elt-y-ext)))
311
312     (ly:stencil-translate
313      stencil
314      (cons
315       (-
316        (ly:grob-relative-coordinate me x-ref X))
317       (-
318        y-center
319        (ly:grob-relative-coordinate me y-ref Y))))
320     ))
321
322        
323        
324
325 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
326 ;; 
327
328 (define-public (chain-grob-member-functions grob value . funcs)
329   (for-each
330    (lambda (func)
331      (set! value (func grob value)))
332    funcs)
333
334   value)
335
336 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
337 ;; falls
338
339 (define-public (fall::print spanner)
340   (let*
341       ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
342        (left-span (ly:spanner-bound spanner LEFT))
343        (right-span (ly:spanner-bound spanner RIGHT))
344        (thickness (* (ly:grob-property spanner 'thickness)
345                      (ly:output-def-lookup (ly:grob-layout spanner)
346                                            'line-thickness)))
347        (padding (ly:grob-property spanner 'padding 0.5))
348        (common (ly:grob-common-refpoint right-span
349                                         (ly:grob-common-refpoint spanner
350                                                 left-span X)
351                                         X))
352        (left-x (+ padding  (interval-end  (ly:grob-robust-relative-extent left-span common X))))
353        (right-x (- (interval-start  (ly:grob-robust-relative-extent right-span common X)) 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 (ly:grob-array-ref cols (1+ idx)) 'when)
385                         (ly:grob-property (ly:grob-array-ref cols idx) 'when))))
386       
387       (moment-min (lambda (x y)
388                     (cond
389                      ((and x y)
390                       (if (ly:moment<? x y)
391                             x
392                             y))
393                      (x x)
394                      (y y)))))
395                      
396     (fold moment-min #f (map get-difference
397                              (iota (1- (ly:grob-array-length cols)))))))
398
399
400
401 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
402 ;; fingering
403
404 (define-public (fingering::calc-text grob)
405   (let*
406       ((event (ly:grob-property grob 'cause))
407        (digit (ly:event-property event 'digit)))
408     
409     (if (> digit 5)
410         (ly:input-message (ly:music-property event 'origin)
411                           "Music for the martians"))
412
413     (number->string digit 10)
414   ))
415
416 (define-public (string-number::calc-text grob)
417   (let*
418       ((event (ly:grob-property grob 'cause))
419        (digit (ly:event-property event 'string-number)))
420     
421     (number->string digit 10)
422   ))