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 ;; (debug-enable 'backtrace)
10 (define-module (scm output-tex)
12 #:export (define-fonts
51 (use-modules (ice-9 regex)
63 (define (font-command font)
66 (string-encode-integer
67 (hashq (ly:font-filename font) 1000000))
69 (string-encode-integer
70 (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
72 (define (define-fonts bookpaper)
74 "\\def\\lilypondpaperunit{mm}" ;; UGH. FIXME.
75 (tex-number-def "lilypondpaper" 'outputscale
76 (number->string (exact->inexact
77 (ly:bookpaper-outputscale bookpaper))))
79 (map (lambda (x) (font-load-command bookpaper x))
80 (ly:bookpaper-fonts bookpaper)
86 (define (symbol->tex-key sym)
87 (regexp-substitute/global
88 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
90 (define (string->param string)
91 (string-append "{" string "}"))
93 (define (number->param number)
94 (string->param (ly:number->string number)))
96 (define (number-pair->param o)
97 (string-append (number->param (car o)) (number->param (cdr o))))
99 (define (tex-string-def prefix key str)
100 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
101 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
102 (string-append "\\def\\" prefix (symbol->tex-key key)
103 "{" (output-tex-string str) "}%\n")))
105 (define (tex-number-def prefix key number)
107 "\\def\\" prefix (symbol->tex-key key) (string->param number) "%\n"))
109 (define (output-paper-def paper)
114 (let ((val (variable-ref var))
115 (key (symbol->tex-key sym)))
119 (tex-string-def "lilypondpaper" sym val))
121 (tex-number-def "lilypondpaper" sym
124 (number->string (exact->inexact val)))))
127 (ly:output-def-scope pd))))
129 (define (output-scopes paper scopes fields basename)
130 (define (output-scope scope)
135 (let (;;(val (variable-ref var))
136 (val (if (variable-bound? var) (variable-ref var) '""))
137 (tex-key (symbol->string sym)))
139 (if (and (memq sym fields) (string? val))
140 (header-to-file basename sym val))
144 (tex-string-def "lilypond" sym val))
146 (tex-number-def "lilypond" sym
149 (number->string (exact->inexact val)))))
154 (map output-scope scopes)))
159 (define (dot x y radius)
160 (embedded-ps (list 'dot x y radius)))
162 (define (beam width slope thick blot)
163 (embedded-ps (list 'beam width slope thick blot)))
165 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
166 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
168 (define (dashed-slur thick dash l)
169 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
171 (define (char font i)
172 (string-append "\\" (font-command font)
173 "\\char" (ly:inexact->string i 10) " "))
175 (define (dashed-line thick on off dx dy)
176 (embedded-ps (list 'dashed-line thick on off dx dy)))
178 (define (zigzag-line centre? zzw zzh thick dx dy)
179 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
181 (define (symmetric-x-triangle t w h)
182 (embedded-ps (list 'symmetric-x-triangle t w h)))
184 (define (font-load-command bookpaper font)
186 "\\font\\" (font-command font) "="
187 (ly:font-filename font)
189 (ly:number->string (inexact->exact
191 (ly:font-magnification font)
192 (ly:bookpaper-outputscale bookpaper)))))
195 (define (ez-ball c l b)
196 (embedded-ps (list 'ez-ball c l b)))
198 (define (header-to-file fn key val)
199 (set! key (symbol->string key))
200 (if (not (equal? "-" fn))
201 (set! fn (string-append fn "." key))
204 (format "writing header field `~a' to `~a'..."
206 (if (equal? "-" fn) "<stdout>" fn)
208 (current-error-port))
211 (display val (open-file fn "w"))
213 (display "\n" (current-error-port))
217 (define (embedded-ps expr)
219 (with-output-to-string
220 (lambda () (ps-output-expression expr (current-output-port))))))
221 (string-append "\\embeddedps{" ps-string "}")))
224 (string-append "% " s "\n"))
228 ;; uncomment for some stats about lily memory
229 ;; (display (gc-stats))
232 ;; Put GC stats here.
235 (define (repeat-slash w a t)
236 (embedded-ps (list 'repeat-slash w a t)))
240 "\\def\\scaletounit{ "
241 (number->string (cond
242 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
243 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
244 (else (error "unknown unit" (ly:unit)))
247 "\\ifx\\lilypondstart\\undefined\n"
248 " \\input lilyponddefs\n"
250 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
252 "\\lilypondspecial\n"
253 "\\lilypondpostscript\n"))
256 (define (header creator time-stamp paper page-count classic?)
258 "% Generated by " creator "\n"
259 "% at " time-stamp "\n"
261 (tex-string-def "lilypond" 'classic "1")
263 ;; FIXME: duplicated in every backend
264 "\\def\\lilypondtagline{Engraved by LilyPond (version "
265 (lilypond-version)")}\n"))
267 (define (output-tex-string s)
268 (if (ly:get-option 'safe)
269 (regexp-substitute/global #f "\\\\"
270 (regexp-substitute/global #f "([{}])" "bla{}" 'pre "\\" 1 'post )
271 'pre "$\\backslash$" 'post)
275 (define (lily-def key val)
277 (regexp-substitute/global
278 #f "_" (output-tex-string key) 'pre "X" 'post))
280 (tex-val (output-tex-string val)))
281 (if (equal? (sans-surrounding-whitespace tex-val) "")
282 (string-append "\\let\\" tex-key "\\undefined\n")
283 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
285 (define (number->dim x)
287 ;;ugh ly:* in backend needs compatibility func for standalone output
288 (ly:number->string x) " \\outputscale "))
290 (define (placebox x y s)
292 "\\lyitem" (number->param x) (number->param y) (string->param s) "%\n"))
294 (define (bezier-sandwich l thick)
295 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
297 (define (start-system origin dim)
300 "\\lybox" (number-pair->param origin) (number-pair->param dim)
303 (define (stop-system last?)
306 ;; FIXME: still used by lilypond.py for --preview
307 "}%\n%\n\\interscoreline\n%\n"))
309 ;; WTF is this in every backend?
310 (define (horizontal-line x1 x2 th)
311 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
313 (define (filledbox breapth width depth height)
314 (if (and #f (defined? 'ps-testing))
316 (string-append (ly:numbers->string (list breapth width depth height))
318 (string-append "\\lyvrule{"
319 (ly:number->string (- breapth)) "}{"
320 (ly:number->string (+ breapth width)) "}{"
321 (ly:number->string depth) "}{"
322 (ly:number->string height) "}")))
324 (define (round-filled-box x y width height blotdiam)
325 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
327 (define (text font s)
329 ((mapping #f) ;; (assoc-get 'char-mapping (ly:font-encoding-alist font))))
332 ;; TODO: we'd better do this for PS only
333 ;; LaTeX gets in the way, and we need to remap
334 ;; nonprintable chars.
337 (input-enc-name #f) ;; (assoc-get 'input-name (ly:font-encoding-alist font) ))
340 (string-append "\\hbox{\\" (font-command font)
341 (if (string? input-enc-name)
342 (string-append "\\inputencoding{" input-enc-name "}")
345 (if (vector? mapping)
346 (reencode-string mapping s)
351 (define (tuplet ht gapx dx dy thick dir)
352 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
354 (define (polygon points blotdiameter)
355 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
357 (define (draw-line thick fx fy tx ty)
358 (embedded-ps (list 'draw-line thick fx fy tx ty)))
360 (define (define-origin file line col)
361 (if (procedure? point-and-click)
362 (string-append "\\special{src:" ;;; \\string ?
363 (point-and-click line col file)
367 ;; no-origin not yet supported by Xdvi
368 (define (no-origin) "")
371 "\n\\vbox to 0pt{\n")
373 (define (stop-page last?)
375 "\\vss\n}\n\\vfill\n"
376 "\\vss\n}\n\\vfill\\lilypondpagebreak\n"))