]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
2004-09-10 Juergen Reuter <reuter@ipd.uka.de>
[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 ; 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))
13
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                 (- string 1) ; remove 1 because list index starts at 0 and guitar string at 1.
22                 )
23       ))) 
24
25 (define-public (hammer-print-function grob)
26   (let* ((note-collums (ly:grob-property grob 'note-columns))
27          (note-column1 (cadr note-collums))
28          (note-column2 (car  note-collums))
29          (note1        (car (ly:grob-property note-column1 'note-heads)))
30          (note2        (car (ly:grob-property note-column2 'note-heads)))
31          (text1  (ly:grob-property note1 'text))
32          (text2  (ly:grob-property note2 'text))
33          (fret1        (if (string? text1) (string->number text1) 0))
34          (fret2        (if (string? text2) (string->number text2) 0))
35          (letter       (cond
36                         ((< fret1 fret2) "H")
37                         ((> fret1 fret2) "P")
38                         (else "")))
39                                    
40          )
41     (let* (
42            (slur ; (Slur::print grob)
43
44             ;; 
45             ;; FIXME: a hammer is not a slur.
46             ;; 
47             (ly:make-stencil '() '(0 . 0) '(0 . 0)))
48            (paper (ly:grob-paper grob))
49            (text (interpret-markup
50                   paper
51                   (ly:grob-alist-chain grob (ly:output-def-lookup paper 'text-font-defaults))
52                   letter)))
53     
54       (let ((x (/ (- (cdr (ly:stencil-extent slur 0)) 
55                      (/ (cdr (ly:stencil-extent text 0)) 2.0)
56                      )
57                   -2.0)))
58       
59         (ly:stencil-set-extent! text 0 (cons x x))
60         (ly:stencil-align-to! text 0 1)
61         )
62
63 ;      (ly:stencil-combine-at-edge slur 1 1 text -0.6)
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-stencil-boxer line-thick x-padding y-padding callback)
74   "Makes a routine that adds a box around the grob parsed as argument"
75   (define (stencil-boxer grob)
76     (let*
77         (
78          (mol    (callback grob))
79          (x-ext (interval-widen (ly:stencil-extent mol 0) x-padding))
80          (y-ext (interval-widen (ly:stencil-extent mol 1) y-padding))
81          (x-rule (make-filled-box-stencil (interval-widen x-ext line-thick)
82                                           (cons 0 line-thick)))
83          (y-rule (make-filled-box-stencil (cons 0 line-thick) y-ext))
84          )
85
86       (set! mol (ly:stencil-combine-at-edge mol 0 1 y-rule x-padding))
87       (set! mol (ly:stencil-combine-at-edge mol 0 -1 y-rule x-padding))
88       (set! mol (ly:stencil-combine-at-edge mol 1 1 x-rule 0))  
89       (set! mol (ly:stencil-combine-at-edge mol 1 -1 x-rule 0))
90       
91       mol))
92   stencil-boxer)
93
94 (define-public (arg->string arg)
95   (cond ((number? arg) (ly:inexact->string arg 10))
96         ((string? arg) (string-append "\"" arg "\""))
97         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
98
99 (define-public (func name . args)
100   (string-append
101    "(" name
102    (if (null? args)
103        ""
104        (apply string-append
105               (map (lambda (x) (string-append " " (arg->string x))) args)))
106    ")\n"))
107
108 ;;(define (mm-to-pt x)
109 ;;  (* (/ 72.27 25.40) x))
110
111 ;; do nothing in .scm output
112
113 (define-public (ly:numbers->string lst)
114   (string-join (map ly:number->string lst) " "))
115
116 (define (number->octal-string x)
117   (let* ((n (inexact->exact x))
118          (n64 (quotient n 64))
119          (n8 (quotient (- n (* n64 64)) 8)))
120     (string-append
121      (number->string n64)
122      (number->string n8)
123      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
124
125 (define-public (ly:inexact->string x radix)
126   (let ((n (inexact->exact x)))
127     (number->string n radix)))
128
129 (define-public (ly:number-pair->string c)
130   (string-append (ly:number->string (car c)) " "
131                  (ly:number->string (cdr c))))
132
133 (define (font i)
134   (string-append
135    "font"
136    (make-string 1 (integer->char (+ (char->integer #\A) i)))))
137
138 (define (scm-scm action-name)
139   1)
140
141
142 ;; silly, use alist? 
143 (define-public (find-notehead-symbol duration style)
144   (case style
145    ((xcircle) "2xcircle")
146    ((harmonic) "0harmonic")
147    ((baroque) 
148     ;; Oops, I actually would not call this "baroque", but, for
149     ;; backwards compatibility to 1.4, this is supposed to take
150     ;; brevis, longa and maxima from the neo-mensural font and all
151     ;; other note heads from the default font.  -- jr
152     (if (< duration 0)
153         (string-append (number->string duration) "neomensural")
154         (number->string duration)))
155    ((mensural)
156     (string-append (number->string duration) (symbol->string style)))
157    ((neomensural)
158     (string-append (number->string duration) (symbol->string style)))
159    ((default)
160     ;; The default font in mf/feta-bolletjes.mf defines a brevis, but
161     ;; neither a longa nor a maxima.  Hence let us, for the moment,
162     ;; take these from the neo-mensural font.  TODO: mf/feta-bolletjes
163     ;; should define at least a longa for the default font.  The longa
164     ;; should look exactly like the brevis of the default font, but
165     ;; with a stem exactly like that of the quarter note. -- jr
166     (if (< duration -1)
167         (string-append (number->string duration) "neomensural")
168         (number->string duration)))
169    (else
170     (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
171         (symbol->string style)
172         (string-append (number->string (max 0 duration))
173                        (symbol->string style))))))
174
175
176 ;; TODO junk completely?
177 (define (note-head-style->attachment-coordinates grob axis)
178   "Return pair (X . Y), containing multipliers for the note head
179 bounding box, where to attach the stem. e.g.: X==0 means horizontally
180 centered, X==1 is at the right, X == -1 is at the left."
181
182    '(1.0 . 0.0))
183
184
185 (define-public (string-encode-integer i)
186   (cond
187    ((= i  0) "o")
188    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
189    (else (string-append
190           (make-string 1 (integer->char (+ 65 (modulo i 26))))
191           (string-encode-integer (quotient i 26))))))
192
193
194 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
195
196 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
197
198 (define-public ((set-bar-number-visibility n) tr)
199   (let* ((bn (ly:context-property tr 'currentBarNumber)))
200    (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
201
202 (define-public (default-bar-number-visibility barnum) (> barnum 1))
203
204 ;; See documentation of Item::visibility_lambda_
205 (define-public (begin-of-line-visible d) (if (= d 1) '(#f . #f) '(#t . #t)))
206 (define-public (end-of-line-visible d) (if (= d -1) '(#f . #f) '(#t . #t)))
207 (define-public (spanbar-begin-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
208
209 (define-public (all-visible d) '(#f . #f))
210 (define-public (all-invisible d) '(#t . #t))
211 (define-public (begin-of-line-invisible d) (if (= d 1) '(#t . #t) '(#f . #f)))
212 (define-public (end-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
213
214
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216 ;; Bar lines.
217
218 ;
219 ; How should a  bar line behave at a break? 
220 ;
221 ;; Why prepend `default-' to every scm identifier?
222 (define-public (default-break-barline glyph dir)
223    (let ((result (assoc glyph 
224                         '((":|:" . (":|" . "|:"))
225                           ("||:" . ("||" . "|:"))
226                           ("|" . ("|" . ()))
227                           ("||:" . ("||" . "|:"))
228                           ("|s" . (() . "|"))
229                           ("|:" . ("|" . "|:"))
230                           ("|." . ("|." . ()))
231
232                           ;; hmm... should we end with a bar line here?
233                           (".|" . ("|" . ".|"))
234                           (":|" . (":|" . ()))
235                           ("||" . ("||" . ()))
236                           (".|." . (".|." . ()))
237                           ("" . ("" . ""))
238                           (":" . (":" . ""))
239                           ("empty" . (() . ()))
240                           ("brace" . (() . "brace"))
241                           ("bracket" . (() . "bracket"))  
242                           )
243                         )))
244
245      (if (equal? result #f)
246          (ly:warn "Unknown bar glyph: `~S'" glyph)
247          (index-cell (cdr result) dir))
248      ) )
249      
250
251
252 (define-public (shift-right-at-line-begin g)
253   "Shift an item to the right, but only at the start of the line."
254   (if (and (ly:item? g)  (equal? (ly:item-break-dir g) RIGHT))
255       (ly:grob-translate-axis! g 3.5 X)
256   ))