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)))
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 (define (embedded-ps expr)
143 (with-output-to-string
144 (lambda () (ps-output-expression expr (current-output-port))))))
145 (string-append "\\embeddedps{" ps-string "}")))
148 (string-append "% " s "\n"))
152 ; uncomment for some stats about lily memory
153 ; (display (gc-stats))
159 (define (experimental-on)
162 (define (repeat-slash w a t)
163 (embedded-ps (list 'repeat-slash w a t)))
167 "\\def\\scaletounit{ "
168 (number->string (cond
169 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
170 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
171 (else (error "unknown unit" (ly:unit)))
174 "\\ifx\\lilypondstart\\undefined\n"
175 " \\input lilyponddefs\n"
177 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
179 "\\lilypondspecial\n"
180 "\\lilypondpostscript\n"))
182 ;; Note: this string must match the string in ly2dvi.py!!!
183 (define (header creator generate)
185 "% Generated automatically by: " creator generate "\n"))
187 (define (invoke-char s i)
189 "\n\\" s "{" (inexact->string i 10) "}" ))
192 ;; need to do something to make this really safe.
194 (define-public (output-tex-string s)
195 (if security-paranoia
196 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
199 (define (lily-def key val)
201 (regexp-substitute/global
202 #f "_" (output-tex-string key) 'pre "X" 'post))
204 (tex-val (output-tex-string val)))
205 (if (equal? (sans-surrounding-whitespace tex-val) "")
206 (string-append "\\let\\" tex-key "\\undefined\n")
207 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
209 (define (number->dim x)
211 ;;ugh ly:* in backend needs compatibility func for standalone output
212 (ly:number->string x) " \\outputscale "))
214 (define (placebox x y s)
215 (string-append "\\lyitem{"
216 (ly:number->string y) "}{"
217 (ly:number->string x) "}{"
220 (define (bezier-sandwich l thick)
221 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
223 (define (start-system wd ht)
224 (string-append "\\leavevmode\n"
225 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
226 "\\lilypondifundefined{lilypondscoreshift}%\n"
228 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
230 (ly:number->string wd) "}{"
231 (ly:number->string ht) "}{%\n"))
233 (define (stop-system)
234 "}%\n%\n\\interscoreline\n%\n")
235 (define (stop-last-system)
238 (define (horizontal-line x1 x2 th)
239 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
241 (define (filledbox breapth width depth height)
242 (if (and #f (defined? 'ps-testing))
244 (string-append (numbers->string (list breapth width depth height))
246 (string-append "\\lyvrule{"
247 (ly:number->string (- breapth)) "}{"
248 (ly:number->string (+ breapth width)) "}{"
249 (ly:number->string depth) "}{"
250 (ly:number->string height) "}")))
252 (define (roundfilledbox x y width height blotdiam)
253 (embedded-ps (list 'roundfilledbox x y width height blotdiam)))
256 (string-append "\\hbox{" (output-tex-string s) "}"))
258 (define (tuplet ht gapx dx dy thick dir)
259 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
261 (define (polygon points blotdiameter)
262 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
264 (define (draw-line thick fx fy tx ty)
265 (embedded-ps (list 'draw-line thick fx fy tx ty)))
267 ;; TODO: this should be a default, which is overriden in PS
268 (define (between-system-string string)
271 (define (define-origin file line col)
272 (if (procedure? point-and-click)
273 (string-append "\\special{src:" ;;; \\string ?
274 (point-and-click line col file)
279 ;; no-origin not yet supported by Xdvi
280 (define (no-origin) "")
282 (define-public (tex-output-expression expr port)
283 (display (eval expr this-module) port )