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