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