]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-tex.scm
* scm/define-markup-commands.scm (smallcaps): New markup command.
[lilypond.git] / scm / output-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--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8
9 (define-module (scm output-tex) )
10 ; (debug-enable 'backtrace)
11 (use-modules (scm output-ps)
12              (ice-9 regex)
13              (ice-9 string-fun)
14              (ice-9 format)
15              (guile)
16              (srfi srfi-13)
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 (symbol->tex-key sym)
63   (regexp-substitute/global
64    #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
65
66 (define (tex-string-def prefix key str)
67   (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
68       (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
69       (string-append "\\def\\" prefix (symbol->tex-key key) "{"  (output-tex-string str) "}%\n")
70       ))
71
72 (define (tex-number-def prefix key number)
73   (string-append "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
74
75 (define (output-paper-def pd)
76   (apply
77    string-append
78    (module-map
79     (lambda (sym var)
80       (let ((val (variable-ref var))
81             (key (symbol->tex-key sym)))
82
83         (cond
84          ((string? val)
85           (tex-string-def "lilypondpaper" sym val))
86          ((number? val)
87           (tex-number-def "lilypondpaper" sym
88                           (if (integer? val)
89                               (number->string val)
90                               (number->string (exact->inexact val)))))
91          (else ""))))
92       
93     (ly:output-def-scope pd))))
94
95 (define (output-scopes paper scopes fields basename)
96   (define (output-scope scope)
97     (apply
98      string-append
99      (module-map
100      (lambda (sym var)
101        (let ((val (variable-ref var))
102              (tex-key (symbol->string sym)))
103          
104          (if (memq sym fields)
105              (header-to-file basename sym val))
106
107          (cond
108           ((string? val)
109            (tex-string-def "lilypond" sym val))
110           ((number? val)
111            (tex-number-def "lilypond" sym
112                            (if (integer? val)
113                                (number->string val)
114                                (number->string (exact->inexact val)))))
115           (else ""))))
116      scope)))
117   
118   (apply string-append
119          (map output-scope scopes)))
120
121 (define (select-font name-mag-pair)
122   (let ((c (assoc name-mag-pair font-name-alist)))
123
124     (if c
125         (string-append "\\" (cddr c))
126         (begin
127           (ly:warn (string-append
128                     "Programming error: No such font known "
129                     (car name-mag-pair) " "
130                     (ly:number->string (cdr name-mag-pair))))
131           
132           (display "FAILED\n" (current-error-port))
133           (if #f ;(pair? name-mag-pair))
134               (display (object-type (car name-mag-pair)) (current-error-port))
135               (write name-mag-pair (current-error-port)))
136           (if #f ;  (pair? font-name-alist)
137               (display
138                (object-type (caaar font-name-alist)) (current-error-port))
139               (write font-name-alist (current-error-port)))
140
141           ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair))
142           ""))))
143
144 ;; top-of-file, wtf?  ugh: tagline?
145 (define (top-of-file)
146   (string-append
147    "% generated by LilyPond "
148     (lilypond-version) " (http://lilypond.org).\n"
149     "\\def\\lilypondtagline{Engraved by LilyPond (version "
150     (lilypond-version)
151     ")}\n"
152     ))
153
154 (define (blank)
155   "")
156
157 (define (dot x y radius)
158   (embedded-ps (list 'dot x y radius)))
159
160 (define (beam width slope thick blot)
161   (embedded-ps (list 'beam  width slope thick blot)))
162
163 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
164   (embedded-ps (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
165
166 (define (dashed-slur thick dash l)
167   (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
168
169 (define (char i)
170   (string-append "\\char" (inexact->string i 10) " "))
171
172 (define (dashed-line thick on off dx dy)
173   (embedded-ps (list 'dashed-line  thick on off dx dy)))
174
175 (define (zigzag-line centre? zzw zzh thick dx dy)
176   (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy)))
177
178 (define (symmetric-x-triangle t w h)
179   (embedded-ps (list 'symmetric-x-triangle t w h)))
180
181 (define (font-load-command name-mag command)
182   (string-append
183    "\\font\\" command "="
184    (car name-mag)
185    " scaled "
186    (ly:number->string (inexact->exact (round (* 1000  (cdr name-mag)))))
187    "\n"))
188
189 (define (ez-ball c l b)
190   (embedded-ps (list 'ez-ball  c  l b)))
191
192 (define (header-to-file fn key val)
193   (set! key (symbol->string key))
194   (if (not (equal? "-" fn))
195       (set! fn (string-append fn "." key))
196       )
197   (display
198    (format "writing header field `~a' to `~a'..."
199            key
200            (if (equal? "-" fn) "<stdout>" fn)
201            )
202    (current-error-port))
203   (if (equal? fn "-")
204       (display val)
205       (display val (open-file fn "w"))
206   )
207   (display "\n" (current-error-port))
208   ""
209   )
210
211 (define (embedded-ps expr)
212   (let ((ps-string
213          (with-output-to-string
214            (lambda () (ps-output-expression expr (current-output-port))))))
215     (string-append "\\embeddedps{" ps-string "}")))
216   
217 (define (comment s)
218   (string-append "% " s "\n"))
219
220 (define (end-output) 
221   (begin
222                                         ; uncomment for some stats about lily memory      
223                                         ;               (display (gc-stats))
224     (string-append
225      "\\lilypondend\n"
226                                         ; Put GC stats here.
227                    )))
228
229 (define (experimental-on)
230   "")
231
232 (define (repeat-slash w a t)
233   (embedded-ps (list 'repeat-slash  w a t)))
234
235 (define (header-end)
236   (string-append
237    "\\def\\scaletounit{ "
238    (number->string (cond
239                      ((equal? (ly:unit) "mm") (/ 72.0  25.4))
240                      ((equal? (ly:unit) "pt") (/ 72.0  72.27))
241                      (else (error "unknown unit" (ly:unit)))
242                      ))
243    " mul }%\n"
244    "\\ifx\\lilypondstart\\undefined\n"
245    "  \\input lilyponddefs\n"
246    "\\fi\n"
247    "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
248    "\\lilypondstart\n"
249    "\\lilypondspecial\n"
250    "\\lilypondpostscript\n"))
251
252 ;; Note: this string must match the string in lilypond.py!!!
253 (define (header creator generate) 
254   (string-append
255    "% Generated automatically by: " creator generate "\n"))
256
257 (define (invoke-char s i)
258   (string-append 
259    "\n\\" s "{" (inexact->string i 10) "}" ))
260
261 ;;
262 ;; need to do something to make this really safe.
263 ;;
264 (define-public (output-tex-string s)
265   (if security-paranoia
266       (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
267       s))
268
269 (define (lily-def key val)
270   (let ((tex-key
271          (regexp-substitute/global
272               #f "_" (output-tex-string key) 'pre "X" 'post))
273          
274         (tex-val (output-tex-string val)))
275     (if (equal? (sans-surrounding-whitespace tex-val) "")
276         (string-append "\\let\\" tex-key "\\undefined\n")
277         (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
278
279 (define (number->dim x)
280   (string-append
281    ;;ugh ly:* in backend needs compatibility func for standalone output
282    (ly:number->string x) " \\outputscale "))
283
284 (define (placebox x y s) 
285   (string-append "\\lyitem{"
286                  (ly:number->string y) "}{"
287                  (ly:number->string x) "}{"
288                  s "}%\n"))
289
290 (define (bezier-sandwich l thick)
291   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
292
293 (define (start-system wd ht)
294   (string-append "\\leavevmode\n"
295                  "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
296                  "\\lilypondifundefined{lilypondscoreshift}%\n"
297                  "  {}%\n"
298                  "  {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
299                  "\\lybox{"
300                  (ly:number->string wd) "}{"
301                  (ly:number->string ht) "}{%\n"))
302
303 (define (stop-system) 
304   "}%\n%\n\\interscoreline\n%\n")
305 (define (stop-last-system)
306   "}%\n")
307
308 (define (horizontal-line x1 x2 th)
309   (filledbox (- x1)  (- x2 x1) (* .5 th)  (* .5 th )))
310
311 (define (filledbox breapth width depth height)
312   (if (and #f (defined? 'ps-testing))
313       (embedded-ps
314        (string-append (numbers->string (list breapth width depth height))
315                       " draw_box" ))
316       (string-append "\\lyvrule{"
317                      (ly:number->string (- breapth)) "}{"
318                      (ly:number->string (+ breapth width)) "}{"
319                      (ly:number->string depth) "}{"
320                      (ly:number->string height) "}")))
321
322 (define (round-filled-box x y width height blotdiam)
323   (embedded-ps (list 'round-filled-box  x y width height blotdiam)))
324
325 (define (text s)
326   (string-append "\\hbox{" (output-tex-string s) "}"))
327
328 (define (tuplet ht gapx dx dy thick dir)
329   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
330
331 (define (polygon points blotdiameter)
332   (embedded-ps (list 'polygon `(quote ,points) blotdiameter)))
333
334 (define (draw-line thick fx fy tx ty)
335   (embedded-ps (list 'draw-line thick fx fy tx ty)))
336
337 ;; TODO: this should be a default, which is overriden in PS
338 (define (between-system-string string)
339   string
340   )
341 (define (define-origin file line col)
342   (if (procedure? point-and-click)
343       (string-append "\\special{src:" ;;; \\string ? 
344                      (point-and-click line col file)
345                      "}" )
346       "")
347   )
348
349 ;; no-origin not yet supported by Xdvi
350 (define (no-origin) "")
351
352 (define-public (tex-output-expression expr port)
353   (display (eval expr this-module) port ))
354
355