]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
(fingering::calc-text): use origin
[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       ((xcircle) "2xcircle")
123       ((harmonic) "0harmonic")
124       ((baroque) 
125        ;; Oops, I actually would not call this "baroque", but, for
126        ;; backwards compatibility to 1.4, this is supposed to take
127        ;; brevis, longa and maxima from the neo-mensural font and all
128        ;; other note heads from the default font.  -- jr
129        (if (< log 0)
130            (string-append (number->string log) "neomensural")
131            (number->string log)))
132       ((mensural)
133        (string-append (number->string log) (symbol->string style)))
134       ((petrucci)
135        (if (< log 0)
136            (string-append (number->string log) "mensural")
137            (string-append (number->string log) (symbol->string style))))
138       ((neomensural)
139        (string-append (number->string log) (symbol->string style)))
140       ((default)
141        ;; The default font in mf/feta-bolletjes.mf defines a brevis, but
142        ;; neither a longa nor a maxima.  Hence let us, for the moment,
143        ;; take these from the neo-mensural font.  TODO: mf/feta-bolletjes
144        ;; should define at least a longa for the default font.  The longa
145        ;; should look exactly like the brevis of the default font, but
146        ;; with a stem exactly like that of the quarter note. -- jr
147        (if (< log -1)
148            (string-append (number->string log) "neomensural")
149            (number->string log)))
150       (else
151        (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
152            (symbol->string style)
153            (string-append (number->string (max 0 log))
154                           (symbol->string style)))))))
155
156 ;; TODO junk completely?
157 (define (note-head-style->attachment-coordinates grob axis)
158   "Return pair (X . Y), containing multipliers for the note head
159 bounding box, where to attach the stem. e.g.: X==0 means horizontally
160 centered, X==1 is at the right, X == -1 is at the left."
161
162   '(1.0 . 0.0))
163
164
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 ;; bar numbers
167
168 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
169
170 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
171
172 (define-public ((set-bar-number-visibility n) tr)
173   (let* ((bn (ly:context-property tr 'currentBarNumber)))
174     (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
175
176 (define-public (first-bar-number-invisible barnum) (> barnum 1))
177
178 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
179 ;; break visibility
180
181 (define-public begin-of-line-visible
182   #(#f #f #t))
183 (define-public end-of-line-visible
184   #(#t #f #f))
185 (define-public end-of-line-invisible
186   #(#f #t #t))
187 (define-public spanbar-begin-of-line-invisible
188   #(#t #f #f))
189 (define-public all-visible #(#t #t #t))
190 (define-public all-invisible #(#f #f #f))
191 (define-public begin-of-line-invisible
192   #(#t #t #f))
193 (define-public center-invisible #(#t #f #t))
194
195 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 ;; Bar lines.
197
198 ;;
199 ;; How should a  bar line behave at a break? 
200 ;;
201 ;; Why prepend `default-' to every scm identifier?
202 (define-public (bar-line::calc-glyph-name grob)
203   (let* (
204          (glyph (ly:grob-property grob 'glyph))
205          (dir (ly:item-break-dir grob))
206          (result (assoc glyph 
207                        '((":|:" . (":|" . "|:"))
208                          ("||:" . ("||" . "|:"))
209                          ("dashed" . ("dashed" . '())) 
210                          ("|" . ("|" . ()))
211                          ("||:" . ("||" . "|:"))
212                          ("|s" . (() . "|"))
213                          ("|:" . ("|" . "|:"))
214                          ("|." . ("|." . ()))
215
216                          ;; hmm... should we end with a bar line here?
217                          (".|" . ("|" . ".|"))
218                          (":|" . (":|" . ()))
219                          ("||" . ("||" . ()))
220                          (".|." . (".|." . ()))
221                          ("" . ("" . ""))
222                          (":" . (":" . ""))
223                          ("." . ("." . ()))
224                          ("empty" . (() . ()))
225                          ("brace" . (() . "brace"))
226                          ("bracket" . (() . "bracket"))  )))
227          (glyph-name (if (= dir CENTER)
228                          glyph
229                          (if (and result (string? (index-cell (cdr result) dir)))
230                              (index-cell (cdr result) dir)
231                              #f)))
232          )
233
234     (if (not glyph-name)
235         (ly:grob-suicide! grob))
236
237     glyph-name))
238
239
240 (define-public (shift-right-at-line-begin g)
241   "Shift an item to the right, but only at the start of the line."
242   (if (and (ly:item? g)
243            (equal? (ly:item-break-dir g) RIGHT))
244       (ly:grob-translate-axis! g 3.5 X)))
245
246
247 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248 ;; Tuplets
249
250 (define-public (tuplet-number::calc-denominator-text grob)
251   (number->string (ly:event-property (event-cause grob) 'denominator)))
252
253 (define-public (tuplet-number::calc-fraction-text grob)
254   (let*
255       ((ev (event-cause grob)))
256
257     (format "~a:~a" 
258             (ly:event-property ev 'denominator)
259             (ly:event-property ev 'numerator))))
260
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
262 ;; Color
263
264 (define-public color? list?)
265
266 ; predefined colors
267 (define-public black       '(0.0 0.0 0.0))
268 (define-public white       '(1.0 1.0 1.0))
269 (define-public red         '(1.0 0.0 0.0))
270 (define-public green       '(0.0 1.0 0.0))
271 (define-public blue        '(0.0 0.0 1.0))
272 (define-public cyan        '(0.0 1.0 1.0))
273 (define-public magenta     '(1.0 0.0 1.0))
274 (define-public yellow      '(1.0 1.0 0.0))
275
276 (define-public grey        '(0.5 0.5 0.5))
277 (define-public darkred     '(0.5 0.0 0.0))
278 (define-public darkgreen   '(0.0 0.5 0.0))
279 (define-public darkblue    '(0.0 0.0 0.5))
280 (define-public darkcyan    '(0.0 0.5 0.5))
281 (define-public darkmagenta '(0.5 0.0 0.5))
282 (define-public darkyellow  '(0.5 0.5 0.0))
283
284 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
285 ;; * Pitch Trill Heads
286 ;; * Parentheses
287
288 (define (parenthesize-elements grob . rest)
289   (let*
290       ((refp (if (null? rest)
291                  grob
292                  (car rest)))
293        (elts (ly:grob-object grob 'elements))
294        (x-ext (ly:relative-group-extent elts refp X))
295
296        (font (ly:grob-default-font grob))
297        (lp (ly:font-get-glyph font "accidentals.leftparen"))
298        (rp (ly:font-get-glyph font "accidentals.rightparen"))
299        (padding (ly:grob-property grob 'padding 0.1)))
300     
301     (ly:stencil-add
302      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
303      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
304   ))
305
306
307 (define (parentheses-item::print me)
308   (let*
309       ((elts (ly:grob-object me 'elements))
310        (y-ref (ly:grob-common-refpoint-of-array me elts Y))
311        (x-ref (ly:grob-common-refpoint-of-array me elts X))
312        (stencil (parenthesize-elements me x-ref))
313        (elt-y-ext  (ly:relative-group-extent elts y-ref Y))
314        (y-center (interval-center elt-y-ext)))
315
316     (ly:stencil-translate
317      stencil
318      (cons
319       (-
320        (ly:grob-relative-coordinate me x-ref X))
321       (-
322        y-center
323        (ly:grob-relative-coordinate me y-ref Y))))
324     ))
325
326        
327        
328
329 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
330 ;; 
331
332 (define-public (chain-grob-member-functions grob value . funcs)
333   (for-each
334    (lambda (func)
335      (set! value (func grob value)))
336    funcs)
337
338   value)
339
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 ;; falls
342
343 (define-public (fall::print spanner)
344   (let*
345       ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
346        (left-span (ly:spanner-bound spanner LEFT))
347        (right-span (ly:spanner-bound spanner RIGHT))
348        (thickness (* (ly:grob-property spanner 'thickness)
349                      (ly:output-def-lookup (ly:grob-layout spanner)
350                                            'line-thickness)))
351        (padding (ly:grob-property spanner 'padding 0.5))
352        (common (ly:grob-common-refpoint right-span
353                                         (ly:grob-common-refpoint spanner
354                                                 left-span X)
355                                         X))
356        (left-x (+ padding  (interval-end  (ly:grob-robust-relative-extent left-span common X))))
357        (right-x (- (interval-start  (ly:grob-robust-relative-extent right-span common X)) padding))
358        (self-x (ly:grob-relative-coordinate spanner common X))
359        (dx (- right-x left-x))
360        (exp (list 'path thickness 
361                   `(quote
362                     (rmoveto
363                      ,(- left-x self-x) 0
364
365                      rcurveto                
366                      ,(/ dx 3)
367                      0
368                      ,dx ,(* 0.66 delta-y)
369                      ,dx ,delta-y
370                      ))))
371        )
372
373     (ly:make-stencil
374      exp
375      (cons 0 dx)
376      (cons (min 0 delta-y)
377            (max 0 delta-y)))))
378
379 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
380 ;; grace spacing
381
382
383 (define-public (grace-spacing::calc-shortest-duration grob)
384   (let*
385      ((cols (ly:grob-object grob 'columns))
386       (get-difference
387        (lambda (idx)
388          (ly:moment-sub (ly:grob-property (ly:grob-array-ref cols (1+ idx)) 'when)
389                         (ly:grob-property (ly:grob-array-ref cols idx) 'when))))
390       
391       (moment-min (lambda (x y)
392                     (cond
393                      ((and x y)
394                       (if (ly:moment<? x y)
395                             x
396                             y))
397                      (x x)
398                      (y y)))))
399                      
400     (fold moment-min #f (map get-difference
401                              (iota (1- (ly:grob-array-length cols)))))))
402
403
404
405 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
406 ;; fingering
407
408 (define-public (fingering::calc-text grob)
409   (let*
410       ((event (event-cause grob))
411        (digit (ly:event-property event 'digit)))
412     
413     (if (> digit 5)
414         (ly:input-message (ly:event-property event 'origin)
415                           "Music for the martians"))
416
417     (number->string digit 10)
418   ))
419
420 (define-public (string-number::calc-text grob)
421   (let*
422       ((digit (ly:event-property (event-cause  grob) 'string-number)))
423     
424     (number->string digit 10)
425   ))
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       ))