2 ;;; tex.scm -- implement Scheme output routines for TeX
4 ;;; source file of the GNU LilyPond music typesetter
6 ;;; (c) 1998--2002 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)))
87 (define (dot x y radius)
88 (embedded-ps (list 'dot x y radius)))
90 (define (beam width slope thick)
91 (embedded-ps (list 'beam width slope thick)))
93 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
94 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
96 (define (dashed-slur thick dash l)
97 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
100 (string-append "\\char" (inexact->string i 10) " "))
102 (define (dashed-line thick on off dx dy)
103 (embedded-ps (list 'dashed-line thick on off dx dy)))
105 (define (zigzag-line centre? zzw zzh thick dx dy)
106 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
108 (define (symmetric-x-triangle t w h)
109 (embedded-ps (list 'symmetric-x-triangle t w h)))
111 (define (font-load-command name-mag command)
113 "\\font\\" command "="
116 (ly:number->string (inexact->exact (* 1000 (cdr name-mag))))
119 (define (ez-ball c l b)
120 (embedded-ps (list 'ez-ball c l b)))
122 (define (header-to-file fn key val)
123 (set! key (symbol->string key))
124 (if (not (equal? "-" fn))
125 (set! fn (string-append fn "." key))
128 (format "writing header field `~a' to `~a'..."
130 (if (equal? "-" fn) "<stdout>" fn)
132 (current-error-port))
135 (display val (open-file fn "w"))
137 (display "\n" (current-error-port))
141 (if (or (equal? (minor-version) "4.1")
142 (equal? (minor-version) "4")
143 (equal? (minor-version) "3.4"))
144 (define (embedded-ps expr)
146 (with-output-to-string
147 (lambda () (ps-output-expression expr (current-output-port))))))
148 (string-append "\\embeddedps{" ps-string "}")))
149 (define (embedded-ps expr)
151 ((os (open-output-string)))
152 (ps-output-expression expr os)
153 (string-append "\\embeddedps{" (get-output-string os) "}"))))
156 (string-append "% " s "\n"))
160 ; uncomment for some stats about lily memory
161 ; (display (gc-stats))
167 (define (experimental-on)
170 (define (repeat-slash w a t)
171 (embedded-ps (list 'repeat-slash w a t)))
175 "\\def\\scaletounit{ "
176 (number->string (cond
177 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
178 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
179 (else (error "unknown unit" (ly:unit)))
182 "\\ifx\\lilypondstart\\undefined\n"
183 " \\input lilyponddefs\n"
185 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
187 "\\lilypondspecial\n"
188 "\\lilypondpostscript\n"))
190 ;; Note: this string must match the string in ly2dvi.py!!!
191 (define (header creator generate)
193 "% Generated automatically by: " creator generate "\n"))
195 (define (invoke-char s i)
197 "\n\\" s "{" (inexact->string i 10) "}" ))
200 ;; need to do something to make this really safe.
202 (define-public (output-tex-string s)
203 (if security-paranoia
204 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
207 (define (lily-def key val)
209 (regexp-substitute/global
210 #f "_" (output-tex-string key) 'pre "X" 'post))
212 (tex-val (output-tex-string val)))
213 (if (equal? (sans-surrounding-whitespace tex-val) "")
214 (string-append "\\let\\" tex-key "\\undefined\n")
215 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
217 (define (number->dim x)
219 ;;ugh ly:* in backend needs compatibility func for standalone output
220 (ly:number->string x) " \\outputscale "))
222 (define (placebox x y s)
223 (string-append "\\lyitem{"
224 (ly:number->string y) "}{"
225 (ly:number->string x) "}{"
228 (define (bezier-bow l thick)
229 (embedded-ps (list 'bezier-bow `(quote ,l) thick)))
231 (define (bezier-sandwich l thick)
232 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
234 (define (start-system wd ht)
235 (string-append "\\leavevmode\n"
236 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
237 "\\lilypondifundefined{lilypondscoreshift}%\n"
239 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
241 (ly:number->string wd) "}{"
242 (ly:number->string ht) "}{%\n"))
244 (define (stop-system)
245 "}%\n%\n\\interscoreline\n%\n")
246 (define (stop-last-system)
249 (define (filledbox breapth width depth height)
250 (if (and #f (defined? 'ps-testing))
252 (string-append (numbers->string (list breapth width depth height))
254 (string-append "\\lyvrule{"
255 (ly:number->string (- breapth)) "}{"
256 (ly:number->string (+ breapth width)) "}{"
257 (ly:number->string depth) "}{"
258 (ly:number->string height) "}")))
260 (define (roundfilledbox x y width height blotdiam)
261 (embedded-ps (list 'roundfilledbox x y width height blotdiam)))
264 (string-append "\\hbox{" (output-tex-string s) "}"))
266 (define (tuplet ht gapx dx dy thick dir)
267 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
269 (define (polygon points blotdiameter)
270 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
272 (define (draw-line thick fx fy tx ty)
273 (embedded-ps (list 'draw-line thick fx fy tx ty)))
275 (define (between-system-string string)
278 (define (define-origin file line col)
279 (if (procedure? point-and-click)
280 (string-append "\\special{src:" ;;; \\string ?
281 (point-and-click line col file)
286 ;; no-origin not yet supported by Xdvi
287 (define (no-origin) "")
289 (define-public (tex-output-expression expr port)
290 (display (eval expr this-module) port )