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:bookpaper-def-scope bookpaper)))
81 (map (lambda (x) (font-load-command bookpaper x))
82 (ly:bookpaper-fonts bookpaper)
88 (define (symbol->tex-key sym)
89 (regexp-substitute/global
90 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
92 (define (string->param string)
93 (string-append "{" string "}"))
95 (define (number->param number)
96 (string->param (ly:number->string number)))
98 (define (number-pair->param o)
99 (string-append (number->param (car o)) (number->param (cdr o))))
101 (define (tex-string-def prefix key str)
102 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
103 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
104 (string-append "\\def\\" prefix (symbol->tex-key key)
105 "{" (output-tex-string str) "}%\n")))
107 (define (tex-number-def prefix key number)
109 "\\def\\" prefix (symbol->tex-key key) (string->param number) "%\n"))
111 (define (output-paper-def paper)
116 (let ((val (variable-ref var))
117 (key (symbol->tex-key sym)))
121 (tex-string-def "lilypondpaper" sym val))
123 (tex-number-def "lilypondpaper" sym
126 (number->string (exact->inexact val)))))
129 (ly:output-def-scope pd))))
131 (define (output-scopes paper scopes fields basename)
132 (define (output-scope scope)
137 (let (;;(val (variable-ref var))
138 (val (if (variable-bound? var) (variable-ref var) '""))
139 (tex-key (symbol->string sym)))
141 (if (and (memq sym fields) (string? val))
142 (header-to-file basename sym val))
146 (tex-string-def "lilypond" sym val))
148 (tex-number-def "lilypond" sym
151 (number->string (exact->inexact val)))))
156 (map output-scope scopes)))
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))))
173 (define (char font i)
174 (string-append "\\" (font-command font)
175 "\\char" (ly:inexact->string i 10) " "))
177 (define (dashed-line thick on off dx dy)
178 (embedded-ps (list 'dashed-line thick on off dx dy)))
180 (define (zigzag-line centre? zzw zzh thick dx dy)
181 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
183 (define (symmetric-x-triangle t w h)
184 (embedded-ps (list 'symmetric-x-triangle t w h)))
186 (define (font-load-command bookpaper font)
188 "\\font\\" (font-command font) "="
189 (ly:font-filename font)
191 (ly:number->string (inexact->exact
193 (ly:font-magnification font)
194 (ly:bookpaper-outputscale bookpaper)))))
197 (define (ez-ball c l b)
198 (embedded-ps (list 'ez-ball c l b)))
200 (define (header-to-file fn key val)
201 (set! key (symbol->string key))
202 (if (not (equal? "-" fn))
203 (set! fn (string-append fn "." key))
206 (format "writing header field `~a' to `~a'..."
208 (if (equal? "-" fn) "<stdout>" fn)
210 (current-error-port))
213 (display val (open-file fn "w"))
215 (display "\n" (current-error-port))
219 (define (embedded-ps expr)
221 (with-output-to-string
222 (lambda () (ps-output-expression expr (current-output-port))))))
223 (string-append "\\embeddedps{" ps-string "}")))
226 (string-append "% " s "\n"))
230 ;; uncomment for some stats about lily memory
231 ;; (display (gc-stats))
234 ;; Put GC stats here.
237 (define (repeat-slash w a t)
238 (embedded-ps (list 'repeat-slash w a t)))
242 "\\def\\scaletounit{ "
243 (number->string (cond
244 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
245 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
246 (else (error "unknown unit" (ly:unit)))
249 "\\ifx\\lilypondstart\\undefined\n"
250 " \\input lilyponddefs\n"
252 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
254 "\\lilypondspecial\n"
255 "\\lilypondpostscript\n"))
258 (define (header creator time-stamp paper 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"))
269 (define (output-tex-string s)
270 (if (ly:get-option 'safe)
271 (regexp-substitute/global #f "\\\\"
272 (regexp-substitute/global #f "([{}])" "bla{}" 'pre "\\" 1 'post )
273 'pre "$\\backslash$" 'post)
277 (define (lily-def key val)
279 (regexp-substitute/global
280 #f "_" (output-tex-string key) 'pre "X" 'post))
282 (tex-val (output-tex-string val)))
283 (if (equal? (sans-surrounding-whitespace tex-val) "")
284 (string-append "\\let\\" tex-key "\\undefined\n")
285 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
287 (define (number->dim x)
289 ;;ugh ly:* in backend needs compatibility func for standalone output
290 (ly:number->string x) " \\outputscale "))
292 (define (placebox x y s)
294 "\\lyitem" (number->param x) (number->param y) (string->param s) "%\n"))
296 (define (bezier-sandwich l thick)
297 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
299 (define (start-system origin dim)
302 "\\lybox" (number-pair->param origin) (number-pair->param dim)
305 (define (stop-system last?)
308 ;; FIXME: still used by lilypond.py for --preview
309 "}%\n%\n\\interscoreline\n%\n"))
311 ;; WTF is this in every backend?
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 (ly: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)))
329 (define (text font s)
331 ((mapping #f) ;; (assoc-get 'char-mapping (ly:font-encoding-alist font))))
334 ;; TODO: we'd better do this for PS only
335 ;; LaTeX gets in the way, and we need to remap
336 ;; nonprintable chars.
339 (input-enc-name #f) ;; (assoc-get 'input-name (ly:font-encoding-alist font) ))
342 (string-append "\\hbox{\\" (font-command font)
343 (if (string? input-enc-name)
344 (string-append "\\inputencoding{" input-enc-name "}")
347 (if (vector? mapping)
348 (reencode-string mapping s)
353 (define (tuplet ht gapx dx dy thick dir)
354 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
356 (define (polygon points blotdiameter)
357 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
359 (define (draw-line thick fx fy tx ty)
360 (embedded-ps (list 'draw-line thick fx fy tx ty)))
362 (define (define-origin file line col)
363 (if (procedure? point-and-click)
364 (string-append "\\special{src:" ;;; \\string ?
365 (point-and-click line col file)
369 ;; no-origin not yet supported by Xdvi
370 (define (no-origin) "")
373 "\n\\vbox to 0pt{\n")
375 (define (stop-page last?)
377 "\\vss\n}\n\\vfill\n"
378 "\\vss\n}\n\\vfill\\lilypondpagebreak\n"))