]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-tex.scm
* lily/parser.yy (book_body): set default bookpaper.
[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              define-origin
46              no-origin
47              start-page
48              stop-page
49              ))
50
51 (use-modules (ice-9 regex)
52              (ice-9 string-fun)
53              (ice-9 format)
54              (guile)
55              (srfi srfi-13)
56              (lily))
57
58 ;;;;;;;;
59 ;;;;;;;; DOCUMENT ME!
60 ;;;;;;;;
61
62
63 (define (font-command font)
64   (string-append
65    "magfont"
66    (string-encode-integer
67     (hashq (ly:font-filename font) 1000000))
68    "m"
69    (string-encode-integer
70     (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
71
72 (define (define-fonts bookpaper)
73   (string-append
74    "\\def\\lilypondpaperunit{mm}" ;; UGH. FIXME. 
75    (tex-number-def "lilypondpaper" 'outputscale
76                    (number->string (exact->inexact
77                                     (ly:bookpaper-outputscale bookpaper))))
78    (tex-string-def "lilypondpapersize" 'papersize
79                    (eval 'papersize (ly:bookpaper-def-scope bookpaper)))
80    (apply string-append
81           (map (lambda (x) (font-load-command bookpaper x))
82                (ly:bookpaper-fonts bookpaper)
83                )))
84   )
85 (define (unknown) 
86   "%\n\\unknown\n")
87
88 (define (symbol->tex-key sym)
89   (regexp-substitute/global
90    #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
91
92 (define (string->param string)
93   (string-append "{" string "}"))
94
95 (define (number->param number)
96   (string->param (ly:number->string number)))
97
98 (define (number-pair->param o)
99   (string-append (number->param (car o)) (number->param (cdr o))))
100
101 (define (tex-string-def prefix key str)
102   (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
103       (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
104       (string-append "\\def\\" prefix (symbol->tex-key key)
105                      "{" (output-tex-string str) "}%\n")))
106
107 (define (tex-number-def prefix key number)
108   (string-append
109    "\\def\\" prefix (symbol->tex-key key) (string->param number) "%\n"))
110
111 (define (output-paper-def paper)
112       (apply
113        string-append
114        (module-map
115         (lambda (sym var)
116           (let ((val (variable-ref var))
117                 (key (symbol->tex-key sym)))
118
119             (cond
120              ((string? val)
121               (tex-string-def "lilypondpaper" sym val))
122              ((number? val)
123               (tex-number-def "lilypondpaper" sym
124                               (if (integer? val)
125                                   (number->string val)
126                                   (number->string (exact->inexact val)))))
127              (else ""))))
128         
129         (ly:output-def-scope pd))))
130
131 (define (output-scopes paper scopes fields basename)
132   (define (output-scope scope)
133     (apply
134      string-append
135      (module-map
136      (lambda (sym var)
137        (let (;;(val (variable-ref var))
138              (val (if (variable-bound? var) (variable-ref var) '""))
139              (tex-key (symbol->string sym)))
140          
141          (if (and (memq sym fields) (string? val))
142              (header-to-file basename sym val))
143
144          (cond
145           ((string? val)
146            (tex-string-def "lilypond" sym val))
147           ((number? val)
148            (tex-number-def "lilypond" sym
149                            (if (integer? val)
150                                (number->string val)
151                                (number->string (exact->inexact val)))))
152           (else ""))))
153      scope)))
154   
155   (apply string-append
156          (map output-scope scopes)))
157
158 (define (blank)
159   "")
160
161 (define (dot x y radius)
162   (embedded-ps (list 'dot x y radius)))
163
164 (define (beam width slope thick blot)
165   (embedded-ps (list 'beam  width slope thick blot)))
166
167 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
168   (embedded-ps (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
169
170 (define (dashed-slur thick dash l)
171   (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
172
173 (define (char font i)
174   (string-append "\\" (font-command font)
175                  "\\char" (ly:inexact->string i 10) " "))
176
177 (define (dashed-line thick on off dx dy)
178   (embedded-ps (list 'dashed-line  thick on off dx dy)))
179
180 (define (zigzag-line centre? zzw zzh thick dx dy)
181   (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
182
183 (define (symmetric-x-triangle t w h)
184   (embedded-ps (list 'symmetric-x-triangle t w h)))
185
186 (define (font-load-command bookpaper font)
187   (string-append
188    "\\font\\" (font-command font) "="
189    (ly:font-filename font)
190    " scaled "
191    (ly:number->string (inexact->exact
192                        (round (* 1000
193                           (ly:font-magnification font)
194                           (ly:bookpaper-outputscale bookpaper)))))
195    "\n"))
196
197 (define (ez-ball c l b)
198   (embedded-ps (list 'ez-ball  c  l b)))
199
200 (define (header-to-file fn key val)
201   (set! key (symbol->string key))
202   (if (not (equal? "-" fn))
203       (set! fn (string-append fn "." key))
204       )
205   (display
206    (format "writing header field `~a' to `~a'..."
207            key
208            (if (equal? "-" fn) "<stdout>" fn)
209            )
210    (current-error-port))
211   (if (equal? fn "-")
212       (display val)
213       (display val (open-file fn "w"))
214   )
215   (display "\n" (current-error-port))
216   ""
217   )
218
219 (define (embedded-ps expr)
220   (let ((ps-string
221          (with-output-to-string
222            (lambda () (ps-output-expression expr (current-output-port))))))
223     (string-append "\\embeddedps{" ps-string "}")))
224   
225 (define (comment s)
226   (string-append "% " s "\n"))
227
228 (define (end-output)
229   (begin
230     ;; uncomment for some stats about lily memory         
231     ;; (display (gc-stats))
232     (string-append
233      "\\lilypondend\n"
234      ;; Put GC stats here.
235      )))
236
237 (define (repeat-slash w a t)
238   (embedded-ps (list 'repeat-slash  w a t)))
239
240 (define (header-end)
241   (string-append
242    "\\def\\scaletounit{ "
243    (number->string (cond
244                      ((equal? (ly:unit) "mm") (/ 72.0  25.4))
245                      ((equal? (ly:unit) "pt") (/ 72.0  72.27))
246                      (else (error "unknown unit" (ly:unit)))
247                      ))
248    " mul }%\n"
249    "\\ifx\\lilypondstart\\undefined\n"
250    "  \\input lilyponddefs\n"
251    "\\fi\n"
252    "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
253    "\\lilypondstart\n"
254    "\\lilypondspecial\n"
255    "\\lilypondpostscript\n"))
256
257 ; why paper? 
258 (define (header creator time-stamp paper page-count classic?)
259   (string-append
260    "% Generated by " creator "\n"
261    "% at " time-stamp "\n"
262    (if classic?
263        (tex-string-def "lilypond" 'classic "1")
264        "")
265    ;; FIXME: duplicated in every backend
266    "\\def\\lilypondtagline{Engraved by LilyPond (version "
267    (lilypond-version)")}\n"
268
269    ;; FIXME
270    ;; this is -of course- severely broken, (--hwn)
271    (tex-string-def  "lilypondpaper" 'linewidth
272                     (ly:number->string (/ 18 0.175))) ; 18 cm.
273    (tex-string-def  "lilypondpaper" 'interscoreline
274                     (ly:number->string 0.0))
275    ))
276
277 (define (output-tex-string s)
278    (if (ly:get-option 'safe)
279       (regexp-substitute/global #f "\\\\"
280                                 (regexp-substitute/global #f "([{}])" "bla{}" 'pre  "\\" 1 'post )
281                                 'pre "$\\backslash$" 'post)
282       
283       s))
284
285 (define (lily-def key val)
286   (let ((tex-key
287          (regexp-substitute/global
288               #f "_" (output-tex-string key) 'pre "X" 'post))
289          
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"))))
294
295 (define (number->dim x)
296   (string-append
297    ;;ugh ly:* in backend needs compatibility func for standalone output
298    (ly:number->string x) " \\outputscale "))
299
300 (define (placebox x y s) 
301   (string-append
302    "\\lyitem" (number->param x) (number->param y) (string->param s) "%\n"))
303
304 (define (bezier-sandwich l thick)
305   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
306
307 (define (start-system origin dim)
308   (string-append
309    "\\leavevmode\n"
310    "\\lybox" (number-pair->param origin) (number-pair->param dim)
311   "{%\n"))
312
313 (define (stop-system last?)
314   (if last?
315       "}%\n"
316       ;; FIXME: still used by lilypond.py for --preview
317       "}%\n%\n\\interscoreline\n%\n"))
318
319 ;; WTF is this in every backend?
320 (define (horizontal-line x1 x2 th)
321   (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
322
323 (define (filledbox breapth width depth height)
324   (if (and #f (defined? 'ps-testing))
325       (embedded-ps
326        (string-append (ly:numbers->string (list breapth width depth height))
327                       " draw_box" ))
328       (string-append "\\lyvrule{"
329                      (ly:number->string (- breapth)) "}{"
330                      (ly:number->string (+ breapth width)) "}{"
331                      (ly:number->string depth) "}{"
332                      (ly:number->string height) "}")))
333
334 (define (round-filled-box x y width height blotdiam)
335   (embedded-ps (list 'round-filled-box  x y width height blotdiam)))
336
337 (define (text font s)
338   (let*
339       ((mapping #f)       ;; (assoc-get  'char-mapping (ly:font-encoding-alist font))))
340
341
342        ;; TODO: we'd better do this for PS only
343        ;; LaTeX gets in the way, and we need to remap
344        ;; nonprintable chars.
345        
346
347        (input-enc-name #f) ;; (assoc-get 'input-name (ly:font-encoding-alist font) ))
348        )
349
350     (string-append "\\hbox{\\" (font-command font)
351                    (if (string? input-enc-name)
352                        (string-append "\\inputencoding{" input-enc-name "}")
353                        "{}")
354                    (output-tex-string
355                     (if (vector? mapping)
356                         (reencode-string mapping s)
357                         s))
358                    "}")))
359
360
361 (define (tuplet ht gapx dx dy thick dir)
362   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
363
364 (define (polygon points blotdiameter)
365   (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
366
367 (define (draw-line thick fx fy tx ty)
368   (embedded-ps (list 'draw-line thick fx fy tx ty)))
369
370 (define (define-origin file line col)
371   (if (procedure? point-and-click)
372       (string-append "\\special{src:" ;;; \\string ? 
373                      (point-and-click line col file)
374                      "}" )
375       ""))
376
377 ;; no-origin not yet supported by Xdvi
378 (define (no-origin) "")
379
380 (define (start-page)
381   "\n\\vbox to 0pt{\n")
382
383 (define (stop-page last?)
384   (if last?
385       "\\vss\n}\n\\vfill\n"
386       "\\vss\n}\n\\vfill\\lilypondpagebreak\n"))