]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-tex.scm
* tex/lilyponddefs.tex (lilypondpagebreak): New overridable macro.
[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 paper font-list)
73   (apply string-append
74          (map (lambda (x) (font-load-command paper x)) font-list)))
75
76 (define (unknown) 
77   "%\n\\unknown\n")
78
79 (define (symbol->tex-key sym)
80   (regexp-substitute/global
81    #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
82
83 (define (string->param string)
84   (string-append "{" string "}"))
85
86 (define (number->param number)
87   (string->param (ly:number->string number)))
88
89 (define (number-pair->param o)
90   (string-append (number->param (car o)) (number->param (cdr o))))
91
92 (define (tex-string-def prefix key str)
93   (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
94       (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
95       (string-append "\\def\\" prefix (symbol->tex-key key)
96                      "{" (output-tex-string str) "}%\n")))
97
98 (define (tex-number-def prefix key number)
99   (string-append
100    "\\def\\" prefix (symbol->tex-key key) (string->param number) "%\n"))
101
102 (define (output-paper-def pd)
103   (apply
104    string-append
105    (module-map
106     (lambda (sym var)
107       (let ((val (variable-ref var))
108             (key (symbol->tex-key sym)))
109
110         (cond
111          ((string? val)
112           (tex-string-def "lilypondpaper" sym val))
113          ((number? val)
114           (tex-number-def "lilypondpaper" sym
115                           (if (integer? val)
116                               (number->string val)
117                               (number->string (exact->inexact val)))))
118          (else ""))))
119       
120     (ly:output-def-scope pd))))
121
122 (define (output-scopes paper scopes fields basename)
123   (define (output-scope scope)
124     (apply
125      string-append
126      (module-map
127      (lambda (sym var)
128        (let (;;(val (variable-ref var))
129              (val (if (variable-bound? var) (variable-ref var) '""))
130              (tex-key (symbol->string sym)))
131          
132          (if (and (memq sym fields) (string? val))
133              (header-to-file basename sym val))
134
135          (cond
136           ((string? val)
137            (tex-string-def "lilypond" sym val))
138           ((number? val)
139            (tex-number-def "lilypond" sym
140                            (if (integer? val)
141                                (number->string val)
142                                (number->string (exact->inexact val)))))
143           (else ""))))
144      scope)))
145   
146   (apply string-append
147          (map output-scope scopes)))
148
149 (define (blank)
150   "")
151
152 (define (dot x y radius)
153   (embedded-ps (list 'dot x y radius)))
154
155 (define (beam width slope thick blot)
156   (embedded-ps (list 'beam  width slope thick blot)))
157
158 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
159   (embedded-ps (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
160
161 (define (dashed-slur thick dash l)
162   (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
163
164 (define (char font i)
165   (string-append "\\" (font-command font)
166                  "\\char" (ly:inexact->string i 10) " "))
167
168 (define (dashed-line thick on off dx dy)
169   (embedded-ps (list 'dashed-line  thick on off dx dy)))
170
171 (define (zigzag-line centre? zzw zzh thick dx dy)
172   (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
173
174 (define (symmetric-x-triangle t w h)
175   (embedded-ps (list 'symmetric-x-triangle t w h)))
176
177 (define (font-load-command paper font)
178   (string-append
179    "\\font\\" (font-command font) "="
180    (ly:font-filename font)
181    " scaled "
182    (ly:number->string (inexact->exact
183                        (round (* 1000
184                           (ly:font-magnification font)
185                           (ly:paper-lookup paper 'outputscale)))))
186    "\n"))
187
188 (define (ez-ball c l b)
189   (embedded-ps (list 'ez-ball  c  l b)))
190
191 (define (header-to-file fn key val)
192   (set! key (symbol->string key))
193   (if (not (equal? "-" fn))
194       (set! fn (string-append fn "." key))
195       )
196   (display
197    (format "writing header field `~a' to `~a'..."
198            key
199            (if (equal? "-" fn) "<stdout>" fn)
200            )
201    (current-error-port))
202   (if (equal? fn "-")
203       (display val)
204       (display val (open-file fn "w"))
205   )
206   (display "\n" (current-error-port))
207   ""
208   )
209
210 (define (embedded-ps expr)
211   (let ((ps-string
212          (with-output-to-string
213            (lambda () (ps-output-expression expr (current-output-port))))))
214     (string-append "\\embeddedps{" ps-string "}")))
215   
216 (define (comment s)
217   (string-append "% " s "\n"))
218
219 (define (end-output)
220   (begin
221     ;; uncomment for some stats about lily memory         
222     ;; (display (gc-stats))
223     (string-append
224      "\\lilypondend\n"
225      ;; Put GC stats here.
226      )))
227
228 (define (repeat-slash w a t)
229   (embedded-ps (list 'repeat-slash  w a t)))
230
231 (define (header-end)
232   (string-append
233    "\\def\\scaletounit{ "
234    (number->string (cond
235                      ((equal? (ly:unit) "mm") (/ 72.0  25.4))
236                      ((equal? (ly:unit) "pt") (/ 72.0  72.27))
237                      (else (error "unknown unit" (ly:unit)))
238                      ))
239    " mul }%\n"
240    "\\ifx\\lilypondstart\\undefined\n"
241    "  \\input lilyponddefs\n"
242    "\\fi\n"
243    "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
244    "\\lilypondstart\n"
245    "\\lilypondspecial\n"
246    "\\lilypondpostscript\n"))
247
248 (define (header creator time-stamp page-count)
249   (string-append
250    "% Generated by " creator "\n"
251    "% at " time-stamp "\n"
252    ;; FIXME: duplicated in every backend
253    "\\def\\lilypondtagline{Engraved by LilyPond (version "
254    (lilypond-version)")}\n"))
255
256 ;; FIXME: explain ploblem: need to do something to make this really safe.  
257 (define (output-tex-string s)
258    (if safe-mode?
259        (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
260        s))
261
262 (define (lily-def key val)
263   (let ((tex-key
264          (regexp-substitute/global
265               #f "_" (output-tex-string key) 'pre "X" 'post))
266          
267         (tex-val (output-tex-string val)))
268     (if (equal? (sans-surrounding-whitespace tex-val) "")
269         (string-append "\\let\\" tex-key "\\undefined\n")
270         (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
271
272 (define (number->dim x)
273   (string-append
274    ;;ugh ly:* in backend needs compatibility func for standalone output
275    (ly:number->string x) " \\outputscale "))
276
277 (define (placebox x y s) 
278   (string-append
279    "\\lyitem" (number->param x) (number->param y) (string->param s) "%\n"))
280
281 (define (bezier-sandwich l thick)
282   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
283
284 (define (start-system origin dim)
285   (string-append
286    "\\leavevmode\n"
287    "\\lybox" (number-pair->param origin) (number-pair->param dim)
288   "{%\n"))
289
290 (define (stop-system last?)
291   (if last?
292       "}%\n"
293       ;; FIXME: still used by lilypond.py for --preview
294       "}%\n%\n\\interscoreline\n%\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       ((mapping #f))
316
317     ;; TODO: we'd better do this for PS only
318     ;; LaTeX gets in the way, and we need to remap
319     ;; nonprintable chars.
320     
321     ;; (assoc-get  'char-mapping (ly:font-encoding-alist font))))
322
323     (string-append "\\hbox{\\" (font-command font) "{}"
324                    (output-tex-string
325                     (if (vector? mapping)
326                         (reencode-string mapping s)
327                         s))
328                    "}")))
329
330
331 (define (tuplet ht gapx dx dy thick dir)
332   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
333
334 (define (polygon points blotdiameter)
335   (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
336
337 (define (draw-line thick fx fy tx ty)
338   (embedded-ps (list 'draw-line thick fx fy tx ty)))
339
340 (define (define-origin file line col)
341   (if (procedure? point-and-click)
342       (string-append "\\special{src:" ;;; \\string ? 
343                      (point-and-click line col file)
344                      "}" )
345       ""))
346
347 ;; no-origin not yet supported by Xdvi
348 (define (no-origin) "")
349
350 (define (start-page)
351   "\n\\vbox to 0pt{\n")
352
353 (define (stop-page last?)
354   (if last?
355       "\\vss\n}\n\\vfill\n"
356       "\\vss\n}\n\\vfill\\lilypondpagebreak\n"))