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)
76 (map (lambda (x) (font-load-command paper x)) font-list)))
78 (define (fontify font exp)
79 (string-append "\\" (font-command font) exp))
84 (define (symbol->tex-key sym)
85 (regexp-substitute/global
86 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
88 (define (tex-string-def prefix key str)
89 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
90 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
91 (string-append "\\def\\" prefix (symbol->tex-key key) "{" (output-tex-string str) "}%\n")
94 (define (tex-number-def prefix key number)
95 (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
97 (define (output-paper-def pd)
102 (let ((val (variable-ref var))
103 (key (symbol->tex-key sym)))
107 (tex-string-def "lilypondpaper" sym val))
109 (tex-number-def "lilypondpaper" sym
112 (number->string (exact->inexact val)))))
115 (ly:output-def-scope pd))))
117 (define (output-scopes paper scopes fields basename)
118 (define (output-scope scope)
123 (let ((val (variable-ref var))
124 ;;(val (if (variable-bound? var) (variable-ref var) '""))
125 (tex-key (symbol->string sym)))
127 (if (and (memq sym fields) (string? val))
128 (header-to-file basename sym val))
132 (tex-string-def "lilypond" sym val))
134 (tex-number-def "lilypond" sym
137 (number->string (exact->inexact val)))))
142 (map output-scope scopes)))
147 (define (dot x y radius)
148 (embedded-ps (list 'dot x y radius)))
150 (define (beam width slope thick blot)
151 (embedded-ps (list 'beam width slope thick blot)))
153 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
154 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
156 (define (dashed-slur thick dash l)
157 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
160 (string-append "\\char" (ly:inexact->string i 10) " "))
162 (define (dashed-line thick on off dx dy)
163 (embedded-ps (list 'dashed-line thick on off dx dy)))
165 (define (zigzag-line centre? zzw zzh thick dx dy)
166 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
168 (define (symmetric-x-triangle t w h)
169 (embedded-ps (list 'symmetric-x-triangle t w h)))
171 (define (font-load-command paper font)
173 "\\font\\" (font-command font) "="
176 (ly:number->string (inexact->exact
178 (ly:font-magnification font)
179 (ly:paper-lookup paper 'outputscale)))))
182 (define (ez-ball c l b)
183 (embedded-ps (list 'ez-ball c l b)))
185 (define (header-to-file fn key val)
186 (set! key (symbol->string key))
187 (if (not (equal? "-" fn))
188 (set! fn (string-append fn "." key))
191 (format "writing header field `~a' to `~a'..."
193 (if (equal? "-" fn) "<stdout>" fn)
195 (current-error-port))
198 (display val (open-file fn "w"))
200 (display "\n" (current-error-port))
204 (define (embedded-ps expr)
206 (with-output-to-string
207 (lambda () (ps-output-expression expr (current-output-port))))))
208 (string-append "\\embeddedps{" ps-string "}")))
211 (string-append "% " s "\n"))
215 ;; uncomment for some stats about lily memory
216 ;; (display (gc-stats))
219 ;; Put GC stats here.
222 (define (experimental-on)
225 (define (repeat-slash w a t)
226 (embedded-ps (list 'repeat-slash w a t)))
230 "\\def\\scaletounit{ "
231 (number->string (cond
232 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
233 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
234 (else (error "unknown unit" (ly:unit)))
237 "\\ifx\\lilypondstart\\undefined\n"
238 " \\input lilyponddefs\n"
240 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
242 "\\lilypondspecial\n"
243 "\\lilypondpostscript\n"))
245 (define (header creator time-stamp page-count)
247 "% Generated by " creator "\n"
248 "% at " time-stamp "\n"
249 ;; FIXME: duplicated in every backend
250 "\\def\\lilypondtagline{Engraved by LilyPond (version "
251 (lilypond-version)")}\n"))
253 (define (invoke-char s i)
255 "\n\\" s "{" (ly:inexact->string i 10) "}" ))
257 ;; FIXME: explain ploblem: need to do something to make this really safe.
258 (define (output-tex-string s)
260 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
263 (define (lily-def key val)
265 (regexp-substitute/global
266 #f "_" (output-tex-string key) 'pre "X" 'post))
268 (tex-val (output-tex-string val)))
269 (if (equal? (sans-surrounding-whitespace tex-val) "")
270 (string-append "\\let\\" tex-key "\\undefined\n")
271 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
273 (define (number->dim x)
275 ;;ugh ly:* in backend needs compatibility func for standalone output
276 (ly:number->string x) " \\outputscale "))
278 (define (placebox x y s)
279 (string-append "\\lyitem{"
280 (ly:number->string y) "}{"
281 (ly:number->string x) "}{"
284 (define (bezier-sandwich l thick)
285 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
287 (define (start-system wd ht)
288 (string-append "\\leavevmode\n"
289 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
290 "\\lilypondifundefined{lilypondscoreshift}%\n"
292 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
294 (ly:number->string wd) "}{"
295 (ly:number->string ht) "}{%\n"))
297 (define (stop-system)
298 "}%\n%\n\\interscoreline\n%\n")
299 (define (stop-last-system)
302 (define (horizontal-line x1 x2 th)
303 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
305 (define (filledbox breapth width depth height)
306 (if (and #f (defined? 'ps-testing))
308 (string-append (ly:numbers->string (list breapth width depth height))
310 (string-append "\\lyvrule{"
311 (ly:number->string (- breapth)) "}{"
312 (ly:number->string (+ breapth width)) "}{"
313 (ly:number->string depth) "}{"
314 (ly:number->string height) "}")))
316 (define (round-filled-box x y width height blotdiam)
317 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
320 (string-append "\\hbox{" (output-tex-string s) "}"))
322 (define (tuplet ht gapx dx dy thick dir)
323 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
325 (define (polygon points blotdiameter)
326 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
328 (define (draw-line thick fx fy tx ty)
329 (embedded-ps (list 'draw-line thick fx fy tx ty)))
331 ;; TODO: this should be a default, which is overriden in PS
332 (define (between-system-string string)
335 (define (define-origin file line col)
336 (if (procedure? point-and-click)
337 (string-append "\\special{src:" ;;; \\string ?
338 (point-and-click line col file)
343 ;; no-origin not yet supported by Xdvi
344 (define (no-origin) "")
349 (define (stop-page last?)
352 "\n%}\n\\newpage\n"))