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