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 ;; dumper-compatibility
24 (define (ps-output-expression expr port)
25 (let ((output-ps (resolve-module '(scm output-ps))))
26 (display (eval expr output-ps) port)))
28 ;;; Output interface entry
29 (define-public (tex-output-expression expr port)
30 (display (eval expr this-module) port ))
36 (define font-name-alist '())
38 (define (tex-encoded-fontswitch name-mag)
39 (let* ((iname-mag (car name-mag))
40 (ename-mag (cdr name-mag)))
44 (string-append "magfont"
45 (string-encode-integer
46 (hashq (car ename-mag) 1000000))
48 (string-encode-integer
49 (inexact->exact (round (* 1000 (cdr ename-mag))))))))))
51 (define (define-fonts internal-external-name-mag-pairs)
52 (set! font-name-alist (map tex-encoded-fontswitch
53 internal-external-name-mag-pairs))
56 (font-load-command (car x) (cdr x)))
57 (map cdr font-name-alist))))
60 ;; urg, how can exp be #unspecified? -- in sketch output
62 ;; set! returns #<unspecified> --hwn
64 (define (fontify name-mag-pair exp)
65 (string-append (select-font name-mag-pair)
72 (define (symbol->tex-key sym)
73 (regexp-substitute/global
74 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
76 (define (tex-string-def prefix key str)
77 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
78 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
79 (string-append "\\def\\" prefix (symbol->tex-key key) "{" (output-tex-string str) "}%\n")
82 (define (tex-number-def prefix key number)
83 (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
85 (define (output-paper-def pd)
90 (let ((val (variable-ref var))
91 (key (symbol->tex-key sym)))
95 (tex-string-def "lilypondpaper" sym val))
97 (tex-number-def "lilypondpaper" sym
100 (number->string (exact->inexact val)))))
103 (ly:output-def-scope pd))))
105 (define (output-scopes paper scopes fields basename)
106 (define (output-scope scope)
111 (let ((val (variable-ref var))
112 ;;(val (if (variable-bound? var) (variable-ref var) '""))
113 (tex-key (symbol->string sym)))
115 (if (and (memq sym fields) (string? val))
116 (header-to-file basename sym val))
120 (tex-string-def "lilypond" sym val))
122 (tex-number-def "lilypond" sym
125 (number->string (exact->inexact val)))))
130 (map output-scope scopes)))
132 (define (select-font name-mag-pair)
133 (let ((c (assoc name-mag-pair font-name-alist)))
135 (string-append "\\" (cddr c))
138 (format "Programming error: No such font: ~S" name-mag-pair))
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))))
157 (string-append "\\char" (inexact->string i 10) " "))
159 (define (dashed-line thick on off dx dy)
160 (embedded-ps (list 'dashed-line thick on off dx dy)))
162 (define (zigzag-line centre? zzw zzh thick dx dy)
163 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
165 (define (symmetric-x-triangle t w h)
166 (embedded-ps (list 'symmetric-x-triangle t w h)))
168 (define (font-load-command name-mag command)
170 "\\font\\" command "="
173 (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag)))))
176 (define (ez-ball c l b)
177 (embedded-ps (list 'ez-ball c l b)))
179 (define (header-to-file fn key val)
180 (set! key (symbol->string key))
181 (if (not (equal? "-" fn))
182 (set! fn (string-append fn "." key))
185 (format "writing header field `~a' to `~a'..."
187 (if (equal? "-" fn) "<stdout>" fn)
189 (current-error-port))
192 (display val (open-file fn "w"))
194 (display "\n" (current-error-port))
198 (define (embedded-ps expr)
200 (with-output-to-string
201 (lambda () (ps-output-expression expr (current-output-port))))))
202 (string-append "\\embeddedps{" ps-string "}")))
205 (string-append "% " s "\n"))
209 ;; uncomment for some stats about lily memory
210 ;; (display (gc-stats))
213 ;; Put GC stats here.
216 (define (experimental-on)
219 (define (repeat-slash w a t)
220 (embedded-ps (list 'repeat-slash w a t)))
224 "\\def\\scaletounit{ "
225 (number->string (cond
226 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
227 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
228 (else (error "unknown unit" (ly:unit)))
231 "\\ifx\\lilypondstart\\undefined\n"
232 " \\input lilyponddefs\n"
234 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
236 "\\lilypondspecial\n"
237 "\\lilypondpostscript\n"))
239 (define (header creator time-stamp page-count)
241 "% Generated by " creator "\n"
242 "% at " time-stamp "\n"
243 ;; FIXME: duplicated in every backend
244 "\\def\\lilypondtagline{Engraved by LilyPond (version "
245 (lilypond-version)")}\n"))
247 (define (invoke-char s i)
249 "\n\\" s "{" (inexact->string i 10) "}" ))
251 ;; FIXME: explain ploblem: need to do something to make this really safe.
252 (define-public (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 (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)))
314 (string-append "\\hbox{" (output-tex-string s) "}"))
316 (define (tuplet ht gapx dx dy thick dir)
317 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
319 (define (polygon points blotdiameter)
320 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
322 (define (draw-line thick fx fy tx ty)
323 (embedded-ps (list 'draw-line thick fx fy tx ty)))
325 ;; TODO: this should be a default, which is overriden in PS
326 (define (between-system-string string)
329 (define (define-origin file line col)
330 (if (procedure? point-and-click)
331 (string-append "\\special{src:" ;;; \\string ?
332 (point-and-click line col file)
337 ;; no-origin not yet supported by Xdvi
338 (define (no-origin) "")
343 (define (stop-page last?)
346 "\n%}\n\\newpage\n"))