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