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 (tex-number-def prefix key number)
73 (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
75 (define (output-paper-def pd)
80 (let ((val (variable-ref var))
81 (key (symbol->tex-key sym)))
85 (tex-string-def "lilypondpaper" sym val))
87 (tex-number-def "lilypondpaper" sym
90 (number->string (exact->inexact val)))))
93 (ly:output-def-scope pd))))
95 (define (output-scopes scopes fields basename)
96 (define (output-scope scope)
101 (let ((val (variable-ref var))
102 (tex-key (symbol->string sym)))
104 (if (memq sym fields)
105 (header-to-file basename sym val))
109 (tex-string-def "lilypond" sym val))
111 (tex-number-def "lilypond" sym
114 (number->string (exact->inexact val)))))
119 (map output-scope scopes)))
121 (define (select-font name-mag-pair)
124 (c (assoc name-mag-pair font-name-alist))
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))))
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)
140 (object-type (caaar font-name-alist)) (current-error-port))
141 (write font-name-alist (current-error-port)))
143 ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
146 (string-append "\\" (cddr c)))))
148 ;; top-of-file, wtf? ugh: tagline?
149 (define (top-of-file)
151 "% generated by LilyPond "
152 (lilypond-version) " (http://lilypond.org).\n"
153 "\\def\\lilypondtagline{Engraved by LilyPond (version "
161 (define (dot x y radius)
162 (embedded-ps (list 'dot x y radius)))
164 (define (beam width slope thick blot)
165 (embedded-ps (list 'beam width slope thick blot)))
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)))
170 (define (dashed-slur thick dash l)
171 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
174 (string-append "\\char" (inexact->string i 10) " "))
176 (define (dashed-line thick on off dx dy)
177 (embedded-ps (list 'dashed-line thick on off dx dy)))
179 (define (zigzag-line centre? zzw zzh thick dx dy)
180 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
182 (define (symmetric-x-triangle t w h)
183 (embedded-ps (list 'symmetric-x-triangle t w h)))
185 (define (font-load-command name-mag command)
187 "\\font\\" command "="
190 (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag)))))
193 (define (ez-ball c l b)
194 (embedded-ps (list 'ez-ball c l b)))
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))
202 (format "writing header field `~a' to `~a'..."
204 (if (equal? "-" fn) "<stdout>" fn)
206 (current-error-port))
209 (display val (open-file fn "w"))
211 (display "\n" (current-error-port))
215 (define (embedded-ps expr)
217 (with-output-to-string
218 (lambda () (ps-output-expression expr (current-output-port))))))
219 (string-append "\\embeddedps{" ps-string "}")))
222 (string-append "% " s "\n"))
226 ; uncomment for some stats about lily memory
227 ; (display (gc-stats))
233 (define (experimental-on)
236 (define (repeat-slash w a t)
237 (embedded-ps (list 'repeat-slash w a t)))
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)))
248 "\\ifx\\lilypondstart\\undefined\n"
249 " \\input lilyponddefs\n"
251 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
253 "\\lilypondspecial\n"
254 "\\lilypondpostscript\n"))
256 ;; Note: this string must match the string in lilypond.py!!!
257 (define (header creator generate)
259 "% Generated automatically by: " creator generate "\n"))
261 (define (invoke-char s i)
263 "\n\\" s "{" (inexact->string i 10) "}" ))
266 ;; need to do something to make this really safe.
268 (define-public (output-tex-string s)
269 (if security-paranoia
270 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
273 (define (lily-def key val)
275 (regexp-substitute/global
276 #f "_" (output-tex-string key) 'pre "X" 'post))
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"))))
283 (define (number->dim x)
285 ;;ugh ly:* in backend needs compatibility func for standalone output
286 (ly:number->string x) " \\outputscale "))
288 (define (placebox x y s)
289 (string-append "\\lyitem{"
290 (ly:number->string y) "}{"
291 (ly:number->string x) "}{"
294 (define (bezier-sandwich l thick)
295 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
297 (define (start-system wd ht)
298 (string-append "\\leavevmode\n"
299 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
300 "\\lilypondifundefined{lilypondscoreshift}%\n"
302 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
304 (ly:number->string wd) "}{"
305 (ly:number->string ht) "}{%\n"))
307 (define (stop-system)
308 "}%\n%\n\\interscoreline\n%\n")
309 (define (stop-last-system)
312 (define (horizontal-line x1 x2 th)
313 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
315 (define (filledbox breapth width depth height)
316 (if (and #f (defined? 'ps-testing))
318 (string-append (numbers->string (list breapth width depth height))
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) "}")))
326 (define (round-filled-box x y width height blotdiam)
327 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
330 (string-append "\\hbox{" (output-tex-string s) "}"))
332 (define (tuplet ht gapx dx dy thick dir)
333 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
335 (define (polygon points blotdiameter)
336 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
338 (define (draw-line thick fx fy tx ty)
339 (embedded-ps (list 'draw-line thick fx fy tx ty)))
341 ;; TODO: this should be a default, which is overriden in PS
342 (define (between-system-string string)
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)
353 ;; no-origin not yet supported by Xdvi
354 (define (no-origin) "")
356 (define-public (tex-output-expression expr port)
357 (display (eval expr this-module) port ))