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