]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-tex.scm
* scm/lily.scm (tex-output-expression): new function, eval within
[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              unknown
14              output-paper-def
15              output-scopes
16              select-font
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 (define font-name-alist  '())
65
66 (define (tex-encoded-fontswitch name-mag)
67   (let* ((iname-mag (car name-mag))
68          (ename-mag (cdr name-mag)))
69
70     (cons iname-mag
71           (cons ename-mag
72                 (string-append  "magfont"
73                           (string-encode-integer
74                            (hashq (car ename-mag) 1000000))
75                           "m"
76                           (string-encode-integer
77                            (inexact->exact (round (* 1000 (cdr ename-mag))))))))))
78
79 (define-public (define-fonts internal-external-name-mag-pairs)
80   (set! font-name-alist (map tex-encoded-fontswitch
81                              internal-external-name-mag-pairs))
82   (apply string-append
83          (map (lambda (x)
84                 (font-load-command (car x) (cdr x)))
85               (map cdr font-name-alist))))
86
87 ;;
88 ;; urg, how can exp be #unspecified?  -- in sketch output
89 ;;
90 ;; set! returns #<unspecified>  --hwn
91 ;;
92 (define-public (fontify name-mag-pair exp)
93   (string-append (select-font name-mag-pair)
94                  exp))
95
96
97 (define-public (unknown) 
98   "%\n\\unknown\n")
99
100 (define (symbol->tex-key sym)
101   (regexp-substitute/global
102    #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
103
104 (define (tex-string-def prefix key str)
105   (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
106       (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
107       (string-append "\\def\\" prefix (symbol->tex-key key) "{"  (output-tex-string str) "}%\n")
108       ))
109
110 (define (tex-number-def prefix key number)
111   (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
112
113 (define (output-paper-def pd)
114   (apply
115    string-append
116    (module-map
117     (lambda (sym var)
118       (let ((val (variable-ref var))
119             (key (symbol->tex-key sym)))
120
121         (cond
122          ((string? val)
123           (tex-string-def "lilypondpaper" sym val))
124          ((number? val)
125           (tex-number-def "lilypondpaper" sym
126                           (if (integer? val)
127                               (number->string val)
128                               (number->string (exact->inexact val)))))
129          (else ""))))
130       
131     (ly:output-def-scope pd))))
132
133 (define (output-scopes paper scopes fields basename)
134   (define (output-scope scope)
135     (apply
136      string-append
137      (module-map
138      (lambda (sym var)
139        (let ((val (variable-ref var))
140              ;;(val (if (variable-bound? var) (variable-ref var) '""))
141              (tex-key (symbol->string sym)))
142          
143          (if (and (memq sym fields) (string? val))
144              (header-to-file basename sym val))
145
146          (cond
147           ((string? val)
148            (tex-string-def "lilypond" sym val))
149           ((number? val)
150            (tex-number-def "lilypond" sym
151                            (if (integer? val)
152                                (number->string val)
153                                (number->string (exact->inexact val)))))
154           (else ""))))
155      scope)))
156   
157   (apply string-append
158          (map output-scope scopes)))
159
160 (define (select-font name-mag-pair)
161   (let ((c (assoc name-mag-pair font-name-alist)))
162     (if c
163         (string-append "\\" (cddr c))
164         (begin
165           (ly:warn
166            (format "Programming error: No such font: ~S" name-mag-pair))
167           ""))))
168
169 (define (blank)
170   "")
171
172 (define (dot x y radius)
173   (embedded-ps (list 'dot x y radius)))
174
175 (define (beam width slope thick blot)
176   (embedded-ps (list 'beam  width slope thick blot)))
177
178 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
179   (embedded-ps (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
180
181 (define (dashed-slur thick dash l)
182   (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
183
184 (define (char i)
185   (string-append "\\char" (inexact->string i 10) " "))
186
187 (define (dashed-line thick on off dx dy)
188   (embedded-ps (list 'dashed-line  thick on off dx dy)))
189
190 (define (zigzag-line centre? zzw zzh thick dx dy)
191   (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
192
193 (define (symmetric-x-triangle t w h)
194   (embedded-ps (list 'symmetric-x-triangle t w h)))
195
196 (define (font-load-command name-mag command)
197   (string-append
198    "\\font\\" command "="
199    (car name-mag)
200    " scaled "
201    (ly:number->string (inexact->exact (round (* 1000  (cdr name-mag)))))
202    "\n"))
203
204 (define (ez-ball c l b)
205   (embedded-ps (list 'ez-ball  c  l b)))
206
207 (define (header-to-file fn key val)
208   (set! key (symbol->string key))
209   (if (not (equal? "-" fn))
210       (set! fn (string-append fn "." key))
211       )
212   (display
213    (format "writing header field `~a' to `~a'..."
214            key
215            (if (equal? "-" fn) "<stdout>" fn)
216            )
217    (current-error-port))
218   (if (equal? fn "-")
219       (display val)
220       (display val (open-file fn "w"))
221   )
222   (display "\n" (current-error-port))
223   ""
224   )
225
226 (define (embedded-ps expr)
227   (let ((ps-string
228          (with-output-to-string
229            (lambda () (ps-output-expression expr (current-output-port))))))
230     (string-append "\\embeddedps{" ps-string "}")))
231   
232 (define (comment s)
233   (string-append "% " s "\n"))
234
235 (define (end-output) 
236   (begin
237     ;; uncomment for some stats about lily memory         
238     ;; (display (gc-stats))
239     (string-append
240      "\\lilypondend\n"
241      ;; Put GC stats here.
242      )))
243
244 (define (experimental-on)
245   "")
246
247 (define (repeat-slash w a t)
248   (embedded-ps (list 'repeat-slash  w a t)))
249
250 (define (header-end)
251   (string-append
252    "\\def\\scaletounit{ "
253    (number->string (cond
254                      ((equal? (ly:unit) "mm") (/ 72.0  25.4))
255                      ((equal? (ly:unit) "pt") (/ 72.0  72.27))
256                      (else (error "unknown unit" (ly:unit)))
257                      ))
258    " mul }%\n"
259    "\\ifx\\lilypondstart\\undefined\n"
260    "  \\input lilyponddefs\n"
261    "\\fi\n"
262    "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
263    "\\lilypondstart\n"
264    "\\lilypondspecial\n"
265    "\\lilypondpostscript\n"))
266
267 (define (header creator time-stamp page-count)
268   (string-append
269    "% Generated by " creator "\n"
270    "% at " time-stamp "\n"
271    ;; FIXME: duplicated in every backend
272    "\\def\\lilypondtagline{Engraved by LilyPond (version "
273    (lilypond-version)")}\n"))
274
275 (define (invoke-char s i)
276   (string-append 
277    "\n\\" s "{" (inexact->string i 10) "}" ))
278
279 ;; FIXME: explain ploblem: need to do something to make this really safe.  
280 (define-public (output-tex-string s)
281   (if safe-mode?
282       (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
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 "\\lyitem{"
302                  (ly:number->string y) "}{"
303                  (ly:number->string x) "}{"
304                  s "}%\n"))
305
306 (define (bezier-sandwich l thick)
307   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
308
309 (define (start-system wd ht)
310   (string-append "\\leavevmode\n"
311                  "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
312                  "\\lilypondifundefined{lilypondscoreshift}%\n"
313                  "  {}%\n"
314                  "  {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
315                  "\\lybox{"
316                  (ly:number->string wd) "}{"
317                  (ly:number->string ht) "}{%\n"))
318
319 (define (stop-system) 
320   "}%\n%\n\\interscoreline\n%\n")
321 (define (stop-last-system)
322   "}%\n")
323
324 (define (horizontal-line x1 x2 th)
325   (filledbox (- x1)  (- x2 x1) (* .5 th)  (* .5 th )))
326
327 (define (filledbox breapth width depth height)
328   (if (and #f (defined? 'ps-testing))
329       (embedded-ps
330        (string-append (numbers->string (list breapth width depth height))
331                       " draw_box" ))
332       (string-append "\\lyvrule{"
333                      (ly:number->string (- breapth)) "}{"
334                      (ly:number->string (+ breapth width)) "}{"
335                      (ly:number->string depth) "}{"
336                      (ly:number->string height) "}")))
337
338 (define (round-filled-box x y width height blotdiam)
339   (embedded-ps (list 'round-filled-box  x y width height blotdiam)))
340
341 (define (text s)
342   (string-append "\\hbox{" (output-tex-string s) "}"))
343
344 (define (tuplet ht gapx dx dy thick dir)
345   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
346
347 (define (polygon points blotdiameter)
348   (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
349
350 (define (draw-line thick fx fy tx ty)
351   (embedded-ps (list 'draw-line thick fx fy tx ty)))
352
353 ;; TODO: this should be a default, which is overriden in PS
354 (define (between-system-string string)
355   string
356   )
357 (define (define-origin file line col)
358   (if (procedure? point-and-click)
359       (string-append "\\special{src:" ;;; \\string ? 
360                      (point-and-click line col file)
361                      "}" )
362       "")
363   )
364
365 ;; no-origin not yet supported by Xdvi
366 (define (no-origin) "")
367
368 (define (start-page)
369   "\n%\\vbox{\n")
370
371 (define (stop-page last?)
372   (if last?
373       "\n%}\n"
374       "\n%}\n\\newpage\n"))