2 ;;; tex.scm -- implement Scheme output routines for TeX
4 ;;; source file of the GNU LilyPond music typesetter
6 ;;; (c) 1998--2003 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 (define-module (scm output-tex) )
11 ; (debug-enable 'backtrace)
12 (use-modules (scm output-ps)
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)))
34 (string-append "magfont"
35 (string-encode-integer
36 (hashq (car ename-mag) 1000000))
38 (string-encode-integer
39 (inexact->exact (round (* 1000 (cdr ename-mag))))))))))
41 (define (define-fonts internal-external-name-mag-pairs)
42 (set! font-name-alist (map tex-encoded-fontswitch
43 internal-external-name-mag-pairs))
46 (font-load-command (car x) (cdr x)))
47 (map cdr font-name-alist))))
51 ;; urg, how can exp be #unspecified? -- in sketch output
53 ;; set! returns #<unspecified> --hwn
54 (define (fontify name-mag-pair exp)
55 (string-append (select-font name-mag-pair)
62 (define (select-font name-mag-pair)
65 (c (assoc name-mag-pair font-name-alist))
71 (display (object-type (car name-mag-pair)))
72 (display (object-type (caaar font-name-alist)))
74 (ly:warn (string-append
75 "Programming error: No such font known "
76 (car name-mag-pair) " "
77 (ly:number->string (cdr name-mag-pair))
79 "") ; issue no command
80 (string-append "\\" (cddr c)))
88 (define (dot x y radius)
89 (embedded-ps (list 'dot x y radius)))
91 (define (beam width slope thick)
92 (embedded-ps (list 'beam width slope thick)))
94 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
95 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
97 (define (dashed-slur thick dash l)
98 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
101 (string-append "\\char" (inexact->string i 10) " "))
103 (define (dashed-line thick on off dx dy)
104 (embedded-ps (list 'dashed-line thick on off dx dy)))
106 (define (zigzag-line centre? zzw zzh thick dx dy)
107 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
109 (define (symmetric-x-triangle t w h)
110 (embedded-ps (list 'symmetric-x-triangle t w h)))
112 (define (font-load-command name-mag command)
114 "\\font\\" command "="
117 (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag)))))
120 (define (ez-ball c l b)
121 (embedded-ps (list 'ez-ball c l b)))
123 (define (header-to-file fn key val)
124 (set! key (symbol->string key))
125 (if (not (equal? "-" fn))
126 (set! fn (string-append fn "." key))
129 (format "writing header field `~a' to `~a'..."
131 (if (equal? "-" fn) "<stdout>" fn)
133 (current-error-port))
136 (display val (open-file fn "w"))
138 (display "\n" (current-error-port))
142 (define (embedded-ps expr)
144 (with-output-to-string
145 (lambda () (ps-output-expression expr (current-output-port))))))
146 (string-append "\\embeddedps{" ps-string "}")))
149 (string-append "% " s "\n"))
153 ; uncomment for some stats about lily memory
154 ; (display (gc-stats))
160 (define (experimental-on)
163 (define (repeat-slash w a t)
164 (embedded-ps (list 'repeat-slash w a t)))
168 "\\def\\scaletounit{ "
169 (number->string (cond
170 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
171 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
172 (else (error "unknown unit" (ly:unit)))
175 "\\ifx\\lilypondstart\\undefined\n"
176 " \\input lilyponddefs\n"
178 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
180 "\\lilypondspecial\n"
181 "\\lilypondpostscript\n"))
183 ;; Note: this string must match the string in lilypond.py!!!
184 (define (header creator generate)
186 "% Generated automatically by: " creator generate "\n"))
188 (define (invoke-char s i)
190 "\n\\" s "{" (inexact->string i 10) "}" ))
193 ;; need to do something to make this really safe.
195 (define-public (output-tex-string s)
196 (if security-paranoia
197 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
200 (define (lily-def key val)
202 (regexp-substitute/global
203 #f "_" (output-tex-string key) 'pre "X" 'post))
205 (tex-val (output-tex-string val)))
206 (if (equal? (sans-surrounding-whitespace tex-val) "")
207 (string-append "\\let\\" tex-key "\\undefined\n")
208 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
210 (define (number->dim x)
212 ;;ugh ly:* in backend needs compatibility func for standalone output
213 (ly:number->string x) " \\outputscale "))
215 (define (placebox x y s)
216 (string-append "\\lyitem{"
217 (ly:number->string y) "}{"
218 (ly:number->string x) "}{"
221 (define (bezier-sandwich l thick)
222 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
224 (define (start-system wd ht)
225 (string-append "\\leavevmode\n"
226 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
227 "\\lilypondifundefined{lilypondscoreshift}%\n"
229 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
231 (ly:number->string wd) "}{"
232 (ly:number->string ht) "}{%\n"))
234 (define (stop-system)
235 "}%\n%\n\\interscoreline\n%\n")
236 (define (stop-last-system)
239 (define (horizontal-line x1 x2 th)
240 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
242 (define (filledbox breapth width depth height)
243 (if (and #f (defined? 'ps-testing))
245 (string-append (numbers->string (list breapth width depth height))
247 (string-append "\\lyvrule{"
248 (ly:number->string (- breapth)) "}{"
249 (ly:number->string (+ breapth width)) "}{"
250 (ly:number->string depth) "}{"
251 (ly:number->string height) "}")))
253 (define (round-filled-box x y width height blotdiam)
254 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
257 (string-append "\\hbox{" (output-tex-string s) "}"))
259 (define (tuplet ht gapx dx dy thick dir)
260 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
262 (define (polygon points blotdiameter)
263 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
265 (define (draw-line thick fx fy tx ty)
266 (embedded-ps (list 'draw-line thick fx fy tx ty)))
268 ;; TODO: this should be a default, which is overriden in PS
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-public (tex-output-expression expr port)
284 (display (eval expr this-module) port ))