]> git.donarmstrong.com Git - lilypond.git/blob - scm/pdf.scm
50c674e88f6e5d516a17bc04a0821dbc86a01aa2
[lilypond.git] / scm / pdf.scm
1 ;;; pdf.scm -- implement Scheme output routines for PDF.
2 ;;;
3 ;;;  source file of the GNU LilyPond music typesetter
4 ;;; 
5 ;;; (c) 2001 Stephen Peters <portnoy@portnoy.org>
6
7 ; currently no font commands; this is a helper for pdftex.scm.
8
9 (define (pdf-scm action-name)
10   ; simple commands to store and update currentpoint.  This makes the
11   ; other procedures simple rewrites of the PostScript code.
12   (define currentpoint (cons 0 0))
13   (define (showcp) 
14     (string-append (ly-number->string (car currentpoint)) " " 
15                    (ly-number->string (cdr currentpoint)) " "))
16   (define (moveto x y)
17     (set! currentpoint (cons x y))
18     (string-append (showcp) "m "))
19   (define (moveto-pair pair)
20     (moveto (car pair) (cdr pair)))
21   (define (rmoveto x y)
22     (moveto (+ x (car currentpoint)) (+ y (cdr currentpoint))))
23   (define (lineto x y)
24     (set! currentpoint (cons x y))
25     (string-append (showcp) "l "))
26   (define (lineto-pair pair)
27     (lineto (car pair) (cdr pair)))
28   (define (rlineto x y)
29     (lineto (+ x (car currentpoint)) (+ y (cdr currentpoint))))
30   (define (curveto x1 y1 x2 y2 x y)
31     (set! currentpoint (cons x y))
32     (string-append (ly-number->string x1) (ly-number->string y1)
33                    (ly-number->string x2) (ly-number->string y2)
34                    (ly-number->string x) (ly-number->string y) "c "))
35   (define (curveto-pairs pt1 pt2 pt)
36     (curveto (car pt1) (cdr pt1) (car pt2) (cdr pt2) (car pt) (cdr pt)))
37   (define (closefill) "h f ")
38   (define (closestroke) "S ")
39   (define (setlinewidth w) (string-append (ly-number->string w) "w "))
40   (define (setgray g) (string-append (ly-number->string g) "g "))
41   (define (setlineparams) "1 j 1 J ")
42   
43   (define (beam width slope thick)
44     (let ((ht (* slope width)))
45       (string-append (moveto 0 (- (/ thick 2)))
46                      (rlineto width ht)
47                      (rlineto 0 thick)
48                      (lineto 0 (/ thick 2))
49                      (closefill))))
50
51   (define (comment s) 
52     (string-append "% " s "\n"))
53
54   (define (brack-traject pair ds alpha)
55     (let ((alpha-rad (* alpha (/ 3.141592654 180))))
56       (cons (+ (car pair) (* (cos alpha-rad) ds))
57             (+ (cdr pair) (* (sin alpha-rad) ds)))))
58     
59   (define (bracket arch_angle arch_width arch_height height arch_thick thick)
60     (let* ((halfht (+ (/ height 2) thick))
61            (farpt (cons (+ thick arch_height) 
62                         (+ (- halfht arch_thick) arch_width)))
63            (halfbrack 
64             (string-append (moveto 0 0)
65                            (lineto thick 0)
66                            (lineto thick (- halfht arch_thick))
67                            (curveto-pairs
68                             (brack-traject (cons thick 
69                                                  (- halfht arch_thick))
70                                            (* 0.4 arch_height) 0)
71                             (brack-traject farpt 
72                                            (* -0.25 arch_height) 
73                                            arch_angle)
74                             farpt)
75                            (curveto-pairs 
76                             (brack-traject farpt
77                                            (* -0.15 arch_height)
78                                            arch_angle)
79                             (brack-traject (cons (/ thick 2) halfht)
80                                            (/ arch_height 2) 0)
81                             (cons 0 halfht))
82                            (lineto 0 0)
83                            (closefill))))
84       (string-append (setlinewidth (/ thick 2))
85                      (setlineparams)
86                      "q 1 0 0 -1 0 0 cm " ; flip coords
87                      halfbrack
88                      "Q " ; grestore
89                      halfbrack)))
90   
91   (define (char i)
92     (invoke-char " show" i))
93
94   (define (hairpin thick width starth endh )
95     (string-append (setlinewidth thick)
96                    (moveto 0 starth)
97                    (lineto width endh)
98                    (moveto 0 (- starth))
99                    (lineto width (- endh))
100                    (closestroke)))
101
102   (define (dashed-slur thick dash l)
103     (string-append (setlineparams)
104                    "[ " (ly-number->string dash) " "
105                    (ly-number->string (* 10 thick)) " ] 0 d "
106                    (setlinewidth thick)
107                    (moveto-pair (car l))
108                    (apply curveto (cdr l))
109                    (closestroke)))
110                    
111   (define (dashed-line thick on off dx dy)
112     (string-append (setlineparams)
113                    "[ " (ly-number->string on) " "
114                    (ly-number->string off) " ] 0 d "
115                    (setlinewidth thick)
116                    (moveto 0 0)
117                    (lineto dx dy)
118                    (closestroke)))
119
120   (define (repeat-slash width slope beamthick)
121     (let* ((height (/ beamthick slope))
122            (xwid (sqrt (+ (* beamthick beamthick) (* height height)))))
123       (string-append (moveto 0 0)
124                      (rlineto xwid 0)
125                      (rlineto width (* slope width))
126                      (rlineto (- xwid) 0)
127                      (closefill))))
128
129   (define (end-output) "")
130   
131   (define (experimental-on) "")
132   
133   (define (filledbox breadth width depth height) 
134     (string-append (ly-number->string (- breadth))
135                    (ly-number->string (- depth))
136                    (ly-number->string (+ breadth width))
137                    (ly-number->string (+ depth height))
138                    " re f "))
139
140   (define (font-def i s) "")
141
142   (define (font-switch i) "")
143
144   (define (header-end) "")
145   
146   (define (lily-def key val) "")
147
148   (define (header creator generate) "")
149   
150   (define (invoke-char s i)
151     (string-append 
152      "(\\" (inexact->string i 8) ") " s " " ))
153   
154   (define (invoke-dim1 s d) 
155     (string-append
156      (ly-number->string (* d  (/ 72.27 72))) " " s ))
157
158   (define (placebox x y s) "")
159
160   (define (bezier-sandwich l thick)
161     (string-append (setlinewidth thick)
162                    (moveto-pair (list-ref l 7))
163                    (curveto-pairs (list-ref l 4)
164                                   (list-ref l 5)
165                                   (list-ref l 6))
166                    (lineto-pair (list-ref l 3))
167                    (curveto-pairs (list-ref l 0)
168                                   (list-ref l 1)
169                                   (list-ref l 2))
170                    "B "))
171
172   (define (start-line height) "")
173   
174   (define (stem breadth width depth height) 
175     (filledbox breadth width depth height))
176
177   (define (stop-line) "")
178
179   (define (text s) "")
180
181   (define (volta h w thick vert_start vert_end)
182     (string-append (setlinewidth thick)
183                    (setlineparams)
184                    (if (= vert_start 0) 
185                        (string-append (moveto 0 0)
186                                       (lineto 0 h))
187                        (moveto 0 h))
188                    (lineto w h)
189                    (if (= vert_end 0) (lineto w 0) "")
190                    (closestroke)))
191
192   (define (tuplet ht gap dx dy thick dir)
193     (let ((gapy (* (/ dy dx) gap)))
194       (string-append (setlinewidth thick)
195                      (setlineparams)
196                      (moveto 0 (- (* ht dir)))
197                      (lineto 0 0)
198                      (lineto (/ (- dx gap) 2)
199                              (/ (- dy gapy) 2))
200                      (moveto (/ (+ dx gap) 2)
201                              (/ (+ dy gapy) 2))
202                      (lineto dx dy)
203                      (lineto dx (- dy (* ht dir)))
204                      (closestroke))))
205
206   (define (unknown) "\n unknown\n")
207
208   ; Problem here -- we're using /F18 for the font, but we don't know
209   ; for sure that that will exist.
210   (define (ez-ball ch letter-col ball-col)
211     (let ((origin (cons 0.45 0)))
212       (string-append (setgray 0)
213                      (setlinewidth 1.1)
214                      (moveto-pair origin) (lineto-pair origin)
215                      (closestroke)
216                      (setgray ball-col)
217                      (setlinewidth 0.9)
218                      (moveto-pair origin) (lineto-pair origin)
219                      (closestroke)
220                      (setgray letter-col)
221                      (moveto-pair origin)
222                      "BT "
223                      "/F18 0.85 Tf "
224                      "-0.28 -0.30 Td " ; move for text block
225                      "[(" ch ")] TJ ET ")))
226
227   (define (define-origin a b c ) "")
228   (define (no-origin) "")
229   
230   ;; PS
231   (cond ((eq? action-name 'all-definitions)
232          `(begin
233             (define beam ,beam)
234             (define tuplet ,tuplet)
235             (define bracket ,bracket)
236             (define char ,char)
237             (define volta ,volta)
238             (define bezier-sandwich ,bezier-sandwich)
239             (define dashed-line ,dashed-line) 
240             (define dashed-slur ,dashed-slur) 
241             (define hairpin ,hairpin) 
242             (define end-output ,end-output)
243             (define experimental-on ,experimental-on)
244             (define filledbox ,filledbox)
245             (define font-def ,font-def)
246             (define font-switch ,font-switch)
247             (define header-end ,header-end)
248             (define lily-def ,lily-def)
249             (define font-load-command ,font-load-command)
250             (define header ,header) 
251             (define invoke-char ,invoke-char) 
252             (define invoke-dim1 ,invoke-dim1)
253             (define placebox ,placebox)
254             (define repeat-slash ,repeat-slash) 
255             (define select-font ,select-font)
256             (define start-line ,start-line)
257             (define stem ,stem)
258             (define stop-line ,stop-line)
259             (define stop-last-line ,stop-line)
260             (define text ,text)
261             (define no-origin ,no-origin)
262             (define define-origin ,define-origin)
263             (define ez-ball ,ez-ball)
264             ))
265         ((eq? action-name 'tuplet) tuplet)
266         ((eq? action-name 'beam) beam)
267         ((eq? action-name 'bezier-sandwich) bezier-sandwich)
268         ((eq? action-name 'bracket) bracket)
269         ((eq? action-name 'char) char)
270         ((eq? action-name 'dashed-line) dashed-line) 
271         ((eq? action-name 'dashed-slur) dashed-slur) 
272         ((eq? action-name 'hairpin) hairpin)
273         ((eq? action-name 'experimental-on) experimental-on)
274         ((eq? action-name 'ez-ball) ez-ball)    
275         ((eq? action-name 'filledbox) filledbox)
276         ((eq? action-name 'repeat-slash) repeat-slash)
277         ((eq? action-name 'select-font) select-font)
278         ((eq? action-name 'volta) volta)
279         (else (error "unknown tag -- PDF-SCM " action-name))
280         )
281   )
282
283 (define (scm-pdf-output)
284   (primitive-eval (pdf-scm 'all-definitions)))
285
286 ; Local Variables:
287 ; scheme-program-name: "guile"
288 ; End: