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