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