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
32 (define-public (tex-output-expression expr port)
33 (display (eval expr this-module) port ))
39 (define font-name-alist '())
41 (define (tex-encoded-fontswitch name-mag)
42 (let* ((iname-mag (car name-mag))
43 (ename-mag (cdr name-mag)))
47 (string-append "magfont"
48 (string-encode-integer
49 (hashq (car ename-mag) 1000000))
51 (string-encode-integer
52 (inexact->exact (round (* 1000 (cdr ename-mag))))))))))
54 (define (define-fonts internal-external-name-mag-pairs)
55 (set! font-name-alist (map tex-encoded-fontswitch
56 internal-external-name-mag-pairs))
59 (font-load-command (car x) (cdr x)))
60 (map cdr font-name-alist))))
63 ;; urg, how can exp be #unspecified? -- in sketch output
65 ;; set! returns #<unspecified> --hwn
67 (define (fontify name-mag-pair exp)
68 (string-append (select-font name-mag-pair)
75 (define (symbol->tex-key sym)
76 (regexp-substitute/global
77 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
79 (define (tex-string-def prefix key str)
80 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
81 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
82 (string-append "\\def\\" prefix (symbol->tex-key key) "{" (output-tex-string str) "}%\n")
85 (define (tex-number-def prefix key number)
86 (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
88 (define (output-paper-def pd)
93 (let ((val (variable-ref var))
94 (key (symbol->tex-key sym)))
98 (tex-string-def "lilypondpaper" sym val))
100 (tex-number-def "lilypondpaper" sym
103 (number->string (exact->inexact val)))))
106 (ly:output-def-scope pd))))
108 (define (output-scopes paper scopes fields basename)
109 (define (output-scope scope)
114 (let ((val (variable-ref var))
115 ;;(val (if (variable-bound? var) (variable-ref var) '""))
116 (tex-key (symbol->string sym)))
118 (if (and (memq sym fields) (string? val))
119 (header-to-file basename sym val))
123 (tex-string-def "lilypond" sym val))
125 (tex-number-def "lilypond" sym
128 (number->string (exact->inexact val)))))
133 (map output-scope scopes)))
135 (define (select-font name-mag-pair)
136 (let ((c (assoc name-mag-pair font-name-alist)))
138 (string-append "\\" (cddr c))
141 (format "Programming error: No such font: ~S" name-mag-pair))
147 (define (dot x y radius)
148 (embedded-ps (list 'dot x y radius)))
150 (define (beam width slope thick blot)
151 (embedded-ps (list 'beam width slope thick blot)))
153 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
154 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
156 (define (dashed-slur thick dash l)
157 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
160 (string-append "\\char" (inexact->string i 10) " "))
162 (define (dashed-line thick on off dx dy)
163 (embedded-ps (list 'dashed-line thick on off dx dy)))
165 (define (zigzag-line centre? zzw zzh thick dx dy)
166 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
168 (define (symmetric-x-triangle t w h)
169 (embedded-ps (list 'symmetric-x-triangle t w h)))
171 (define (font-load-command name-mag command)
173 "\\font\\" command "="
176 (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag)))))
179 (define (ez-ball c l b)
180 (embedded-ps (list 'ez-ball c l b)))
182 (define (header-to-file fn key val)
183 (set! key (symbol->string key))
184 (if (not (equal? "-" fn))
185 (set! fn (string-append fn "." key))
188 (format "writing header field `~a' to `~a'..."
190 (if (equal? "-" fn) "<stdout>" fn)
192 (current-error-port))
195 (display val (open-file fn "w"))
197 (display "\n" (current-error-port))
201 (define (embedded-ps expr)
203 (with-output-to-string
204 (lambda () (ps-output-expression expr (current-output-port))))))
205 (string-append "\\embeddedps{" ps-string "}")))
208 (string-append "% " s "\n"))
212 ;; uncomment for some stats about lily memory
213 ;; (display (gc-stats))
216 ;; Put GC stats here.
219 (define (experimental-on)
222 (define (repeat-slash w a t)
223 (embedded-ps (list 'repeat-slash w a t)))
227 "\\def\\scaletounit{ "
228 (number->string (cond
229 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
230 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
231 (else (error "unknown unit" (ly:unit)))
234 "\\ifx\\lilypondstart\\undefined\n"
235 " \\input lilyponddefs\n"
237 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
239 "\\lilypondspecial\n"
240 "\\lilypondpostscript\n"))
242 (define (header creator time-stamp page-count)
244 "% Generated by " creator "\n"
245 "% at " time-stamp "\n"
246 ;; FIXME: duplicated in every backend
247 "\\def\\lilypondtagline{Engraved by LilyPond (version "
248 (lilypond-version)")}\n"))
250 (define (invoke-char s i)
252 "\n\\" s "{" (inexact->string i 10) "}" ))
254 ;; FIXME: explain ploblem: need to do something to make this really safe.
255 (define-public (output-tex-string s)
257 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
260 (define (lily-def key val)
262 (regexp-substitute/global
263 #f "_" (output-tex-string key) 'pre "X" 'post))
265 (tex-val (output-tex-string val)))
266 (if (equal? (sans-surrounding-whitespace tex-val) "")
267 (string-append "\\let\\" tex-key "\\undefined\n")
268 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
270 (define (number->dim x)
272 ;;ugh ly:* in backend needs compatibility func for standalone output
273 (ly:number->string x) " \\outputscale "))
275 (define (placebox x y s)
276 (string-append "\\lyitem{"
277 (ly:number->string y) "}{"
278 (ly:number->string x) "}{"
281 (define (bezier-sandwich l thick)
282 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
284 (define (start-system wd ht)
285 (string-append "\\leavevmode\n"
286 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
287 "\\lilypondifundefined{lilypondscoreshift}%\n"
289 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
291 (ly:number->string wd) "}{"
292 (ly:number->string ht) "}{%\n"))
294 (define (stop-system)
295 "}%\n%\n\\interscoreline\n%\n")
296 (define (stop-last-system)
299 (define (horizontal-line x1 x2 th)
300 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
302 (define (filledbox breapth width depth height)
303 (if (and #f (defined? 'ps-testing))
305 (string-append (numbers->string (list breapth width depth height))
307 (string-append "\\lyvrule{"
308 (ly:number->string (- breapth)) "}{"
309 (ly:number->string (+ breapth width)) "}{"
310 (ly:number->string depth) "}{"
311 (ly:number->string height) "}")))
313 (define (round-filled-box x y width height blotdiam)
314 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
317 (string-append "\\hbox{" (output-tex-string s) "}"))
319 (define (tuplet ht gapx dx dy thick dir)
320 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
322 (define (polygon points blotdiameter)
323 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
325 (define (draw-line thick fx fy tx ty)
326 (embedded-ps (list 'draw-line thick fx fy tx ty)))
328 ;; TODO: this should be a default, which is overriden in PS
329 (define (between-system-string string)
332 (define (define-origin file line col)
333 (if (procedure? point-and-click)
334 (string-append "\\special{src:" ;;; \\string ?
335 (point-and-click line col file)
340 ;; no-origin not yet supported by Xdvi
341 (define (no-origin) "")
346 (define (stop-page last?)
349 "\n%}\n\\newpage\n"))