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)
19 (define this-module (current-module))
25 (define font-name-alist '())
27 (define (tex-encoded-fontswitch name-mag)
28 (let* ((iname-mag (car name-mag))
29 (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 (round (* 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))
69 (ly:warn (string-append
70 "Programming error: No such font known "
71 (car name-mag-pair) " "
72 (ly:number->string (cdr name-mag-pair))))
74 (display "FAILED\n" (current-error-port))
75 (if #f ;(pair? name-mag-pair))
76 (display (object-type (car name-mag-pair)) (current-error-port))
77 (write name-mag-pair (current-error-port)))
78 (if #f ; (pair? font-name-alist)
80 (object-type (caaar font-name-alist)) (current-error-port))
81 (write font-name-alist (current-error-port)))
83 ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
86 (string-append "\\" (cddr c)))))
91 (define (dot x y radius)
92 (embedded-ps (list 'dot x y radius)))
94 (define (beam width slope thick blot)
95 (embedded-ps (list 'beam width slope thick blot)))
97 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
98 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
100 (define (dashed-slur thick dash l)
101 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
104 (string-append "\\char" (inexact->string i 10) " "))
106 (define (dashed-line thick on off dx dy)
107 (embedded-ps (list 'dashed-line thick on off dx dy)))
109 (define (zigzag-line centre? zzw zzh thick dx dy)
110 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
112 (define (symmetric-x-triangle t w h)
113 (embedded-ps (list 'symmetric-x-triangle t w h)))
115 (define (font-load-command name-mag command)
117 "\\font\\" command "="
120 (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag)))))
123 (define (ez-ball c l b)
124 (embedded-ps (list 'ez-ball c l b)))
126 (define (header-to-file fn key val)
127 (set! key (symbol->string key))
128 (if (not (equal? "-" fn))
129 (set! fn (string-append fn "." key))
132 (format "writing header field `~a' to `~a'..."
134 (if (equal? "-" fn) "<stdout>" fn)
136 (current-error-port))
139 (display val (open-file fn "w"))
141 (display "\n" (current-error-port))
145 (define (embedded-ps expr)
147 (with-output-to-string
148 (lambda () (ps-output-expression expr (current-output-port))))))
149 (string-append "\\embeddedps{" ps-string "}")))
152 (string-append "% " s "\n"))
156 ; uncomment for some stats about lily memory
157 ; (display (gc-stats))
163 (define (experimental-on)
166 (define (repeat-slash w a t)
167 (embedded-ps (list 'repeat-slash w a t)))
171 "\\def\\scaletounit{ "
172 (number->string (cond
173 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
174 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
175 (else (error "unknown unit" (ly:unit)))
178 "\\ifx\\lilypondstart\\undefined\n"
179 " \\input lilyponddefs\n"
181 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
183 "\\lilypondspecial\n"
184 "\\lilypondpostscript\n"))
186 ;; Note: this string must match the string in lilypond.py!!!
187 (define (header creator generate)
189 "% Generated automatically by: " creator generate "\n"))
191 (define (invoke-char s i)
193 "\n\\" s "{" (inexact->string i 10) "}" ))
196 ;; need to do something to make this really safe.
198 (define-public (output-tex-string s)
199 (if security-paranoia
200 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
203 (define (lily-def key val)
205 (regexp-substitute/global
206 #f "_" (output-tex-string key) 'pre "X" 'post))
208 (tex-val (output-tex-string val)))
209 (if (equal? (sans-surrounding-whitespace tex-val) "")
210 (string-append "\\let\\" tex-key "\\undefined\n")
211 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
213 (define (number->dim x)
215 ;;ugh ly:* in backend needs compatibility func for standalone output
216 (ly:number->string x) " \\outputscale "))
218 (define (placebox x y s)
219 (string-append "\\lyitem{"
220 (ly:number->string y) "}{"
221 (ly:number->string x) "}{"
224 (define (bezier-sandwich l thick)
225 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
227 (define (start-system wd ht)
228 (string-append "\\leavevmode\n"
229 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
230 "\\lilypondifundefined{lilypondscoreshift}%\n"
232 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
234 (ly:number->string wd) "}{"
235 (ly:number->string ht) "}{%\n"))
237 (define (stop-system)
238 "}%\n%\n\\interscoreline\n%\n")
239 (define (stop-last-system)
242 (define (horizontal-line x1 x2 th)
243 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
245 (define (filledbox breapth width depth height)
246 (if (and #f (defined? 'ps-testing))
248 (string-append (numbers->string (list breapth width depth height))
250 (string-append "\\lyvrule{"
251 (ly:number->string (- breapth)) "}{"
252 (ly:number->string (+ breapth width)) "}{"
253 (ly:number->string depth) "}{"
254 (ly:number->string height) "}")))
256 (define (round-filled-box x y width height blotdiam)
257 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
260 (string-append "\\hbox{" (output-tex-string s) "}"))
262 (define (tuplet ht gapx dx dy thick dir)
263 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
265 (define (polygon points blotdiameter)
266 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
268 (define (draw-line thick fx fy tx ty)
269 (embedded-ps (list 'draw-line thick fx fy tx ty)))
271 ;; TODO: this should be a default, which is overriden in PS
272 (define (between-system-string string)
275 (define (define-origin file line col)
276 (if (procedure? point-and-click)
277 (string-append "\\special{src:" ;;; \\string ?
278 (point-and-click line col file)
283 ;; no-origin not yet supported by Xdvi
284 (define (no-origin) "")
286 (define-public (tex-output-expression expr port)
287 (display (eval expr this-module) port ))