]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
(conv): add rules.
[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 (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          (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 (Text_interface::interpret_markup
83                   layout props (make-draw-circle-markup 0.8 0.1 #f)))
84          (text-stencil (Text_interface::interpret_markup layout props text)))
85     
86     (ly:stencil-add (centered-stencil text-stencil) circle)))
87
88
89 ;;(define (mm-to-pt x)
90 ;;  (* (/ 72.27 25.40) x))
91
92 ;; do nothing in .scm output
93
94 (define-public (ly:numbers->string lst)
95   (string-join (map ly:number->string lst) " "))
96
97 (define (number->octal-string x)
98   (let* ((n (inexact->exact x))
99          (n64 (quotient n 64))
100          (n8 (quotient (- n (* n64 64)) 8)))
101     (string-append
102      (number->string n64)
103      (number->string n8)
104      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
105
106 (define-public (ly:inexact->string x radix)
107   (let ((n (inexact->exact x)))
108     (number->string n radix)))
109
110 (define-public (ly:number-pair->string c)
111   (string-append (ly:number->string (car c)) " "
112                  (ly:number->string (cdr c))))
113
114
115 ;; silly, use alist? 
116 (define-public (note-head::calc-glyph-name grob)
117   (let*
118       ((style (ly:grob-property grob 'style))
119        (log (min 2 (ly:grob-property grob 'duration-log))))
120     
121     (case style
122       ((xcircle) "2xcircle")
123       ((harmonic) "0harmonic")
124       ((baroque) 
125        ;; Oops, I actually would not call this "baroque", but, for
126        ;; backwards compatibility to 1.4, this is supposed to take
127        ;; brevis, longa and maxima from the neo-mensural font and all
128        ;; other note heads from the default font.  -- jr
129        (if (< log 0)
130            (string-append (number->string log) "neomensural")
131            (number->string log)))
132       ((mensural)
133        (string-append (number->string log) (symbol->string style)))
134       ((petrucci)
135        (if (< log 0)
136            (string-append (number->string log) "mensural")
137            (string-append (number->string log) (symbol->string style))))
138       ((neomensural)
139        (string-append (number->string log) (symbol->string style)))
140       ((default)
141        ;; The default font in mf/feta-bolletjes.mf defines a brevis, but
142        ;; neither a longa nor a maxima.  Hence let us, for the moment,
143        ;; take these from the neo-mensural font.  TODO: mf/feta-bolletjes
144        ;; should define at least a longa for the default font.  The longa
145        ;; should look exactly like the brevis of the default font, but
146        ;; with a stem exactly like that of the quarter note. -- jr
147        (if (< log -1)
148            (string-append (number->string log) "neomensural")
149            (number->string log)))
150       (else
151        (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
152            (symbol->string style)
153            (string-append (number->string (max 0 log))
154                           (symbol->string style)))))))
155
156 ;; TODO junk completely?
157 (define (note-head-style->attachment-coordinates grob axis)
158   "Return pair (X . Y), containing multipliers for the note head
159 bounding box, where to attach the stem. e.g.: X==0 means horizontally
160 centered, X==1 is at the right, X == -1 is at the left."
161
162   '(1.0 . 0.0))
163
164 (define-public (string-encode-integer i)
165   (cond
166    ((= i  0) "o")
167    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
168    (else (string-append
169           (make-string 1 (integer->char (+ 65 (modulo i 26))))
170           (string-encode-integer (quotient i 26))))))
171
172 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
173
174 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
175
176 (define-public ((set-bar-number-visibility n) tr)
177   (let* ((bn (ly:context-property tr 'currentBarNumber)))
178     (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
179
180 (define-public (first-bar-number-invisible barnum) (> barnum 1))
181
182 ;; See documentation of Item::visibility_lambda_
183 (define-public begin-of-line-visible
184   #(#f #f #t))
185 (define-public end-of-line-visible
186   #(#t #f #f))
187 (define-public end-of-line-invisible
188   #(#f #t #t))
189 (define-public spanbar-begin-of-line-invisible
190   #(#t #f #f))
191 (define-public all-visible #(#t #t #t))
192 (define-public all-invisible #(#f #f #f))
193 (define-public begin-of-line-invisible
194   #(#t #t #f))
195 (define-public center-invisible #(#t #f #t))
196
197 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
198 ;; Bar lines.
199
200 ;;
201 ;; How should a  bar line behave at a break? 
202 ;;
203 ;; Why prepend `default-' to every scm identifier?
204 (define-public (bar-line::calc-glyph-name grob)
205   (let* (
206          (glyph (ly:grob-property grob 'glyph))
207          (dir (ly:item-break-dir grob))
208          (result (assoc glyph 
209                        '((":|:" . (":|" . "|:"))
210                          ("||:" . ("||" . "|:"))
211                          ("|" . ("|" . ()))
212                          ("||:" . ("||" . "|:"))
213                          ("|s" . (() . "|"))
214                          ("|:" . ("|" . "|:"))
215                          ("|." . ("|." . ()))
216
217                          ;; hmm... should we end with a bar line here?
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 glyph-name
235         (set! (ly:grob-property grob 'glyph-name) glyph-name)
236         (ly:grob-suicide! grob))))
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        '(1.0 1.0 0.0))
258 (define-public magenta     '(1.0 0.0 1.0))
259 (define-public yellow      '(0.0 1.0 1.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.5 0.5 0.0))
266 (define-public darkmagenta '(0.5 0.0 0.5))
267 (define-public darkyellow  '(0.0 0.5 0.5))
268
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 ;; Pitch Trill Heads
271
272 (define (parenthesize-elements grob)
273   (let*
274       ((elts (ly:grob-object grob 'elements))
275        (x-ext (ly:relative-group-extent elts grob X))
276        (font (ly:grob-default-font grob))
277        (lp (ly:font-get-glyph font "accidentals.leftparen"))
278        (rp (ly:font-get-glyph font "accidentals.rightparen"))
279        (padding 0.1))
280
281     (ly:stencil-add
282      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
283      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))
284   ))