]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-tex.scm
* Documentation/user/lilypond-book.itely
[lilypond.git] / scm / output-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 output-tex) )
11 ; (debug-enable 'backtrace)
12 (use-modules (scm output-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 (define (embedded-ps expr)
142   (let ((ps-string
143          (with-output-to-string
144            (lambda () (ps-output-expression expr (current-output-port))))))
145     (string-append "\\embeddedps{" ps-string "}")))
146   
147 (define (comment s)
148   (string-append "% " s "\n"))
149
150 (define (end-output) 
151   (begin
152                                         ; uncomment for some stats about lily memory      
153                                         ;               (display (gc-stats))
154     (string-append
155      "\\lilypondend\n"
156                                         ; Put GC stats here.
157                    )))
158
159 (define (experimental-on)
160   "")
161
162 (define (repeat-slash w a t)
163   (embedded-ps (list 'repeat-slash  w a t)))
164
165 (define (header-end)
166   (string-append
167    "\\def\\scaletounit{ "
168    (number->string (cond
169                      ((equal? (ly:unit) "mm") (/ 72.0  25.4))
170                      ((equal? (ly:unit) "pt") (/ 72.0  72.27))
171                      (else (error "unknown unit" (ly:unit)))
172                      ))
173    " mul }%\n"
174    "\\ifx\\lilypondstart\\undefined\n"
175    "  \\input lilyponddefs\n"
176    "\\fi\n"
177    "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
178    "\\lilypondstart\n"
179    "\\lilypondspecial\n"
180    "\\lilypondpostscript\n"))
181
182 ;; Note: this string must match the string in lilypond.py!!!
183 (define (header creator generate) 
184   (string-append
185    "% Generated automatically by: " creator generate "\n"))
186
187 (define (invoke-char s i)
188   (string-append 
189    "\n\\" s "{" (inexact->string i 10) "}" ))
190
191 ;;
192 ;; need to do something to make this really safe.
193 ;;
194 (define-public (output-tex-string s)
195   (if security-paranoia
196       (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
197       s))
198
199 (define (lily-def key val)
200   (let ((tex-key
201          (regexp-substitute/global
202               #f "_" (output-tex-string key) 'pre "X" 'post))
203          
204         (tex-val (output-tex-string val)))
205     (if (equal? (sans-surrounding-whitespace tex-val) "")
206         (string-append "\\let\\" tex-key "\\undefined\n")
207         (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
208
209 (define (number->dim x)
210   (string-append
211    ;;ugh ly:* in backend needs compatibility func for standalone output
212    (ly:number->string x) " \\outputscale "))
213
214 (define (placebox x y s) 
215   (string-append "\\lyitem{"
216                  (ly:number->string y) "}{"
217                  (ly:number->string x) "}{"
218                  s "}%\n"))
219
220 (define (bezier-sandwich l thick)
221   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
222
223 (define (start-system wd ht)
224   (string-append "\\leavevmode\n"
225                  "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
226                  "\\lilypondifundefined{lilypondscoreshift}%\n"
227                  "  {}%\n"
228                  "  {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
229                  "\\lybox{"
230                  (ly:number->string wd) "}{"
231                  (ly:number->string ht) "}{%\n"))
232
233 (define (stop-system) 
234   "}%\n%\n\\interscoreline\n%\n")
235 (define (stop-last-system)
236   "}%\n")
237
238 (define (horizontal-line x1 x2 th)
239   (filledbox (- x1)  (- x2 x1) (* .5 th)  (* .5 th )))
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 "\\lyvrule{"
247                      (ly:number->string (- breapth)) "}{"
248                      (ly:number->string (+ breapth width)) "}{"
249                      (ly:number->string depth) "}{"
250                      (ly:number->string height) "}")))
251
252 (define (round-filled-box x y width height blotdiam)
253   (embedded-ps (list 'round-filled-box  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 (polygon points blotdiameter)
262   (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
263
264 (define (draw-line thick fx fy tx ty)
265   (embedded-ps (list 'draw-line thick fx fy tx ty)))
266
267 ;; TODO: this should be a default, which is overriden in PS
268 (define (between-system-string string)
269   string
270   )
271 (define (define-origin file line col)
272   (if (procedure? point-and-click)
273       (string-append "\\special{src:" ;;; \\string ? 
274                      (point-and-click line col file)
275                      "}" )
276       "")
277   )
278
279 ;; no-origin not yet supported by Xdvi
280 (define (no-origin) "")
281
282 (define-public (tex-output-expression expr port)
283   (display (eval expr this-module) port )
284   )
285
286