]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-tex.scm
* scm/paper.scm (set-staff-size): new function: set default
[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
32     (cons iname-mag
33           (cons ename-mag
34                 (string-append  "magfont"
35                           (string-encode-integer
36                            (hashq (car ename-mag) 1000000))
37                           "m"
38                           (string-encode-integer
39                            (inexact->exact (round (* 1000 (cdr ename-mag))))))))))
40
41 (define (define-fonts internal-external-name-mag-pairs)
42   (set! font-name-alist (map tex-encoded-fontswitch
43                              internal-external-name-mag-pairs))
44   (apply string-append
45          (map (lambda (x)
46                 (font-load-command (car x) (cdr x)))
47               (map cdr font-name-alist))))
48
49
50
51 ;; urg, how can exp be #unspecified?  -- in sketch output
52 ;;
53 ;; set! returns #<unspecified>  --hwn
54 (define (fontify name-mag-pair exp)
55   (string-append (select-font name-mag-pair)
56                  exp))
57
58
59 (define (unknown) 
60   "%\n\\unknown\n")
61
62 (define (select-font name-mag-pair)
63   (let*
64       (
65        (c (assoc name-mag-pair font-name-alist))
66        )
67
68     (if (eq? c #f)
69         (begin
70           (display "FAILED\n")
71           (display (object-type (car name-mag-pair)))
72           (display (object-type (caaar font-name-alist)))
73
74           (ly:warn (string-append
75                     "Programming error: No such font known "
76                     (car name-mag-pair) " "
77                     (ly:number->string (cdr name-mag-pair))
78                     ))
79           "") ; issue no command
80         (string-append "\\" (cddr c)))
81     
82     
83     ))
84
85 (define (blank)
86   "")
87
88 (define (dot x y radius)
89   (embedded-ps (list 'dot x y radius)))
90
91 (define (beam width slope thick)
92   (embedded-ps (list 'beam  width slope thick)))
93
94 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
95   (embedded-ps (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
96
97 (define (dashed-slur thick dash l)
98   (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
99
100 (define (char i)
101   (string-append "\\char" (inexact->string i 10) " "))
102
103 (define (dashed-line thick on off dx dy)
104   (embedded-ps (list 'dashed-line  thick on off dx dy)))
105
106 (define (zigzag-line centre? zzw zzh thick dx dy)
107   (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
108
109 (define (symmetric-x-triangle t w h)
110   (embedded-ps (list 'symmetric-x-triangle t w h)))
111
112 (define (font-load-command name-mag command)
113   (string-append
114    "\\font\\" command "="
115    (car name-mag)
116    " scaled "
117    (ly:number->string (inexact->exact (round (* 1000  (cdr name-mag)))))
118    "\n"))
119
120 (define (ez-ball c l b)
121   (embedded-ps (list 'ez-ball  c  l b)))
122
123 (define (header-to-file fn key val)
124   (set! key (symbol->string key))
125   (if (not (equal? "-" fn))
126       (set! fn (string-append fn "." key))
127       )
128   (display
129    (format "writing header field `~a' to `~a'..."
130            key
131            (if (equal? "-" fn) "<stdout>" fn)
132            )
133    (current-error-port))
134   (if (equal? fn "-")
135       (display val)
136       (display val (open-file fn "w"))
137   )
138   (display "\n" (current-error-port))
139   ""
140   )
141
142 (define (embedded-ps expr)
143   (let ((ps-string
144          (with-output-to-string
145            (lambda () (ps-output-expression expr (current-output-port))))))
146     (string-append "\\embeddedps{" ps-string "}")))
147   
148 (define (comment s)
149   (string-append "% " s "\n"))
150
151 (define (end-output) 
152   (begin
153                                         ; uncomment for some stats about lily memory      
154                                         ;               (display (gc-stats))
155     (string-append
156      "\\lilypondend\n"
157                                         ; Put GC stats here.
158                    )))
159
160 (define (experimental-on)
161   "")
162
163 (define (repeat-slash w a t)
164   (embedded-ps (list 'repeat-slash  w a t)))
165
166 (define (header-end)
167   (string-append
168    "\\def\\scaletounit{ "
169    (number->string (cond
170                      ((equal? (ly:unit) "mm") (/ 72.0  25.4))
171                      ((equal? (ly:unit) "pt") (/ 72.0  72.27))
172                      (else (error "unknown unit" (ly:unit)))
173                      ))
174    " mul }%\n"
175    "\\ifx\\lilypondstart\\undefined\n"
176    "  \\input lilyponddefs\n"
177    "\\fi\n"
178    "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
179    "\\lilypondstart\n"
180    "\\lilypondspecial\n"
181    "\\lilypondpostscript\n"))
182
183 ;; Note: this string must match the string in lilypond.py!!!
184 (define (header creator generate) 
185   (string-append
186    "% Generated automatically by: " creator generate "\n"))
187
188 (define (invoke-char s i)
189   (string-append 
190    "\n\\" s "{" (inexact->string i 10) "}" ))
191
192 ;;
193 ;; need to do something to make this really safe.
194 ;;
195 (define-public (output-tex-string s)
196   (if security-paranoia
197       (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
198       s))
199
200 (define (lily-def key val)
201   (let ((tex-key
202          (regexp-substitute/global
203               #f "_" (output-tex-string key) 'pre "X" 'post))
204          
205         (tex-val (output-tex-string val)))
206     (if (equal? (sans-surrounding-whitespace tex-val) "")
207         (string-append "\\let\\" tex-key "\\undefined\n")
208         (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
209
210 (define (number->dim x)
211   (string-append
212    ;;ugh ly:* in backend needs compatibility func for standalone output
213    (ly:number->string x) " \\outputscale "))
214
215 (define (placebox x y s) 
216   (string-append "\\lyitem{"
217                  (ly:number->string y) "}{"
218                  (ly:number->string x) "}{"
219                  s "}%\n"))
220
221 (define (bezier-sandwich l thick)
222   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
223
224 (define (start-system wd ht)
225   (string-append "\\leavevmode\n"
226                  "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
227                  "\\lilypondifundefined{lilypondscoreshift}%\n"
228                  "  {}%\n"
229                  "  {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
230                  "\\lybox{"
231                  (ly:number->string wd) "}{"
232                  (ly:number->string ht) "}{%\n"))
233
234 (define (stop-system) 
235   "}%\n%\n\\interscoreline\n%\n")
236 (define (stop-last-system)
237   "}%\n")
238
239 (define (horizontal-line x1 x2 th)
240   (filledbox (- x1)  (- x2 x1) (* .5 th)  (* .5 th )))
241
242 (define (filledbox breapth width depth height)
243   (if (and #f (defined? 'ps-testing))
244       (embedded-ps
245        (string-append (numbers->string (list breapth width depth height))
246                       " draw_box" ))
247       (string-append "\\lyvrule{"
248                      (ly:number->string (- breapth)) "}{"
249                      (ly:number->string (+ breapth width)) "}{"
250                      (ly:number->string depth) "}{"
251                      (ly:number->string height) "}")))
252
253 (define (round-filled-box x y width height blotdiam)
254   (embedded-ps (list 'round-filled-box  x y width height blotdiam)))
255
256 (define (text s)
257   (string-append "\\hbox{" (output-tex-string s) "}"))
258
259 (define (tuplet ht gapx dx dy thick dir)
260   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
261
262 (define (polygon points blotdiameter)
263   (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
264
265 (define (draw-line thick fx fy tx ty)
266   (embedded-ps (list 'draw-line thick fx fy tx ty)))
267
268 ;; TODO: this should be a default, which is overriden in PS
269 (define (between-system-string string)
270   string
271   )
272 (define (define-origin file line col)
273   (if (procedure? point-and-click)
274       (string-append "\\special{src:" ;;; \\string ? 
275                      (point-and-click line col file)
276                      "}" )
277       "")
278   )
279
280 ;; no-origin not yet supported by Xdvi
281 (define (no-origin) "")
282
283 (define-public (tex-output-expression expr port)
284   (display (eval expr this-module) port ))
285
286