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
46 (use-modules (ice-9 regex)
58 (define (font-command font)
61 (string-encode-integer
62 (hashq (ly:font-filename font) 1000000))
64 (string-encode-integer
65 (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
70 (define-public (symbol->tex-key sym)
71 (regexp-substitute/global
72 #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
74 (define (string->param string)
75 (string-append "{" string "}"))
77 (define (number->param number)
78 (string->param (ly:number->string number)))
80 (define (number-pair->param o)
81 (string-append (number->param (car o)) (number->param (cdr o))))
83 (define-public (tex-number-def prefix key number)
85 "\\def\\" prefix (symbol->tex-key key) (string->param number) "%\n"))
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))))
103 (define (char font i)
104 (string-append "\\" (font-command font)
105 "\\char" (ly:inexact->string i 10) " "))
107 (define (dashed-line thick on off dx dy)
108 (embedded-ps (list 'dashed-line thick on off dx dy)))
110 (define (zigzag-line centre? zzw zzh thick dx dy)
111 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
113 (define (symmetric-x-triangle t w h)
114 (embedded-ps (list 'symmetric-x-triangle t w h)))
116 (define-public (font-load-command bookpaper font)
118 "\\font\\" (font-command font) "="
119 (ly:font-filename font)
121 (ly:number->string (inexact->exact
123 (ly:font-magnification font)
124 (ly:bookpaper-outputscale bookpaper)))))
127 (define (ez-ball c l b)
128 (embedded-ps (list 'ez-ball c l b)))
130 (define (header-to-file fn key val)
131 (set! key (symbol->string key))
132 (if (not (equal? "-" fn))
133 (set! fn (string-append fn "." key))
136 (format "Writing header field `~a' to `~a'..."
138 (if (equal? "-" fn) "<stdout>" fn)
140 (current-error-port))
143 (display val (open-file fn "w"))
145 (display "\n" (current-error-port))
149 (define (embedded-ps expr)
151 (with-output-to-string
152 (lambda () (ps-output-expression expr (current-output-port))))))
153 (string-append "\\embeddedps{" ps-string "}")))
156 (string-append "% " s "\n"))
160 ;; uncomment for some stats about lily memory
161 ;; (display (gc-stats))
164 ;; Put GC stats here.
167 (define (repeat-slash w a t)
168 (embedded-ps (list 'repeat-slash w a t)))
171 (define-public (output-tex-string s) ;; todo: rename
172 (if (ly:get-option 'safe)
173 (regexp-substitute/global #f "\\\\"
174 (regexp-substitute/global #f "([{}])" "bla{}" 'pre "\\" 1 'post )
175 'pre "$\\backslash$" 'post)
179 (define (lily-def key val)
181 (regexp-substitute/global
182 #f "_" (output-tex-string key) 'pre "X" 'post))
184 (tex-val (output-tex-string val)))
185 (if (equal? (sans-surrounding-whitespace tex-val) "")
186 (string-append "\\let\\" tex-key "\\undefined\n")
187 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
189 (define (number->dim x)
191 ;;ugh ly:* in backend needs compatibility func for standalone output
192 (ly:number->string x) " \\outputscale "))
194 (define (placebox x y s)
196 "\\lyitem" (number->param x) (number->param y) (string->param s) "%\n"))
198 (define (bezier-sandwich l thick)
199 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
201 ;; WTF is this in every backend?
202 (define (horizontal-line x1 x2 th)
203 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
205 (define (filledbox breapth width depth height)
206 (if (and #f (defined? 'ps-testing))
208 (string-append (ly:numbers->string (list breapth width depth height))
210 (string-append "\\lyvrule{"
211 (ly:number->string (- breapth)) "}{"
212 (ly:number->string (+ breapth width)) "}{"
213 (ly:number->string depth) "}{"
214 (ly:number->string height) "}")))
216 (define (round-filled-box x y width height blotdiam)
217 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
219 (define (text font s)
221 ((mapping #f) ;; (assoc-get 'char-mapping (ly:font-encoding-alist font))))
224 ;; TODO: we'd better do this for PS only
225 ;; LaTeX gets in the way, and we need to remap
226 ;; nonprintable chars.
229 (input-enc-name #f) ;; (assoc-get 'input-name (ly:font-encoding-alist font) ))
232 (string-append "\\hbox{\\" (font-command font)
233 (if (string? input-enc-name)
234 (string-append "\\inputencoding{" input-enc-name "}")
237 (if (vector? mapping)
238 (reencode-string mapping s)
243 (define (tuplet ht gapx dx dy thick dir)
244 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
246 (define (polygon points blotdiameter)
247 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
249 (define (draw-line thick fx fy tx ty)
250 (embedded-ps (list 'draw-line thick fx fy tx ty)))
252 (define (define-origin file line col)
253 (if (procedure? point-and-click)
254 (string-append "\\special{src:" ;;; \\string ?
255 (point-and-click line col file)
259 ;; no-origin not yet supported by Xdvi
260 (define (no-origin) "")