]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-tex.scm
* scm/output-tex.scm: remove fontify; text and char take a font
[lilypond.git] / scm / output-tex.scm
1 ;;;; tex.scm -- implement Scheme output routines for TeX
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c)  1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;;                  Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8
9 ;; (debug-enable 'backtrace)
10 (define-module (scm output-tex)
11   #:re-export (quote)
12   #:export (define-fonts
13              font-command
14              unknown
15              output-paper-def
16              output-scopes
17              blank
18              dot
19              beam
20              bracket
21              dashed-slur
22              char
23              dashed-line
24              zigzag-line
25              symmetric-x-triangle
26              ez-ball
27              comment
28              end-output
29              experimental-on
30              repeat-slash
31              header-end
32              header
33              placebox
34              bezier-sandwich
35              start-system
36              stop-system
37              stop-last-system
38              horizontal-line
39              filledbox
40              round-filled-box
41              text
42              tuplet
43              polygon
44              draw-line
45              between-system-string
46              define-origin
47              no-origin
48              start-page
49              stop-page
50              )
51   )
52
53 (use-modules (ice-9 regex)
54              (ice-9 string-fun)
55              (ice-9 format)
56              (guile)
57              (srfi srfi-13)
58              (lily))
59
60 ;;;;;;;;
61 ;;;;;;;; DOCUMENT ME!
62 ;;;;;;;;
63
64
65 (define (font-command font)
66   (string-append
67    "magfont"
68    (string-encode-integer
69     (hashq (ly:font-name font) 1000000))
70    "m"
71    (string-encode-integer
72     (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
73
74 (define (define-fonts paper font-list)
75   (apply string-append
76          (map (lambda (x) (font-load-command paper x)) font-list)))
77
78 (define (unknown) 
79   "%\n\\unknown\n")
80
81 (define (symbol->tex-key sym)
82   (regexp-substitute/global
83    #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
84
85 (define (tex-string-def prefix key str)
86   (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
87       (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
88       (string-append "\\def\\" prefix (symbol->tex-key key) "{"  (output-tex-string str) "}%\n")
89       ))
90
91 (define (tex-number-def prefix key number)
92   (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
93
94 (define (output-paper-def pd)
95   (apply
96    string-append
97    (module-map
98     (lambda (sym var)
99       (let ((val (variable-ref var))
100             (key (symbol->tex-key sym)))
101
102         (cond
103          ((string? val)
104           (tex-string-def "lilypondpaper" sym val))
105          ((number? val)
106           (tex-number-def "lilypondpaper" sym
107                           (if (integer? val)
108                               (number->string val)
109                               (number->string (exact->inexact val)))))
110          (else ""))))
111       
112     (ly:output-def-scope pd))))
113
114 (define (output-scopes paper scopes fields basename)
115   (define (output-scope scope)
116     (apply
117      string-append
118      (module-map
119      (lambda (sym var)
120        (let ((val (variable-ref var))
121              ;;(val (if (variable-bound? var) (variable-ref var) '""))
122              (tex-key (symbol->string sym)))
123          
124          (if (and (memq sym fields) (string? val))
125              (header-to-file basename sym val))
126
127          (cond
128           ((string? val)
129            (tex-string-def "lilypond" sym val))
130           ((number? val)
131            (tex-number-def "lilypond" sym
132                            (if (integer? val)
133                                (number->string val)
134                                (number->string (exact->inexact val)))))
135           (else ""))))
136      scope)))
137   
138   (apply string-append
139          (map output-scope scopes)))
140
141 (define (blank)
142   "")
143
144 (define (dot x y radius)
145   (embedded-ps (list 'dot x y radius)))
146
147 (define (beam width slope thick blot)
148   (embedded-ps (list 'beam  width slope thick blot)))
149
150 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
151   (embedded-ps (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
152
153 (define (dashed-slur thick dash l)
154   (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
155
156 (define (char font i)
157   (string-append "\\" (font-command font)
158                  "\\char" (ly:inexact->string i 10) " "))
159
160 (define (dashed-line thick on off dx dy)
161   (embedded-ps (list 'dashed-line  thick on off dx dy)))
162
163 (define (zigzag-line centre? zzw zzh thick dx dy)
164   (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
165
166 (define (symmetric-x-triangle t w h)
167   (embedded-ps (list 'symmetric-x-triangle t w h)))
168
169 (define (font-load-command paper font)
170   (string-append
171    "\\font\\" (font-command font) "="
172    (ly:font-name font)
173    " scaled "
174    (ly:number->string (inexact->exact
175                        (round (* 1000
176                           (ly:font-magnification font)
177                           (ly:paper-lookup paper 'outputscale)))))
178    "\n"))
179
180 (define (ez-ball c l b)
181   (embedded-ps (list 'ez-ball  c  l b)))
182
183 (define (header-to-file fn key val)
184   (set! key (symbol->string key))
185   (if (not (equal? "-" fn))
186       (set! fn (string-append fn "." key))
187       )
188   (display
189    (format "writing header field `~a' to `~a'..."
190            key
191            (if (equal? "-" fn) "<stdout>" fn)
192            )
193    (current-error-port))
194   (if (equal? fn "-")
195       (display val)
196       (display val (open-file fn "w"))
197   )
198   (display "\n" (current-error-port))
199   ""
200   )
201
202 (define (embedded-ps expr)
203   (let ((ps-string
204          (with-output-to-string
205            (lambda () (ps-output-expression expr (current-output-port))))))
206     (string-append "\\embeddedps{" ps-string "}")))
207   
208 (define (comment s)
209   (string-append "% " s "\n"))
210
211 (define (end-output) 
212   (begin
213     ;; uncomment for some stats about lily memory         
214     ;; (display (gc-stats))
215     (string-append
216      "\\lilypondend\n"
217      ;; Put GC stats here.
218      )))
219
220 (define (experimental-on)
221   "")
222
223 (define (repeat-slash w a t)
224   (embedded-ps (list 'repeat-slash  w a t)))
225
226 (define (header-end)
227   (string-append
228    "\\def\\scaletounit{ "
229    (number->string (cond
230                      ((equal? (ly:unit) "mm") (/ 72.0  25.4))
231                      ((equal? (ly:unit) "pt") (/ 72.0  72.27))
232                      (else (error "unknown unit" (ly:unit)))
233                      ))
234    " mul }%\n"
235    "\\ifx\\lilypondstart\\undefined\n"
236    "  \\input lilyponddefs\n"
237    "\\fi\n"
238    "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
239    "\\lilypondstart\n"
240    "\\lilypondspecial\n"
241    "\\lilypondpostscript\n"))
242
243 (define (header creator time-stamp page-count)
244   (string-append
245    "% Generated by " creator "\n"
246    "% at " time-stamp "\n"
247    ;; FIXME: duplicated in every backend
248    "\\def\\lilypondtagline{Engraved by LilyPond (version "
249    (lilypond-version)")}\n"))
250
251 ;; FIXME: explain ploblem: need to do something to make this really safe.  
252 (define (output-tex-string s)
253   (if safe-mode?
254       (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
255       s))
256
257 (define (lily-def key val)
258   (let ((tex-key
259          (regexp-substitute/global
260               #f "_" (output-tex-string key) 'pre "X" 'post))
261          
262         (tex-val (output-tex-string val)))
263     (if (equal? (sans-surrounding-whitespace tex-val) "")
264         (string-append "\\let\\" tex-key "\\undefined\n")
265         (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
266
267 (define (number->dim x)
268   (string-append
269    ;;ugh ly:* in backend needs compatibility func for standalone output
270    (ly:number->string x) " \\outputscale "))
271
272 (define (placebox x y s) 
273   (string-append "\\lyitem{"
274                  (ly:number->string y) "}{"
275                  (ly:number->string x) "}{"
276                  s "}%\n"))
277
278 (define (bezier-sandwich l thick)
279   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
280
281 (define (start-system wd ht)
282   (string-append "\\leavevmode\n"
283                  "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
284                  "\\lilypondifundefined{lilypondscoreshift}%\n"
285                  "  {}%\n"
286                  "  {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
287                  "\\lybox{"
288                  (ly:number->string wd) "}{"
289                  (ly:number->string ht) "}{%\n"))
290
291 (define (stop-system) 
292   "}%\n%\n\\interscoreline\n%\n")
293 (define (stop-last-system)
294   "}%\n")
295
296 (define (horizontal-line x1 x2 th)
297   (filledbox (- x1)  (- x2 x1) (* .5 th)  (* .5 th )))
298
299 (define (filledbox breapth width depth height)
300   (if (and #f (defined? 'ps-testing))
301       (embedded-ps
302        (string-append (ly:numbers->string (list breapth width depth height))
303                       " draw_box" ))
304       (string-append "\\lyvrule{"
305                      (ly:number->string (- breapth)) "}{"
306                      (ly:number->string (+ breapth width)) "}{"
307                      (ly:number->string depth) "}{"
308                      (ly:number->string height) "}")))
309
310 (define (round-filled-box x y width height blotdiam)
311   (embedded-ps (list 'round-filled-box  x y width height blotdiam)))
312
313 (define (text font s)
314   (let*
315       ((perm (caddr (ly:font-encoding font))))
316   (string-append "\\hbox{\\" (font-command font) "{}"
317                  (output-tex-string
318                   (if (vector? perm)
319                       (reencode-string perm s)
320                       s))
321                   "}")))
322
323 (define (tuplet ht gapx dx dy thick dir)
324   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
325
326 (define (polygon points blotdiameter)
327   (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
328
329 (define (draw-line thick fx fy tx ty)
330   (embedded-ps (list 'draw-line thick fx fy tx ty)))
331
332 ;; TODO: this should be a default, which is overriden in PS
333 (define (between-system-string string)
334   string
335   )
336 (define (define-origin file line col)
337   (if (procedure? point-and-click)
338       (string-append "\\special{src:" ;;; \\string ? 
339                      (point-and-click line col file)
340                      "}" )
341       ""))
342
343 ;; no-origin not yet supported by Xdvi
344 (define (no-origin) "")
345
346 (define (start-page)
347   "\n%\\vbox{\n")
348
349 (define (stop-page last?)
350   (if last?
351       "\n%}\n"
352       "\n%}\n\\newpage\n"))