]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
(all-grob-descriptions): remove all
[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--2004 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         (ly:stencil-set-extent! text 0 (cons x x))
69         (ly:stencil-align-to! text 0 1)))))
70
71 ;; (ly:stencil-combine-at-edge slur 1 1 text -0.6)
72
73
74
75 (define-public guitar-tuning '(4 -1 -5 -10 -15 -20))
76 (define-public bass-tuning '(-17 -22 -27 -32))
77
78 ;; tunings for 5-string banjo
79 (define-public banjo-open-g-tuning '(2 -1 -5 -10 7))
80 (define-public banjo-c-tuning '(2 -1 -5 -12 7))
81 (define-public banjo-modal-tuning '(2 0 -5 -10 7))
82 (define-public banjo-open-d-tuning '(2 -3 -6 -10 9))
83 (define-public banjo-open-dm-tuning '(2 -3 -6 -10 9))
84 ;; convert 5-string banjo tunings to 4-string tunings by
85 ;; removing the 5th string
86 ;;
87 ;; example:
88 ;; \set TabStaff.stringTunings = #(four-string-banjo banjo-open-g-tuning)
89 (define-public (four-string-banjo tuning)
90   (reverse (cdr (reverse tuning))))
91
92 ;;; end of tablature functions
93
94
95 (define-public (make-stencil-boxer line-thick x-padding y-padding callback)
96   "Makes a routine that adds a box around the grob parsed as argument"
97   (define (stencil-boxer grob)
98     (let* ((mol (callback grob))
99            (x-ext (interval-widen (ly:stencil-extent mol 0) x-padding))
100            (y-ext (interval-widen (ly:stencil-extent mol 1) y-padding))
101            (x-rule (make-filled-box-stencil (interval-widen x-ext line-thick)
102                                             (cons 0 line-thick)))
103            (y-rule (make-filled-box-stencil (cons 0 line-thick) y-ext)))
104       
105       (set! mol (ly:stencil-combine-at-edge mol 0 1 y-rule x-padding))
106       (set! mol (ly:stencil-combine-at-edge mol 0 -1 y-rule x-padding))
107       (set! mol (ly:stencil-combine-at-edge mol 1 1 x-rule 0))  
108       (set! mol (ly:stencil-combine-at-edge mol 1 -1 x-rule 0))
109       mol))
110   stencil-boxer)
111
112 (define-public (arg->string arg)
113   (cond ((number? arg) (ly:inexact->string arg 10))
114         ((string? arg) (string-append "\"" arg "\""))
115         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
116
117 (define-public (func name . args)
118   (string-append
119    "(" name
120    (if (null? args)
121        ""
122        (apply string-append
123               (map (lambda (x) (string-append " " (arg->string x))) args)))
124    ")\n"))
125
126 ;;(define (mm-to-pt x)
127 ;;  (* (/ 72.27 25.40) x))
128
129 ;; do nothing in .scm output
130
131 (define-public (ly:numbers->string lst)
132   (string-join (map ly:number->string lst) " "))
133
134 (define (number->octal-string x)
135   (let* ((n (inexact->exact x))
136          (n64 (quotient n 64))
137          (n8 (quotient (- n (* n64 64)) 8)))
138     (string-append
139      (number->string n64)
140      (number->string n8)
141      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
142
143 (define-public (ly:inexact->string x radix)
144   (let ((n (inexact->exact x)))
145     (number->string n radix)))
146
147 (define-public (ly:number-pair->string c)
148   (string-append (ly:number->string (car c)) " "
149                  (ly:number->string (cdr c))))
150
151 (define (font i)
152   (string-append
153    "font"
154    (make-string 1 (integer->char (+ (char->integer #\A) i)))))
155
156 (define (scm-scm action-name)
157   1)
158
159 ;; silly, use alist? 
160 (define-public (find-notehead-symbol duration style)
161   (case style
162     ((xcircle) "2xcircle")
163     ((harmonic) "0harmonic")
164     ((baroque) 
165      ;; Oops, I actually would not call this "baroque", but, for
166      ;; backwards compatibility to 1.4, this is supposed to take
167      ;; brevis, longa and maxima from the neo-mensural font and all
168      ;; other note heads from the default font.  -- jr
169      (if (< duration 0)
170          (string-append (number->string duration) "neomensural")
171          (number->string duration)))
172     ((mensural)
173      (string-append (number->string duration) (symbol->string style)))
174     ((neomensural)
175      (string-append (number->string duration) (symbol->string style)))
176     ((default)
177      ;; The default font in mf/feta-bolletjes.mf defines a brevis, but
178      ;; neither a longa nor a maxima.  Hence let us, for the moment,
179      ;; take these from the neo-mensural font.  TODO: mf/feta-bolletjes
180      ;; should define at least a longa for the default font.  The longa
181      ;; should look exactly like the brevis of the default font, but
182      ;; with a stem exactly like that of the quarter note. -- jr
183      (if (< duration -1)
184          (string-append (number->string duration) "neomensural")
185          (number->string duration)))
186     (else
187      (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
188          (symbol->string style)
189          (string-append (number->string (max 0 duration))
190                         (symbol->string style))))))
191
192 ;; TODO junk completely?
193 (define (note-head-style->attachment-coordinates grob axis)
194   "Return pair (X . Y), containing multipliers for the note head
195 bounding box, where to attach the stem. e.g.: X==0 means horizontally
196 centered, X==1 is at the right, X == -1 is at the left."
197
198   '(1.0 . 0.0))
199
200 (define-public (string-encode-integer i)
201   (cond
202    ((= i  0) "o")
203    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
204    (else (string-append
205           (make-string 1 (integer->char (+ 65 (modulo i 26))))
206           (string-encode-integer (quotient i 26))))))
207
208 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
209
210 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
211
212 (define-public ((set-bar-number-visibility n) tr)
213   (let* ((bn (ly:context-property tr 'currentBarNumber)))
214     (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
215
216 (define-public (default-bar-number-visibility barnum) (> barnum 1))
217
218 ;; See documentation of Item::visibility_lambda_
219 (define-public (begin-of-line-visible d) (if (= d 1) '(#f . #f) '(#t . #t)))
220 (define-public (end-of-line-visible d) (if (= d -1) '(#f . #f) '(#t . #t)))
221 (define-public (spanbar-begin-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
222
223 (define-public (all-visible d) '(#f . #f))
224 (define-public (all-invisible d) '(#t . #t))
225 (define-public (begin-of-line-invisible d) (if (= d 1) '(#t . #t) '(#f . #f)))
226 (define-public (end-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
227
228
229 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
230 ;; Bar lines.
231
232 ;;
233 ;; How should a  bar line behave at a break? 
234 ;;
235 ;; Why prepend `default-' to every scm identifier?
236 (define-public (default-break-barline glyph dir)
237   (let ((result (assoc glyph 
238                        '((":|:" . (":|" . "|:"))
239                          ("||:" . ("||" . "|:"))
240                          ("|" . ("|" . ()))
241                          ("||:" . ("||" . "|:"))
242                          ("|s" . (() . "|"))
243                          ("|:" . ("|" . "|:"))
244                          ("|." . ("|." . ()))
245
246                          ;; hmm... should we end with a bar line here?
247                          (".|" . ("|" . ".|"))
248                          (":|" . (":|" . ()))
249                          ("||" . ("||" . ()))
250                          (".|." . (".|." . ()))
251                          ("" . ("" . ""))
252                          (":" . (":" . ""))
253                          ("empty" . (() . ()))
254                          ("brace" . (() . "brace"))
255                          ("bracket" . (() . "bracket"))  ))))
256
257     (if (equal? result #f)
258         (ly:warn "Unknown bar glyph: `~S'" glyph)
259         (index-cell (cdr result) dir))))
260
261 (define-public (shift-right-at-line-begin g)
262   "Shift an item to the right, but only at the start of the line."
263   (if (and (ly:item? g)  (equal? (ly:item-break-dir g) RIGHT))
264       (ly:grob-translate-axis! g 3.5 X)))