]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-pdftex.scm
ad3e3469b51aa859c0ab7cc3daae63f22139dda9
[lilypond.git] / scm / output-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--2004 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 output-pdftex))
14
15 (use-modules (scm output-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 blot)
52   (embedded-pdf (list 'beam  width slope thick blot)))
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 (define (embedded-pdf expr)
97   (let
98       ((os (open-output-string)))
99     (pdf-output-expression expr os)
100     (string-append "\\embeddedpdf{" (get-output-string os) "}")))
101
102
103 (define (comment s)
104   (simple-format #f "% ~a\n" s))
105
106 (define (end-output) 
107   (begin
108                                         ; uncomment for some stats about lily memory      
109                                         ;               (display (gc-stats))
110     (string-append "%\n\\lilypondend\n"
111                                         ; Put GC stats here.
112                    )))
113
114 (define (experimental-on)
115   "")
116
117 (define (repeat-slash w a t)
118   (embedded-pdf (list 'repeat-slash w a t)))
119 (define (fontify name-mag-pair exp)
120   (string-append (select-font name-mag-pair)
121                  exp))
122
123
124 (define (tex-encoded-fontswitch name-mag)
125   (let* ((iname-mag (car name-mag))
126          (ename-mag (cdr name-mag)))
127     (cons iname-mag
128           (cons ename-mag
129                 (string-append  "magfont"
130                           (string-encode-integer
131                            (hashq (car ename-mag) 1000000))
132                           "m"
133                           (string-encode-integer
134                            (inexact->exact (* 1000 (cdr ename-mag)))))))))
135 (define (define-fonts internal-external-name-mag-pairs)
136   (set! font-name-alist (map tex-encoded-fontswitch
137                              internal-external-name-mag-pairs))
138   (apply string-append
139          (map (lambda (x)
140                 (font-load-command (car x) (cdr x)))
141               (map cdr font-name-alist))))
142
143
144 (define (font-switch i)
145   (string-append
146    "\\" (font i) "\n"))
147
148 (define (font-def i s)
149   (string-append
150    "\\font" (font-switch i) "=" s "\n"))
151
152 (define (header-end)
153   (string-append
154    "\\def\\lilyoutputscalefactor{"
155    (number->string (cond
156                     ((equal? (ly:unit) "mm") (/ 72.0  25.4))
157                     ((equal? (ly:unit) "pt") (/ 72.0  72.27))
158                     (else (error "unknown unit" (ly:unit)))
159                     ))
160    "}%\n"
161    "\\ifx\\lilypondstart\\undefined\n"
162    "  \\input lilyponddefs\n"
163    "\\fi\n"
164    "\\outputscale=\\lilypondpaperoutputscale \\lilypondpaperunit\n"
165    "\\lilypondpostscript\n"
166    "\\pdfcompresslevel=0"))
167
168 ;; Note: this string must match the string in lilypond.py!!!
169 (define (header creator generate) 
170   (string-append
171    "% Generated automatically by: " creator generate "\n"))
172
173 (define (invoke-char s i)
174   (string-append 
175    "\n\\" s "{" (inexact->string i 10) "}" ))
176
177 ;;
178 ;; need to do something to make this really safe.
179 ;;
180 (define (output-tex-string s)
181   (if security-paranoia
182       (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post)
183       s))
184
185 (define (lily-def key val)
186   (let ((tex-key
187          (regexp-substitute/global 
188               #f "_" (output-tex-string key) 'pre "X" 'post))
189         (tex-val (output-tex-string val)))
190     (if (equal? (sans-surrounding-whitespace tex-val) "")
191         (string-append "\\let\\" tex-key "\\undefined\n")
192         (string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
193
194 (define (number->dim x)
195   (string-append
196    ;;ugh ly:* in backend needs compatibility func for standalone output
197    (ly:number->string x) " \\outputscale "))
198
199 (define (placebox x y s) 
200   (string-append "\\lyitem{"
201                  (ly:number->string y) "}{"
202                  (ly:number->string x) "}{"
203                  s "}%\n"))
204
205 (define (bezier-sandwich l thick)
206   (embedded-pdf (list 'bezier-sandwich  `(quote ,l) thick)))
207
208 (define (start-system wd ht)
209   (string-append "\\leavevmode\n"
210                  "\\scoreshift = " (number->dim (* ht 0.5)) "\n"
211                  "\\lilypondifundefined{lilypondscoreshift}%\n"
212                  "  {}%\n"
213                  "  {\\advance\\scoreshift by -\\lilypondscoreshift}%\n"
214                  "\\lybox{"
215                  (ly:number->string wd) "}{"
216                  (ly:number->string ht) "}{%\n"))
217
218 (define (stop-system) 
219   "}%\n%\n\\interscoreline\n%\n")
220 (define (stop-last-system)
221   "}%\n")
222
223 (define (filledbox breapth width depth height) 
224   (string-append "\\lyvrule{"
225                  (ly:number->string (- breapth)) "}{"
226                  (ly:number->string (+ breapth width)) "}{"
227                  (ly:number->string depth) "}{"
228                  (ly:number->string height) "}"))
229
230 (define (round-filled-box x y width height blotdiam)
231   (embedded-pdf (list 'round-filled-box  x y width height blotdiam)))
232
233 (define (text s)
234   (string-append "\\hbox{" (output-tex-string s) "}"))
235
236 (define (draw-line thick fx fy tx ty)
237   (embedded-pdf (list 'draw-line thick fx fy tx ty)))
238
239 (define (define-origin file line col)
240   (if (procedure? point-and-click)
241       (string-append "\\special{src:\\string:"
242                      (point-and-click line col file)
243                      "}" )
244       "")
245   )
246
247                                         ; no-origin not supported in PDFTeX
248 (define (no-origin) "")
249
250
251
252
253 (define-public (pdftex-output-expression expr port)
254   (display (eval expr this-module) port) )