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