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