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