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 paper font-list)
74 (map (lambda (x) (font-load-command paper x)) font-list)))
79 (define (symbol->tex-key sym)
80 (regexp-substitute/global
81 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
83 (define (string->param string)
84 (string-append "{" string "}"))
86 (define (number->param number)
87 (string->param (ly:number->string number)))
89 (define (number-pair->param o)
90 (string-append (number->param (car o)) (number->param (cdr o))))
92 (define (tex-string-def prefix key str)
93 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
94 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
95 (string-append "\\def\\" prefix (symbol->tex-key key)
96 "{" (output-tex-string str) "}%\n")))
98 (define (tex-number-def prefix key number)
100 "\\def\\" prefix (symbol->tex-key key) (string->param number) "%\n"))
102 (define (output-paper-def pd)
107 (let ((val (variable-ref var))
108 (key (symbol->tex-key sym)))
112 (tex-string-def "lilypondpaper" sym val))
114 (tex-number-def "lilypondpaper" sym
117 (number->string (exact->inexact val)))))
120 (ly:output-def-scope pd))))
122 (define (output-scopes paper scopes fields basename)
123 (define (output-scope scope)
128 (let (;;(val (variable-ref var))
129 (val (if (variable-bound? var) (variable-ref var) '""))
130 (tex-key (symbol->string sym)))
132 (if (and (memq sym fields) (string? val))
133 (header-to-file basename sym val))
137 (tex-string-def "lilypond" sym val))
139 (tex-number-def "lilypond" sym
142 (number->string (exact->inexact val)))))
147 (map output-scope scopes)))
152 (define (dot x y radius)
153 (embedded-ps (list 'dot x y radius)))
155 (define (beam width slope thick blot)
156 (embedded-ps (list 'beam width slope thick blot)))
158 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
159 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
161 (define (dashed-slur thick dash l)
162 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
164 (define (char font i)
165 (string-append "\\" (font-command font)
166 "\\char" (ly:inexact->string i 10) " "))
168 (define (dashed-line thick on off dx dy)
169 (embedded-ps (list 'dashed-line thick on off dx dy)))
171 (define (zigzag-line centre? zzw zzh thick dx dy)
172 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
174 (define (symmetric-x-triangle t w h)
175 (embedded-ps (list 'symmetric-x-triangle t w h)))
177 (define (font-load-command paper font)
179 "\\font\\" (font-command font) "="
180 (ly:font-filename font)
182 (ly:number->string (inexact->exact
184 (ly:font-magnification font)
185 (ly:paper-lookup paper 'outputscale)))))
188 (define (ez-ball c l b)
189 (embedded-ps (list 'ez-ball c l b)))
191 (define (header-to-file fn key val)
192 (set! key (symbol->string key))
193 (if (not (equal? "-" fn))
194 (set! fn (string-append fn "." key))
197 (format "writing header field `~a' to `~a'..."
199 (if (equal? "-" fn) "<stdout>" fn)
201 (current-error-port))
204 (display val (open-file fn "w"))
206 (display "\n" (current-error-port))
210 (define (embedded-ps expr)
212 (with-output-to-string
213 (lambda () (ps-output-expression expr (current-output-port))))))
214 (string-append "\\embeddedps{" ps-string "}")))
217 (string-append "% " s "\n"))
221 ;; uncomment for some stats about lily memory
222 ;; (display (gc-stats))
225 ;; Put GC stats here.
228 (define (repeat-slash w a t)
229 (embedded-ps (list 'repeat-slash w a t)))
233 "\\def\\scaletounit{ "
234 (number->string (cond
235 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
236 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
237 (else (error "unknown unit" (ly:unit)))
240 "\\ifx\\lilypondstart\\undefined\n"
241 " \\input lilyponddefs\n"
243 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
245 "\\lilypondspecial\n"
246 "\\lilypondpostscript\n"))
248 (define (header creator time-stamp paper page-count classic?)
250 "% Generated by " creator "\n"
251 "% at " time-stamp "\n"
253 (tex-string-def "lilypond" 'classic "1")
255 ;; FIXME: duplicated in every backend
256 "\\def\\lilypondtagline{Engraved by LilyPond (version "
257 (lilypond-version)")}\n"))
259 ;; FIXME: explain ploblem: need to do something to make this really safe.
260 (define (output-tex-string s)
262 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
265 (define (lily-def key val)
267 (regexp-substitute/global
268 #f "_" (output-tex-string key) 'pre "X" 'post))
270 (tex-val (output-tex-string val)))
271 (if (equal? (sans-surrounding-whitespace tex-val) "")
272 (string-append "\\let\\" tex-key "\\undefined\n")
273 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
275 (define (number->dim x)
277 ;;ugh ly:* in backend needs compatibility func for standalone output
278 (ly:number->string x) " \\outputscale "))
280 (define (placebox x y s)
282 "\\lyitem" (number->param x) (number->param y) (string->param s) "%\n"))
284 (define (bezier-sandwich l thick)
285 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
287 (define (start-system origin dim)
290 "\\lybox" (number-pair->param origin) (number-pair->param dim)
293 (define (stop-system last?)
296 ;; FIXME: still used by lilypond.py for --preview
297 "}%\n%\n\\interscoreline\n%\n"))
299 ;; WTF is this in every backend?
300 (define (horizontal-line x1 x2 th)
301 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
303 (define (filledbox breapth width depth height)
304 (if (and #f (defined? 'ps-testing))
306 (string-append (ly:numbers->string (list breapth width depth height))
308 (string-append "\\lyvrule{"
309 (ly:number->string (- breapth)) "}{"
310 (ly:number->string (+ breapth width)) "}{"
311 (ly:number->string depth) "}{"
312 (ly:number->string height) "}")))
314 (define (round-filled-box x y width height blotdiam)
315 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
317 (define (text font s)
319 ((mapping #f) ;; (assoc-get 'char-mapping (ly:font-encoding-alist font))))
322 ;; TODO: we'd better do this for PS only
323 ;; LaTeX gets in the way, and we need to remap
324 ;; nonprintable chars.
327 (input-enc-name #f) ;; (assoc-get 'input-name (ly:font-encoding-alist font) ))
330 (string-append "\\hbox{\\" (font-command font)
331 (if (string? input-enc-name)
332 (string-append "\\inputencoding{" input-enc-name "}")
335 (if (vector? mapping)
336 (reencode-string mapping s)
341 (define (tuplet ht gapx dx dy thick dir)
342 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
344 (define (polygon points blotdiameter)
345 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
347 (define (draw-line thick fx fy tx ty)
348 (embedded-ps (list 'draw-line thick fx fy tx ty)))
350 (define (define-origin file line col)
351 (if (procedure? point-and-click)
352 (string-append "\\special{src:" ;;; \\string ?
353 (point-and-click line col file)
357 ;; no-origin not yet supported by Xdvi
358 (define (no-origin) "")
361 "\n\\vbox to 0pt{\n")
363 (define (stop-page last?)
365 "\\vss\n}\n\\vfill\n"
366 "\\vss\n}\n\\vfill\\lilypondpagebreak\n"))