]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
93e61dd9d96f1c25f31b230169b2d305715697cf
[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   (number->string (ly:event-property (event-cause grob) 'denominator)))
245
246 (define-public (tuplet-number::calc-fraction-text grob)
247   (let*
248       ((ev (event-cause grob)))
249
250     (format "~a:~a" 
251             (ly:event-property ev 'denominator)
252             (ly:event-property ev 'numerator))))
253
254 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 ;; Color
256
257 (define-public color? list?)
258
259 ; predefined colors
260 (define-public black       '(0.0 0.0 0.0))
261 (define-public white       '(1.0 1.0 1.0))
262 (define-public red         '(1.0 0.0 0.0))
263 (define-public green       '(0.0 1.0 0.0))
264 (define-public blue        '(0.0 0.0 1.0))
265 (define-public cyan        '(0.0 1.0 1.0))
266 (define-public magenta     '(1.0 0.0 1.0))
267 (define-public yellow      '(1.0 1.0 0.0))
268
269 (define-public grey        '(0.5 0.5 0.5))
270 (define-public darkred     '(0.5 0.0 0.0))
271 (define-public darkgreen   '(0.0 0.5 0.0))
272 (define-public darkblue    '(0.0 0.0 0.5))
273 (define-public darkcyan    '(0.0 0.5 0.5))
274 (define-public darkmagenta '(0.5 0.0 0.5))
275 (define-public darkyellow  '(0.5 0.5 0.0))
276
277 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
278 ;; * Pitch Trill Heads
279 ;; * Parentheses
280
281 (define (parenthesize-elements grob . rest)
282   (let*
283       ((refp (if (null? rest)
284                  grob
285                  (car rest)))
286        (elts (ly:grob-object grob 'elements))
287        (x-ext (ly:relative-group-extent elts refp X))
288
289        (font (ly:grob-default-font grob))
290        (lp (ly:font-get-glyph font "accidentals.leftparen"))
291        (rp (ly:font-get-glyph font "accidentals.rightparen"))
292        (padding (ly:grob-property grob 'padding 0.1)))
293     
294     (ly:stencil-add
295      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
296      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
297   ))
298
299
300 (define (parentheses-item::print me)
301   (let*
302       ((elts (ly:grob-object me 'elements))
303        (y-ref (ly:grob-common-refpoint-of-array me elts Y))
304        (x-ref (ly:grob-common-refpoint-of-array me elts X))
305        (stencil (parenthesize-elements me x-ref))
306        (elt-y-ext  (ly:relative-group-extent elts y-ref Y))
307        (y-center (interval-center elt-y-ext)))
308
309     (ly:stencil-translate
310      stencil
311      (cons
312       (-
313        (ly:grob-relative-coordinate me x-ref X))
314       (-
315        y-center
316        (ly:grob-relative-coordinate me y-ref Y))))
317     ))
318
319        
320        
321
322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
323 ;; 
324
325 (define-public (chain-grob-member-functions grob value . funcs)
326   (for-each
327    (lambda (func)
328      (set! value (func grob value)))
329    funcs)
330
331   value)
332
333 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
334 ;; falls
335
336 (define-public (fall::print spanner)
337   (let*
338       ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
339        (left-span (ly:spanner-bound spanner LEFT))
340        (right-span (ly:spanner-bound spanner RIGHT))
341        (thickness (* (ly:grob-property spanner 'thickness)
342                      (ly:output-def-lookup (ly:grob-layout spanner)
343                                            'line-thickness)))
344        (padding (ly:grob-property spanner 'padding 0.5))
345        (common (ly:grob-common-refpoint right-span
346                                         (ly:grob-common-refpoint spanner
347                                                 left-span X)
348                                         X))
349        (left-x (+ padding  (interval-end  (ly:grob-robust-relative-extent left-span common X))))
350        (right-x (- (interval-start  (ly:grob-robust-relative-extent right-span common X)) padding))
351        (self-x (ly:grob-relative-coordinate spanner common X))
352        (dx (- right-x left-x))
353        (exp (list 'path thickness 
354                   `(quote
355                     (rmoveto
356                      ,(- left-x self-x) 0
357
358                      rcurveto                
359                      ,(/ dx 3)
360                      0
361                      ,dx ,(* 0.66 delta-y)
362                      ,dx ,delta-y
363                      ))))
364        )
365
366     (ly:make-stencil
367      exp
368      (cons 0 dx)
369      (cons (min 0 delta-y)
370            (max 0 delta-y)))))
371
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
373 ;; grace spacing
374
375
376 (define-public (grace-spacing::calc-shortest-duration grob)
377   (let*
378      ((cols (ly:grob-object grob 'columns))
379       (get-difference
380        (lambda (idx)
381          (ly:moment-sub (ly:grob-property (ly:grob-array-ref cols (1+ idx)) 'when)
382                         (ly:grob-property (ly:grob-array-ref cols idx) 'when))))
383       
384       (moment-min (lambda (x y)
385                     (cond
386                      ((and x y)
387                       (if (ly:moment<? x y)
388                             x
389                             y))
390                      (x x)
391                      (y y)))))
392                      
393     (fold moment-min #f (map get-difference
394                              (iota (1- (ly:grob-array-length cols)))))))
395
396
397
398 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
399 ;; fingering
400
401 (define-public (fingering::calc-text grob)
402   (let*
403       ((event (event-cause grob))
404        (digit (ly:event-property event 'digit)))
405     
406     (if (> digit 5)
407         (ly:input-message (ly:music-property event 'origin)
408                           "Music for the martians"))
409
410     (number->string digit 10)
411   ))
412
413 (define-public (string-number::calc-text grob)
414   (let*
415       ((digit (ly:event-property (event-cause  grob) 'string-number)))
416     
417     (number->string digit 10)
418   ))
419
420 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
421 ;; dynamics
422 (define-public (hairpin::calc-grow-direction grob)
423   (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
424       START
425       STOP
426       ))