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 ;; (debug-enable 'backtrace)
10 (define-module (scm output-tex))
11 (use-modules (ice-9 regex)
18 (define this-module (current-module))
20 ;; dumper-compatibility
22 (define (ps-output-expression expr port)
24 (let ((ps-module (resolve-module '(scm output-ps))))
25 (eval '(use-modules (guile) (ice-9 regex) (srfi srfi-13) (lily))
27 (set! output-ps ps-module)))
28 (display (eval expr output-ps) port))
30 ;;; Output interface entry
31 (define-public (tex-output-expression expr port)
32 (display (eval expr this-module) port ))
38 (define font-name-alist '())
40 (define (tex-encoded-fontswitch name-mag)
41 (let* ((iname-mag (car name-mag))
42 (ename-mag (cdr name-mag)))
46 (string-append "magfont"
47 (string-encode-integer
48 (hashq (car ename-mag) 1000000))
50 (string-encode-integer
51 (inexact->exact (round (* 1000 (cdr ename-mag))))))))))
53 (define (define-fonts internal-external-name-mag-pairs)
54 (set! font-name-alist (map tex-encoded-fontswitch
55 internal-external-name-mag-pairs))
58 (font-load-command (car x) (cdr x)))
59 (map cdr font-name-alist))))
62 ;; urg, how can exp be #unspecified? -- in sketch output
64 ;; set! returns #<unspecified> --hwn
66 (define (fontify name-mag-pair exp)
67 (string-append (select-font name-mag-pair)
74 (define (symbol->tex-key sym)
75 (regexp-substitute/global
76 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
78 (define (tex-string-def prefix key str)
79 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
80 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
81 (string-append "\\def\\" prefix (symbol->tex-key key) "{" (output-tex-string str) "}%\n")
84 (define (tex-number-def prefix key number)
85 (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
87 (define (output-paper-def pd)
92 (let ((val (variable-ref var))
93 (key (symbol->tex-key sym)))
97 (tex-string-def "lilypondpaper" sym val))
99 (tex-number-def "lilypondpaper" sym
102 (number->string (exact->inexact val)))))
105 (ly:output-def-scope pd))))
107 (define (output-scopes paper scopes fields basename)
108 (define (output-scope scope)
113 (let ((val (variable-ref var))
114 ;;(val (if (variable-bound? var) (variable-ref var) '""))
115 (tex-key (symbol->string sym)))
117 (if (and (memq sym fields) (string? val))
118 (header-to-file basename sym val))
122 (tex-string-def "lilypond" sym val))
124 (tex-number-def "lilypond" sym
127 (number->string (exact->inexact val)))))
132 (map output-scope scopes)))
134 (define (select-font name-mag-pair)
135 (let ((c (assoc name-mag-pair font-name-alist)))
137 (string-append "\\" (cddr c))
140 (format "Programming error: No such font: ~S" name-mag-pair))
146 (define (dot x y radius)
147 (embedded-ps (list 'dot x y radius)))
149 (define (beam width slope thick blot)
150 (embedded-ps (list 'beam width slope thick blot)))
152 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
153 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
155 (define (dashed-slur thick dash l)
156 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
159 (string-append "\\char" (inexact->string i 10) " "))
161 (define (dashed-line thick on off dx dy)
162 (embedded-ps (list 'dashed-line thick on off dx dy)))
164 (define (zigzag-line centre? zzw zzh thick dx dy)
165 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
167 (define (symmetric-x-triangle t w h)
168 (embedded-ps (list 'symmetric-x-triangle t w h)))
170 (define (font-load-command name-mag command)
172 "\\font\\" command "="
175 (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag)))))
178 (define (ez-ball c l b)
179 (embedded-ps (list 'ez-ball c l b)))
181 (define (header-to-file fn key val)
182 (set! key (symbol->string key))
183 (if (not (equal? "-" fn))
184 (set! fn (string-append fn "." key))
187 (format "writing header field `~a' to `~a'..."
189 (if (equal? "-" fn) "<stdout>" fn)
191 (current-error-port))
194 (display val (open-file fn "w"))
196 (display "\n" (current-error-port))
200 (define (embedded-ps expr)
202 (with-output-to-string
203 (lambda () (ps-output-expression expr (current-output-port))))))
204 (string-append "\\embeddedps{" ps-string "}")))
207 (string-append "% " s "\n"))
211 ;; uncomment for some stats about lily memory
212 ;; (display (gc-stats))
215 ;; Put GC stats here.
218 (define (experimental-on)
221 (define (repeat-slash w a t)
222 (embedded-ps (list 'repeat-slash w a t)))
226 "\\def\\scaletounit{ "
227 (number->string (cond
228 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
229 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
230 (else (error "unknown unit" (ly:unit)))
233 "\\ifx\\lilypondstart\\undefined\n"
234 " \\input lilyponddefs\n"
236 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
238 "\\lilypondspecial\n"
239 "\\lilypondpostscript\n"))
241 (define (header creator time-stamp page-count)
243 "% Generated by " creator "\n"
244 "% at " time-stamp "\n"
245 ;; FIXME: duplicated in every backend
246 "\\def\\lilypondtagline{Engraved by LilyPond (version "
247 (lilypond-version)")}\n"))
249 (define (invoke-char s i)
251 "\n\\" s "{" (inexact->string i 10) "}" ))
253 ;; FIXME: explain ploblem: need to do something to make this really safe.
254 (define-public (output-tex-string s)
256 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
259 (define (lily-def key val)
261 (regexp-substitute/global
262 #f "_" (output-tex-string key) 'pre "X" 'post))
264 (tex-val (output-tex-string val)))
265 (if (equal? (sans-surrounding-whitespace tex-val) "")
266 (string-append "\\let\\" tex-key "\\undefined\n")
267 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
269 (define (number->dim x)
271 ;;ugh ly:* in backend needs compatibility func for standalone output
272 (ly:number->string x) " \\outputscale "))
274 (define (placebox x y s)
275 (string-append "\\lyitem{"
276 (ly:number->string y) "}{"
277 (ly:number->string x) "}{"
280 (define (bezier-sandwich l thick)
281 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
283 (define (start-system wd ht)
284 (string-append "\\leavevmode\n"
285 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
286 "\\lilypondifundefined{lilypondscoreshift}%\n"
288 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
290 (ly:number->string wd) "}{"
291 (ly:number->string ht) "}{%\n"))
293 (define (stop-system)
294 "}%\n%\n\\interscoreline\n%\n")
295 (define (stop-last-system)
298 (define (horizontal-line x1 x2 th)
299 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
301 (define (filledbox breapth width depth height)
302 (if (and #f (defined? 'ps-testing))
304 (string-append (numbers->string (list breapth width depth height))
306 (string-append "\\lyvrule{"
307 (ly:number->string (- breapth)) "}{"
308 (ly:number->string (+ breapth width)) "}{"
309 (ly:number->string depth) "}{"
310 (ly:number->string height) "}")))
312 (define (round-filled-box x y width height blotdiam)
313 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
316 (string-append "\\hbox{" (output-tex-string s) "}"))
318 (define (tuplet ht gapx dx dy thick dir)
319 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
321 (define (polygon points blotdiameter)
322 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
324 (define (draw-line thick fx fy tx ty)
325 (embedded-ps (list 'draw-line thick fx fy tx ty)))
327 ;; TODO: this should be a default, which is overriden in PS
328 (define (between-system-string string)
331 (define (define-origin file line col)
332 (if (procedure? point-and-click)
333 (string-append "\\special{src:" ;;; \\string ?
334 (point-and-click line col file)
339 ;; no-origin not yet supported by Xdvi
340 (define (no-origin) "")
345 (define (stop-page last?)
348 "\n%}\n\\newpage\n"))