]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
* lily/general-scheme.cc (LY_DEFINE): elucidate docstring.
[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 ;; 
325
326 (define-public (chain-grob-member-functions grob value . funcs)
327   (for-each
328    (lambda (func)
329      (set! value (func grob value)))
330    funcs)
331
332   value)
333
334 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
335 ;; falls
336
337 (define-public (fall::print spanner)
338   (let*
339       ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
340        (left-span (ly:spanner-bound spanner LEFT))
341        (right-span (ly:spanner-bound spanner RIGHT))
342        (thickness (* (ly:grob-property spanner 'thickness)
343                      (ly:output-def-lookup (ly:grob-layout spanner)
344                                            'line-thickness)))
345        (padding (ly:grob-property spanner 'padding 0.5))
346        (common (ly:grob-common-refpoint right-span
347                                         (ly:grob-common-refpoint spanner
348                                                 left-span X)
349                                         X))
350        (left-x (+ padding  (interval-end  (ly:grob-robust-relative-extent left-span common X))))
351        (right-x (- (interval-start  (ly:grob-robust-relative-extent right-span common X)) padding))
352        (self-x (ly:grob-relative-coordinate spanner common X))
353        (dx (- right-x left-x))
354        (exp (list 'path thickness 
355                   `(quote
356                     (rmoveto
357                      ,(- left-x self-x) 0
358
359                      rcurveto                
360                      ,(/ dx 3)
361                      0
362                      ,dx ,(* 0.66 delta-y)
363                      ,dx ,delta-y
364                      ))))
365        )
366
367     (ly:make-stencil
368      exp
369      (cons 0 dx)
370      (cons (min 0 delta-y)
371            (max 0 delta-y)))))
372
373 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
374 ;; grace spacing
375
376
377 (define-public (grace-spacing::calc-shortest-duration grob)
378   (let*
379      ((cols (ly:grob-object grob 'columns))
380       (get-difference
381        (lambda (idx)
382          (ly:moment-sub (ly:grob-property (ly:grob-array-ref cols (1+ idx)) 'when)
383                         (ly:grob-property (ly:grob-array-ref cols idx) 'when))))
384       
385       (moment-min (lambda (x y)
386                     (cond
387                      ((and x y)
388                       (if (ly:moment<? x y)
389                             x
390                             y))
391                      (x x)
392                      (y y)))))
393                      
394     (fold moment-min #f (map get-difference
395                              (iota (1- (ly:grob-array-length cols)))))))
396
397
398
399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
400 ;; fingering
401
402 (define-public (fingering::calc-text grob)
403   (let*
404       ((event (event-cause grob))
405        (digit (ly:event-property event 'digit)))
406     
407     (if (> digit 5)
408         (ly:input-message (ly:event-property event 'origin)
409                           "Music for the martians"))
410
411     (number->string digit 10)
412   ))
413
414 (define-public (string-number::calc-text grob)
415   (let*
416       ((digit (ly:event-property (event-cause  grob) 'string-number)))
417     
418     (number->string digit 10)
419   ))
420
421 (define-public (string-finger::calc-text grob)
422   (let*
423       ((text (ly:event-property (event-cause  grob) 'text)))
424
425     text))
426
427 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
428 ;; dynamics
429 (define-public (hairpin::calc-grow-direction grob)
430   (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
431       START
432       STOP
433       ))
434
435 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
436 ;; lyrics
437
438 (define-public (lyric-text::print grob)
439   "Allow interpretation of tildes as lyric tieing marks."
440   
441   (let*
442       ((text (ly:grob-property grob 'text))
443        (layout (ly:grob-layout grob))
444        (defs (ly:output-def-lookup layout 'text-font-defaults))
445        (props (ly:grob-alist-chain grob defs)))
446
447     (ly:text-interface::interpret-markup layout
448                                          props
449                                          (if (string? text)
450                                              (make-tied-lyric-markup text)
451                                              text))))
452
453 (define-public (lyric-text::calc-text grob)
454    (ly:event-property (event-cause grob) 'text))