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