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)
11 ;; the public interface is tight.
12 ;; It has to be, because user-code is evalled with this module.
14 (define-module (scm output-tex)
16 #:export (font-command
43 (use-modules (ice-9 regex)
55 (define (font-command font)
58 (string-encode-integer
59 (hashq (ly:font-filename font) 1000000))
61 (string-encode-integer
62 (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
68 (define-public (symbol->tex-key sym)
69 (regexp-substitute/global
70 #f "_" (sanitize-tex-string (symbol->string sym)) 'pre "X" 'post) )
72 (define (string->param string)
73 (string-append "{" string "}"))
75 (define (number->param number)
76 (string->param (ly:number->string number)))
78 (define (number-pair->param o)
79 (string-append (number->param (car o)) (number->param (cdr o))))
81 (define-public (tex-number-def prefix key number)
83 "\\def\\" prefix (symbol->tex-key key) (string->param number) "%\n"))
89 (define (dot x y radius)
90 (embedded-ps (list 'dot x y radius)))
92 (define (beam width slope thick blot)
93 (embedded-ps (list 'beam width slope thick blot)))
95 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
96 (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick)))
98 (define (dashed-slur thick dash l)
99 (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
101 (define (char font i)
102 (string-append "\\" (font-command font)
103 "\\char" (ly:inexact->string i 10) " "))
105 (define (dashed-line thick on off dx dy)
106 (embedded-ps (list 'dashed-line thick on off dx dy)))
108 (define (zigzag-line centre? zzw zzh thick dx dy)
109 (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
111 (define (symmetric-x-triangle t w h)
112 (embedded-ps (list 'symmetric-x-triangle t w h)))
114 (define-public (font-load-command bookpaper font)
116 "\\font\\" (font-command font) "="
117 (ly:font-filename font)
119 (ly:number->string (inexact->exact
121 (ly:font-magnification font)
122 (ly:bookpaper-outputscale bookpaper)))))
125 (define (ez-ball c l b)
126 (embedded-ps (list 'ez-ball c l b)))
130 (define (embedded-ps expr)
132 (with-output-to-string
133 (lambda () (ps-output-expression expr (current-output-port))))))
134 (string-append "\\embeddedps{" ps-string "}")))
137 (string-append "% " s "\n"))
141 ;; uncomment for some stats about lily memory
142 ;; (display (gc-stats))
145 ;; Put GC stats here.
148 (define (repeat-slash w a t)
149 (embedded-ps (list 'repeat-slash w a t)))
152 (define-public (sanitize-tex-string s) ;; todo: rename
153 (if (ly:get-option 'safe)
154 (regexp-substitute/global #f "\\\\"
155 (regexp-substitute/global #f "([{}])" "bla{}" 'pre "\\" 1 'post )
156 'pre "$\\backslash$" 'post)
160 (define (lily-def key val)
162 (regexp-substitute/global
163 #f "_" (sanitize-tex-string key) 'pre "X" 'post))
165 (tex-val (sanitize-tex-string val)))
166 (if (equal? (sans-surrounding-whitespace tex-val) "")
167 (string-append "\\let\\" tex-key "\\undefined\n")
168 (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
170 (define (number->dim x)
172 ;;ugh ly:* in backend needs compatibility func for standalone output
173 (ly:number->string x) " \\outputscale "))
175 (define (placebox x y s)
177 "\\lyitem" (number->param x) (number->param y) (string->param s) "%\n"))
179 (define (bezier-sandwich l thick)
180 (embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
182 ;; WTF is this in every backend?
183 (define (horizontal-line x1 x2 th)
184 (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
186 (define (filledbox breapth width depth height)
187 (if (and #f (defined? 'ps-testing))
189 (string-append (ly:numbers->string (list breapth width depth height))
191 (string-append "\\lyvrule{"
192 (ly:number->string (- breapth)) "}{"
193 (ly:number->string (+ breapth width)) "}{"
194 (ly:number->string depth) "}{"
195 (ly:number->string height) "}")))
197 (define (round-filled-box x y width height blotdiam)
198 (embedded-ps (list 'round-filled-box x y width height blotdiam)))
200 (define (text font s)
202 ((mapping #f) ;; (assoc-get 'char-mapping (ly:font-encoding-alist font))))
205 ;; TODO: we'd better do this for PS only
206 ;; LaTeX gets in the way, and we need to remap
207 ;; nonprintable chars.
209 (input-enc-name #f) ;; (assoc-get 'input-name (ly:font-encoding-alist font) ))
212 (string-append "\\hbox{\\" (font-command font)
213 (if (string? input-enc-name)
214 (string-append "\\inputencoding{" input-enc-name "}")
217 (if (vector? mapping)
218 (reencode-string mapping s)
223 (define (tuplet ht gapx dx dy thick dir)
224 (embedded-ps (list 'tuplet ht gapx dx dy thick dir)))
226 (define (polygon points blotdiameter)
227 (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
229 (define (draw-line thick fx fy tx ty)
230 (embedded-ps (list 'draw-line thick fx fy tx ty)))
232 (define (define-origin file line col)
233 (if (procedure? point-and-click)
234 (string-append "\\special{src:" ;;; \\string ?
235 (point-and-click line col file)
239 ;; no-origin not yet supported by Xdvi
240 (define (no-origin) "")