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