]> git.donarmstrong.com Git - lilypond.git/blob - scm/tex.scm
* scm/tex.scm (zigzig-line): added.
[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--2002 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 (beam width slope thick)
85   (embedded-ps (list 'beam  width slope thick)))
86
87 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
88   (embedded-ps (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
89
90 (define (dashed-slur thick dash l)
91   (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
92
93 (define (char i)
94   (string-append "\\char" (inexact->string i 10) " "))
95
96 (define (dashed-line thick on off dx dy)
97   (embedded-ps (list 'dashed-line  thick on off dx dy)))
98
99 (define (zigzag-line centre? zzw zzh thick dx dy)
100   (embedded-ps (list 'zigzag-line centre? zzw zzh thick 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 (if (or (equal? (minor-version) "4.1")
133         (equal? (minor-version) "4")
134         (equal? (minor-version) "3.4"))
135     (define (embedded-ps expr)
136       (let ((ps-string
137              (with-output-to-string
138                (lambda () (ps-output-expression expr (current-output-port))))))
139         (string-append "\\embeddedps{" ps-string "}")))
140     (define (embedded-ps expr)
141       (let
142           ((os (open-output-string)))
143         (ps-output-expression expr os)
144         (string-append "\\embeddedps{" (get-output-string os) "}"))))
145
146 (define (comment s)
147   (string-append "% " s "\n"))
148
149 (define (end-output) 
150   (begin
151                                         ; uncomment for some stats about lily memory      
152                                         ;               (display (gc-stats))
153     (string-append
154      "%\n"
155      "\\EndLilyPondOutput\n"
156      "\\begingroup\n"
157      "\\ifx\\LilyPondDocument\\undefined\n"
158      "  \\def\\x{\\endgroup}%\n"
159      "\\else\n"
160      "  \\def\\x{%\n"
161      "    \\endgroup\n"
162      "    \\enddocument\n"
163      "  }\n"
164      "\\fi\n"
165      "\\x\n"
166                                         ; Put GC stats here.
167                    )))
168
169 (define (experimental-on)
170   "")
171
172 (define (repeat-slash w a t)
173   (embedded-ps (list 'repeat-slash  w a t)))
174
175 (define (header-end)
176   (string-append
177    "\\def\\scaletounit{ "
178    (number->string (cond
179                      ((equal? (ly:unit) "mm") (/ 72.0  25.4))
180                      ((equal? (ly:unit) "pt") (/ 72.0  72.27))
181                      (else (error "unknown unit" (ly:unit)))
182                      ))
183     " mul }%\n"
184    "\\special{\\string! "
185    
186    ;; URG: ly:gulp-file: now we can't use scm output without Lily
187    (regexp-substitute/global
188     #f "\n"
189     (ly:gulp-file "music-drawing-routines.ps") 'pre " %\n" 'post)
190    ;; (if (defined? 'ps-testing) "/testing true def%\n" "")
191    "}%\n"
192    "\\begingroup\n"
193    "\\catcode `\\@=11\n"
194    "\\expandafter\\ifx\\csname @nodocument\\endcsname \\relax\n"
195    "  \\def\\x{\\endgroup}%\n"
196    "\\else\n"
197    "  \\def\\x{%\n"
198    "    \\endgroup\n"
199    "    \\def\\LilyPondDocument{}\n"
200    "    \\documentclass{article}\n"
201    "    \\pagestyle{empty}\n"
202    ;; argh, we can't say \begin{document} because \begin is defined as
203    ;; \outer in texinfo
204    "    \\begingroup\n"
205    "    \\document\n"
206    "    \\ifdim\\lilypondpaperlinewidth\\lilypondpaperunit > 0pt\n"
207    "      \\hsize\\lilypondpaperlinewidth\\lilypondpaperunit\n"
208    "    \\fi\n"
209    "    \\parindent 0pt\n"
210    "  }\n"
211    "\\fi\n"
212    "\\x\n"
213    "\\input lilyponddefs\n"
214    "\\outputscale=\\lilypondpaperoutputscale \\lilypondpaperunit\n"
215    "\\turnOnPostScript\n"))
216
217 ;; Note: this string must match the string in ly2dvi.py!!!
218 (define (header creator generate) 
219   (string-append
220    "% Generated automatically by: " creator generate "\n"))
221
222 (define (invoke-char s i)
223   (string-append 
224    "\n\\" s "{" (inexact->string i 10) "}" ))
225
226 ;;
227 ;; need to do something to make this really safe.
228 ;;
229 (define-public (output-tex-string s)
230   (if security-paranoia
231       (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
232       s))
233
234 (define (lily-def key val)
235   (let ((tex-key
236          (regexp-substitute/global
237               #f "_" (output-tex-string key) 'pre "X" 'post))
238          
239         (tex-val (output-tex-string val)))
240     (if (equal? (sans-surrounding-whitespace tex-val) "")
241         (string-append "\\let\\" tex-key "\\undefined\n")
242         (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
243
244 (define (number->dim x)
245   (string-append
246    ;;ugh ly:* in backend needs compatibility func for standalone output
247    (ly:number->string x) " \\outputscale "))
248
249 (define (placebox x y s) 
250   (string-append 
251    "\\placebox{"
252    (number->dim y) "}{" (number->dim x) "}{" s "}%\n"))
253
254 (define (bezier-bow l thick)
255   (embedded-ps (list 'bezier-bow  `(quote ,l) thick)))
256
257 (define (bezier-sandwich l thick)
258   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
259
260 (define (start-system wd ht)
261   (string-append "\\leavevmode\n"
262                  "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
263                  "\\ifundefined{lilypondscoreshift}%\n"
264                  "\\else\n"
265                  "  \\advance\\scoreshift by -\\lilypondscoreshift\n"
266                  "\\fi\n"
267                  "\\hbox to " (number->dim wd) "{%\n"
268                  "\\lower\\scoreshift\n"
269                  "\\vbox to " (number->dim ht) "{\\hbox{%\n"))
270
271 (define (stop-system) 
272   "}\\vss}\\hss}\\interscoreline\n")
273 (define (stop-last-system)
274   "}\\vss}\\hss}")
275
276 (define (filledbox breapth width depth height)
277   (if (and #f (defined? 'ps-testing))
278       (embedded-ps
279        (string-append (numbers->string (list breapth width depth height))
280                       " draw_box" ))
281       (string-append 
282        "\\kern" (number->dim (- breapth))
283        "\\vrule width " (number->dim (+ breapth width))
284        "depth " (number->dim depth)
285        "height " (number->dim height) " ")))
286
287 (define (roundfilledbox x y width height blotdiam)
288   (embedded-ps (list 'roundfilledbox  x y width height blotdiam)))
289
290 (define (text s)
291   (string-append "\\hbox{" (output-tex-string s) "}"))
292
293 (define (tuplet ht gapx dx dy thick dir)
294   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
295
296 (define (draw-line thick fx fy tx ty)
297   (embedded-ps (list 'draw-line thick fx fy tx ty)))
298
299 (define (between-system-string string)
300   string
301   )
302 (define (define-origin file line col)
303   (if (procedure? point-and-click)
304       (string-append "\\special{src:" ;;; \\string ? 
305                      (point-and-click line col file)
306                      "}" )
307       "")
308   )
309
310 ;; no-origin not yet supported by Xdvi
311 (define (no-origin) "")
312
313 (define my-eval-in-module eval)
314
315 (if (or (equal? (minor-version) "4.1")
316         (equal? (minor-version) "4")
317         (equal? (minor-version) "3.4"))
318     (set! my-eval-in-module eval-in-module))
319
320 (define-public (tex-output-expression expr port)
321   (display (my-eval-in-module expr this-module) port )
322   )
323
324