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