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))))
78 (tex-string-def "lilypondpapersize" 'papersize
79 (eval 'papersize (ly:output-def-scope bookpaper)))
82 (map (lambda (x) (font-load-command bookpaper x))
83 (ly:bookpaper-fonts bookpaper)
89 (define (symbol->tex-key sym)
90 (regexp-substitute/global
91 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
93 (define (string->param string)
94 (string-append "{" string "}"))
96 (define (number->param number)
97 (string->param (ly:number->string number)))
99 (define (number-pair->param o)
100 (string-append (number->param (car o)) (number->param (cdr o))))
102 (define (tex-string-def prefix key str)
103 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
104 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
105 (string-append "\\def\\" prefix (symbol->tex-key key)
106 "{" (output-tex-string str) "}%\n")))
108 (define (tex-number-def prefix key number)
110 "\\def\\" prefix (symbol->tex-key key) (string->param number) "%\n"))
112 (define (output-paper-def paper)
117 (let ((val (variable-ref var))
118 (key (symbol->tex-key sym)))
122 (tex-string-def "lilypondpaper" sym val))
124 (tex-number-def "lilypondpaper" sym
127 (number->string (exact->inexact val)))))
130 (ly:output-def-scope pd))))
132 (define (output-scopes paper scopes fields basename)
133 (define (output-scope scope)
138 (let (;;(val (variable-ref var))
139 (val (if (variable-bound? var) (variable-ref var) '""))
140 (tex-key (symbol->string sym)))
142 (if (and (memq sym fields) (string? val))
143 (header-to-file basename sym val))
147 (tex-string-def "lilypond" sym val))
149 (tex-number-def "lilypond" sym
152 (number->string (exact->inexact val)))))
157 (map output-scope scopes)))
162 (define (dot x y radius)
163 (embedded-ps (list 'dot x y radius)))
165 (define (beam width slope thick blot)
166 (embedded-ps (list 'beam width slope thick blot)))
168 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
169 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
171 (define (dashed-slur thick dash l)
172 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
174 (define (char font i)
175 (string-append "\\" (font-command font)
176 "\\char" (ly:inexact->string i 10) " "))
178 (define (dashed-line thick on off dx dy)
179 (embedded-ps (list 'dashed-line thick on off dx dy)))
181 (define (zigzag-line centre? zzw zzh thick dx dy)
182 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
184 (define (symmetric-x-triangle t w h)
185 (embedded-ps (list 'symmetric-x-triangle t w h)))
187 (define (font-load-command bookpaper font)
189 "\\font\\" (font-command font) "="
190 (ly:font-filename font)
192 (ly:number->string (inexact->exact
194 (ly:font-magnification font)
195 (ly:bookpaper-outputscale bookpaper)))))
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))
235 ;; Put GC stats here.
238 (define (repeat-slash w a t)
239 (embedded-ps (list 'repeat-slash w a t)))
243 "\\def\\scaletounit{ "
244 (number->string (cond
245 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
246 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
247 (else (error "unknown unit" (ly:unit)))
250 "\\ifx\\lilypondstart\\undefined\n"
251 " \\input lilyponddefs\n"
253 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
255 "\\lilypondspecial\n"
256 "\\lilypondpostscript\n"))
258 (define (header creator time-stamp bookpaper page-count classic?)
260 "% Generated by " creator "\n"
261 "% at " time-stamp "\n"
263 (tex-string-def "lilypond" 'classic "1")
265 ;; FIXME: duplicated in every backend
266 "\\def\\lilypondtagline{Engraved by LilyPond (version "
267 (lilypond-version)")}\n"
270 ;; this is -of course- severely broken, (--hwn)
271 (tex-string-def "lilypondpaper" 'linewidth
272 (ly:number->string (/ 18 0.175))) ; 18 cm.
273 (tex-string-def "lilypondpaper" 'interscoreline
274 (ly:number->string 0.0))
277 (define (output-tex-string s)
278 (if (ly:get-option 'safe)
279 (regexp-substitute/global #f "\\\\"
280 (regexp-substitute/global #f "([{}])" "bla{}" 'pre "\\" 1 'post )
281 'pre "$\\backslash$" 'post)
285 (define (lily-def key val)
287 (regexp-substitute/global
288 #f "_" (output-tex-string key) 'pre "X" 'post))
290 (tex-val (output-tex-string val)))
291 (if (equal? (sans-surrounding-whitespace tex-val) "")
292 (string-append "\\let\\" tex-key "\\undefined\n")
293 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
295 (define (number->dim x)
297 ;;ugh ly:* in backend needs compatibility func for standalone output
298 (ly:number->string x) " \\outputscale "))
300 (define (placebox x y s)
302 "\\lyitem" (number->param x) (number->param y) (string->param s) "%\n"))
304 (define (bezier-sandwich l thick)
305 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
307 (define (start-system origin dim)
310 "\\lybox" (number-pair->param origin) (number-pair->param dim)
313 (define (stop-system last?)
316 ;; FIXME: still used by lilypond.py for --preview
317 "}%\n%\n\\interscoreline\n%\n"))
319 ;; WTF is this in every backend?
320 (define (horizontal-line x1 x2 th)
321 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
323 (define (filledbox breapth width depth height)
324 (if (and #f (defined? 'ps-testing))
326 (string-append (ly:numbers->string (list breapth width depth height))
328 (string-append "\\lyvrule{"
329 (ly:number->string (- breapth)) "}{"
330 (ly:number->string (+ breapth width)) "}{"
331 (ly:number->string depth) "}{"
332 (ly:number->string height) "}")))
334 (define (round-filled-box x y width height blotdiam)
335 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
337 (define (text font s)
339 ((mapping #f) ;; (assoc-get 'char-mapping (ly:font-encoding-alist font))))
342 ;; TODO: we'd better do this for PS only
343 ;; LaTeX gets in the way, and we need to remap
344 ;; nonprintable chars.
347 (input-enc-name #f) ;; (assoc-get 'input-name (ly:font-encoding-alist font) ))
350 (string-append "\\hbox{\\" (font-command font)
351 (if (string? input-enc-name)
352 (string-append "\\inputencoding{" input-enc-name "}")
355 (if (vector? mapping)
356 (reencode-string mapping s)
361 (define (tuplet ht gapx dx dy thick dir)
362 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
364 (define (polygon points blotdiameter)
365 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
367 (define (draw-line thick fx fy tx ty)
368 (embedded-ps (list 'draw-line thick fx fy tx ty)))
370 (define (define-origin file line col)
371 (if (procedure? point-and-click)
372 (string-append "\\special{src:" ;;; \\string ?
373 (point-and-click line col file)
377 ;; no-origin not yet supported by Xdvi
378 (define (no-origin) "")
381 "\n\\vbox to 0pt{\n")
383 (define (stop-page last?)
385 "\\vss\n}\n\\vfill\n"
386 "\\vss\n}\n\\vfill\\lilypondpagebreak\n"))