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 paper scopes fields basename)
96 (define (output-scope scope)
101 (let ((val (variable-ref var))
102 ;;(val (if (variable-bound? var) (variable-ref var) '""))
103 (tex-key (symbol->string sym)))
105 (if (memq sym fields)
106 (header-to-file basename sym val))
110 (tex-string-def "lilypond" sym val))
112 (tex-number-def "lilypond" sym
115 (number->string (exact->inexact val)))))
120 (map output-scope scopes)))
122 (define (select-font name-mag-pair)
123 (let ((c (assoc name-mag-pair font-name-alist)))
126 (string-append "\\" (cddr c))
128 (ly:warn (string-append
129 "Programming error: No such font known "
130 (car name-mag-pair) " "
131 (ly:number->string (cdr name-mag-pair))))
133 (display "FAILED\n" (current-error-port))
134 (if #f ;(pair? name-mag-pair))
135 (display (object-type (car name-mag-pair)) (current-error-port))
136 (write name-mag-pair (current-error-port)))
137 (if #f ; (pair? font-name-alist)
139 (object-type (caaar font-name-alist)) (current-error-port))
140 (write font-name-alist (current-error-port)))
142 ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
145 ;; top-of-file, wtf? ugh: tagline?
146 (define (top-of-file)
148 "% generated by LilyPond "
149 (lilypond-version) " (http://lilypond.org).\n"
150 "\\def\\lilypondtagline{Engraved by LilyPond (version "
158 (define (dot x y radius)
159 (embedded-ps (list 'dot x y radius)))
161 (define (beam width slope thick blot)
162 (embedded-ps (list 'beam width slope thick blot)))
164 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
165 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
167 (define (dashed-slur thick dash l)
168 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
171 (string-append "\\char" (inexact->string i 10) " "))
173 (define (dashed-line thick on off dx dy)
174 (embedded-ps (list 'dashed-line thick on off dx dy)))
176 (define (zigzag-line centre? zzw zzh thick dx dy)
177 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
179 (define (symmetric-x-triangle t w h)
180 (embedded-ps (list 'symmetric-x-triangle t w h)))
182 (define (font-load-command name-mag command)
184 "\\font\\" command "="
187 (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag)))))
190 (define (ez-ball c l b)
191 (embedded-ps (list 'ez-ball c l b)))
193 (define (header-to-file fn key val)
194 (set! key (symbol->string key))
195 (if (not (equal? "-" fn))
196 (set! fn (string-append fn "." key))
199 (format "writing header field `~a' to `~a'..."
201 (if (equal? "-" fn) "<stdout>" fn)
203 (current-error-port))
206 (display val (open-file fn "w"))
208 (display "\n" (current-error-port))
212 (define (embedded-ps expr)
214 (with-output-to-string
215 (lambda () (ps-output-expression expr (current-output-port))))))
216 (string-append "\\embeddedps{" ps-string "}")))
219 (string-append "% " s "\n"))
223 ; uncomment for some stats about lily memory
224 ; (display (gc-stats))
230 (define (experimental-on)
233 (define (repeat-slash w a t)
234 (embedded-ps (list 'repeat-slash w a t)))
238 "\\def\\scaletounit{ "
239 (number->string (cond
240 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
241 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
242 (else (error "unknown unit" (ly:unit)))
245 "\\ifx\\lilypondstart\\undefined\n"
246 " \\input lilyponddefs\n"
248 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
250 "\\lilypondspecial\n"
251 "\\lilypondpostscript\n"))
253 ;; Note: this string must match the string in lilypond.py!!!
254 (define (header creator generate)
256 "% Generated automatically by: " creator generate "\n"))
258 (define (invoke-char s i)
260 "\n\\" s "{" (inexact->string i 10) "}" ))
263 ;; need to do something to make this really safe.
265 (define-public (output-tex-string s)
266 (if security-paranoia
267 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
270 (define (lily-def key val)
272 (regexp-substitute/global
273 #f "_" (output-tex-string key) 'pre "X" 'post))
275 (tex-val (output-tex-string val)))
276 (if (equal? (sans-surrounding-whitespace tex-val) "")
277 (string-append "\\let\\" tex-key "\\undefined\n")
278 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
280 (define (number->dim x)
282 ;;ugh ly:* in backend needs compatibility func for standalone output
283 (ly:number->string x) " \\outputscale "))
285 (define (placebox x y s)
286 (string-append "\\lyitem{"
287 (ly:number->string y) "}{"
288 (ly:number->string x) "}{"
291 (define (bezier-sandwich l thick)
292 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
294 (define (start-system wd ht)
295 (string-append "\\leavevmode\n"
296 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
297 "\\lilypondifundefined{lilypondscoreshift}%\n"
299 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
301 (ly:number->string wd) "}{"
302 (ly:number->string ht) "}{%\n"))
304 (define (stop-system)
305 "}%\n%\n\\interscoreline\n%\n")
306 (define (stop-last-system)
309 (define (horizontal-line x1 x2 th)
310 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
312 (define (filledbox breapth width depth height)
313 (if (and #f (defined? 'ps-testing))
315 (string-append (numbers->string (list breapth width depth height))
317 (string-append "\\lyvrule{"
318 (ly:number->string (- breapth)) "}{"
319 (ly:number->string (+ breapth width)) "}{"
320 (ly:number->string depth) "}{"
321 (ly:number->string height) "}")))
323 (define (round-filled-box x y width height blotdiam)
324 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
327 (string-append "\\hbox{" (output-tex-string s) "}"))
329 (define (tuplet ht gapx dx dy thick dir)
330 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
332 (define (polygon points blotdiameter)
333 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
335 (define (draw-line thick fx fy tx ty)
336 (embedded-ps (list 'draw-line thick fx fy tx ty)))
338 ;; TODO: this should be a default, which is overriden in PS
339 (define (between-system-string string)
342 (define (define-origin file line col)
343 (if (procedure? point-and-click)
344 (string-append "\\special{src:" ;;; \\string ?
345 (point-and-click line col file)
350 ;; no-origin not yet supported by Xdvi
351 (define (no-origin) "")
353 (define-public (tex-output-expression expr port)
354 (display (eval expr this-module) port ))
356 (define-public (start-page)
359 (define-public (stop-page last?)