]> git.donarmstrong.com Git - lilypond.git/blob - scm/pdftex.scm
* VERSION (MY_PATCH_LEVEL): make 1.7.0
[lilypond.git] / scm / pdftex.scm
1 ;;; pdftex.scm -- implement Scheme output routines for PDFTeX
2 ;;;
3 ;;;  source file of the GNU LilyPond music typesetter
4 ;;;  modified from the existing tex.scm
5 ;;; 
6 ;;; (c) 1998--2001 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 ;;; Stephen Peters <portnoy@portnoy.org>
9
10
11 ;; TODO: port this  to the new module framework.
12
13 (define-module (scm pdftex))
14
15 (use-modules (scm pdf)
16              (guile)
17              (ice-9 regex)
18              (ice-9 string-fun)   
19              )
20
21 (define this-module (current-module))
22 (define (unknown) 
23   "%\n\\unknown%\n")
24
25
26 (define (select-font name-mag-pair)
27   (let*
28       (
29        (c (assoc name-mag-pair font-name-alist))
30        )
31
32     (if (eq? c #f)
33         (begin
34           (display "FAILED\n")
35           (display (object-type (car name-mag-pair)))
36           (display (object-type (caaar font-name-alist)))
37
38           (ly-warn (string-append
39                     "Programming error: No such font known "
40                     (car name-mag-pair) " "
41                     (ly-number->string (cdr name-mag-pair))
42                     ))
43           "") ; issue no command
44         (string-append "\\" (cddr c)))
45     
46     
47     ))
48
49 (define (beam width slope thick)
50   (embedded-pdf (list 'beam  width slope thick)))
51
52 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
53   (embedded-pdf (list 'bracket  arch_angle arch_width arch_height height arch_thick thick)))
54
55 (define (dashed-slur thick dash l)
56   (embedded-pdf (list 'dashed-slur   thick dash l)))
57
58 (define (char i)
59   (string-append "\\char" (inexact->string i 10) " "))
60
61 (define (dashed-line thick on off dx dy)
62   (embedded-pdf (list 'dashed-line  thick on off dx dy)))
63
64 (define (font-load-command name-mag command)
65   (string-append
66    "\\font\\" command "="
67    (car name-mag)
68    " scaled "
69    (ly-number->string (inexact->exact (* 1000  (cdr name-mag))))
70    "\n"))
71
72 (define (ez-ball c l b)
73   (embedded-pdf (list 'ez-ball  c  l b)))
74
75 (define (header-to-file fn key val)
76   (set! key (symbol->string key))
77   (if (not (equal? "-" fn))
78       (set! fn (string-append fn "." key))
79       )
80   (display
81    (format "writing header field `~a' to `~a'..."
82            key
83            (if (equal? "-" fn) "<stdout>" fn)
84            )
85    (current-error-port))
86   (if (equal? fn "-")
87       (display val)
88       (display val (open-file fn "w"))
89       )
90   (display "\n" (current-error-port))
91   ""
92   )
93
94 (if (or (equal? (minor-version) "4.1")
95         (equal? (minor-version) "4")
96         (equal? (minor-version) "3.4"))
97     (define (embedded-pdf expr)
98       (let ((ps-string
99              (with-output-to-string
100                (lambda () (pdf-output-expression expr (current-output-port))))))
101         (string-append "\\embeddedpdf{" ps-string "}")))
102     (define (embedded-pdf expr)
103       (let
104           ((os (open-output-string)))
105         (pdf-output-expression expr os)
106         (string-append "\\embeddedpdf{" (get-output-string os) "}"))))
107
108
109 (define (comment s)
110   (simple-format #f "% ~a\n" s))
111
112 (define (end-output) 
113   (begin
114                                         ; uncomment for some stats about lily memory      
115                                         ;               (display (gc-stats))
116     (string-append "\n\\EndLilyPondOutput"
117                                         ; Put GC stats here.
118                    )))
119
120 (define (experimental-on)
121   "")
122
123 (define (repeat-slash w a t)
124   (embedded-pdf (list 'repeat-slash w a t)))
125 (define (fontify name-mag-pair exp)
126   (string-append (select-font name-mag-pair)
127                  exp))
128
129
130 (define (tex-encoded-fontswitch name-mag)
131   (let* ((iname-mag (car name-mag))
132          (ename-mag (cdr name-mag)))
133     (cons iname-mag
134           (cons ename-mag
135                 (string-append  "magfont"
136                           (string-encode-integer
137                            (hashq (car ename-mag) 1000000))
138                           "m"
139                           (string-encode-integer
140                            (inexact->exact (* 1000 (cdr ename-mag)))))))))
141 (define (define-fonts internal-external-name-mag-pairs)
142   (set! font-name-alist (map tex-encoded-fontswitch
143                              internal-external-name-mag-pairs))
144   (apply string-append
145          (map (lambda (x)
146                 (font-load-command (car x) (cdr x)))
147               (map cdr font-name-alist))))
148
149
150 (define (font-switch i)
151   (string-append
152    "\\" (font i) "\n"))
153
154 (define (font-def i s)
155   (string-append
156    "\\font" (font-switch i) "=" s "\n"))
157
158 (define (header-end)
159   (string-append
160    "\\def\\lilyoutputscalefactor{"
161    (number->string (cond
162                     ((equal? (ly-unit) "mm") (/ 72.0  25.4))
163                     ((equal? (ly-unit) "pt") (/ 72.0  72.27))
164                     (else (error "unknown unit" (ly-unit)))
165                     ))
166    "}\n"
167    "\\input lilyponddefs \\outputscale=\\lilypondpaperoutputscale \\lilypondpaperunit"
168    "\\turnOnPostScript"
169    "\\pdfcompresslevel=0"))
170
171 ;; Note: this string must match the string in ly2dvi.py!!!
172 (define (header creator generate) 
173   (string-append
174    "% Generated automatically by: " creator generate "\n"))
175
176 (define (invoke-char s i)
177   (string-append 
178    "\n\\" s "{" (inexact->string i 10) "}" ))
179
180 ;;
181 ;; need to do something to make this really safe.
182 ;;
183 (define (output-tex-string s)
184   (if security-paranoia
185       (if use-regex
186           (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
187           (begin (display "warning: not paranoid") (newline) s))
188       s))
189
190 (define (lily-def key val)
191   (let ((tex-key
192          (if use-regex
193              (regexp-substitute/global 
194               #f "_" (output-tex-string key) 'pre "X" 'post)      
195              (output-tex-string key)))
196         (tex-val (output-tex-string val)))
197     (if (equal? (sans-surrounding-whitespace tex-val) "")
198         (string-append "\\let\\" tex-key "\\undefined\n")
199         (string-append "\\def\\" tex-key "{" tex-val "}\n"))))
200
201 (define (number->dim x)
202   (string-append
203    ;;ugh ly-* in backend needs compatibility func for standalone output
204    (ly-number->string x) " \\outputscale "))
205
206 (define (placebox x y s) 
207   (string-append 
208    "\\placebox{"
209    (number->dim y) "}{" (number->dim x) "}{" s "}%\n"))
210
211 (define (bezier-bow l thick)
212   (embedded-pdf (list 'bezier-bow  `(quote ,l) thick)))
213
214 (define (bezier-sandwich l thick)
215   (embedded-pdf (list 'bezier-sandwich  `(quote ,l) thick)))
216
217 (define (start-system ht)
218   (string-append"\\vbox to " (number->dim ht) "{\\hbox{%\n"))
219
220 (define (stop-system) 
221   "}\\vss}\\interscoreline\n")
222 (define (stop-last-system)
223   "}\\vss}")
224 (define (filledbox breapth width depth height) 
225   (string-append 
226    "\\kern" (number->dim (- breapth))
227    "\\vrule width " (number->dim (+ breapth width))
228    "depth " (number->dim depth)
229    "height " (number->dim height) " "))
230
231 (define (roundfilledbox x y width height blotdiam)
232   (embedded-pdf (list 'roundfilledbox  x y width height blotdiam)))
233
234 (define (text s)
235   (string-append "\\hbox{" (output-tex-string s) "}"))
236
237 (define (draw-line thick fx fy tx ty)
238   (embedded-pdf (list 'draw-line thick fx fy tx ty)))
239
240 (define (define-origin file line col)
241   (if (procedure? point-and-click)
242       (string-append "\\special{src:\\string:"
243                      (point-and-click line col file)
244                      "}" )
245       "")
246   )
247
248                                         ; no-origin not supported in PDFTeX
249 (define (no-origin) "")
250
251
252
253 (define my-eval-in-module eval)
254
255 (if (or (equal? (minor-version) "4.1")
256         (equal? (minor-version) "4")
257         (equal? (minor-version) "3.4"))
258     (set! my-eval-in-module eval-in-module))
259
260 (define-public (pdftex-output-expression expr port)
261   (display (my-eval-in-module expr this-module) port) )