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)
12 #:export (define-fonts
53 (use-modules (ice-9 regex)
64 (define font-name-alist '())
66 (define (tex-encoded-fontswitch name-mag)
67 (let* ((iname-mag (car name-mag))
68 (ename-mag (cdr name-mag)))
72 (string-append "magfont"
73 (string-encode-integer
74 (hashq (car ename-mag) 1000000))
76 (string-encode-integer
77 (inexact->exact (round (* 1000 (cdr ename-mag))))))))))
79 (define-public (define-fonts internal-external-name-mag-pairs)
80 (set! font-name-alist (map tex-encoded-fontswitch
81 internal-external-name-mag-pairs))
84 (font-load-command (car x) (cdr x)))
85 (map cdr font-name-alist))))
88 ;; urg, how can exp be #unspecified? -- in sketch output
90 ;; set! returns #<unspecified> --hwn
92 (define-public (fontify name-mag-pair exp)
93 (string-append (select-font name-mag-pair)
97 (define-public (unknown)
100 (define (symbol->tex-key sym)
101 (regexp-substitute/global
102 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
104 (define (tex-string-def prefix key str)
105 (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
106 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
107 (string-append "\\def\\" prefix (symbol->tex-key key) "{" (output-tex-string str) "}%\n")
110 (define (tex-number-def prefix key number)
111 (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
113 (define (output-paper-def pd)
118 (let ((val (variable-ref var))
119 (key (symbol->tex-key sym)))
123 (tex-string-def "lilypondpaper" sym val))
125 (tex-number-def "lilypondpaper" sym
128 (number->string (exact->inexact val)))))
131 (ly:output-def-scope pd))))
133 (define (output-scopes paper scopes fields basename)
134 (define (output-scope scope)
139 (let ((val (variable-ref var))
140 ;;(val (if (variable-bound? var) (variable-ref var) '""))
141 (tex-key (symbol->string sym)))
143 (if (and (memq sym fields) (string? val))
144 (header-to-file basename sym val))
148 (tex-string-def "lilypond" sym val))
150 (tex-number-def "lilypond" sym
153 (number->string (exact->inexact val)))))
158 (map output-scope scopes)))
160 (define (select-font name-mag-pair)
161 (let ((c (assoc name-mag-pair font-name-alist)))
163 (string-append "\\" (cddr c))
166 (format "Programming error: No such font: ~S" name-mag-pair))
172 (define (dot x y radius)
173 (embedded-ps (list 'dot x y radius)))
175 (define (beam width slope thick blot)
176 (embedded-ps (list 'beam width slope thick blot)))
178 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
179 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
181 (define (dashed-slur thick dash l)
182 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
185 (string-append "\\char" (ly:inexact->string i 10) " "))
187 (define (dashed-line thick on off dx dy)
188 (embedded-ps (list 'dashed-line thick on off dx dy)))
190 (define (zigzag-line centre? zzw zzh thick dx dy)
191 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
193 (define (symmetric-x-triangle t w h)
194 (embedded-ps (list 'symmetric-x-triangle t w h)))
196 (define (font-load-command name-mag command)
198 "\\font\\" command "="
201 (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag)))))
204 (define (ez-ball c l b)
205 (embedded-ps (list 'ez-ball c l b)))
207 (define (header-to-file fn key val)
208 (set! key (symbol->string key))
209 (if (not (equal? "-" fn))
210 (set! fn (string-append fn "." key))
213 (format "writing header field `~a' to `~a'..."
215 (if (equal? "-" fn) "<stdout>" fn)
217 (current-error-port))
220 (display val (open-file fn "w"))
222 (display "\n" (current-error-port))
226 (define (embedded-ps expr)
228 (with-output-to-string
229 (lambda () (ps-output-expression expr (current-output-port))))))
230 (string-append "\\embeddedps{" ps-string "}")))
233 (string-append "% " s "\n"))
237 ;; uncomment for some stats about lily memory
238 ;; (display (gc-stats))
241 ;; Put GC stats here.
244 (define (experimental-on)
247 (define (repeat-slash w a t)
248 (embedded-ps (list 'repeat-slash w a t)))
252 "\\def\\scaletounit{ "
253 (number->string (cond
254 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
255 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
256 (else (error "unknown unit" (ly:unit)))
259 "\\ifx\\lilypondstart\\undefined\n"
260 " \\input lilyponddefs\n"
262 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
264 "\\lilypondspecial\n"
265 "\\lilypondpostscript\n"))
267 (define (header creator time-stamp page-count)
269 "% Generated by " creator "\n"
270 "% at " time-stamp "\n"
271 ;; FIXME: duplicated in every backend
272 "\\def\\lilypondtagline{Engraved by LilyPond (version "
273 (lilypond-version)")}\n"))
275 (define (invoke-char s i)
277 "\n\\" s "{" (ly:inexact->string i 10) "}" ))
279 ;; FIXME: explain ploblem: need to do something to make this really safe.
280 (define-public (output-tex-string s)
282 (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
285 (define (lily-def key val)
287 (regexp-substitute/global
288 #f "_" (output-tex-string key) 'pre "X" 'post))
290 (tex-val (output-tex-string val)))
291 (if (equal? (sans-surrounding-whitespace tex-val) "")
292 (string-append "\\let\\" tex-key "\\undefined\n")
293 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
295 (define (number->dim x)
297 ;;ugh ly:* in backend needs compatibility func for standalone output
298 (ly:number->string x) " \\outputscale "))
300 (define (placebox x y s)
301 (string-append "\\lyitem{"
302 (ly:number->string y) "}{"
303 (ly:number->string x) "}{"
306 (define (bezier-sandwich l thick)
307 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
309 (define (start-system wd ht)
310 (string-append "\\leavevmode\n"
311 "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
312 "\\lilypondifundefined{lilypondscoreshift}%\n"
314 " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
316 (ly:number->string wd) "}{"
317 (ly:number->string ht) "}{%\n"))
319 (define (stop-system)
320 "}%\n%\n\\interscoreline\n%\n")
321 (define (stop-last-system)
324 (define (horizontal-line x1 x2 th)
325 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th )))
327 (define (filledbox breapth width depth height)
328 (if (and #f (defined? 'ps-testing))
330 (string-append (ly:numbers->string (list breapth width depth height))
332 (string-append "\\lyvrule{"
333 (ly:number->string (- breapth)) "}{"
334 (ly:number->string (+ breapth width)) "}{"
335 (ly:number->string depth) "}{"
336 (ly:number->string height) "}")))
338 (define (round-filled-box x y width height blotdiam)
339 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
342 (string-append "\\hbox{" (output-tex-string s) "}"))
344 (define (tuplet ht gapx dx dy thick dir)
345 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
347 (define (polygon points blotdiameter)
348 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
350 (define (draw-line thick fx fy tx ty)
351 (embedded-ps (list 'draw-line thick fx fy tx ty)))
353 ;; TODO: this should be a default, which is overriden in PS
354 (define (between-system-string string)
357 (define (define-origin file line col)
358 (if (procedure? point-and-click)
359 (string-append "\\special{src:" ;;; \\string ?
360 (point-and-click line col file)
365 ;; no-origin not yet supported by Xdvi
366 (define (no-origin) "")
371 (define (stop-page last?)
374 "\n%}\n\\newpage\n"))