]> git.donarmstrong.com Git - lilypond.git/blob - scm/tex.scm
lots of changes (see diff :-)
[lilypond.git] / scm / 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--2001 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8
9 (define-module (scm tex) )
10 (debug-enable 'backtrace)
11 (use-modules (scm 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     (cons iname-mag
31           (cons ename-mag
32                 (string-append  "magfont"
33                           (string-encode-integer
34                            (hashq (car ename-mag) 1000000))
35                           "m"
36                           (string-encode-integer
37                            (inexact->exact (* 1000 (cdr ename-mag)))))))))
38
39 (define (define-fonts internal-external-name-mag-pairs)
40   (set! font-name-alist (map tex-encoded-fontswitch
41                              internal-external-name-mag-pairs))
42   (apply string-append
43          (map (lambda (x)
44                 (font-load-command (car x) (cdr x)))
45               (map cdr font-name-alist))))
46
47
48
49 ;; urg, how can exp be #unspecified?  -- in sketch output
50 ;;
51 ;; set! returns #<unspecified>  --hwn
52 (define (fontify name-mag-pair exp)
53   (string-append (select-font name-mag-pair)
54                  exp))
55
56
57 (define (unknown) 
58   "%\n\\unknown%\n")
59
60 (define (select-font name-mag-pair)
61   (let*
62       (
63        (c (assoc name-mag-pair font-name-alist))
64        )
65
66     (if (eq? c #f)
67         (begin
68           (display "FAILED\n")
69           (display (object-type (car name-mag-pair)))
70           (display (object-type (caaar font-name-alist)))
71
72           (ly-warn (string-append
73                     "Programming error: No such font known "
74                     (car name-mag-pair) " "
75                     (ly-number->string (cdr name-mag-pair))
76                     ))
77           "") ; issue no command
78         (string-append "\\" (cddr c)))
79     
80     
81     ))
82
83 (define (beam width slope thick)
84   (embedded-ps (list 'beam  width slope thick)))
85
86 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
87   (embedded-ps (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
88
89 (define (dashed-slur thick dash l)
90   (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
91
92 (define (char i)
93   (string-append "\\char" (inexact->string i 10) " "))
94
95 (define (dashed-line thick on off dx dy)
96   (embedded-ps (list 'dashed-line  thick on off dx dy)))
97
98 (define (font-load-command name-mag command)
99   (string-append
100    "\\font\\" command "="
101    (car name-mag)
102    " scaled "
103    (ly-number->string (inexact->exact (* 1000  (cdr name-mag))))
104    "\n"))
105
106 (define (ez-ball c l b)
107   (embedded-ps (list 'ez-ball  c  l b)))
108
109 (define (header-to-file fn key val)
110   (set! key (symbol->string key))
111   (if (not (equal? "-" fn))
112       (set! fn (string-append fn "." key))
113       )
114   (display
115    (format "writing header field `~a' to `~a'..."
116            key
117            (if (equal? "-" fn) "<stdout>" fn)
118            )
119    (current-error-port))
120   (if (equal? fn "-")
121       (display val)
122       (display val (open-file fn "w"))
123   )
124   (display "\n" (current-error-port))
125   ""
126   )
127
128 (if (or (equal? (minor-version) "4.1")
129         (equal? (minor-version) "4")
130         (equal? (minor-version) "3.4"))
131     (define (embedded-ps expr)
132       (let ((ps-string
133              (with-output-to-string
134                (lambda () (ps-output-expression expr (current-output-port))))))
135         (string-append "\\embeddedps{" ps-string "}")))
136     (define (embedded-ps expr)
137       (let
138           ((os (open-output-string)))
139         (ps-output-expression expr os)
140         (string-append "\\embeddedps{" (get-output-string os) "}"))))
141
142 (define (comment s)
143   (string-append "% " s "\n"))
144
145 (define (end-output) 
146   (begin
147                                         ; uncomment for some stats about lily memory      
148                                         ;               (display (gc-stats))
149     (string-append "\n\\EndLilyPondOutput\n"
150                                         ; Put GC stats here.
151                    )))
152
153 (define (experimental-on)
154   "")
155
156 (define (repeat-slash w a t)
157   (embedded-ps (list 'repeat-slash  w a t)))
158
159 (define (font-switch i)
160   (string-append
161    "\\" (font i) "\n"))
162
163 (define (font-def i s)
164   (string-append
165    "\\font" (font-switch i) "=" s "\n"))
166
167 (define (header-end)
168   (string-append
169    "\\def\\scaletounit{ "
170    (number->string (cond
171                      ((equal? (ly-unit) "mm") (/ 72.0  25.4))
172                      ((equal? (ly-unit) "pt") (/ 72.0  72.27))
173                      (else (error "unknown unit" (ly-unit)))
174                      ))
175     " mul }"
176    "\\special{\\string! "
177    
178    ;; URG: ly-gulp-file: now we can't use scm output without Lily
179    (if use-regex
180        ;; fixed in 1.3.4 for powerpc -- broken on Windows
181        (regexp-substitute/global #f "\n"
182                                  (ly-gulp-file "music-drawing-routines.ps") 'pre " %\n" 'post)
183        (ly-gulp-file "music-drawing-routines.ps"))
184 ;   (if (defined? 'ps-testing) "/testing true def%\n" "")
185    "}"
186    "\\input lilyponddefs \\outputscale=\\lilypondpaperoutputscale \\lilypondpaperunit"
187    "\\turnOnPostScript"))
188
189 ;; Note: this string must match the string in ly2dvi.py!!!
190 (define (header creator generate) 
191   (string-append
192    "% Generated automatically by: " creator generate "\n"))
193
194 (define (invoke-char s i)
195   (string-append 
196    "\n\\" s "{" (inexact->string i 10) "}" ))
197
198 ;;
199 ;; need to do something to make this really safe.
200 ;;
201 (define-public (output-tex-string s)
202   (if security-paranoia
203       (if use-regex
204           (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
205           (begin (display "warning: not paranoid") (newline) s))
206       s))
207
208 (define (lily-def key val)
209   (let ((tex-key
210          (if use-regex
211              ;; fixed in 1.3.4 for powerpc -- broken on Windows
212              (regexp-substitute/global
213               #f "_" (output-tex-string key) 'pre "X" 'post)
214              (output-tex-string key)))
215         (tex-val (output-tex-string val)))
216     (if (equal? (sans-surrounding-whitespace tex-val) "")
217         (string-append "\\let\\" tex-key "\\undefined\n")
218         (string-append "\\def\\" tex-key "{" tex-val "}\n"))))
219
220 (define (number->dim x)
221   (string-append
222    ;;ugh ly-* in backend needs compatibility func for standalone output
223    (ly-number->string x) " \\outputscale "))
224
225 (define (placebox x y s) 
226   (string-append 
227    "\\placebox{"
228    (number->dim y) "}{" (number->dim x) "}{" s "}%\n"))
229
230 (define (bezier-bow l thick)
231   (embedded-ps (list 'bezier-bow  `(quote ,l) thick)))
232
233 (define (bezier-sandwich l thick)
234   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
235
236 (define (start-system ht)
237   (string-append "\\vbox to " (number->dim ht) "{\\hbox{"
238                  "%\n"))
239
240 (define (stop-system) 
241   "}\\vss}\\interscoreline\n")
242 (define (stop-last-system)
243   "}\\vss}")
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 
251        "\\kern" (number->dim (- breapth))
252        "\\vrule width " (number->dim (+ breapth width))
253        "depth " (number->dim depth)
254        "height " (number->dim height) " ")))
255
256 (define (roundfilledbox x y width height blotdiam)
257   (embedded-ps (list 'roundfilledbox  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 (draw-line thick fx fy tx ty)
266   (embedded-ps (list 'draw-line thick fx fy tx ty)))
267
268 (define (between-system-string string)
269   string
270   )
271 (define (define-origin file line col)
272   (if (procedure? point-and-click)
273       (string-append "\\special{src:" ;;; \\string ? 
274                      (point-and-click line col file)
275                      "}" )
276       "")
277   )
278
279                                         ; no-origin not yet supported by Xdvi
280 (define (no-origin) "")
281
282 (define my-eval-in-module eval)
283
284 (if (or (equal? (minor-version) "4.1")
285         (equal? (minor-version) "4")
286         (equal? (minor-version) "3.4"))
287     (set! my-eval-in-module eval-in-module))
288
289 (define-public (tex-output-expression expr port)
290   (display (my-eval-in-module expr this-module) port )
291   )
292