]> git.donarmstrong.com Git - lilypond.git/blob - scm/tex.scm
* Another grand 2003 update.
[lilypond.git] / scm / tex.scm
1
2 ;;; tex.scm -- implement Scheme output routines for TeX
3 ;;;
4 ;;;  source file of the GNU LilyPond music typesetter
5 ;;; 
6 ;;; (c)  1998--2003 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
8
9
10 (define-module (scm tex) )
11 (debug-enable 'backtrace)
12 (use-modules (scm ps)
13              (ice-9 regex)
14              (ice-9 string-fun)
15              (ice-9 format)
16              (guile)
17              (lily)
18              )
19
20 (define this-module (current-module))
21
22 ;;;;;;;;
23 ;;;;;;;; DOCUMENT ME!
24 ;;;;;;;;
25
26 (define font-name-alist  '())
27
28 (define (tex-encoded-fontswitch name-mag)
29   (let* ((iname-mag (car name-mag))
30          (ename-mag (cdr name-mag)))
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 (* 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           (display "FAILED\n")
70           (display (object-type (car name-mag-pair)))
71           (display (object-type (caaar font-name-alist)))
72
73           (ly:warn (string-append
74                     "Programming error: No such font known "
75                     (car name-mag-pair) " "
76                     (ly:number->string (cdr name-mag-pair))
77                     ))
78           "") ; issue no command
79         (string-append "\\" (cddr c)))
80     
81     
82     ))
83
84 (define (blank)
85   "")
86
87 (define (dot x y radius)
88   (embedded-ps (list 'dot x y radius)))
89
90 (define (beam width slope thick)
91   (embedded-ps (list 'beam  width slope thick)))
92
93 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
94   (embedded-ps (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
95
96 (define (dashed-slur thick dash l)
97   (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
98
99 (define (char i)
100   (string-append "\\char" (inexact->string i 10) " "))
101
102 (define (dashed-line thick on off dx dy)
103   (embedded-ps (list 'dashed-line  thick on off dx dy)))
104
105 (define (zigzag-line centre? zzw zzh thick dx dy)
106   (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
107
108 (define (symmetric-x-triangle t w h)
109   (embedded-ps (list 'symmetric-x-triangle t w h)))
110
111 (define (font-load-command name-mag command)
112   (string-append
113    "\\font\\" command "="
114    (car name-mag)
115    " scaled "
116    (ly:number->string (inexact->exact (* 1000  (cdr name-mag))))
117    "\n"))
118
119 (define (ez-ball c l b)
120   (embedded-ps (list 'ez-ball  c  l b)))
121
122 (define (header-to-file fn key val)
123   (set! key (symbol->string key))
124   (if (not (equal? "-" fn))
125       (set! fn (string-append fn "." key))
126       )
127   (display
128    (format "writing header field `~a' to `~a'..."
129            key
130            (if (equal? "-" fn) "<stdout>" fn)
131            )
132    (current-error-port))
133   (if (equal? fn "-")
134       (display val)
135       (display val (open-file fn "w"))
136   )
137   (display "\n" (current-error-port))
138   ""
139   )
140
141 (if (or (equal? (minor-version) "4.1")
142         (equal? (minor-version) "4")
143         (equal? (minor-version) "3.4"))
144     (define (embedded-ps expr)
145       (let ((ps-string
146              (with-output-to-string
147                (lambda () (ps-output-expression expr (current-output-port))))))
148         (string-append "\\embeddedps{" ps-string "}")))
149     (define (embedded-ps expr)
150       (let
151           ((os (open-output-string)))
152         (ps-output-expression expr os)
153         (string-append "\\embeddedps{" (get-output-string os) "}"))))
154
155 (define (comment s)
156   (string-append "% " s "\n"))
157
158 (define (end-output) 
159   (begin
160                                         ; uncomment for some stats about lily memory      
161                                         ;               (display (gc-stats))
162     (string-append
163      "\\lilypondend\n"
164                                         ; Put GC stats here.
165                    )))
166
167 (define (experimental-on)
168   "")
169
170 (define (repeat-slash w a t)
171   (embedded-ps (list 'repeat-slash  w a t)))
172
173 (define (header-end)
174   (string-append
175    "\\def\\scaletounit{ "
176    (number->string (cond
177                      ((equal? (ly:unit) "mm") (/ 72.0  25.4))
178                      ((equal? (ly:unit) "pt") (/ 72.0  72.27))
179                      (else (error "unknown unit" (ly:unit)))
180                      ))
181    " mul }%\n"
182    "\\ifx\\lilypondstart\\undefined\n"
183    "  \\input lilyponddefs\n"
184    "\\fi\n"
185    "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
186    "\\lilypondstart\n"
187    "\\lilypondspecial\n"
188    "\\lilypondpostscript\n"))
189
190 ;; Note: this string must match the string in ly2dvi.py!!!
191 (define (header creator generate) 
192   (string-append
193    "% Generated automatically by: " creator generate "\n"))
194
195 (define (invoke-char s i)
196   (string-append 
197    "\n\\" s "{" (inexact->string i 10) "}" ))
198
199 ;;
200 ;; need to do something to make this really safe.
201 ;;
202 (define-public (output-tex-string s)
203   (if security-paranoia
204       (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
205       s))
206
207 (define (lily-def key val)
208   (let ((tex-key
209          (regexp-substitute/global
210               #f "_" (output-tex-string key) 'pre "X" 'post))
211          
212         (tex-val (output-tex-string val)))
213     (if (equal? (sans-surrounding-whitespace tex-val) "")
214         (string-append "\\let\\" tex-key "\\undefined\n")
215         (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
216
217 (define (number->dim x)
218   (string-append
219    ;;ugh ly:* in backend needs compatibility func for standalone output
220    (ly:number->string x) " \\outputscale "))
221
222 (define (placebox x y s) 
223   (string-append "\\lyitem{"
224                  (ly:number->string y) "}{"
225                  (ly:number->string x) "}{"
226                  s "}%\n"))
227
228 (define (bezier-bow l thick)
229   (embedded-ps (list 'bezier-bow  `(quote ,l) thick)))
230
231 (define (bezier-sandwich l thick)
232   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
233
234 (define (start-system wd ht)
235   (string-append "\\leavevmode\n"
236                  "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
237                  "\\lilypondifundefined{lilypondscoreshift}%\n"
238                  "  {}%\n"
239                  "  {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
240                  "\\lybox{"
241                  (ly:number->string wd) "}{"
242                  (ly:number->string ht) "}{%\n"))
243
244 (define (stop-system) 
245   "}%\n%\n\\interscoreline\n%\n")
246 (define (stop-last-system)
247   "}%\n")
248
249 (define (filledbox breapth width depth height)
250   (if (and #f (defined? 'ps-testing))
251       (embedded-ps
252        (string-append (numbers->string (list breapth width depth height))
253                       " draw_box" ))
254       (string-append "\\lyvrule{"
255                      (ly:number->string (- breapth)) "}{"
256                      (ly:number->string (+ breapth width)) "}{"
257                      (ly:number->string depth) "}{"
258                      (ly:number->string height) "}")))
259
260 (define (roundfilledbox x y width height blotdiam)
261   (embedded-ps (list 'roundfilledbox  x y width height blotdiam)))
262
263 (define (text s)
264   (string-append "\\hbox{" (output-tex-string s) "}"))
265
266 (define (tuplet ht gapx dx dy thick dir)
267   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
268
269 (define (polygon points blotdiameter)
270   (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
271
272 (define (draw-line thick fx fy tx ty)
273   (embedded-ps (list 'draw-line thick fx fy tx ty)))
274
275 (define (between-system-string string)
276   string
277   )
278 (define (define-origin file line col)
279   (if (procedure? point-and-click)
280       (string-append "\\special{src:" ;;; \\string ? 
281                      (point-and-click line col file)
282                      "}" )
283       "")
284   )
285
286 ;; no-origin not yet supported by Xdvi
287 (define (no-origin) "")
288
289 (define-public (tex-output-expression expr port)
290   (display (eval expr this-module) port )
291   )
292
293