]> git.donarmstrong.com Git - lilypond.git/blob - scm/tex.scm
release: 1.5.21
[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   :export (tex-output-expression)
11   :no-backtrace
12   )
13
14 (use-modules (scm ps)
15              (ice-9 regex)
16              (ice-9 string-fun)
17              (ice-9 format)
18              (guile-user)
19              (guile)
20              )
21
22 (define this-module (current-module))
23
24 ;;;;;;;;
25 ;;;;;;;; DOCUMENT ME!
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 l)))
91
92 (define (hairpin thick w sh eh)
93   (embedded-ps (list 'hairpin thick w sh eh))
94 )
95
96 (define (char i)
97   (string-append "\\char" (inexact->string i 10) " "))
98
99 (define (dashed-line thick on off dx dy)
100   (embedded-ps (list 'dashed-line  thick on off dx dy)))
101
102 (define (font-load-command name-mag command)
103   (string-append
104    "\\font\\" command "="
105    (car name-mag)
106    " scaled "
107    (ly-number->string (inexact->exact (* 1000  (cdr name-mag))))
108    "\n"))
109
110 (define (ez-ball c l b)
111   (embedded-ps (list 'ez-ball  c  l b)))
112
113 (define (header-to-file fn key val)
114   (set! key (symbol->string key))
115   (if (not (equal? "-" fn))
116       (set! fn (string-append fn "." key))
117       )
118   (display
119    (format "writing header field `~a' to `~a'..."
120            key
121            (if (equal? "-" fn) "<stdout>" fn)
122            )
123    (current-error-port))
124   (if (equal? fn "-")
125       (display val)
126       (display val (open-file fn "w"))
127   )
128   (display "\n" (current-error-port))
129   ""
130   )
131
132
133 (define (embedded-ps expr)
134   (let
135       ((os (open-output-string)))
136     (ps-output-expression expr os)
137     (string-append "\\embeddedps{" (get-output-string os) "}")
138   ))
139
140 (define (comment s)
141   (string-append "% " s "\n"))
142
143 (define (end-output) 
144   (begin
145                                         ; uncomment for some stats about lily memory      
146                                         ;               (display (gc-stats))
147     (string-append "\n\\EndLilyPondOutput"
148                                         ; Put GC stats here.
149                    )))
150
151 (define (experimental-on)
152   "")
153
154 (define (repeat-slash w a t)
155   (embedded-ps (list 'repeat-slash  w a t)))
156
157 (define (font-switch i)
158   (string-append
159    "\\" (font i) "\n"))
160
161 (define (font-def i s)
162   (string-append
163    "\\font" (font-switch i) "=" s "\n"))
164
165 (define (header-end)
166   (string-append
167    "\\special{\\string! "
168    
169    ;; URG: ly-gulp-file: now we can't use scm output without Lily
170    (if use-regex
171        ;; fixed in 1.3.4 for powerpc -- broken on Windows
172        (regexp-substitute/global #f "\n"
173                                  (ly-gulp-file "music-drawing-routines.ps") 'pre " %\n" 'post)
174        (ly-gulp-file "music-drawing-routines.ps"))
175    (if (defined? 'ps-testing) "/testing true def%\n" "")
176    "}"
177    "\\input lilyponddefs \\outputscale=\\lilypondpaperoutputscale pt\\turnOnPostScript"))
178
179 ;; Note: this string must match the string in ly2dvi.py!!!
180 (define (header creator generate) 
181   (string-append
182    "% Generated automatically by: " creator generate "\n"))
183
184 (define (invoke-char s i)
185   (string-append 
186    "\n\\" s "{" (inexact->string i 10) "}" ))
187
188 (define (invoke-dim1 s d)
189   (string-append
190    "\n\\" s "{" (number->dim d) "}"))
191 (define (pt->sp x)
192   (* 65536 x))
193
194 ;;
195 ;; need to do something to make this really safe.
196 ;;
197 (define (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-sandwich l thick)
227   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
228
229 (define (start-line ht)
230   (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
231
232 (define (stop-line) 
233   "}\\vss}\\interscoreline\n")
234 (define (stop-last-line)
235   "}\\vss}")
236
237 (define (filledbox breapth width depth height)
238   (if (defined? 'ps-testing)
239       (embedded-ps
240        (string-append (numbers->string (list breapth width depth height))
241                       " draw_box" ))
242       (string-append 
243        "\\kern" (number->dim (- breapth))
244        "\\vrule width " (number->dim (+ breapth width))
245        "depth " (number->dim depth)
246        "height " (number->dim height) " ")))
247
248 (define (text s)
249   (string-append "\\hbox{" (output-tex-string s) "}"))
250
251 (define (tuplet ht gapx dx dy thick dir)
252   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
253
254 (define (volta h w thick vert_start vert_end)
255   (embedded-ps (list 'volta  h w thick vert_start vert_end)))
256 (define (between-system-string string)
257   string
258   )
259 (define (define-origin file line col)
260   (if (procedure? point-and-click)
261       (string-append "\\special{src\\string:"
262                      (point-and-click line col file)
263                      "}" )
264       "")
265   )
266
267                                         ; no-origin not yet supported by Xdvi
268 (define (no-origin) "")
269
270 (define (tex-output-expression expr port)
271   (display (eval expr this-module) port )
272   )