]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
* The grand 2005-2006 replace.
[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 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
43 (define-public bass-tuning '(-17 -22 -27 -32))
44
45 ;; tunings for 5-string banjo
46 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
47 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
48 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
49 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
50 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
51 ;; convert 5-string banjo tunings to 4-string tunings by
52 ;; removing the 5th string
53 ;;
54 ;; example:
55 ;; \set TabStaff.stringTunings = #(four-string-banjo banjo-open-g-tuning)
56 (define-public (four-string-banjo tuning)
57   (reverse (cdr (reverse tuning))))
58
59 ;;; end of tablature functions
60
61 (define-public (make-stencil-boxer thickness padding callback)
62
63   "Return function that adds a box around the grob passed as argument."
64   (lambda (grob)
65     
66     (box-stencil (callback grob) thickness padding)))
67
68 (define-public (make-stencil-circler thickness padding callback)
69   "Return function that adds a circle around the grob passed as argument."
70   (lambda (grob) (circle-stencil (callback grob) thickness padding)))
71
72 (define-public (arg->string arg)
73   (cond ((number? arg) (ly:inexact->string arg 10))
74         ((string? arg) (string-append "\"" arg "\""))
75         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
76
77 (define-public (print-circled-text-callback grob)
78   (let* ((text (ly:grob-property grob 'text))
79          
80          (layout (ly:grob-layout grob))
81          (defs (ly:output-def-lookup layout 'text-font-defaults))
82          (props (ly:grob-alist-chain grob defs))
83          (circle (Text_interface::interpret_markup
84                   layout props (make-circle-markup text))))
85     circle))
86
87
88 ;;(define (mm-to-pt x)
89 ;;  (* (/ 72.27 25.40) x))
90
91 ;; do nothing in .scm output
92
93 (define-public (ly:numbers->string lst)
94   (string-join (map ly:number->string lst) " "))
95
96 (define (number->octal-string x)
97   (let* ((n (inexact->exact x))
98          (n64 (quotient n 64))
99          (n8 (quotient (- n (* n64 64)) 8)))
100     (string-append
101      (number->string n64)
102      (number->string n8)
103      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
104
105 (define-public (ly:inexact->string x radix)
106   (let ((n (inexact->exact x)))
107     (number->string n radix)))
108
109 (define-public (ly:number-pair->string c)
110   (string-append (ly:number->string (car c)) " "
111                  (ly:number->string (cdr c))))
112
113
114 ;; silly, use alist? 
115 (define-public (note-head::calc-glyph-name grob)
116   (let*
117       ((style (ly:grob-property grob 'style))
118        (log (min 2 (ly:grob-property grob 'duration-log))))
119     
120     (case style
121       ((xcircle) "2xcircle")
122       ((harmonic) "0harmonic")
123       ((baroque) 
124        ;; Oops, I actually would not call this "baroque", but, for
125        ;; backwards compatibility to 1.4, this is supposed to take
126        ;; brevis, longa and maxima from the neo-mensural font and all
127        ;; other note heads from the default font.  -- jr
128        (if (< log 0)
129            (string-append (number->string log) "neomensural")
130            (number->string log)))
131       ((mensural)
132        (string-append (number->string log) (symbol->string style)))
133       ((petrucci)
134        (if (< log 0)
135            (string-append (number->string log) "mensural")
136            (string-append (number->string log) (symbol->string style))))
137       ((neomensural)
138        (string-append (number->string log) (symbol->string style)))
139       ((default)
140        ;; The default font in mf/feta-bolletjes.mf defines a brevis, but
141        ;; neither a longa nor a maxima.  Hence let us, for the moment,
142        ;; take these from the neo-mensural font.  TODO: mf/feta-bolletjes
143        ;; should define at least a longa for the default font.  The longa
144        ;; should look exactly like the brevis of the default font, but
145        ;; with a stem exactly like that of the quarter note. -- jr
146        (if (< log -1)
147            (string-append (number->string log) "neomensural")
148            (number->string log)))
149       (else
150        (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
151            (symbol->string style)
152            (string-append (number->string (max 0 log))
153                           (symbol->string style)))))))
154
155 ;; TODO junk completely?
156 (define (note-head-style->attachment-coordinates grob axis)
157   "Return pair (X . Y), containing multipliers for the note head
158 bounding box, where to attach the stem. e.g.: X==0 means horizontally
159 centered, X==1 is at the right, X == -1 is at the left."
160
161   '(1.0 . 0.0))
162
163 (define-public (string-encode-integer i)
164   (cond
165    ((= i  0) "o")
166    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
167    (else (string-append
168           (make-string 1 (integer->char (+ 65 (modulo i 26))))
169           (string-encode-integer (quotient i 26))))))
170
171 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
172
173 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
174
175 (define-public ((set-bar-number-visibility n) tr)
176   (let* ((bn (ly:context-property tr 'currentBarNumber)))
177     (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
178
179 (define-public (first-bar-number-invisible barnum) (> barnum 1))
180
181 ;; See documentation of Item::visibility_lambda_
182 (define-public begin-of-line-visible
183   #(#f #f #t))
184 (define-public end-of-line-visible
185   #(#t #f #f))
186 (define-public end-of-line-invisible
187   #(#f #t #t))
188 (define-public spanbar-begin-of-line-invisible
189   #(#t #f #f))
190 (define-public all-visible #(#t #t #t))
191 (define-public all-invisible #(#f #f #f))
192 (define-public begin-of-line-invisible
193   #(#t #t #f))
194 (define-public center-invisible #(#t #f #t))
195
196 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
197 ;; Bar lines.
198
199 ;;
200 ;; How should a  bar line behave at a break? 
201 ;;
202 ;; Why prepend `default-' to every scm identifier?
203 (define-public (bar-line::calc-glyph-name grob)
204   (let* (
205          (glyph (ly:grob-property grob 'glyph))
206          (dir (ly:item-break-dir grob))
207          (result (assoc glyph 
208                        '((":|:" . (":|" . "|:"))
209                          ("||:" . ("||" . "|:"))
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 ;; Color
249
250 (define-public color? list?)
251
252 ; predefined colors
253 (define-public black       '(0.0 0.0 0.0))
254 (define-public white       '(1.0 1.0 1.0))
255 (define-public red         '(1.0 0.0 0.0))
256 (define-public green       '(0.0 1.0 0.0))
257 (define-public blue        '(0.0 0.0 1.0))
258 (define-public cyan        '(1.0 1.0 0.0))
259 (define-public magenta     '(1.0 0.0 1.0))
260 (define-public yellow      '(0.0 1.0 1.0))
261
262 (define-public grey        '(0.5 0.5 0.5))
263 (define-public darkred     '(0.5 0.0 0.0))
264 (define-public darkgreen   '(0.0 0.5 0.0))
265 (define-public darkblue    '(0.0 0.0 0.5))
266 (define-public darkcyan    '(0.5 0.5 0.0))
267 (define-public darkmagenta '(0.5 0.0 0.5))
268 (define-public darkyellow  '(0.0 0.5 0.5))
269
270 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
271 ;; Pitch Trill Heads
272
273 (define (parenthesize-elements grob)
274   (let*
275       ((elts (ly:grob-object grob 'elements))
276        (x-ext (ly:relative-group-extent elts grob X))
277        (font (ly:grob-default-font grob))
278        (lp (ly:font-get-glyph font "accidentals.leftparen"))
279        (rp (ly:font-get-glyph font "accidentals.rightparen"))
280        (padding 0.1))
281
282     (ly:stencil-add
283      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
284      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
285   ))
286
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288 ;; 
289
290 (define-public (chain-grob-member-functions grob value . funcs)
291   (for-each
292    (lambda (func)
293      (set! value (func grob value)))
294    funcs)
295
296   value)