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