2 ;;; tex.scm -- implement Scheme output routines for TeX
4 ;;; source file of the GNU LilyPond music typesetter
6 ;;; (c) 1998--2001 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 (define-module (scm tex) )
11 (debug-enable 'backtrace)
20 (define this-module (current-module))
26 (define font-name-alist '())
28 (define (tex-encoded-fontswitch name-mag)
29 (let* ((iname-mag (car name-mag))
30 (ename-mag (cdr name-mag)))
33 (string-append "magfont"
34 (string-encode-integer
35 (hashq (car ename-mag) 1000000))
37 (string-encode-integer
38 (inexact->exact (* 1000 (cdr ename-mag)))))))))
40 (define (define-fonts internal-external-name-mag-pairs)
41 (set! font-name-alist (map tex-encoded-fontswitch
42 internal-external-name-mag-pairs))
45 (font-load-command (car x) (cdr x)))
46 (map cdr font-name-alist))))
50 ;; urg, how can exp be #unspecified? -- in sketch output
52 ;; set! returns #<unspecified> --hwn
53 (define (fontify name-mag-pair exp)
54 (string-append (select-font name-mag-pair)
61 (define (select-font name-mag-pair)
64 (c (assoc name-mag-pair font-name-alist))
70 (display (object-type (car name-mag-pair)))
71 (display (object-type (caaar font-name-alist)))
73 (ly-warn (string-append
74 "Programming error: No such font known "
75 (car name-mag-pair) " "
76 (ly-number->string (cdr name-mag-pair))
78 "") ; issue no command
79 (string-append "\\" (cddr c)))
84 (define (beam width slope thick)
85 (embedded-ps (list 'beam width slope thick)))
87 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
88 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
90 (define (dashed-slur thick dash l)
91 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
94 (string-append "\\char" (inexact->string i 10) " "))
96 (define (dashed-line thick on off dx dy)
97 (embedded-ps (list 'dashed-line thick on off dx dy)))
99 (define (font-load-command name-mag command)
101 "\\font\\" command "="
104 (ly-number->string (inexact->exact (* 1000 (cdr name-mag))))
107 (define (ez-ball c l b)
108 (embedded-ps (list 'ez-ball c l b)))
110 (define (header-to-file fn key val)
111 (set! key (symbol->string key))
112 (if (not (equal? "-" fn))
113 (set! fn (string-append fn "." key))
116 (format "writing header field `~a' to `~a'..."
118 (if (equal? "-" fn) "<stdout>" fn)
120 (current-error-port))
123 (display val (open-file fn "w"))
125 (display "\n" (current-error-port))
129 (if (or (equal? (minor-version) "4.1")
130 (equal? (minor-version) "4")
131 (equal? (minor-version) "3.4"))
132 (define (embedded-ps expr)
134 (with-output-to-string
135 (lambda () (ps-output-expression expr (current-output-port))))))
136 (string-append "\\embeddedps{" ps-string "}")))
137 (define (embedded-ps expr)
139 ((os (open-output-string)))
140 (ps-output-expression expr os)
141 (string-append "\\embeddedps{" (get-output-string os) "}"))))
144 (string-append "% " s "\n"))
148 ; uncomment for some stats about lily memory
149 ; (display (gc-stats))
150 (string-append "%\n\\endgroup\\EndLilyPondOutput\n"
154 (define (experimental-on)
157 (define (repeat-slash w a t)
158 (embedded-ps (list 'repeat-slash w a t)))
160 (define (font-switch i)
164 (define (font-def i s)
166 "\\font" (font-switch i) "=" s "\n"))
170 "\\def\\scaletounit{ "
171 (number->string (cond
172 ((equal? (ly-unit) "mm") (/ 72.0 25.4))
173 ((equal? (ly-unit) "pt") (/ 72.0 72.27))
174 (else (error "unknown unit" (ly-unit)))
177 "\\special{\\string! "
179 ;; URG: ly-gulp-file: now we can't use scm output without Lily
180 (regexp-substitute/global #f "\n"
181 (ly-gulp-file "music-drawing-routines.ps") 'pre " %\n" 'post)
183 "\\input lilyponddefs\n"
184 "\\outputscale=\\lilypondpaperoutputscale \\lilypondpaperunit\n"
185 "\\turnOnPostScript\\begingroup\\parindent0pt\n"))
187 ;; Note: this string must match the string in ly2dvi.py!!!
188 (define (header creator generate)
190 "% Generated automatically by: " creator generate "\n"))
192 (define (invoke-char s i)
194 "\n\\" s "{" (inexact->string i 10) "}" ))
197 ;; need to do something to make this really safe.
199 (define-public (output-tex-string s)
200 (if security-paranoia
201 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
204 (define (lily-def key val)
206 (regexp-substitute/global
207 #f "_" (output-tex-string key) 'pre "X" 'post))
209 (tex-val (output-tex-string val)))
210 (if (equal? (sans-surrounding-whitespace tex-val) "")
211 (string-append "\\let\\" tex-key "\\undefined\n")
212 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
214 (define (number->dim x)
216 ;;ugh ly-* in backend needs compatibility func for standalone output
217 (ly-number->string x) " \\outputscale "))
219 (define (placebox x y s)
222 (number->dim y) "}{" (number->dim x) "}{" s "}%\n"))
224 (define (bezier-bow l thick)
225 (embedded-ps (list 'bezier-bow `(quote ,l) thick)))
227 (define (bezier-sandwich l thick)
228 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
230 (define (start-system wd ht)
231 (string-append "\\leavevmode\n"
232 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
233 "\\ifundefined{lilypondscoreshift}%\n"
235 " \\advance\\scoreshift by -\\lilypondscoreshift\n"
237 "\\hbox to " (number->dim wd) "{%\n"
238 "\\lower\\scoreshift\n"
239 "\\vbox to " (number->dim ht) "{\\hbox{%\n"))
241 (define (stop-system)
242 "}\\vss}\\hss}\\interscoreline\n")
243 (define (stop-last-system)
246 (define (filledbox breapth width depth height)
247 (if (and #f (defined? 'ps-testing))
249 (string-append (numbers->string (list breapth width depth height))
252 "\\kern" (number->dim (- breapth))
253 "\\vrule width " (number->dim (+ breapth width))
254 "depth " (number->dim depth)
255 "height " (number->dim height) " ")))
257 (define (roundfilledbox x y width height blotdiam)
258 (embedded-ps (list 'roundfilledbox x y width height blotdiam)))
261 (string-append "\\hbox{" (output-tex-string s) "}"))
263 (define (tuplet ht gapx dx dy thick dir)
264 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
266 (define (draw-line thick fx fy tx ty)
267 (embedded-ps (list 'draw-line thick fx fy tx ty)))
269 (define (between-system-string string)
272 (define (define-origin file line col)
273 (if (procedure? point-and-click)
274 (string-append "\\special{src:" ;;; \\string ?
275 (point-and-click line col file)
280 ; no-origin not yet supported by Xdvi
281 (define (no-origin) "")
283 (define my-eval-in-module eval)
285 (if (or (equal? (minor-version) "4.1")
286 (equal? (minor-version) "4")
287 (equal? (minor-version) "3.4"))
288 (set! my-eval-in-module eval-in-module))
290 (define-public (tex-output-expression expr port)
291 (display (my-eval-in-module expr this-module) port )