]> git.donarmstrong.com Git - lilypond.git/blob - scm/tex.scm
c002a22b3f3dea5eb4445d47818e04e3a851167f
[lilypond.git] / scm / tex.scm
1 ;;; tex.scm -- implement Scheme output routines for TeX
2 ;;;
3 ;;;  source file of the GNU LilyPond music typesetter
4 ;;; 
5 ;;; (c) 1998--2001 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8
9
10 ;;
11 ;; todo: this dispatch is totally LAME
12  
13 (define (tex-scm action-name)
14   (define (unknown) 
15     "%\n\\unknown%\n")
16
17
18   (define (select-font name-mag-pair)
19     (let*
20         (
21          (c (assoc name-mag-pair font-name-alist))
22          )
23
24       (if (eq? c #f)
25           (begin
26             (display "FAILED\n")
27             (display (object-type (car name-mag-pair)))
28             (display (object-type (caaar font-name-alist)))
29
30             (ly-warn (string-append
31                       "Programming error: No such font known "
32                       (car name-mag-pair) " "
33                       (ly-number->string (cdr name-mag-pair))
34                       ))
35             "") ; issue no command
36           (string-append "\\" (cddr c)))
37       
38       
39       ))
40   
41   (define (beam width slope thick)
42     (embedded-ps ((ps-scm 'beam) width slope thick)))
43
44   (define (bracket arch_angle arch_width arch_height height arch_thick thick)
45     (embedded-ps ((ps-scm 'bracket) arch_angle arch_width arch_height height arch_thick thick)))
46
47   (define (dashed-slur thick dash l)
48     (embedded-ps ((ps-scm 'dashed-slur)  thick dash l)))
49
50   (define (hairpin thick w sh eh)
51     (embedded-ps ((ps-scm 'hairpin) thick w sh eh)))
52
53   (define (char i)
54     (string-append "\\char" (inexact->string i 10) " "))
55   
56   (define (dashed-line thick on off dx dy)
57     (embedded-ps ((ps-scm 'dashed-line) thick on off dx dy)))
58
59   (define (font-load-command name-mag command)
60     (string-append
61      "\\font\\" command "="
62      (car name-mag)
63      " scaled "
64      (ly-number->string (inexact->exact (* 1000  (cdr name-mag))))
65      "\n"))
66
67   (define (ez-ball c l b)
68     (embedded-ps ((ps-scm 'ez-ball) c  l b)))
69   (define (embedded-ps s)
70     (string-append "\\embeddedps{" s "}"))
71
72   (define (comment s)
73     (string-append "% " s))
74   
75   (define (end-output) 
76         (begin
77 ; uncomment for some stats about lily memory      
78 ;               (display (gc-stats))
79     (string-append "\n\\EndLilyPondOutput"
80                    ; Put GC stats here.
81                    )))
82   
83   (define (experimental-on)
84     "")
85
86   (define (repeat-slash w a t)
87     (embedded-ps ((ps-scm 'repeat-slash) w a t)))
88   
89   (define (font-switch i)
90     (string-append
91      "\\" (font i) "\n"))
92
93   (define (font-def i s)
94     (string-append
95      "\\font" (font-switch i) "=" s "\n"))
96
97   (define (header-end)
98     (string-append
99      "\\special{\\string! "
100      
101      ;; URG: ly-gulp-file: now we can't use scm output without Lily
102      (if use-regex
103          ;; fixed in 1.3.4 for powerpc -- broken on Windows
104          (regexp-substitute/global #f "\n"
105                                    (ly-gulp-file "music-drawing-routines.ps") 'pre " %\n" 'post)
106          (ly-gulp-file "music-drawing-routines.ps"))
107      (if (defined? 'ps-testing) "/testing true def%\n" "")
108      "}"
109      "\\input lilyponddefs\\newdimen\\outputscale \\outputscale=\\lilypondpaperoutputscale pt\\turnOnPostScript"))
110
111   ;; Note: this string must match the string in ly2dvi.py!!!
112   (define (header creator generate) 
113     (string-append
114      "% Generated automatically by: " creator generate "\n"))
115
116   (define (invoke-char s i)
117     (string-append 
118      "\n\\" s "{" (inexact->string i 10) "}" ))
119
120   (define (invoke-dim1 s d)
121     (string-append
122      "\n\\" s "{" (number->dim d) "}"))
123   (define (pt->sp x)
124     (* 65536 x))
125   
126   ;;
127   ;; need to do something to make this really safe.
128   ;;
129   (define (output-tex-string s)
130       (if security-paranoia
131           (if use-regex
132               (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
133               (begin (display "warning: not paranoid") (newline) s))
134           s))
135       
136   (define (lily-def key val)
137     (let ((tex-key
138            (if use-regex
139                ;; fixed in 1.3.4 for powerpc -- broken on Windows
140                (regexp-substitute/global
141                 #f "_" (output-tex-string key) 'pre "X" 'post)
142                (output-tex-string key)))
143           (tex-val (output-tex-string val)))
144       (if (equal? (sans-surrounding-whitespace tex-val) "")
145           (string-append "\\let\\" tex-key "\\undefined\n")
146           (string-append "\\def\\" tex-key "{" tex-val "}\n"))))
147                 
148   (define (number->dim x)
149     (string-append
150      ;;ugh ly-* in backend needs compatibility func for standalone output
151      (ly-number->string x) " \\outputscale "))
152
153   (define (placebox x y s) 
154     (string-append 
155      "\\placebox{"
156      (number->dim y) "}{" (number->dim x) "}{" s "}\n"))
157
158   (define (bezier-sandwich l thick)
159     (embedded-ps ((ps-scm 'bezier-sandwich) l thick)))
160
161   (define (start-line ht)
162       (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
163
164   (define (stop-line) 
165     "}\\vss}\\interscoreline\n")
166   (define (stop-last-line)
167     "}\\vss}")
168   (define (xfilledbox breapth width depth height) 
169     (string-append 
170      "\\kern" (number->dim (- breapth))
171      "\\vrule width " (number->dim (+ breapth width))
172      "depth " (number->dim depth)
173      "height " (number->dim height) " "))
174
175   (define (filledbox breapth width depth height) 
176     (embedded-ps
177      (string-append (numbers->string (list breapth width depth height))
178                    " draw_box" )))
179
180   (define (text s)
181     (string-append "\\hbox{" (output-tex-string s) "}"))
182   
183   (define (tuplet ht gapx dx dy thick dir)
184     (embedded-ps ((ps-scm 'tuplet) ht gapx dx dy thick dir)))
185
186   (define (volta h w thick vert_start vert_end)
187     (embedded-ps ((ps-scm 'volta) h w thick vert_start vert_end)))
188
189   (define (define-origin file line col)
190     (if (procedure? point-and-click)
191         (string-append "\\special{src\\string:"
192                        (point-and-click line col file)
193                        "}" )
194         "")
195   )
196
197   ; no-origin not yet supported by Xdvi
198   (define (no-origin) "")
199   
200   ;; TeX
201   ;; The procedures listed below form the public interface of TeX-scm.
202   ;; (should merge the 2 lists)
203   (cond ((eq? action-name 'all-definitions)
204          `(begin
205             (define font-load-command ,font-load-command)
206             (define beam ,beam)
207             (define bezier-sandwich ,bezier-sandwich)
208             (define bracket ,bracket)
209             (define char ,char)
210             (define dashed-line ,dashed-line) 
211             (define dashed-slur ,dashed-slur) 
212             (define hairpin ,hairpin) 
213             (define end-output ,end-output)
214             (define experimental-on ,experimental-on)
215             (define filledbox ,filledbox)
216             (define font-def ,font-def)
217             (define font-switch ,font-switch)
218             (define header-end ,header-end)
219             (define lily-def ,lily-def)
220             (define ez-ball ,ez-ball)
221             (define header ,header) 
222             (define invoke-char ,invoke-char) 
223             (define invoke-dim1 ,invoke-dim1)
224             (define placebox ,placebox)
225             (define select-font ,select-font)
226             (define start-line ,start-line)
227             (define stop-line ,stop-line)
228             (define stop-last-line ,stop-last-line)
229             (define text ,text)
230             (define tuplet ,tuplet)
231             (define volta ,volta)
232             (define define-origin ,define-origin)
233             (define no-origin ,no-origin)
234             (define repeat-slash ,repeat-slash)
235             ))
236
237         ((eq? action-name 'beam) beam)
238         ((eq? action-name 'tuplet) tuplet)
239         ((eq? action-name 'bracket) bracket)
240         ((eq? action-name 'hairpin) hairpin)
241         ((eq? action-name 'dashed-line) dashed-line) 
242         ((eq? action-name 'dashed-slur) dashed-slur) 
243         ((eq? action-name 'end-output) end-output)
244         ((eq? action-name 'experimental-on) experimental-on)
245         ((eq? action-name 'font-def) font-def)
246         ((eq? action-name 'font-switch) font-switch)
247         ((eq? action-name 'header-end) header-end)
248         ((eq? action-name 'lily-def) lily-def)
249         ((eq? action-name 'header) header) 
250         ((eq? action-name 'invoke-char) invoke-char) 
251         ((eq? action-name 'invoke-dim1) invoke-dim1)
252         ((eq? action-name 'placebox) placebox)
253         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
254         ((eq? action-name 'start-line) start-line)
255         ((eq? action-name 'stem) stem)
256         ((eq? action-name 'stop-line) stop-line)
257         ((eq? action-name 'stop-last-line) stop-last-line)
258         ((eq? action-name 'volta) volta)
259         (else (error "unknown tag -- PS-TEX " action-name))
260         )
261   )
262
263 (define (scm-tex-output)
264   (ly-eval (tex-scm 'all-definitions)))