]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
*** empty log message ***
[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--2005 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 (tablature-stem-attachment-function style duration)
13   (cons 0.0 0.5))
14
15 ;; The TabNoteHead tablatureFormat callback.
16 ;; Compute the text grob-property
17 (define-public (fret-number-tablature-format string tuning pitch)
18   (number->string
19    (- (ly:pitch-semitones pitch)
20       (list-ref tuning
21                 ;; remove 1 because list index starts at 0 and guitar string at 1. 
22                 (- string 1)))))
23
24 ;; The 5-string banjo has got a extra string, the fifth (duh), wich
25 ;; starts at the fifth fret on the neck. Frets on the fifth string
26 ;; are referred to relative to the other frets:
27 ;;   the "first fret" on the fifth string is really the sixth fret
28 ;;   on the banjo neck.
29 ;; We solve this by defining a new fret-number-tablature function:
30 (define-public (fret-number-tablature-format-banjo string tuning pitch)
31     (let ((fret (- (ly:pitch-semitones pitch) (list-ref tuning (- string 1)))))
32         (number->string (cond
33             ((and (> fret 0) (= string 5))
34                 (+ fret 5))
35             (else fret)))))
36
37 (define-public (hammer-print-function grob)
38   (let* ((note-collums (ly:grob-property grob 'note-columns))
39          (note-column1 (cadr note-collums))
40          (note-column2 (car  note-collums))
41          (note1 (car (ly:grob-property note-column1 'note-heads)))
42          (note2 (car (ly:grob-property note-column2 'note-heads)))
43          (text1 (ly:grob-property note1 'text))
44          (text2 (ly:grob-property note2 'text))
45          (fret1 (if (string? text1) (string->number text1) 0))
46          (fret2 (if (string? text2) (string->number text2) 0))
47          (letter (cond
48                   ((< fret1 fret2) "H")
49                   ((> fret1 fret2) "P")
50                   (else ""))))
51     (let* ((slur
52             ;; (Slur::print grob)
53
54             ;; 
55             ;; FIXME: a hammer is not a slur.
56             ;; 
57             (ly:make-stencil '() '(0 . 0) '(0 . 0)))
58            (layout (ly:grob-layout grob))
59            (text (interpret-markup
60                   layout
61                   (ly:grob-alist-chain grob (ly:output-def-lookup layout 'text-font-defaults))
62                   letter)))
63       
64       (let ((x (/ (- (cdr (ly:stencil-extent slur 0)) 
65                      (/ (cdr (ly:stencil-extent text 0)) 2.0))
66                   -2.0)))
67
68         (set! text
69               (ly:make-stencil (ly:stencil-expr text)
70                                (cons x x)
71                                (ly:stencil-extent text Y)))
72         
73         (ly:stencil-aligned-to text X RIGHT)))))
74
75
76 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
77 (define-public bass-tuning '(-17 -22 -27 -32))
78
79 ;; tunings for 5-string banjo
80 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
81 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
82 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
83 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
84 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
85 ;; convert 5-string banjo tunings to 4-string tunings by
86 ;; removing the 5th string
87 ;;
88 ;; example:
89 ;; \set TabStaff.stringTunings = #(four-string-banjo banjo-open-g-tuning)
90 (define-public (four-string-banjo tuning)
91   (reverse (cdr (reverse tuning))))
92
93 ;;; end of tablature functions
94
95
96 (define-public (make-stencil-boxer line-thick x-padding y-padding callback)
97   "Makes a routine that adds a box around the grob parsed as argument"
98   (define (stencil-boxer grob)
99     (let* ((mol (callback grob))
100            (x-ext (interval-widen (ly:stencil-extent mol 0) x-padding))
101            (y-ext (interval-widen (ly:stencil-extent mol 1) y-padding))
102            (x-rule (make-filled-box-stencil (interval-widen x-ext line-thick)
103                                             (cons 0 line-thick)))
104            (y-rule (make-filled-box-stencil (cons 0 line-thick) y-ext)))
105       
106       (set! mol (ly:stencil-combine-at-edge mol 0 1 y-rule x-padding))
107       (set! mol (ly:stencil-combine-at-edge mol 0 -1 y-rule x-padding))
108       (set! mol (ly:stencil-combine-at-edge mol 1 1 x-rule 0))  
109       (set! mol (ly:stencil-combine-at-edge mol 1 -1 x-rule 0))
110       mol))
111   stencil-boxer)
112
113 (define-public (arg->string arg)
114   (cond ((number? arg) (ly:inexact->string arg 10))
115         ((string? arg) (string-append "\"" arg "\""))
116         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
117
118 (define-public (print-circled-text-callback grob)
119   (let*
120       ((text (ly:grob-property grob 'text))
121        (layout (ly:grob-layout grob))
122        (defs (ly:output-def-lookup layout 'text-font-defaults))
123        (props (ly:grob-alist-chain grob defs))
124        (circle (Text_interface::interpret_markup layout props 
125                                                  (make-circle-markup
126                                                   0.8 0.1)))
127        (text-stencil
128         (Text_interface::interpret_markup layout props text)))
129
130     
131     (ly:stencil-add
132      (centered-stencil text-stencil)
133      circle)
134   ))
135
136
137 ;;(define (mm-to-pt x)
138 ;;  (* (/ 72.27 25.40) x))
139
140 ;; do nothing in .scm output
141
142 (define-public (ly:numbers->string lst)
143   (string-join (map ly:number->string lst) " "))
144
145 (define (number->octal-string x)
146   (let* ((n (inexact->exact x))
147          (n64 (quotient n 64))
148          (n8 (quotient (- n (* n64 64)) 8)))
149     (string-append
150      (number->string n64)
151      (number->string n8)
152      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
153
154 (define-public (ly:inexact->string x radix)
155   (let ((n (inexact->exact x)))
156     (number->string n radix)))
157
158 (define-public (ly:number-pair->string c)
159   (string-append (ly:number->string (car c)) " "
160                  (ly:number->string (cdr c))))
161
162
163 ;; silly, use alist? 
164 (define-public (find-notehead-symbol duration style)
165   (case style
166     ((xcircle) "2xcircle")
167     ((harmonic) "0harmonic")
168     ((baroque) 
169      ;; Oops, I actually would not call this "baroque", but, for
170      ;; backwards compatibility to 1.4, this is supposed to take
171      ;; brevis, longa and maxima from the neo-mensural font and all
172      ;; other note heads from the default font.  -- jr
173      (if (< duration 0)
174          (string-append (number->string duration) "neomensural")
175          (number->string duration)))
176     ((mensural)
177      (string-append (number->string duration) (symbol->string style)))
178     ((neomensural)
179      (string-append (number->string duration) (symbol->string style)))
180     ((default)
181      ;; The default font in mf/feta-bolletjes.mf defines a brevis, but
182      ;; neither a longa nor a maxima.  Hence let us, for the moment,
183      ;; take these from the neo-mensural font.  TODO: mf/feta-bolletjes
184      ;; should define at least a longa for the default font.  The longa
185      ;; should look exactly like the brevis of the default font, but
186      ;; with a stem exactly like that of the quarter note. -- jr
187      (if (< duration -1)
188          (string-append (number->string duration) "neomensural")
189          (number->string duration)))
190     (else
191      (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
192          (symbol->string style)
193          (string-append (number->string (max 0 duration))
194                         (symbol->string style))))))
195
196 ;; TODO junk completely?
197 (define (note-head-style->attachment-coordinates grob axis)
198   "Return pair (X . Y), containing multipliers for the note head
199 bounding box, where to attach the stem. e.g.: X==0 means horizontally
200 centered, X==1 is at the right, X == -1 is at the left."
201
202   '(1.0 . 0.0))
203
204 (define-public (string-encode-integer i)
205   (cond
206    ((= i  0) "o")
207    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
208    (else (string-append
209           (make-string 1 (integer->char (+ 65 (modulo i 26))))
210           (string-encode-integer (quotient i 26))))))
211
212 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
213
214 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
215
216 (define-public ((set-bar-number-visibility n) tr)
217   (let* ((bn (ly:context-property tr 'currentBarNumber)))
218     (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
219
220 (define-public (default-bar-number-visibility barnum) (> barnum 1))
221
222 ;; See documentation of Item::visibility_lambda_
223 (define-safe-public (begin-of-line-visible d) (if (= d 1) '(#f . #f) '(#t . #t)))
224 (define-safe-public (end-of-line-visible d) (if (= d -1) '(#f . #f) '(#t . #t)))
225 (define-safe-public (spanbar-begin-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
226
227 (define-safe-public (all-visible d) '(#f . #f))
228 (define-safe-public (all-invisible d) '(#t . #t))
229 (define-safe-public (begin-of-line-invisible d) (if (= d 1) '(#t . #t) '(#f . #f)))
230 (define-safe-public (center-invisible d) (if (= d 0) '(#t . #t) '(#f . #f)))
231 (define-safe-public (end-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
232
233
234 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
235 ;; Bar lines.
236
237 ;;
238 ;; How should a  bar line behave at a break? 
239 ;;
240 ;; Why prepend `default-' to every scm identifier?
241 (define-public (default-break-barline glyph dir)
242   (let ((result (assoc glyph 
243                        '((":|:" . (":|" . "|:"))
244                          ("||:" . ("||" . "|:"))
245                          ("|" . ("|" . ()))
246                          ("||:" . ("||" . "|:"))
247                          ("|s" . (() . "|"))
248                          ("|:" . ("|" . "|:"))
249                          ("|." . ("|." . ()))
250
251                          ;; hmm... should we end with a bar line here?
252                          (".|" . ("|" . ".|"))
253                          (":|" . (":|" . ()))
254                          ("||" . ("||" . ()))
255                          (".|." . (".|." . ()))
256                          ("" . ("" . ""))
257                          (":" . (":" . ""))
258                          ("empty" . (() . ()))
259                          ("brace" . (() . "brace"))
260                          ("bracket" . (() . "bracket"))  ))))
261
262     (if (equal? result #f)
263         (ly:warn "Unknown bar glyph: `~S'" glyph)
264         (index-cell (cdr result) dir))))
265
266 (define-public (shift-right-at-line-begin g)
267   "Shift an item to the right, but only at the start of the line."
268   (if (and (ly:item? g)  (equal? (ly:item-break-dir g) RIGHT))
269       (ly:grob-translate-axis! g 3.5 X)))
270
271
272 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
273 ;; Color
274
275 (define-public color? list?)
276
277 ; predefined colors
278 (define-public black       '(0.0 0.0 0.0))
279 (define-public white       '(1.0 1.0 1.0))
280 (define-public red         '(1.0 0.0 0.0))
281 (define-public green       '(0.0 1.0 0.0))
282 (define-public blue        '(0.0 0.0 1.0))
283 (define-public cyan        '(1.0 1.0 0.0))
284 (define-public magenta     '(1.0 0.0 1.0))
285 (define-public yellow      '(0.0 1.0 1.0))
286
287 (define-public grey        '(0.5 0.5 0.5))
288 (define-public darkred     '(0.5 0.0 0.0))
289 (define-public darkgreen   '(0.0 0.5 0.0))
290 (define-public darkblue    '(0.0 0.0 0.5))
291 (define-public darkcyan    '(0.5 0.5 0.0))
292 (define-public darkmagenta '(0.5 0.0 0.5))
293 (define-public darkyellow  '(0.0 0.5 0.5))