]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Fix some bugs in the dynamic engraver and PostScript backend
[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@cs.uu.nl>
7
8
9 ;;; Tablature functions, by Jiba (jiba@tuxfamily.org)
10
11 ;; The TabNoteHead stem attachment function.
12 (define (note-head::calc-tablature-stem-attachment grob)
13   (cons 0.0 1.35))
14
15 ;; The TabNoteHead tablatureFormat callback.
16 ;; Compute the text grob-property
17 (define-public (fret-number-tablature-format string tuning pitch)
18   (make-whiteout-markup
19    (make-vcenter-markup  
20     (number->string
21      (- (ly:pitch-semitones pitch)
22         (list-ref tuning
23                   ;; remove 1 because list index starts at 0 and guitar string at 1. 
24                   (- string 1)))))))
25
26 ;; The 5-string banjo has got a extra string, the fifth (duh), wich
27 ;; starts at the fifth fret on the neck. Frets on the fifth string
28 ;; are referred to relative to the other frets:
29 ;;   the "first fret" on the fifth string is really the sixth fret
30 ;;   on the banjo neck.
31 ;; We solve this by defining a new fret-number-tablature function:
32 (define-public (fret-number-tablature-format-banjo string tuning pitch)
33   (make-whiteout-markup
34    (make-vcenter-markup  
35     (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (- string 1)))))
36       (number->string (cond
37                        ((and (> fret 0) (= string 5))
38                         (+ fret 5))
39                        (else fret)))))))
40
41
42 ; default tunings for common string instruments
43 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
44 (define-public guitar-open-g-tuning '(2 -1 -5 -10 -17 -22))
45 (define-public bass-tuning '(-17 -22 -27 -32))
46 (define-public mandolin-tuning '(16 9 2 -5))
47
48 ;; tunings for 5-string banjo
49 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
50 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
51 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
52 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
53 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
54 ;; convert 5-string banjo tuning to 4-string by removing the 5th string
55 (define-public (four-string-banjo tuning)
56   (reverse (cdr (reverse tuning))))
57
58 ;;; end of tablature functions
59
60 (define-public (make-stencil-boxer thickness padding callback)
61
62   "Return function that adds a box around the grob passed as argument."
63   (lambda (grob)
64     
65     (box-stencil (callback grob) thickness padding)))
66
67 (define-public (make-stencil-circler thickness padding callback)
68   "Return function that adds a circle around the grob passed as argument."
69   (lambda (grob) (circle-stencil (callback grob) thickness padding)))
70
71 (define-public (arg->string arg)
72   (cond ((number? arg) (ly:inexact->string arg 10))
73         ((string? arg) (string-append "\"" arg "\""))
74         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
75
76 (define-public (print-circled-text-callback grob)
77   (let* ((text (ly:grob-property grob 'text))
78          
79          (layout (ly:grob-layout grob))
80          (defs (ly:output-def-lookup layout 'text-font-defaults))
81          (props (ly:grob-alist-chain grob defs))
82          (circle (ly:text-interface::interpret-markup
83                   layout props (make-circle-markup text))))
84     circle))
85
86
87 ;;(define (mm-to-pt x)
88 ;;  (* (/ 72.27 25.40) x))
89
90 ;; do nothing in .scm output
91
92 (define-public (ly:numbers->string lst)
93   (string-join (map ly:number->string lst) " "))
94
95 (define (number->octal-string x)
96   (let* ((n (inexact->exact x))
97          (n64 (quotient n 64))
98          (n8 (quotient (- n (* n64 64)) 8)))
99     (string-append
100      (number->string n64)
101      (number->string n8)
102      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
103
104 (define-public (ly:inexact->string x radix)
105   (let ((n (inexact->exact x)))
106     (number->string n radix)))
107
108 (define-public (ly:number-pair->string c)
109   (string-append (ly:number->string (car c)) " "
110                  (ly:number->string (cdr c))))
111
112
113 ;; silly, use alist? 
114 (define-public (note-head::calc-glyph-name grob)
115   (let*
116       ((style (ly:grob-property grob 'style))
117        (log (min 2 (ly:grob-property grob 'duration-log))))
118     
119     (case style
120       ((xcircle) "2xcircle")
121       ((harmonic) "0harmonic")
122       ((baroque) 
123        ;; Oops, I actually would not call this "baroque", but, for
124        ;; backwards compatibility to 1.4, this is supposed to take
125        ;; brevis, longa and maxima from the neo-mensural font and all
126        ;; other note heads from the default font.  -- jr
127        (if (< log 0)
128            (string-append (number->string log) "neomensural")
129            (number->string log)))
130       ((mensural)
131        (string-append (number->string log) (symbol->string style)))
132       ((petrucci)
133        (if (< log 0)
134            (string-append (number->string log) "mensural")
135            (string-append (number->string log) (symbol->string style))))
136       ((neomensural)
137        (string-append (number->string log) (symbol->string style)))
138       ((default)
139        ;; The default font in mf/feta-bolletjes.mf defines a brevis, but
140        ;; neither a longa nor a maxima.  Hence let us, for the moment,
141        ;; take these from the neo-mensural font.  TODO: mf/feta-bolletjes
142        ;; should define at least a longa for the default font.  The longa
143        ;; should look exactly like the brevis of the default font, but
144        ;; with a stem exactly like that of the quarter note. -- jr
145        (if (< log -1)
146            (string-append (number->string log) "neomensural")
147            (number->string log)))
148       (else
149        (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
150            (symbol->string style)
151            (string-append (number->string (max 0 log))
152                           (symbol->string style)))))))
153
154 ;; TODO junk completely?
155 (define (note-head-style->attachment-coordinates grob axis)
156   "Return pair (X . Y), containing multipliers for the note head
157 bounding box, where to attach the stem. e.g.: X==0 means horizontally
158 centered, X==1 is at the right, X == -1 is at the left."
159
160   '(1.0 . 0.0))
161
162 (define-public (string-encode-integer i)
163   (cond
164    ((= i  0) "o")
165    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
166    (else (string-append
167           (make-string 1 (integer->char (+ 65 (modulo i 26))))
168           (string-encode-integer (quotient i 26))))))
169
170 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
171
172 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
173
174 (define-public ((set-bar-number-visibility n) tr)
175   (let* ((bn (ly:context-property tr 'currentBarNumber)))
176     (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
177
178 (define-public (first-bar-number-invisible barnum) (> barnum 1))
179
180 ;; See documentation of ly:item::visibility-lambda-
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                          ("|" . ("|" . ()))
210                          ("||:" . ("||" . "|:"))
211                          ("|s" . (() . "|"))
212                          ("|:" . ("|" . "|:"))
213                          ("|." . ("|." . ()))
214
215                          ;; hmm... should we end with a bar line here?
216                          (".|" . ("|" . ".|"))
217                          (":|" . (":|" . ()))
218                          ("||" . ("||" . ()))
219                          (".|." . (".|." . ()))
220                          ("" . ("" . ""))
221                          (":" . (":" . ""))
222                          ("." . ("." . ()))
223                          ("empty" . (() . ()))
224                          ("brace" . (() . "brace"))
225                          ("bracket" . (() . "bracket"))  )))
226          (glyph-name (if (= dir CENTER)
227                          glyph
228                          (if (and result (string? (index-cell (cdr result) dir)))
229                              (index-cell (cdr result) dir)
230                              #f)))
231          )
232
233     (if (not glyph-name)
234         (ly:grob-suicide! grob))
235
236     glyph-name))
237
238
239 (define-public (shift-right-at-line-begin g)
240   "Shift an item to the right, but only at the start of the line."
241   (if (and (ly:item? g)
242            (equal? (ly:item-break-dir g) RIGHT))
243       (ly:grob-translate-axis! g 3.5 X)))
244
245
246 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
247 ;; Color
248
249 (define-public color? list?)
250
251 ; predefined colors
252 (define-public black       '(0.0 0.0 0.0))
253 (define-public white       '(1.0 1.0 1.0))
254 (define-public red         '(1.0 0.0 0.0))
255 (define-public green       '(0.0 1.0 0.0))
256 (define-public blue        '(0.0 0.0 1.0))
257 (define-public cyan        '(0.0 1.0 1.0))
258 (define-public magenta     '(1.0 0.0 1.0))
259 (define-public yellow      '(1.0 1.0 0.0))
260
261 (define-public grey        '(0.5 0.5 0.5))
262 (define-public darkred     '(0.5 0.0 0.0))
263 (define-public darkgreen   '(0.0 0.5 0.0))
264 (define-public darkblue    '(0.0 0.0 0.5))
265 (define-public darkcyan    '(0.0 0.5 0.5))
266 (define-public darkmagenta '(0.5 0.0 0.5))
267 (define-public darkyellow  '(0.5 0.5 0.0))
268
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 ;; * Pitch Trill Heads
271 ;; * Parentheses
272
273 (define (parenthesize-elements grob . rest)
274   (let*
275       (
276        (refp (if (null? rest)
277                  grob
278                  (car rest)))
279        (elts (ly:grob-object grob 'elements))
280        (x-ext (ly:relative-group-extent elts refp X))
281
282        (font (ly:grob-default-font grob))
283        (lp (ly:font-get-glyph font "accidentals.leftparen"))
284        (rp (ly:font-get-glyph font "accidentals.rightparen"))
285        (padding (ly:grob-property grob 'padding 0.1)))
286
287     (ly:stencil-add
288      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
289      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
290   ))
291
292
293 (define (parentheses-item::print me)
294   (let*
295       ((elts (ly:grob-object me 'elements))
296        (y-ref (ly:grob-common-refpoint-of-array me elts Y))
297        (x-ref (ly:grob-common-refpoint-of-array me elts X))
298        (stencil (parenthesize-elements me x-ref))
299        (elt-y-ext  (ly:relative-group-extent elts y-ref Y))
300        (y-center (interval-center elt-y-ext)))
301
302     (ly:stencil-translate
303      stencil
304      (cons
305       (-
306        (ly:grob-relative-coordinate me x-ref X))
307       (-
308        y-center
309        (ly:grob-relative-coordinate me y-ref Y))))
310     ))
311
312        
313        
314
315 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
316 ;; 
317
318 (define-public (chain-grob-member-functions grob value . funcs)
319   (for-each
320    (lambda (func)
321      (set! value (func grob value)))
322    funcs)
323
324   value)
325