]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-tex.scm
* scm/lily.scm (get-output-module): New function.
[lilypond.git] / scm / output-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--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;;                  Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8
9 (define-module (scm output-tex) )
10 ; (debug-enable 'backtrace)
11 (use-modules (scm output-ps)
12              (ice-9 regex)
13              (ice-9 string-fun)
14              (ice-9 format)
15              (guile)
16              (srfi srfi-13)
17              (lily)
18              )
19
20 (define this-module (current-module))
21
22 ;; dumper-compatibility
23
24 (define (ps-output-expression expr port)
25   (let ((output-ps (resolve-module '(scm output-ps))))
26     (display (eval expr output-ps) port)))
27
28 ;;; Output interface entry
29 (define-public (tex-output-expression expr port)
30   (display (eval expr this-module) port ))
31
32 ;;;;;;;;
33 ;;;;;;;; DOCUMENT ME!
34 ;;;;;;;;
35
36 (define font-name-alist  '())
37
38 (define (tex-encoded-fontswitch name-mag)
39   (let* ((iname-mag (car name-mag))
40          (ename-mag (cdr name-mag)))
41
42     (cons iname-mag
43           (cons ename-mag
44                 (string-append  "magfont"
45                           (string-encode-integer
46                            (hashq (car ename-mag) 1000000))
47                           "m"
48                           (string-encode-integer
49                            (inexact->exact (round (* 1000 (cdr ename-mag))))))))))
50
51 (define (define-fonts internal-external-name-mag-pairs)
52   (set! font-name-alist (map tex-encoded-fontswitch
53                              internal-external-name-mag-pairs))
54   (apply string-append
55          (map (lambda (x)
56                 (font-load-command (car x) (cdr x)))
57               (map cdr font-name-alist))))
58
59 ;;
60 ;; urg, how can exp be #unspecified?  -- in sketch output
61 ;;
62 ;; set! returns #<unspecified>  --hwn
63 ;;
64 (define (fontify name-mag-pair exp)
65   (string-append (select-font name-mag-pair)
66                  exp))
67
68
69 (define (unknown) 
70   "%\n\\unknown\n")
71
72 (define (symbol->tex-key sym)
73   (regexp-substitute/global
74    #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
75
76 (define (tex-string-def prefix key str)
77   (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
78       (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
79       (string-append "\\def\\" prefix (symbol->tex-key key) "{"  (output-tex-string str) "}%\n")
80       ))
81
82 (define (tex-number-def prefix key number)
83   (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
84
85 (define (output-paper-def pd)
86   (apply
87    string-append
88    (module-map
89     (lambda (sym var)
90       (let ((val (variable-ref var))
91             (key (symbol->tex-key sym)))
92
93         (cond
94          ((string? val)
95           (tex-string-def "lilypondpaper" sym val))
96          ((number? val)
97           (tex-number-def "lilypondpaper" sym
98                           (if (integer? val)
99                               (number->string val)
100                               (number->string (exact->inexact val)))))
101          (else ""))))
102       
103     (ly:output-def-scope pd))))
104
105 (define (output-scopes paper scopes fields basename)
106   (define (output-scope scope)
107     (apply
108      string-append
109      (module-map
110      (lambda (sym var)
111        (let ((val (variable-ref var))
112              ;;(val (if (variable-bound? var) (variable-ref var) '""))
113              (tex-key (symbol->string sym)))
114          
115          (if (and (memq sym fields) (string? val))
116              (header-to-file basename sym val))
117
118          (cond
119           ((string? val)
120            (tex-string-def "lilypond" sym val))
121           ((number? val)
122            (tex-number-def "lilypond" sym
123                            (if (integer? val)
124                                (number->string val)
125                                (number->string (exact->inexact val)))))
126           (else ""))))
127      scope)))
128   
129   (apply string-append
130          (map output-scope scopes)))
131
132 (define (select-font name-mag-pair)
133   (let ((c (assoc name-mag-pair font-name-alist)))
134     (if c
135         (string-append "\\" (cddr c))
136         (begin
137           (ly:warn
138            (format "Programming error: No such font: ~S" name-mag-pair))
139           ""))))
140
141 (define (blank)
142   "")
143
144 (define (dot x y radius)
145   (embedded-ps (list 'dot x y radius)))
146
147 (define (beam width slope thick blot)
148   (embedded-ps (list 'beam  width slope thick blot)))
149
150 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
151   (embedded-ps (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
152
153 (define (dashed-slur thick dash l)
154   (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
155
156 (define (char i)
157   (string-append "\\char" (inexact->string i 10) " "))
158
159 (define (dashed-line thick on off dx dy)
160   (embedded-ps (list 'dashed-line  thick on off dx dy)))
161
162 (define (zigzag-line centre? zzw zzh thick dx dy)
163   (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
164
165 (define (symmetric-x-triangle t w h)
166   (embedded-ps (list 'symmetric-x-triangle t w h)))
167
168 (define (font-load-command name-mag command)
169   (string-append
170    "\\font\\" command "="
171    (car name-mag)
172    " scaled "
173    (ly:number->string (inexact->exact (round (* 1000  (cdr name-mag)))))
174    "\n"))
175
176 (define (ez-ball c l b)
177   (embedded-ps (list 'ez-ball  c  l b)))
178
179 (define (header-to-file fn key val)
180   (set! key (symbol->string key))
181   (if (not (equal? "-" fn))
182       (set! fn (string-append fn "." key))
183       )
184   (display
185    (format "writing header field `~a' to `~a'..."
186            key
187            (if (equal? "-" fn) "<stdout>" fn)
188            )
189    (current-error-port))
190   (if (equal? fn "-")
191       (display val)
192       (display val (open-file fn "w"))
193   )
194   (display "\n" (current-error-port))
195   ""
196   )
197
198 (define (embedded-ps expr)
199   (let ((ps-string
200          (with-output-to-string
201            (lambda () (ps-output-expression expr (current-output-port))))))
202     (string-append "\\embeddedps{" ps-string "}")))
203   
204 (define (comment s)
205   (string-append "% " s "\n"))
206
207 (define (end-output) 
208   (begin
209     ;; uncomment for some stats about lily memory         
210     ;; (display (gc-stats))
211     (string-append
212      "\\lilypondend\n"
213      ;; Put GC stats here.
214      )))
215
216 (define (experimental-on)
217   "")
218
219 (define (repeat-slash w a t)
220   (embedded-ps (list 'repeat-slash  w a t)))
221
222 (define (header-end)
223   (string-append
224    "\\def\\scaletounit{ "
225    (number->string (cond
226                      ((equal? (ly:unit) "mm") (/ 72.0  25.4))
227                      ((equal? (ly:unit) "pt") (/ 72.0  72.27))
228                      (else (error "unknown unit" (ly:unit)))
229                      ))
230    " mul }%\n"
231    "\\ifx\\lilypondstart\\undefined\n"
232    "  \\input lilyponddefs\n"
233    "\\fi\n"
234    "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
235    "\\lilypondstart\n"
236    "\\lilypondspecial\n"
237    "\\lilypondpostscript\n"))
238
239 (define (header creator time-stamp page-count)
240   (string-append
241    "% Generated by " creator "\n"
242    "% at " time-stamp "\n"
243    ;; FIXME: duplicated in every backend
244    "\\def\\lilypondtagline{Engraved by LilyPond (version "
245    (lilypond-version)")}\n"))
246
247 (define (invoke-char s i)
248   (string-append 
249    "\n\\" s "{" (inexact->string i 10) "}" ))
250
251 ;; FIXME: explain ploblem: need to do something to make this really safe.  
252 (define-public (output-tex-string s)
253   (if safe-mode?
254       (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
255       s))
256
257 (define (lily-def key val)
258   (let ((tex-key
259          (regexp-substitute/global
260               #f "_" (output-tex-string key) 'pre "X" 'post))
261          
262         (tex-val (output-tex-string val)))
263     (if (equal? (sans-surrounding-whitespace tex-val) "")
264         (string-append "\\let\\" tex-key "\\undefined\n")
265         (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
266
267 (define (number->dim x)
268   (string-append
269    ;;ugh ly:* in backend needs compatibility func for standalone output
270    (ly:number->string x) " \\outputscale "))
271
272 (define (placebox x y s) 
273   (string-append "\\lyitem{"
274                  (ly:number->string y) "}{"
275                  (ly:number->string x) "}{"
276                  s "}%\n"))
277
278 (define (bezier-sandwich l thick)
279   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
280
281 (define (start-system wd ht)
282   (string-append "\\leavevmode\n"
283                  "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
284                  "\\lilypondifundefined{lilypondscoreshift}%\n"
285                  "  {}%\n"
286                  "  {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
287                  "\\lybox{"
288                  (ly:number->string wd) "}{"
289                  (ly:number->string ht) "}{%\n"))
290
291 (define (stop-system) 
292   "}%\n%\n\\interscoreline\n%\n")
293 (define (stop-last-system)
294   "}%\n")
295
296 (define (horizontal-line x1 x2 th)
297   (filledbox (- x1)  (- x2 x1) (* .5 th)  (* .5 th )))
298
299 (define (filledbox breapth width depth height)
300   (if (and #f (defined? 'ps-testing))
301       (embedded-ps
302        (string-append (numbers->string (list breapth width depth height))
303                       " draw_box" ))
304       (string-append "\\lyvrule{"
305                      (ly:number->string (- breapth)) "}{"
306                      (ly:number->string (+ breapth width)) "}{"
307                      (ly:number->string depth) "}{"
308                      (ly:number->string height) "}")))
309
310 (define (round-filled-box x y width height blotdiam)
311   (embedded-ps (list 'round-filled-box  x y width height blotdiam)))
312
313 (define (text s)
314   (string-append "\\hbox{" (output-tex-string s) "}"))
315
316 (define (tuplet ht gapx dx dy thick dir)
317   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
318
319 (define (polygon points blotdiameter)
320   (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
321
322 (define (draw-line thick fx fy tx ty)
323   (embedded-ps (list 'draw-line thick fx fy tx ty)))
324
325 ;; TODO: this should be a default, which is overriden in PS
326 (define (between-system-string string)
327   string
328   )
329 (define (define-origin file line col)
330   (if (procedure? point-and-click)
331       (string-append "\\special{src:" ;;; \\string ? 
332                      (point-and-click line col file)
333                      "}" )
334       "")
335   )
336
337 ;; no-origin not yet supported by Xdvi
338 (define (no-origin) "")
339
340 (define (start-page)
341   "\n%\\vbox{\n")
342
343 (define (stop-page last?)
344   (if last?
345       "\n%}\n"
346       "\n%}\n\\newpage\n"))