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