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