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 (define-module (scm output-tex) )
10 ; (debug-enable 'backtrace)
11 (use-modules (scm output-ps)
20 (define this-module (current-module))
22 ;;; Output interface entry
23 (define-public (tex-output-expression expr port)
24 (display (eval expr this-module) port ))
30 (define font-name-alist '())
32 (define (tex-encoded-fontswitch name-mag)
33 (let* ((iname-mag (car name-mag))
34 (ename-mag (cdr name-mag)))
38 (string-append "magfont"
39 (string-encode-integer
40 (hashq (car ename-mag) 1000000))
42 (string-encode-integer
43 (inexact->exact (round (* 1000 (cdr ename-mag))))))))))
45 (define (define-fonts internal-external-name-mag-pairs)
46 (set! font-name-alist (map tex-encoded-fontswitch
47 internal-external-name-mag-pairs))
50 (font-load-command (car x) (cdr x)))
51 (map cdr font-name-alist))))
54 ;; urg, how can exp be #unspecified? -- in sketch output
56 ;; set! returns #<unspecified> --hwn
58 (define (fontify name-mag-pair exp)
59 (string-append (select-font name-mag-pair)
66 (define (symbol->tex-key sym)
67 (regexp-substitute/global
68 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
70 (define (tex-string-def prefix key str)
71 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
72 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
73 (string-append "\\def\\" prefix (symbol->tex-key key) "{" (output-tex-string str) "}%\n")
76 (define (tex-number-def prefix key number)
77 (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
79 (define (output-paper-def pd)
84 (let ((val (variable-ref var))
85 (key (symbol->tex-key sym)))
89 (tex-string-def "lilypondpaper" sym val))
91 (tex-number-def "lilypondpaper" sym
94 (number->string (exact->inexact val)))))
97 (ly:output-def-scope pd))))
99 (define (output-scopes paper scopes fields basename)
100 (define (output-scope scope)
105 (let ((val (variable-ref var))
106 ;;(val (if (variable-bound? var) (variable-ref var) '""))
107 (tex-key (symbol->string sym)))
109 (if (and (memq sym fields) (string? val))
110 (header-to-file basename sym val))
114 (tex-string-def "lilypond" sym val))
116 (tex-number-def "lilypond" sym
119 (number->string (exact->inexact val)))))
124 (map output-scope scopes)))
126 (define (select-font name-mag-pair)
127 (let ((c (assoc name-mag-pair font-name-alist)))
129 (string-append "\\" (cddr c))
132 (format "Programming error: No such font: ~S" name-mag-pair))
138 (define (dot x y radius)
139 (embedded-ps (list 'dot x y radius)))
141 (define (beam width slope thick blot)
142 (embedded-ps (list 'beam width slope thick blot)))
144 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
145 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
147 (define (dashed-slur thick dash l)
148 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
151 (string-append "\\char" (inexact->string i 10) " "))
153 (define (dashed-line thick on off dx dy)
154 (embedded-ps (list 'dashed-line thick on off dx dy)))
156 (define (zigzag-line centre? zzw zzh thick dx dy)
157 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
159 (define (symmetric-x-triangle t w h)
160 (embedded-ps (list 'symmetric-x-triangle t w h)))
162 (define (font-load-command name-mag command)
164 "\\font\\" command "="
167 (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag)))))
170 (define (ez-ball c l b)
171 (embedded-ps (list 'ez-ball c l b)))
173 (define (header-to-file fn key val)
174 (set! key (symbol->string key))
175 (if (not (equal? "-" fn))
176 (set! fn (string-append fn "." key))
179 (format "writing header field `~a' to `~a'..."
181 (if (equal? "-" fn) "<stdout>" fn)
183 (current-error-port))
186 (display val (open-file fn "w"))
188 (display "\n" (current-error-port))
192 (define (embedded-ps expr)
194 (with-output-to-string
195 (lambda () (ps-output-expression expr (current-output-port))))))
196 (string-append "\\embeddedps{" ps-string "}")))
199 (string-append "% " s "\n"))
203 ; uncomment for some stats about lily memory
204 ; (display (gc-stats))
210 (define (experimental-on)
213 (define (repeat-slash w a t)
214 (embedded-ps (list 'repeat-slash w a t)))
218 "\\def\\scaletounit{ "
219 (number->string (cond
220 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
221 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
222 (else (error "unknown unit" (ly:unit)))
225 "\\ifx\\lilypondstart\\undefined\n"
226 " \\input lilyponddefs\n"
228 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
230 "\\lilypondspecial\n"
231 "\\lilypondpostscript\n"))
233 (define (header creator time-stamp)
235 "% Generated by " creator "\n"
236 "% at " time-stamp "\n"
237 ;; FIXME: duplicated in every backend
238 "\\def\\lilypondtagline{Engraved by LilyPond (version "
239 (lilypond-version)")}\n"))
241 (define (invoke-char s i)
243 "\n\\" s "{" (inexact->string i 10) "}" ))
245 ;; FIXME: explain ploblem: need to do something to make this really safe.
246 (define-public (output-tex-string s)
248 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
251 (define (lily-def key val)
253 (regexp-substitute/global
254 #f "_" (output-tex-string key) 'pre "X" 'post))
256 (tex-val (output-tex-string val)))
257 (if (equal? (sans-surrounding-whitespace tex-val) "")
258 (string-append "\\let\\" tex-key "\\undefined\n")
259 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
261 (define (number->dim x)
263 ;;ugh ly:* in backend needs compatibility func for standalone output
264 (ly:number->string x) " \\outputscale "))
266 (define (placebox x y s)
267 (string-append "\\lyitem{"
268 (ly:number->string y) "}{"
269 (ly:number->string x) "}{"
272 (define (bezier-sandwich l thick)
273 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
275 (define (start-system wd ht)
276 (string-append "\\leavevmode\n"
277 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
278 "\\lilypondifundefined{lilypondscoreshift}%\n"
280 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
282 (ly:number->string wd) "}{"
283 (ly:number->string ht) "}{%\n"))
285 (define (stop-system)
286 "}%\n%\n\\interscoreline\n%\n")
287 (define (stop-last-system)
290 (define (horizontal-line x1 x2 th)
291 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
293 (define (filledbox breapth width depth height)
294 (if (and #f (defined? 'ps-testing))
296 (string-append (numbers->string (list breapth width depth height))
298 (string-append "\\lyvrule{"
299 (ly:number->string (- breapth)) "}{"
300 (ly:number->string (+ breapth width)) "}{"
301 (ly:number->string depth) "}{"
302 (ly:number->string height) "}")))
304 (define (round-filled-box x y width height blotdiam)
305 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
308 (string-append "\\hbox{" (output-tex-string s) "}"))
310 (define (tuplet ht gapx dx dy thick dir)
311 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
313 (define (polygon points blotdiameter)
314 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
316 (define (draw-line thick fx fy tx ty)
317 (embedded-ps (list 'draw-line thick fx fy tx ty)))
319 ;; TODO: this should be a default, which is overriden in PS
320 (define (between-system-string string)
323 (define (define-origin file line col)
324 (if (procedure? point-and-click)
325 (string-append "\\special{src:" ;;; \\string ?
326 (point-and-click line col file)
331 ;; no-origin not yet supported by Xdvi
332 (define (no-origin) "")
337 (define (stop-page last?)
340 "\n%}\n\\newpage\n"))