]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
* lily/slur.cc (get_attachment): do something sensible when
[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--2003 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 ; Tablature functions, by Jiba (jiba@tuxfamily.org)
9
10 ; The TabNoteHead stem attachment function.
11 (define (tablature-stem-attachment-function style duration)
12   (cons 0.0 0.5) ;; UGH!
13 )
14
15 ; The TabNoteHead molecule callback.
16 ; Create a text molecule
17 (define-public (tablature-molecule-callback grob)
18   (let ((molecule (fontify-text
19                    (ly:get-default-font grob)
20                    (ly:get-grob-property grob 'text)
21                    )))
22     molecule ; return the molecule.
23     ))
24
25 ; The TabNoteHead tablatureFormat callback.
26 ; Compute the text grob-property
27 (define-public (fret-number-tablature-format string tuning pitch)
28   (number->string
29    (- (ly:pitch-semitones pitch)
30       (list-ref tuning
31                 (- string 1) ; remove 1 because list index starts at 0 and guitar string at 1.
32                 )
33       )
34    )
35   )
36
37 (define-public (hammer-molecule-callback grob)
38   (let* ((note-collums (ly:get-grob-property grob 'note-columns))
39          (note-column1 (cadr note-collums))
40          (note-column2 (car  note-collums))
41          (note1        (car (ly:get-grob-property note-column1 'note-heads)))
42          (note2        (car (ly:get-grob-property note-column2 'note-heads)))
43          (fret1        (string->number (ly:get-grob-property note1 'text)))
44          (fret2        (string->number (ly:get-grob-property note2 'text)))
45          (letter       (if (< fret1 fret2) "H"
46                        (if (> fret1 fret2) "P"
47                                            "")))
48          )
49     (let ((slur (Slur::brew_molecule grob))
50           (text (fontify-text (ly:get-default-font grob) letter)))
51     
52       (let ((x (/ (- (cdr (ly:molecule-get-extent slur 0)) 
53                      (/ (cdr (ly:molecule-get-extent text 0)) 2.0)
54                      )
55                   -2.0)))
56       
57         (ly:molecule-set-extent! text 0 (cons x x))
58         (ly:molecule-align-to! text 0 1)
59         )
60
61       (ly:molecule-combine-at-edge slur 1 1 text -0.6)
62       )
63     )
64   )
65
66
67
68 (define-public guitar-tunings '(4 -1 -5 -10 -15 -20))
69
70 ; end of tablature functions
71
72
73 (define-public (make-molecule-boxer line-thick x-padding y-padding callback)
74    "Makes a routine that adds a box around the grob parsed as argument"
75   (define (molecule-boxer grob)
76   (let*
77    (
78     (mol    (callback grob))
79     (x-ext (interval-widen (ly:molecule-get-extent mol 0) x-padding))
80     (y-ext (interval-widen (ly:molecule-get-extent mol 1) y-padding))
81     (x-rule (make-filled-box-molecule (interval-widen x-ext line-thick)
82                               (cons 0 line-thick)))
83     (y-rule (make-filled-box-molecule (cons 0 line-thick) y-ext))
84     )
85     
86     (set! mol (ly:molecule-combine-at-edge mol 0 1 y-rule x-padding))
87     (set! mol (ly:molecule-combine-at-edge mol 0 -1  y-rule x-padding))
88     (set! mol (ly:molecule-combine-at-edge mol 1 1  x-rule 0))  
89     (set! mol (ly:molecule-combine-at-edge mol 1 -1 x-rule 0))
90     
91     mol
92  ))
93  molecule-boxer
94  )
95
96 (define-public (arg->string arg)
97   (cond ((number? arg) (inexact->string arg 10))
98         ((string? arg) (string-append "\"" arg "\""))
99         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
100
101 (define-public (func name . args)
102   (string-append
103    "(" name
104    (if (null? args)
105        ""
106        (apply string-append
107               (map (lambda (x) (string-append " " (arg->string x))) args)))
108    ")\n"))
109
110 ;;(define (mm-to-pt x)
111 ;;  (* (/ 72.27 25.40) x))
112
113 ;; do nothing in .scm output
114 (define-public (comment s) "")
115
116 (define-public (numbers->string l)
117   (apply string-append (map ly:number->string l)))
118
119 ; (define (chop-decimal x) (if (< (abs x) 0.001) 0.0 x))
120
121 (define (number->octal-string x)
122   (let* ((n (inexact->exact x))
123          (n64 (quotient n 64))
124          (n8 (quotient (- n (* n64 64)) 8)))
125     (string-append
126      (number->string n64)
127      (number->string n8)
128      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
129
130 (define-public (inexact->string x radix)
131   (let ((n (inexact->exact x)))
132     (number->string n radix)))
133
134
135 (define-public (number-pair->string c)
136   (string-append (ly:number->string (car c)) " "
137                  (ly:number->string (cdr c)) " "))
138
139 (define (font i)
140   (string-append
141    "font"
142    (make-string 1 (integer->char (+ (char->integer #\A) i)))))
143
144 (define (scm-scm action-name)
145   1)
146
147
148 ;; silly, use alist? 
149 (define-public (find-notehead-symbol duration style)
150   (case style
151    ((xcircle) "2xcircle")
152    ((harmonic) "0neo_mensural")
153    ((baroque) 
154     ;; Oops, I actually would not call this "baroque", but, for
155     ;; backwards compatibility to 1.4, this is supposed to take
156     ;; brevis, longa and maxima from the neo-mensural font and all
157     ;; other note heads from the default font.  -- jr
158     (if (< duration 0)
159         (string-append (number->string duration) "neo_mensural")
160         (number->string duration)))
161    ((mensural)
162     (string-append (number->string duration) (symbol->string style)))
163    ((neo_mensural)
164     (string-append (number->string duration) (symbol->string style)))
165    ((default)
166     ;; The default font in mf/feta-bolletjes.mf defines a brevis, but
167     ;; neither a longa nor a maxima.  Hence let us, for the moment,
168     ;; take these from the neo-mensural font.  TODO: mf/feta-bolletjes
169     ;; should define at least a longa for the default font.  The longa
170     ;; should look exactly like the brevis of the default font, but
171     ;; with a stem exactly like that of the quarter note. -- jr
172     (if (< duration -1)
173         (string-append (number->string duration) "neo_mensural")
174         (number->string duration)))
175    (else
176     (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
177         (symbol->string style)
178         (string-append (number->string (max 0 duration))
179                        (symbol->string style))))))
180
181
182 (define (note-head-style->attachment-coordinates style duration)
183   "Return pair (X . Y), containing multipliers for the note head
184 bounding box, where to attach the stem. e.g.: X==0 means horizontally
185 centered, X==1 is at the right, X == -1 is at the left."
186
187
188   (case style
189     ((default)
190      (if (< duration -1)
191          '(0.0 . 0.6) ;; neo-mensural
192          '(1.0 . 0.5) ;; default
193          ))
194     ((cross) '(1.0 . 0.75))
195     ((mensural) '(0.0 . 0.6))
196     ((neo_mensural) '(0.0 . 0.6))
197     ((diamond) '(1.0 . 0.8))
198     ((transparent) '(1.0 . 1.0))
199     ((slash) '(1.0 . 1.0))
200     ((harmonic) '(1.0 0.0))
201
202     ;;
203     ;;UGH this needs to be changed: triangle is not point-symmetric, has different attachments
204     ;;  for up/down stem
205     ((triangle) '(0.75 . 0.15))
206     ((baroque)
207      (if (< duration 0)
208          '(0.0 . 0.6) ;; neo-mensural
209          '(1.0 . 0.5) ;; default
210          ))
211     (else
212
213      ;; this also works for easy notation.
214      '(1.0 . 0.0)
215      )))
216
217 (define-public (string-encode-integer i)
218   (cond
219    ((= i  0) "o")
220    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
221    (else (string-append
222           (make-string 1 (integer->char (+ 65 (modulo i 26))))
223           (string-encode-integer (quotient i 26))))))
224
225
226 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
227
228 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
229
230 (define-public ((set-bar-number-visibility n) tr)
231   (let* ((bn (ly:get-context-property tr 'currentBarNumber)))
232    (ly:set-context-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
233
234 (define-public (default-bar-number-visibility barnum) (> barnum 1))
235
236 ;; See documentation of Item::visibility_lambda_
237 (define-public (begin-of-line-visible d) (if (= d 1) '(#f . #f) '(#t . #t)))
238 (define-public (end-of-line-visible d) (if (= d -1) '(#f . #f) '(#t . #t)))
239 (define-public (spanbar-begin-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
240
241 (define-public (all-visible d) '(#f . #f))
242 (define-public (all-invisible d) '(#t . #t))
243 (define-public (begin-of-line-invisible d) (if (= d 1) '(#t . #t) '(#f . #f)))
244 (define-public (end-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
245
246
247 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248 ;; Bar lines.
249
250 ;
251 ; How should a  bar line behave at a break? 
252 ;
253 ;; Why prepend `default-' to every scm identifier?
254 (define-public (default-break-barline glyph dir)
255    (let ((result (assoc glyph 
256                         '((":|:" . (":|" . "|:"))
257                           ("||:" . ("||" . "|:"))
258                           ("|" . ("|" . ()))
259                           ("||:" . ("||" . "|:"))
260                           ("|s" . (() . "|"))
261                           ("|:" . ("|" . "|:"))
262                           ("|." . ("|." . ()))
263
264                           ;; hmm... should we end with a barline here?
265                           (".|" . ("|" . ".|"))
266                           (":|" . (":|" . ()))
267                           ("||" . ("||" . ()))
268                           (".|." . (".|." . ()))
269                           ("" . ("" . ""))
270                           ("empty" . (() . ()))
271                           ("brace" . (() . "brace"))
272                           ("bracket" . (() . "bracket"))  
273                           )
274                         )))
275
276      (if (equal? result #f)
277          (ly:warn (string-append "Unknown bar glyph: `" glyph "'"))
278          (index-cell (cdr result) dir))
279      )
280    )
281