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))
22 ;;; Output interface entry
23 (define-public (tex-output-expression expr port)
24 (display (eval expr this-module) port ))
30 (define font-name-alist '())
32 (define (tex-encoded-fontswitch name-mag)
33 (let* ((iname-mag (car name-mag))
34 (ename-mag (cdr name-mag)))
38 (string-append "magfont"
39 (string-encode-integer
40 (hashq (car ename-mag) 1000000))
42 (string-encode-integer
43 (inexact->exact (round (* 1000 (cdr ename-mag))))))))))
45 (define (define-fonts internal-external-name-mag-pairs)
46 (set! font-name-alist (map tex-encoded-fontswitch
47 internal-external-name-mag-pairs))
50 (font-load-command (car x) (cdr x)))
51 (map cdr font-name-alist))))
55 ;; urg, how can exp be #unspecified? -- in sketch output
57 ;; set! returns #<unspecified> --hwn
58 (define (fontify name-mag-pair exp)
59 (string-append (select-font name-mag-pair)
66 (define (symbol->tex-key sym)
67 (regexp-substitute/global
68 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
70 (define (tex-string-def prefix key str)
71 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
72 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
73 (string-append "\\def\\" prefix (symbol->tex-key key) "{" (output-tex-string str) "}%\n")
76 (define (tex-number-def prefix key number)
77 (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
79 (define (output-paper-def pd)
84 (let ((val (variable-ref var))
85 (key (symbol->tex-key sym)))
89 (tex-string-def "lilypondpaper" sym val))
91 (tex-number-def "lilypondpaper" sym
94 (number->string (exact->inexact val)))))
97 (ly:output-def-scope pd))))
99 (define (output-scopes paper scopes fields basename)
100 (define (output-scope scope)
105 (let ((val (variable-ref var))
106 ;;(val (if (variable-bound? var) (variable-ref var) '""))
107 (tex-key (symbol->string sym)))
109 (if (memq sym fields)
110 (header-to-file basename sym val))
114 (tex-string-def "lilypond" sym val))
116 (tex-number-def "lilypond" sym
119 (number->string (exact->inexact val)))))
124 (map output-scope scopes)))
126 (define (select-font name-mag-pair)
127 (let ((c (assoc name-mag-pair font-name-alist)))
129 (string-append "\\" (cddr c))
132 (format "Programming error: No such font: ~S" name-mag-pair))
135 ;; top-of-file, wtf? ugh: tagline?
136 (define (top-of-file)
138 "% generated by LilyPond "
139 (lilypond-version) " (http://lilypond.org).\n"
140 "\\def\\lilypondtagline{Engraved by LilyPond (version "
148 (define (dot x y radius)
149 (embedded-ps (list 'dot x y radius)))
151 (define (beam width slope thick blot)
152 (embedded-ps (list 'beam width slope thick blot)))
154 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
155 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
157 (define (dashed-slur thick dash l)
158 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
161 (string-append "\\char" (inexact->string i 10) " "))
163 (define (dashed-line thick on off dx dy)
164 (embedded-ps (list 'dashed-line thick on off dx dy)))
166 (define (zigzag-line centre? zzw zzh thick dx dy)
167 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
169 (define (symmetric-x-triangle t w h)
170 (embedded-ps (list 'symmetric-x-triangle t w h)))
172 (define (font-load-command name-mag command)
174 "\\font\\" command "="
177 (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag)))))
180 (define (ez-ball c l b)
181 (embedded-ps (list 'ez-ball c l b)))
183 (define (header-to-file fn key val)
184 (set! key (symbol->string key))
185 (if (not (equal? "-" fn))
186 (set! fn (string-append fn "." key))
189 (format "writing header field `~a' to `~a'..."
191 (if (equal? "-" fn) "<stdout>" fn)
193 (current-error-port))
196 (display val (open-file fn "w"))
198 (display "\n" (current-error-port))
202 (define (embedded-ps expr)
204 (with-output-to-string
205 (lambda () (ps-output-expression expr (current-output-port))))))
206 (string-append "\\embeddedps{" ps-string "}")))
209 (string-append "% " s "\n"))
213 ; uncomment for some stats about lily memory
214 ; (display (gc-stats))
220 (define (experimental-on)
223 (define (repeat-slash w a t)
224 (embedded-ps (list 'repeat-slash w a t)))
228 "\\def\\scaletounit{ "
229 (number->string (cond
230 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
231 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
232 (else (error "unknown unit" (ly:unit)))
235 "\\ifx\\lilypondstart\\undefined\n"
236 " \\input lilyponddefs\n"
238 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
240 "\\lilypondspecial\n"
241 "\\lilypondpostscript\n"))
243 ;; Note: this string must match the string in lilypond.py!!!
244 (define (header creator generate)
246 "% Generated automatically by: " creator generate "\n"))
248 (define (invoke-char s i)
250 "\n\\" s "{" (inexact->string i 10) "}" ))
253 ;; need to do something to make this really safe.
255 (define-public (output-tex-string s)
256 (if security-paranoia
257 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
260 (define (lily-def key val)
262 (regexp-substitute/global
263 #f "_" (output-tex-string key) 'pre "X" 'post))
265 (tex-val (output-tex-string val)))
266 (if (equal? (sans-surrounding-whitespace tex-val) "")
267 (string-append "\\let\\" tex-key "\\undefined\n")
268 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
270 (define (number->dim x)
272 ;;ugh ly:* in backend needs compatibility func for standalone output
273 (ly:number->string x) " \\outputscale "))
275 (define (placebox x y s)
276 (string-append "\\lyitem{"
277 (ly:number->string y) "}{"
278 (ly:number->string x) "}{"
281 (define (bezier-sandwich l thick)
282 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
284 (define (start-system wd ht)
285 (string-append "\\leavevmode\n"
286 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
287 "\\lilypondifundefined{lilypondscoreshift}%\n"
289 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
291 (ly:number->string wd) "}{"
292 (ly:number->string ht) "}{%\n"))
294 (define (stop-system)
295 "}%\n%\n\\interscoreline\n%\n")
296 (define (stop-last-system)
299 (define (horizontal-line x1 x2 th)
300 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
302 (define (filledbox breapth width depth height)
303 (if (and #f (defined? 'ps-testing))
305 (string-append (numbers->string (list breapth width depth height))
307 (string-append "\\lyvrule{"
308 (ly:number->string (- breapth)) "}{"
309 (ly:number->string (+ breapth width)) "}{"
310 (ly:number->string depth) "}{"
311 (ly:number->string height) "}")))
313 (define (round-filled-box x y width height blotdiam)
314 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
317 (string-append "\\hbox{" (output-tex-string s) "}"))
319 (define (tuplet ht gapx dx dy thick dir)
320 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
322 (define (polygon points blotdiameter)
323 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
325 (define (draw-line thick fx fy tx ty)
326 (embedded-ps (list 'draw-line thick fx fy tx ty)))
328 ;; TODO: this should be a default, which is overriden in PS
329 (define (between-system-string string)
332 (define (define-origin file line col)
333 (if (procedure? point-and-click)
334 (string-append "\\special{src:" ;;; \\string ?
335 (point-and-click line col file)
340 ;; no-origin not yet supported by Xdvi
341 (define (no-origin) "")
346 (define (stop-page last?)
349 "\n%}\n\\newpage\n"))