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
54 (use-modules (ice-9 regex)
65 (define (font-command font)
68 (string-encode-integer
69 (hashq (ly:font-name font) 1000000))
71 (string-encode-integer
72 (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
74 (define (define-fonts paper font-list)
77 (font-load-command paper x))
82 ;; urg, how can exp be #unspecified? -- in sketch output
84 ;; set! returns #<unspecified> --hwn
86 (define (fontify font exp)
87 (string-append "\\" (font-command font)
93 (define (symbol->tex-key sym)
94 (regexp-substitute/global
95 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
97 (define (tex-string-def prefix key str)
98 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
99 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
100 (string-append "\\def\\" prefix (symbol->tex-key key) "{" (output-tex-string str) "}%\n")
103 (define (tex-number-def prefix key number)
104 (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
106 (define (output-paper-def pd)
111 (let ((val (variable-ref var))
112 (key (symbol->tex-key sym)))
116 (tex-string-def "lilypondpaper" sym val))
118 (tex-number-def "lilypondpaper" sym
121 (number->string (exact->inexact val)))))
124 (ly:output-def-scope pd))))
126 (define (output-scopes paper scopes fields basename)
127 (define (output-scope scope)
132 (let ((val (variable-ref var))
133 ;;(val (if (variable-bound? var) (variable-ref var) '""))
134 (tex-key (symbol->string sym)))
136 (if (and (memq sym fields) (string? val))
137 (header-to-file basename sym val))
141 (tex-string-def "lilypond" sym val))
143 (tex-number-def "lilypond" sym
146 (number->string (exact->inexact val)))))
151 (map output-scope scopes)))
156 (define (dot x y radius)
157 (embedded-ps (list 'dot x y radius)))
159 (define (beam width slope thick blot)
160 (embedded-ps (list 'beam width slope thick blot)))
162 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
163 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
165 (define (dashed-slur thick dash l)
166 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
169 (string-append "\\char" (ly:inexact->string i 10) " "))
171 (define (dashed-line thick on off dx dy)
172 (embedded-ps (list 'dashed-line thick on off dx dy)))
174 (define (zigzag-line centre? zzw zzh thick dx dy)
175 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
177 (define (symmetric-x-triangle t w h)
178 (embedded-ps (list 'symmetric-x-triangle t w h)))
180 (define (font-load-command paper font)
182 "\\font\\" (font-command font) "="
185 (ly:number->string (inexact->exact
187 (ly:font-magnification font)
188 (ly:paper-lookup paper 'outputscale)))))
191 (define (ez-ball c l b)
192 (embedded-ps (list 'ez-ball c l b)))
194 (define (header-to-file fn key val)
195 (set! key (symbol->string key))
196 (if (not (equal? "-" fn))
197 (set! fn (string-append fn "." key))
200 (format "writing header field `~a' to `~a'..."
202 (if (equal? "-" fn) "<stdout>" fn)
204 (current-error-port))
207 (display val (open-file fn "w"))
209 (display "\n" (current-error-port))
213 (define (embedded-ps expr)
215 (with-output-to-string
216 (lambda () (ps-output-expression expr (current-output-port))))))
217 (string-append "\\embeddedps{" ps-string "}")))
220 (string-append "% " s "\n"))
224 ;; uncomment for some stats about lily memory
225 ;; (display (gc-stats))
228 ;; Put GC stats here.
231 (define (experimental-on)
234 (define (repeat-slash w a t)
235 (embedded-ps (list 'repeat-slash w a t)))
239 "\\def\\scaletounit{ "
240 (number->string (cond
241 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
242 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
243 (else (error "unknown unit" (ly:unit)))
246 "\\ifx\\lilypondstart\\undefined\n"
247 " \\input lilyponddefs\n"
249 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
251 "\\lilypondspecial\n"
252 "\\lilypondpostscript\n"))
254 (define (header creator time-stamp page-count)
256 "% Generated by " creator "\n"
257 "% at " time-stamp "\n"
258 ;; FIXME: duplicated in every backend
259 "\\def\\lilypondtagline{Engraved by LilyPond (version "
260 (lilypond-version)")}\n"))
262 (define (invoke-char s i)
264 "\n\\" s "{" (ly:inexact->string i 10) "}" ))
266 ;; FIXME: explain ploblem: need to do something to make this really safe.
267 (define (output-tex-string s)
269 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
272 (define (lily-def key val)
274 (regexp-substitute/global
275 #f "_" (output-tex-string key) 'pre "X" 'post))
277 (tex-val (output-tex-string val)))
278 (if (equal? (sans-surrounding-whitespace tex-val) "")
279 (string-append "\\let\\" tex-key "\\undefined\n")
280 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
282 (define (number->dim x)
284 ;;ugh ly:* in backend needs compatibility func for standalone output
285 (ly:number->string x) " \\outputscale "))
287 (define (placebox x y s)
288 (string-append "\\lyitem{"
289 (ly:number->string y) "}{"
290 (ly:number->string x) "}{"
293 (define (bezier-sandwich l thick)
294 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
296 (define (start-system wd ht)
297 (string-append "\\leavevmode\n"
298 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
299 "\\lilypondifundefined{lilypondscoreshift}%\n"
301 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
303 (ly:number->string wd) "}{"
304 (ly:number->string ht) "}{%\n"))
306 (define (stop-system)
307 "}%\n%\n\\interscoreline\n%\n")
308 (define (stop-last-system)
311 (define (horizontal-line x1 x2 th)
312 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
314 (define (filledbox breapth width depth height)
315 (if (and #f (defined? 'ps-testing))
317 (string-append (ly:numbers->string (list breapth width depth height))
319 (string-append "\\lyvrule{"
320 (ly:number->string (- breapth)) "}{"
321 (ly:number->string (+ breapth width)) "}{"
322 (ly:number->string depth) "}{"
323 (ly:number->string height) "}")))
325 (define (round-filled-box x y width height blotdiam)
326 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
329 (string-append "\\hbox{" (output-tex-string 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)
352 ;; no-origin not yet supported by Xdvi
353 (define (no-origin) "")
358 (define (stop-page last?)
361 "\n%}\n\\newpage\n"))