]> git.donarmstrong.com Git - lilypond.git/blob - scm/tex.scm
release: 1.5.23
[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 (hairpin thick w sh eh)
89   (embedded-ps (list 'hairpin thick w sh eh))
90 )
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 (define (embedded-ps expr)
129   (let
130       ((os (open-output-string)))
131     (ps-output-expression expr os)
132     (string-append "\\embeddedps{" (get-output-string os) "}")
133   ))
134
135 (define (comment s)
136   (string-append "% " s "\n"))
137
138 (define (end-output) 
139   (begin
140                                         ; uncomment for some stats about lily memory      
141                                         ;               (display (gc-stats))
142     (string-append "\n\\EndLilyPondOutput"
143                                         ; Put GC stats here.
144                    )))
145
146 (define (experimental-on)
147   "")
148
149 (define (repeat-slash w a t)
150   (embedded-ps (list 'repeat-slash  w a t)))
151
152 (define (font-switch i)
153   (string-append
154    "\\" (font i) "\n"))
155
156 (define (font-def i s)
157   (string-append
158    "\\font" (font-switch i) "=" s "\n"))
159
160 (define (header-end)
161   (string-append
162    "\\special{\\string! "
163    
164    ;; URG: ly-gulp-file: now we can't use scm output without Lily
165    (if use-regex
166        ;; fixed in 1.3.4 for powerpc -- broken on Windows
167        (regexp-substitute/global #f "\n"
168                                  (ly-gulp-file "music-drawing-routines.ps") 'pre " %\n" 'post)
169        (ly-gulp-file "music-drawing-routines.ps"))
170    (if (defined? 'ps-testing) "/testing true def%\n" "")
171    "}"
172    "\\input lilyponddefs \\outputscale=\\lilypondpaperoutputscale pt\\turnOnPostScript"))
173
174 ;; Note: this string must match the string in ly2dvi.py!!!
175 (define (header creator generate) 
176   (string-append
177    "% Generated automatically by: " creator generate "\n"))
178
179 (define (invoke-char s i)
180   (string-append 
181    "\n\\" s "{" (inexact->string i 10) "}" ))
182
183 (define (invoke-dim1 s d)
184   (string-append
185    "\n\\" s "{" (number->dim d) "}"))
186 (define (pt->sp x)
187   (* 65536 x))
188
189 ;;
190 ;; need to do something to make this really safe.
191 ;;
192 (define-public (output-tex-string s)
193   (if security-paranoia
194       (if use-regex
195           (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
196           (begin (display "warning: not paranoid") (newline) s))
197       s))
198
199 (define (lily-def key val)
200   (let ((tex-key
201          (if use-regex
202              ;; fixed in 1.3.4 for powerpc -- broken on Windows
203              (regexp-substitute/global
204               #f "_" (output-tex-string key) 'pre "X" 'post)
205              (output-tex-string key)))
206         (tex-val (output-tex-string val)))
207     (if (equal? (sans-surrounding-whitespace tex-val) "")
208         (string-append "\\let\\" tex-key "\\undefined\n")
209         (string-append "\\def\\" tex-key "{" tex-val "}\n"))))
210
211 (define (number->dim x)
212   (string-append
213    ;;ugh ly-* in backend needs compatibility func for standalone output
214    (ly-number->string x) " \\outputscale "))
215
216 (define (placebox x y s) 
217   (string-append 
218    "\\placebox{"
219    (number->dim y) "}{" (number->dim x) "}{" s "}%\n"))
220
221 (define (bezier-sandwich l thick)
222   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
223
224 (define (start-line ht)
225   (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
226
227 (define (stop-line) 
228   "}\\vss}\\interscoreline\n")
229 (define (stop-last-line)
230   "}\\vss}")
231
232 (define (filledbox breapth width depth height)
233   (if (defined? 'ps-testing)
234       (embedded-ps
235        (string-append (numbers->string (list breapth width depth height))
236                       " draw_box" ))
237       (string-append 
238        "\\kern" (number->dim (- breapth))
239        "\\vrule width " (number->dim (+ breapth width))
240        "depth " (number->dim depth)
241        "height " (number->dim height) " ")))
242
243 (define (text s)
244   (string-append "\\hbox{" (output-tex-string s) "}"))
245
246 (define (tuplet ht gapx dx dy thick dir)
247   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
248
249 (define (volta h w thick vert_start vert_end)
250   (embedded-ps (list 'volta  h w thick vert_start vert_end)))
251 (define (between-system-string string)
252   string
253   )
254 (define (define-origin file line col)
255   (if (procedure? point-and-click)
256       (string-append "\\special{src\\string:"
257                      (point-and-click line col file)
258                      "}" )
259       "")
260   )
261
262                                         ; no-origin not yet supported by Xdvi
263 (define (no-origin) "")
264
265 (define my-eval-in-module eval)
266
267 (if (or (equal? (minor-version) "4")
268         (equal? (minor-version) "3.4"))
269     (begin
270       (set! my-eval-in-module eval-in-module)
271
272     ))
273
274 (define-public (tex-output-expression expr port)
275   (display (my-eval-in-module expr this-module) port )
276   )
277