]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Merge with git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond.git
[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
103 (define-public (stem::calc-duration-log grob)
104   (ly:duration-log
105    (ly:event-property (event-cause grob) 'duration)))
106
107 (define-public (note-head::calc-duration-log grob)
108   (min 2 
109        (ly:duration-log
110         (ly:event-property (event-cause grob) 'duration))))
111
112 (define-public (dots::calc-dot-count grob)
113   (ly:duration-dot-count
114    (ly:event-property (event-cause grob) 'duration)))
115
116 (define (note-head::calc-tablature-stem-attachment grob)
117   (cons 0.0 1.35))
118
119
120
121 ;; silly, use alist? 
122 (define-public (note-head::calc-glyph-name grob)
123   (let*
124       ((style (ly:grob-property grob 'style))
125        (log (min 2 (ly:grob-property grob 'duration-log))))
126     
127     (case style
128       ;; "default" style is directly handled in note-head.cc as a
129       ;; special case (HW says, mainly for performance reasons).
130       ;; Therefore, style "default" does not appear in this case
131       ;; statement.  -- jr
132       ((xcircle) "2xcircle")
133       ((harmonic) "0harmonic")
134       ((baroque) 
135        ;; Oops, I actually would not call this "baroque", but, for
136        ;; backwards compatibility to 1.4, this is supposed to take
137        ;; brevis, longa and maxima from the neo-mensural font and all
138        ;; other note heads from the default font.  -- jr
139        (if (< log 0)
140            (string-append (number->string log) "neomensural")
141            (number->string log)))
142       ((mensural)
143        (string-append (number->string log) (symbol->string style)))
144       ((petrucci)
145        (if (< log 0)
146            (string-append (number->string log) "mensural")
147            (string-append (number->string log) (symbol->string style))))
148       ((neomensural)
149        (string-append (number->string log) (symbol->string style)))
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 (define-public (chain-grob-member-functions grob value . funcs)
331   (for-each
332    (lambda (func)
333      (set! value (func grob value)))
334    funcs)
335
336   value)
337
338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
339 ;; falls
340
341 (define-public (fall::print spanner)
342   (let*
343       ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
344        (left-span (ly:spanner-bound spanner LEFT))
345        (right-span (ly:spanner-bound spanner RIGHT))
346        (thickness (* (ly:grob-property spanner 'thickness)
347                      (ly:output-def-lookup (ly:grob-layout spanner)
348                                            'line-thickness)))
349        (padding (ly:grob-property spanner 'padding 0.5))
350        (common (ly:grob-common-refpoint right-span
351                                         (ly:grob-common-refpoint spanner
352                                                                  left-span X)
353                                         X))
354        (left-x (+ padding
355                   (interval-end  (ly:grob-robust-relative-extent
356                                   left-span common X))))
357        (right-x (- (interval-start
358                     (ly:grob-robust-relative-extent right-span common X))
359                    padding))
360        (self-x (ly:grob-relative-coordinate spanner common X))
361        (dx (- right-x left-x))
362        (exp (list 'path thickness 
363                   `(quote
364                     (rmoveto
365                      ,(- left-x self-x) 0
366
367                      rcurveto                
368                      ,(/ dx 3)
369                      0
370                      ,dx ,(* 0.66 delta-y)
371                      ,dx ,delta-y
372                      ))))
373        )
374
375     (ly:make-stencil
376      exp
377      (cons 0 dx)
378      (cons (min 0 delta-y)
379            (max 0 delta-y)))))
380
381 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
382 ;; grace spacing
383
384
385 (define-public (grace-spacing::calc-shortest-duration grob)
386   (let*
387      ((cols (ly:grob-object grob 'columns))
388       (get-difference
389        (lambda (idx)
390          (ly:moment-sub (ly:grob-property
391                          (ly:grob-array-ref cols (1+ idx)) 'when)
392                         (ly:grob-property
393                          (ly:grob-array-ref cols idx) 'when))))
394       
395       (moment-min (lambda (x y)
396                     (cond
397                      ((and x y)
398                       (if (ly:moment<? x y)
399                             x
400                             y))
401                      (x x)
402                      (y y)))))
403                      
404     (fold moment-min #f (map get-difference
405                              (iota (1- (ly:grob-array-length cols)))))))
406
407
408
409 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
410 ;; fingering
411
412 (define-public (fingering::calc-text grob)
413   (let*
414       ((event (event-cause grob))
415        (digit (ly:event-property event 'digit)))
416     
417     (if (> digit 5)
418         (ly:input-message (ly:event-property event 'origin)
419                           "Music for the martians"))
420
421     (number->string digit 10)
422   ))
423
424 (define-public (string-number::calc-text grob)
425   (let*
426       ((digit (ly:event-property (event-cause  grob) 'string-number)))
427     
428     (number->string digit 10)
429   ))
430
431
432 (define-public (stroke-finger::calc-text grob)
433   (let*
434       ((digit (ly:event-property (event-cause grob) 'digit))
435        (text (ly:event-property (event-cause grob) 'text)))
436
437     (if (string? text)
438         text
439         (vector-ref (ly:grob-property grob 'digit-names)  (1- (max (min 5 digit) 1))))))
440
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442 ;; dynamics
443 (define-public (hairpin::calc-grow-direction grob)
444   (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
445       START
446       STOP
447       ))
448
449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
450 ;; lyrics
451
452 (define-public (lyric-text::print grob)
453   "Allow interpretation of tildes as lyric tieing marks."
454   
455   (let*
456       ((text (ly:grob-property grob 'text))
457        (layout (ly:grob-layout grob))
458        (defs (ly:output-def-lookup layout 'text-font-defaults))
459        (props (ly:grob-alist-chain grob defs)))
460
461     (ly:text-interface::interpret-markup layout
462                                          props
463                                          (if (string? text)
464                                              (make-tied-lyric-markup text)
465                                              text))))
466
467 (define-public ((grob::calc-property-by-copy prop) grob)
468   (ly:event-property (event-cause grob) prop))
469
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 ;; fret boards
472
473 (define (string-frets->description string-frets string-count)
474   (let*
475       ((desc (list->vector
476               (map (lambda (x) (list 'mute  (1+ x)))
477                    (iota string-count)))))
478        
479        (for-each (lambda (sf)
480                    (let*
481                        ((string (car sf))
482                         (fret (cadr sf))
483                         (finger (caddr sf)))
484
485                      
486                      (vector-set! desc (1- string)
487                                   (if (= 0 fret)
488                                       (list 'open string)
489                                       (if finger
490                                           (list 'place-fret string fret finger) 
491                                           (list 'place-fret string fret))
492                                           
493
494                                       ))
495                      ))
496                  string-frets)
497
498        (vector->list desc)))
499
500 (define-public (fret-board::calc-stencil grob)
501   (let* ((string-frets (ly:grob-property grob 'string-fret-finger-combinations))
502          (string-count (ly:grob-property grob 'string-count))
503          (layout (ly:grob-layout grob))
504          (defs (ly:output-def-lookup layout 'text-font-defaults))
505          (props (ly:grob-alist-chain grob defs)))
506
507     (make-fret-diagram layout props
508                        (string-frets->description string-frets 6))))