]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Merge branch 'master' of ssh+git://jneem@git.sv.gnu.org/srv/git/lilypond
[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
59                                              context event)
60   (let*
61       ((tuning (ly:context-property context 'stringTunings))
62        (pitch (ly:event-property event 'pitch))
63        (is-harmonic (apply
64                      functional-or
65                      (map
66                       (lambda (ev)
67                         (eq? 'harmonic-event (ly:event-property ev 'class)))
68                       (ly:event-property event 'articulations)))))
69
70     
71     (make-whiteout-markup
72      (make-vcenter-markup
73       (format
74        "~a"
75        (- (ly:pitch-semitones pitch)
76           (list-ref tuning
77                     ;; remove 1 because list index starts at 0 and guitar string at 1. 
78                     (- string 1))))))
79     ))
80
81 ;; The 5-string banjo has got a extra string, the fifth (duh), wich
82 ;; starts at the fifth fret on the neck. Frets on the fifth string
83 ;; are referred to relative to the other frets:
84 ;;   the "first fret" on the fifth string is really the sixth fret
85 ;;   on the banjo neck.
86 ;; We solve this by defining a new fret-number-tablature function:
87 (define-public (fret-number-tablature-format-banjo string 
88                                              context event)
89   (let*
90       ((tuning (ly:context-property context 'stringTunings))
91        (pitch (ly:event-property event 'pitch))
92         )
93   (make-whiteout-markup
94    (make-vcenter-markup  
95     (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (- string 1)))))
96       (number->string (cond
97                        ((and (> fret 0) (= string 5))
98                         (+ fret 5))
99                        (else fret))))))
100   ))
101
102
103 ; default tunings for common string instruments
104 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
105 (define-public guitar-open-g-tuning '(2 -1 -5 -10 -17 -22))
106 (define-public bass-tuning '(-17 -22 -27 -32))
107 (define-public mandolin-tuning '(16 9 2 -5))
108
109 ;; tunings for 5-string banjo
110 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
111 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
112 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
113 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
114 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
115 ;; convert 5-string banjo tuning to 4-string by removing the 5th string
116 (define-public (four-string-banjo tuning)
117   (reverse (cdr (reverse tuning))))
118
119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
120 ;; note heads
121
122
123 (define-public (stem::calc-duration-log grob)
124   (ly:duration-log
125    (ly:event-property (event-cause grob) 'duration)))
126
127 (define-public (note-head::calc-duration-log grob)
128   (min 2 
129        (ly:duration-log
130         (ly:event-property (event-cause grob) 'duration))))
131
132 (define-public (dots::calc-dot-count grob)
133   (ly:duration-dot-count
134    (ly:event-property (event-cause grob) 'duration)))
135
136 (define (note-head::calc-tablature-stem-attachment grob)
137   (cons 0.0 1.35))
138
139
140
141 ;; silly, use alist? 
142 (define-public (note-head::calc-glyph-name grob)
143   (let*
144       ((style (ly:grob-property grob 'style))
145        (log (min 2 (ly:grob-property grob 'duration-log))))
146     
147     (case style
148       ;; "default" style is directly handled in note-head.cc as a
149       ;; special case (HW says, mainly for performance reasons).
150       ;; Therefore, style "default" does not appear in this case
151       ;; statement.  -- jr
152       ((xcircle) "2xcircle")
153       ((harmonic) "0harmonic")
154       ((baroque) 
155        ;; Oops, I actually would not call this "baroque", but, for
156        ;; backwards compatibility to 1.4, this is supposed to take
157        ;; brevis, longa and maxima from the neo-mensural font and all
158        ;; other note heads from the default font.  -- jr
159        (if (< log 0)
160            (string-append (number->string log) "neomensural")
161            (number->string log)))
162       ((mensural)
163        (string-append (number->string log) (symbol->string style)))
164       ((petrucci)
165        (if (< log 0)
166            (string-append (number->string log) "mensural")
167            (string-append (number->string log) (symbol->string style))))
168       ((neomensural)
169        (string-append (number->string log) (symbol->string style)))
170       (else
171        (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
172            (symbol->string style)
173            (string-append (number->string (max 0 log))
174                           (symbol->string style)))))))
175
176 ;; TODO junk completely?
177 (define (note-head-style->attachment-coordinates grob axis)
178   "Return pair (X . Y), containing multipliers for the note head
179 bounding box, where to attach the stem. e.g.: X==0 means horizontally
180 centered, X==1 is at the right, X == -1 is at the left."
181
182   '(1.0 . 0.0))
183
184
185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186 ;; bar numbers
187
188 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
189
190 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
191
192 (define-public ((set-bar-number-visibility n) tr)
193   (let* ((bn (ly:context-property tr 'currentBarNumber)))
194     (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
195
196 (define-public (first-bar-number-invisible barnum) (> barnum 1))
197
198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199 ;; break visibility
200
201 (define-public begin-of-line-visible
202   #(#f #f #t))
203 (define-public end-of-line-visible
204   #(#t #f #f))
205 (define-public end-of-line-invisible
206   #(#f #t #t))
207 (define-public spanbar-begin-of-line-invisible
208   #(#t #f #f))
209 (define-public all-visible #(#t #t #t))
210 (define-public all-invisible #(#f #f #f))
211 (define-public begin-of-line-invisible
212   #(#t #t #f))
213 (define-public center-invisible #(#t #f #t))
214
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216 ;; Bar lines.
217
218 ;;
219 ;; How should a  bar line behave at a break? 
220 ;;
221 ;; Why prepend `default-' to every scm identifier?
222 (define-public (bar-line::calc-glyph-name grob)
223   (let* (
224          (glyph (ly:grob-property grob 'glyph))
225          (dir (ly:item-break-dir grob))
226          (result (assoc glyph 
227                        '((":|:" . (":|" . "|:"))
228                          ("||:" . ("||" . "|:"))
229                          ("dashed" . ("dashed" . '())) 
230                          ("|" . ("|" . ()))
231                          ("||:" . ("||" . "|:"))
232                          ("|s" . (() . "|"))
233                          ("|:" . ("|" . "|:"))
234                          ("|." . ("|." . ()))
235
236                          ;; hmm... should we end with a bar line here?
237                          (".|" . ("|" . ".|"))
238                          (":|" . (":|" . ()))
239                          ("||" . ("||" . ()))
240                          (".|." . (".|." . ()))
241                          ("" . ("" . ""))
242                          (":" . (":" . ""))
243                          ("." . ("." . ()))
244                          ("empty" . (() . ()))
245                          ("brace" . (() . "brace"))
246                          ("bracket" . (() . "bracket"))  )))
247          (glyph-name (if (= dir CENTER)
248                          glyph
249                          (if (and result (string? (index-cell (cdr result) dir)))
250                              (index-cell (cdr result) dir)
251                              #f)))
252          )
253
254     (if (not glyph-name)
255         (ly:grob-suicide! grob))
256
257     glyph-name))
258
259
260 (define-public (shift-right-at-line-begin g)
261   "Shift an item to the right, but only at the start of the line."
262   (if (and (ly:item? g)
263            (equal? (ly:item-break-dir g) RIGHT))
264       (ly:grob-translate-axis! g 3.5 X)))
265
266
267 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
268 ;; Tuplets
269
270 (define-public (tuplet-number::calc-denominator-text grob)
271   (number->string (ly:event-property (event-cause grob) 'denominator)))
272
273 (define-public (tuplet-number::calc-fraction-text grob)
274   (let*
275       ((ev (event-cause grob)))
276
277     (format "~a:~a" 
278             (ly:event-property ev 'denominator)
279             (ly:event-property ev 'numerator))))
280
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
282 ;; Color
283
284 (define-public color? list?)
285
286 ; predefined colors
287 (define-public black       '(0.0 0.0 0.0))
288 (define-public white       '(1.0 1.0 1.0))
289 (define-public red         '(1.0 0.0 0.0))
290 (define-public green       '(0.0 1.0 0.0))
291 (define-public blue        '(0.0 0.0 1.0))
292 (define-public cyan        '(0.0 1.0 1.0))
293 (define-public magenta     '(1.0 0.0 1.0))
294 (define-public yellow      '(1.0 1.0 0.0))
295
296 (define-public grey        '(0.5 0.5 0.5))
297 (define-public darkred     '(0.5 0.0 0.0))
298 (define-public darkgreen   '(0.0 0.5 0.0))
299 (define-public darkblue    '(0.0 0.0 0.5))
300 (define-public darkcyan    '(0.0 0.5 0.5))
301 (define-public darkmagenta '(0.5 0.0 0.5))
302 (define-public darkyellow  '(0.5 0.5 0.0))
303
304 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
305 ;; * Pitch Trill Heads
306 ;; * Parentheses
307
308 (define-public (parentheses-item::calc-parenthesis-stencils grob)
309   (let* (
310          (font (ly:grob-default-font grob))
311          (lp (ly:font-get-glyph font "accidentals.leftparen"))
312          (rp (ly:font-get-glyph font "accidentals.rightparen"))
313          )
314
315     (list lp rp)))
316
317
318 (define (grob-text grob text)
319   (let*
320       ((layout (ly:grob-layout grob))
321        (defs (ly:output-def-lookup layout 'text-font-defaults))
322        (props (ly:grob-alist-chain grob defs)))
323
324     (ly:text-interface::interpret-markup
325      layout props text)))
326
327 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
328   (let* (
329          (font (ly:grob-default-font grob))
330          (lp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-text grob (ly:wide-char->utf-8 #x2329))
331                                                            Y CENTER)  X RIGHT))
332          (rp (ly:stencil-aligned-to (ly:stencil-aligned-to (grob-text grob (ly:wide-char->utf-8 #x232A))
333                                                            Y CENTER) X LEFT))
334          )
335
336     (list (stencil-whiteout lp)
337           (stencil-whiteout rp))))
338
339 (define (parenthesize-elements grob . rest)
340   (let*
341       ((refp (if (null? rest)
342                  grob
343                  (car rest)))
344        (elts (ly:grob-object grob 'elements))
345        (x-ext (ly:relative-group-extent elts refp X))
346        (stencils (ly:grob-property grob 'stencils))
347        (lp (car stencils))
348        (rp (cadr stencils))
349        (padding (ly:grob-property grob 'padding 0.1)))
350     
351     (ly:stencil-add
352      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
353      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
354   ))
355
356
357 (define (parentheses-item::print me)
358   (let*
359       ((elts (ly:grob-object me 'elements))
360        (y-ref (ly:grob-common-refpoint-of-array me elts Y))
361        (x-ref (ly:grob-common-refpoint-of-array me elts X))
362        (stencil (parenthesize-elements me x-ref))
363        (elt-y-ext  (ly:relative-group-extent elts y-ref Y))
364        (y-center (interval-center elt-y-ext)))
365
366     (ly:stencil-translate
367      stencil
368      (cons
369       (-
370        (ly:grob-relative-coordinate me x-ref X))
371       (-
372        y-center
373        (ly:grob-relative-coordinate me y-ref Y))))
374     ))
375
376
377
378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 ;; 
380
381 (define-public (chain-grob-member-functions grob value . funcs)
382   (for-each
383    (lambda (func)
384      (set! value (func grob value)))
385    funcs)
386
387   value)
388
389 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
390 ;; falls
391
392 (define-public (fall::print spanner)
393   (let*
394       ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
395        (left-span (ly:spanner-bound spanner LEFT))
396        (right-span (ly:spanner-bound spanner RIGHT))
397        (thickness (* (ly:grob-property spanner 'thickness)
398                      (ly:output-def-lookup (ly:grob-layout spanner)
399                                            'line-thickness)))
400        (padding (ly:grob-property spanner 'padding 0.5))
401        (common (ly:grob-common-refpoint right-span
402                                         (ly:grob-common-refpoint spanner
403                                                                  left-span X)
404                                         X))
405        (left-x (+ padding
406                   (interval-end  (ly:grob-robust-relative-extent
407                                   left-span common X))))
408        (right-x (- (interval-start
409                     (ly:grob-robust-relative-extent right-span common X))
410                    padding))
411        (self-x (ly:grob-relative-coordinate spanner common X))
412        (dx (- right-x left-x))
413        (exp (list 'path thickness 
414                   `(quote
415                     (rmoveto
416                      ,(- left-x self-x) 0
417
418                      rcurveto                
419                      ,(/ dx 3)
420                      0
421                      ,dx ,(* 0.66 delta-y)
422                      ,dx ,delta-y
423                      ))))
424        )
425
426     (ly:make-stencil
427      exp
428      (cons 0 dx)
429      (cons (min 0 delta-y)
430            (max 0 delta-y)))))
431
432 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
433 ;; grace spacing
434
435
436 (define-public (grace-spacing::calc-shortest-duration grob)
437   (let*
438      ((cols (ly:grob-object grob 'columns))
439       (get-difference
440        (lambda (idx)
441          (ly:moment-sub (ly:grob-property
442                          (ly:grob-array-ref cols (1+ idx)) 'when)
443                         (ly:grob-property
444                          (ly:grob-array-ref cols idx) 'when))))
445       
446       (moment-min (lambda (x y)
447                     (cond
448                      ((and x y)
449                       (if (ly:moment<? x y)
450                             x
451                             y))
452                      (x x)
453                      (y y)))))
454                      
455     (fold moment-min #f (map get-difference
456                              (iota (1- (ly:grob-array-length cols)))))))
457
458
459
460 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
461 ;; fingering
462
463 (define-public (fingering::calc-text grob)
464   (let*
465       ((event (event-cause grob))
466        (digit (ly:event-property event 'digit)))
467     
468     (if (> digit 5)
469         (ly:input-message (ly:event-property event 'origin)
470                           "Warning: Fingering notation for finger number ~a" digit))
471
472     (number->string digit 10)
473   ))
474
475 (define-public (string-number::calc-text grob)
476   (let*
477       ((digit (ly:event-property (event-cause  grob) 'string-number)))
478     
479     (number->string digit 10)
480   ))
481
482
483 (define-public (stroke-finger::calc-text grob)
484   (let*
485       ((digit (ly:event-property (event-cause grob) 'digit))
486        (text (ly:event-property (event-cause grob) 'text)))
487
488     (if (string? text)
489         text
490         (vector-ref (ly:grob-property grob 'digit-names)  (1- (max (min 5 digit) 1))))))
491
492 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
493 ;; dynamics
494 (define-public (hairpin::calc-grow-direction grob)
495   (if (eq? (ly:event-property (event-cause grob) 'class) 'decrescendo-event)
496       START
497       STOP
498       ))
499
500 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
501 ;; lyrics
502
503 (define-public (lyric-text::print grob)
504   "Allow interpretation of tildes as lyric tieing marks."
505   
506   (let*
507       ((text (ly:grob-property grob 'text))
508        (layout (ly:grob-layout grob))
509        (defs (ly:output-def-lookup layout 'text-font-defaults))
510        (props (ly:grob-alist-chain grob defs)))
511
512     (ly:text-interface::interpret-markup layout
513                                          props
514                                          (if (string? text)
515                                              (make-tied-lyric-markup text)
516                                              text))))
517
518 (define-public ((grob::calc-property-by-copy prop) grob)
519   (ly:event-property (event-cause grob) prop))
520
521 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
522 ;; fret boards
523
524 (define (string-frets->description string-frets string-count)
525   (let*
526       ((desc (list->vector
527               (map (lambda (x) (list 'mute  (1+ x)))
528                    (iota string-count)))))
529        
530        (for-each (lambda (sf)
531                    (let*
532                        ((string (car sf))
533                         (fret (cadr sf))
534                         (finger (caddr sf)))
535
536                      
537                      (vector-set! desc (1- string)
538                                   (if (= 0 fret)
539                                       (list 'open string)
540                                       (if finger
541                                           (list 'place-fret string fret finger) 
542                                           (list 'place-fret string fret))
543                                           
544
545                                       ))
546                      ))
547                  string-frets)
548
549        (vector->list desc)))
550
551 (define-public (fret-board::calc-stencil grob)
552   (let* ((string-frets (ly:grob-property grob 'string-fret-finger-combinations))
553          (string-count (ly:grob-property grob 'string-count))
554          (layout (ly:grob-layout grob))
555          (defs (ly:output-def-lookup layout 'text-font-defaults))
556          (props (ly:grob-alist-chain grob defs)))
557
558     (make-fret-diagram layout props
559                        (string-frets->description string-frets 6))))