]> git.donarmstrong.com Git - lilypond.git/blob - scm/tex.scm
ly- -> ly:
[lilypond.git] / scm / 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--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
8
9
10 (define-module (scm tex) )
11 (debug-enable 'backtrace)
12 (use-modules (scm 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 (beam width slope thick)
85   (embedded-ps (list 'beam  width slope thick)))
86
87 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
88   (embedded-ps (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
89
90 (define (dashed-slur thick dash l)
91   (embedded-ps (list 'dashed-slur thick dash `(quote ,l))))
92
93 (define (char i)
94   (string-append "\\char" (inexact->string i 10) " "))
95
96 (define (dashed-line thick on off dx dy)
97   (embedded-ps (list 'dashed-line  thick on off dx dy)))
98
99 (define (font-load-command name-mag command)
100   (string-append
101    "\\font\\" command "="
102    (car name-mag)
103    " scaled "
104    (ly:number->string (inexact->exact (* 1000  (cdr name-mag))))
105    "\n"))
106
107 (define (ez-ball c l b)
108   (embedded-ps (list 'ez-ball  c  l b)))
109
110 (define (header-to-file fn key val)
111   (set! key (symbol->string key))
112   (if (not (equal? "-" fn))
113       (set! fn (string-append fn "." key))
114       )
115   (display
116    (format "writing header field `~a' to `~a'..."
117            key
118            (if (equal? "-" fn) "<stdout>" fn)
119            )
120    (current-error-port))
121   (if (equal? fn "-")
122       (display val)
123       (display val (open-file fn "w"))
124   )
125   (display "\n" (current-error-port))
126   ""
127   )
128
129 (if (or (equal? (minor-version) "4.1")
130         (equal? (minor-version) "4")
131         (equal? (minor-version) "3.4"))
132     (define (embedded-ps expr)
133       (let ((ps-string
134              (with-output-to-string
135                (lambda () (ps-output-expression expr (current-output-port))))))
136         (string-append "\\embeddedps{" ps-string "}")))
137     (define (embedded-ps expr)
138       (let
139           ((os (open-output-string)))
140         (ps-output-expression expr os)
141         (string-append "\\embeddedps{" (get-output-string os) "}"))))
142
143 (define (comment s)
144   (string-append "% " s "\n"))
145
146 (define (end-output) 
147   (begin
148                                         ; uncomment for some stats about lily memory      
149                                         ;               (display (gc-stats))
150     (string-append
151      "%\n"
152      "\\EndLilyPondOutput\n"
153      "\\begingroup\n"
154      "\\ifx\\LilyPondDocument\\undefined\n"
155      "  \\def\\x{\\endgroup}%\n"
156      "\\else\n"
157      "  \\def\\x{%\n"
158      "    \\endgroup\n"
159      "    \\enddocument\n"
160      "  }\n"
161      "\\fi\n"
162      "\\x\n"
163                                         ; Put GC stats here.
164                    )))
165
166 (define (experimental-on)
167   "")
168
169 (define (repeat-slash w a t)
170   (embedded-ps (list 'repeat-slash  w a t)))
171
172 (define (font-switch i)
173   (string-append
174    "\\" (font i) "\n"))
175
176 (define (font-def i s)
177   (string-append
178    "\\font" (font-switch i) "=" s "\n"))
179
180 (define (header-end)
181   (string-append
182    "\\def\\scaletounit{ "
183    (number->string (cond
184                      ((equal? (ly:unit) "mm") (/ 72.0  25.4))
185                      ((equal? (ly:unit) "pt") (/ 72.0  72.27))
186                      (else (error "unknown unit" (ly:unit)))
187                      ))
188     " mul }%\n"
189    "\\special{\\string! "
190    
191    ;; URG: ly:gulp-file: now we can't use scm output without Lily
192    (regexp-substitute/global
193     #f "\n"
194     (ly:gulp-file "music-drawing-routines.ps") 'pre " %\n" 'post)
195    ;; (if (defined? 'ps-testing) "/testing true def%\n" "")
196    "}%\n"
197    "\\begingroup\n"
198    "\\catcode `\\@=11\n"
199    "\\expandafter\\ifx\\csname @nodocument\\endcsname \\relax\n"
200    "  \\def\\x{\\endgroup}%\n"
201    "\\else\n"
202    "  \\def\\x{%\n"
203    "    \\endgroup\n"
204    "    \\def\\LilyPondDocument{}\n"
205    "    \\documentclass{article}\n"
206    "    \\pagestyle{empty}\n"
207    ;; argh, we can't say \begin{document} because \begin is defined as
208    ;; \outer in texinfo
209    "    \\begingroup\n"
210    "    \\document\n"
211    "    \\ifdim\\lilypondpaperlinewidth\\lilypondpaperunit > 0pt\n"
212    "      \\hsize\\lilypondpaperlinewidth\\lilypondpaperunit\n"
213    "    \\fi\n"
214    "    \\parindent 0pt\n"
215    "  }\n"
216    "\\fi\n"
217    "\\x\n"
218    "\\input lilyponddefs\n"
219    "\\outputscale=\\lilypondpaperoutputscale \\lilypondpaperunit\n"
220    "\\turnOnPostScript\n"))
221
222 ;; Note: this string must match the string in ly2dvi.py!!!
223 (define (header creator generate) 
224   (string-append
225    "% Generated automatically by: " creator generate "\n"))
226
227 (define (invoke-char s i)
228   (string-append 
229    "\n\\" s "{" (inexact->string i 10) "}" ))
230
231 ;;
232 ;; need to do something to make this really safe.
233 ;;
234 (define-public (output-tex-string s)
235   (if security-paranoia
236       (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
237       s))
238
239 (define (lily-def key val)
240   (let ((tex-key
241          (regexp-substitute/global
242               #f "_" (output-tex-string key) 'pre "X" 'post))
243          
244         (tex-val (output-tex-string val)))
245     (if (equal? (sans-surrounding-whitespace tex-val) "")
246         (string-append "\\let\\" tex-key "\\undefined\n")
247         (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
248
249 (define (number->dim x)
250   (string-append
251    ;;ugh ly:* in backend needs compatibility func for standalone output
252    (ly:number->string x) " \\outputscale "))
253
254 (define (placebox x y s) 
255   (string-append 
256    "\\placebox{"
257    (number->dim y) "}{" (number->dim x) "}{" s "}%\n"))
258
259 (define (bezier-bow l thick)
260   (embedded-ps (list 'bezier-bow  `(quote ,l) thick)))
261
262 (define (bezier-sandwich l thick)
263   (embedded-ps (list 'bezier-sandwich  `(quote ,l) thick)))
264
265 (define (start-system wd ht)
266   (string-append "\\leavevmode\n"
267                  "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
268                  "\\ifundefined{lilypondscoreshift}%\n"
269                  "\\else\n"
270                  "  \\advance\\scoreshift by -\\lilypondscoreshift\n"
271                  "\\fi\n"
272                  "\\hbox to " (number->dim wd) "{%\n"
273                  "\\lower\\scoreshift\n"
274                  "\\vbox to " (number->dim ht) "{\\hbox{%\n"))
275
276 (define (stop-system) 
277   "}\\vss}\\hss}\\interscoreline\n")
278 (define (stop-last-system)
279   "}\\vss}\\hss}")
280
281 (define (filledbox breapth width depth height)
282   (if (and #f (defined? 'ps-testing))
283       (embedded-ps
284        (string-append (numbers->string (list breapth width depth height))
285                       " draw_box" ))
286       (string-append 
287        "\\kern" (number->dim (- breapth))
288        "\\vrule width " (number->dim (+ breapth width))
289        "depth " (number->dim depth)
290        "height " (number->dim height) " ")))
291
292 (define (roundfilledbox x y width height blotdiam)
293   (embedded-ps (list 'roundfilledbox  x y width height blotdiam)))
294
295 (define (text s)
296   (string-append "\\hbox{" (output-tex-string s) "}"))
297
298 (define (tuplet ht gapx dx dy thick dir)
299   (embedded-ps (list 'tuplet  ht gapx dx dy thick dir)))
300
301 (define (draw-line thick fx fy tx ty)
302   (embedded-ps (list 'draw-line thick fx fy tx ty)))
303
304 (define (between-system-string string)
305   string
306   )
307 (define (define-origin file line col)
308   (if (procedure? point-and-click)
309       (string-append "\\special{src:" ;;; \\string ? 
310                      (point-and-click line col file)
311                      "}" )
312       "")
313   )
314
315                                         ; no-origin not yet supported by Xdvi
316 (define (no-origin) "")
317
318 (define my-eval-in-module eval)
319
320 (if (or (equal? (minor-version) "4.1")
321         (equal? (minor-version) "4")
322         (equal? (minor-version) "3.4"))
323     (set! my-eval-in-module eval-in-module))
324
325 (define-public (tex-output-expression expr port)
326   (display (my-eval-in-module expr this-module) port )
327   )
328
329