1 ;;; tex.scm -- implement Scheme output routines for TeX
3 ;;; source file of the GNU LilyPond music typesetter
5 ;;; (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 (define-module (scm output-tex) )
10 ; (debug-enable 'backtrace)
11 (use-modules (scm output-ps)
20 (define this-module (current-module))
26 (define font-name-alist '())
28 (define (tex-encoded-fontswitch name-mag)
29 (let* ((iname-mag (car name-mag))
30 (ename-mag (cdr name-mag)))
34 (string-append "magfont"
35 (string-encode-integer
36 (hashq (car ename-mag) 1000000))
38 (string-encode-integer
39 (inexact->exact (round (* 1000 (cdr ename-mag))))))))))
41 (define (define-fonts internal-external-name-mag-pairs)
42 (set! font-name-alist (map tex-encoded-fontswitch
43 internal-external-name-mag-pairs))
46 (font-load-command (car x) (cdr x)))
47 (map cdr font-name-alist))))
51 ;; urg, how can exp be #unspecified? -- in sketch output
53 ;; set! returns #<unspecified> --hwn
54 (define (fontify name-mag-pair exp)
55 (string-append (select-font name-mag-pair)
62 (define (symbol->tex-key sym)
63 (regexp-substitute/global
64 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
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")
72 (define (output-paper-def pd)
78 ((val (variable-ref var))
79 (key (symbol->tex-key sym))
84 (tex-string-def "lilypondpaper" sym val))
86 (string-append "\\def\\lilypondpaper" key "{"
89 (number->string (exact->inexact val)))
94 (ly:output-def-scope pd))
97 (define (output-scopes scopes fields basename)
98 (define (output-scope scope)
104 ((val (variable-ref var))
105 (tex-key (symbol->string sym))
107 (if (memq sym fields)
108 (header-to-file basename sym val))
112 (tex-string-def "lilypond" sym val) )
114 (string-append "\\def\\" tex-key "{"
117 (number->string (exact->inexact val)))
126 (map output-scope scopes)) )
129 (define (select-font name-mag-pair)
132 (c (assoc name-mag-pair font-name-alist))
137 (ly:warn (string-append
138 "Programming error: No such font known "
139 (car name-mag-pair) " "
140 (ly:number->string (cdr name-mag-pair))))
142 (display "FAILED\n" (current-error-port))
143 (if #f ;(pair? name-mag-pair))
144 (display (object-type (car name-mag-pair)) (current-error-port))
145 (write name-mag-pair (current-error-port)))
146 (if #f ; (pair? font-name-alist)
148 (object-type (caaar font-name-alist)) (current-error-port))
149 (write font-name-alist (current-error-port)))
151 ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
154 (string-append "\\" (cddr c)))))
156 (define (top-of-file)
159 "generated by LilyPond "
160 (lilypond-version) " (http://lilypond.org)." )
166 (define (dot x y radius)
167 (embedded-ps (list 'dot x y radius)))
169 (define (beam width slope thick blot)
170 (embedded-ps (list 'beam width slope thick blot)))
172 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
173 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
175 (define (dashed-slur thick dash l)
176 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
179 (string-append "\\char" (inexact->string i 10) " "))
181 (define (dashed-line thick on off dx dy)
182 (embedded-ps (list 'dashed-line thick on off dx dy)))
184 (define (zigzag-line centre? zzw zzh thick dx dy)
185 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
187 (define (symmetric-x-triangle t w h)
188 (embedded-ps (list 'symmetric-x-triangle t w h)))
190 (define (font-load-command name-mag command)
192 "\\font\\" command "="
195 (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag)))))
198 (define (ez-ball c l b)
199 (embedded-ps (list 'ez-ball c l b)))
201 (define (header-to-file fn key val)
202 (set! key (symbol->string key))
203 (if (not (equal? "-" fn))
204 (set! fn (string-append fn "." key))
207 (format "writing header field `~a' to `~a'..."
209 (if (equal? "-" fn) "<stdout>" fn)
211 (current-error-port))
214 (display val (open-file fn "w"))
216 (display "\n" (current-error-port))
220 (define (embedded-ps expr)
222 (with-output-to-string
223 (lambda () (ps-output-expression expr (current-output-port))))))
224 (string-append "\\embeddedps{" ps-string "}")))
227 (string-append "% " s "\n"))
231 ; uncomment for some stats about lily memory
232 ; (display (gc-stats))
238 (define (experimental-on)
241 (define (repeat-slash w a t)
242 (embedded-ps (list 'repeat-slash w a t)))
246 "\\def\\scaletounit{ "
247 (number->string (cond
248 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
249 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
250 (else (error "unknown unit" (ly:unit)))
253 "\\ifx\\lilypondstart\\undefined\n"
254 " \\input lilyponddefs\n"
256 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
258 "\\lilypondspecial\n"
259 "\\lilypondpostscript\n"))
261 ;; Note: this string must match the string in lilypond.py!!!
262 (define (header creator generate)
264 "% Generated automatically by: " creator generate "\n"))
266 (define (invoke-char s i)
268 "\n\\" s "{" (inexact->string i 10) "}" ))
271 ;; need to do something to make this really safe.
273 (define-public (output-tex-string s)
274 (if security-paranoia
275 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
278 (define (lily-def key val)
280 (regexp-substitute/global
281 #f "_" (output-tex-string key) 'pre "X" 'post))
283 (tex-val (output-tex-string val)))
284 (if (equal? (sans-surrounding-whitespace tex-val) "")
285 (string-append "\\let\\" tex-key "\\undefined\n")
286 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
288 (define (number->dim x)
290 ;;ugh ly:* in backend needs compatibility func for standalone output
291 (ly:number->string x) " \\outputscale "))
293 (define (placebox x y s)
294 (string-append "\\lyitem{"
295 (ly:number->string y) "}{"
296 (ly:number->string x) "}{"
299 (define (bezier-sandwich l thick)
300 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
302 (define (start-system wd ht)
303 (string-append "\\leavevmode\n"
304 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
305 "\\lilypondifundefined{lilypondscoreshift}%\n"
307 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
309 (ly:number->string wd) "}{"
310 (ly:number->string ht) "}{%\n"))
312 (define (stop-system)
313 "}%\n%\n\\interscoreline\n%\n")
314 (define (stop-last-system)
317 (define (horizontal-line x1 x2 th)
318 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
320 (define (filledbox breapth width depth height)
321 (if (and #f (defined? 'ps-testing))
323 (string-append (numbers->string (list breapth width depth height))
325 (string-append "\\lyvrule{"
326 (ly:number->string (- breapth)) "}{"
327 (ly:number->string (+ breapth width)) "}{"
328 (ly:number->string depth) "}{"
329 (ly:number->string height) "}")))
331 (define (round-filled-box x y width height blotdiam)
332 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
335 (string-append "\\hbox{" (output-tex-string s) "}"))
337 (define (tuplet ht gapx dx dy thick dir)
338 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
340 (define (polygon points blotdiameter)
341 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
343 (define (draw-line thick fx fy tx ty)
344 (embedded-ps (list 'draw-line thick fx fy tx ty)))
346 ;; TODO: this should be a default, which is overriden in PS
347 (define (between-system-string string)
350 (define (define-origin file line col)
351 (if (procedure? point-and-click)
352 (string-append "\\special{src:" ;;; \\string ?
353 (point-and-click line col file)
358 ;; no-origin not yet supported by Xdvi
359 (define (no-origin) "")
361 (define-public (tex-output-expression expr port)
362 (display (eval expr this-module) port ))