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
53 (use-modules (ice-9 regex)
65 (define (font-command font)
68 (string-encode-integer
69 (hashq (ly:font-filename font) 1000000))
71 (string-encode-integer
72 (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
74 (define (define-fonts paper font-list)
76 (map (lambda (x) (font-load-command paper x)) font-list)))
81 (define (symbol->tex-key sym)
82 (regexp-substitute/global
83 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
85 (define (tex-string-def prefix key str)
86 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
87 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
88 (string-append "\\def\\" prefix (symbol->tex-key key) "{" (output-tex-string str) "}%\n")
91 (define (tex-number-def prefix key number)
92 (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
94 (define (output-paper-def pd)
99 (let ((val (variable-ref var))
100 (key (symbol->tex-key sym)))
104 (tex-string-def "lilypondpaper" sym val))
106 (tex-number-def "lilypondpaper" sym
109 (number->string (exact->inexact val)))))
112 (ly:output-def-scope pd))))
114 (define (output-scopes paper scopes fields basename)
115 (define (output-scope scope)
120 (let (;;(val (variable-ref var))
121 (val (if (variable-bound? var) (variable-ref var) '""))
122 (tex-key (symbol->string sym)))
124 (if (and (memq sym fields) (string? val))
125 (header-to-file basename sym val))
129 (tex-string-def "lilypond" sym val))
131 (tex-number-def "lilypond" sym
134 (number->string (exact->inexact val)))))
139 (map output-scope scopes)))
144 (define (dot x y radius)
145 (embedded-ps (list 'dot x y radius)))
147 (define (beam width slope thick blot)
148 (embedded-ps (list 'beam width slope thick blot)))
150 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
151 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
153 (define (dashed-slur thick dash l)
154 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
156 (define (char font i)
157 (string-append "\\" (font-command font)
158 "\\char" (ly:inexact->string i 10) " "))
160 (define (dashed-line thick on off dx dy)
161 (embedded-ps (list 'dashed-line thick on off dx dy)))
163 (define (zigzag-line centre? zzw zzh thick dx dy)
164 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
166 (define (symmetric-x-triangle t w h)
167 (embedded-ps (list 'symmetric-x-triangle t w h)))
169 (define (font-load-command paper font)
171 "\\font\\" (font-command font) "="
172 (ly:font-filename font)
174 (ly:number->string (inexact->exact
176 (ly:font-magnification font)
177 (ly:paper-lookup paper 'outputscale)))))
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))
217 ;; Put GC stats here.
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 (define (header creator time-stamp page-count)
245 "% Generated by " creator "\n"
246 "% at " time-stamp "\n"
247 ;; FIXME: duplicated in every backend
248 "\\def\\lilypondtagline{Engraved by LilyPond (version "
249 (lilypond-version)")}\n"))
251 ;; FIXME: explain ploblem: need to do something to make this really safe.
252 (define (output-tex-string s)
254 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
257 (define (lily-def key val)
259 (regexp-substitute/global
260 #f "_" (output-tex-string key) 'pre "X" 'post))
262 (tex-val (output-tex-string val)))
263 (if (equal? (sans-surrounding-whitespace tex-val) "")
264 (string-append "\\let\\" tex-key "\\undefined\n")
265 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
267 (define (number->dim x)
269 ;;ugh ly:* in backend needs compatibility func for standalone output
270 (ly:number->string x) " \\outputscale "))
272 (define (placebox x y s)
273 (string-append "\\lyitem{"
274 (ly:number->string y) "}{"
275 (ly:number->string x) "}{"
278 (define (bezier-sandwich l thick)
279 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
281 (define (start-system wd ht)
282 (string-append "\\leavevmode\n"
283 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
284 "\\lilypondifundefined{lilypondscoreshift}%\n"
286 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
288 (ly:number->string wd) "}{"
289 (ly:number->string ht) "}{%\n"))
291 (define (stop-system)
292 "}%\n%\n\\interscoreline\n%\n")
293 (define (stop-last-system)
296 (define (horizontal-line x1 x2 th)
297 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
299 (define (filledbox breapth width depth height)
300 (if (and #f (defined? 'ps-testing))
302 (string-append (ly:numbers->string (list breapth width depth height))
304 (string-append "\\lyvrule{"
305 (ly:number->string (- breapth)) "}{"
306 (ly:number->string (+ breapth width)) "}{"
307 (ly:number->string depth) "}{"
308 (ly:number->string height) "}")))
310 (define (round-filled-box x y width height blotdiam)
311 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
313 (define (text font s)
317 ;; TODO: we'd better do this for PS only
318 ;; LaTeX gets in the way, and we need to remap
319 ;; nonprintable chars.
321 ; (assoc-get 'char-mapping (ly:font-encoding-alist font))))
323 (string-append "\\hbox{\\" (font-command font) "{}"
325 (if (vector? mapping)
326 (reencode-string mapping s)
331 (define (tuplet ht gapx dx dy thick dir)
332 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
334 (define (polygon points blotdiameter)
335 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
337 (define (draw-line thick fx fy tx ty)
338 (embedded-ps (list 'draw-line thick fx fy tx ty)))
340 ;; TODO: this should be a default, which is overriden in PS
341 (define (between-system-string string)
344 (define (define-origin file line col)
345 (if (procedure? point-and-click)
346 (string-append "\\special{src:" ;;; \\string ?
347 (point-and-click line col file)
351 ;; no-origin not yet supported by Xdvi
352 (define (no-origin) "")
357 (define (stop-page last?)
360 "\n%}\n\\newpage\n"))