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