release commit
[lilypond.git] / scm / output-pdf.scm
blob78cbe58677f23e4c09f638dec2a27e5dbff381ed
1 ;;; pdf.scm -- implement Scheme output routines for PDF.
2 ;;;
3 ;;;  source file of the GNU LilyPond music typesetter
4 ;;; 
5 ;;; (c)  2001--2004 Stephen Peters <portnoy@portnoy.org>
8 ;currently no font commands; this is a helper for pdftex.scm.
10 (define-module (scm output-pdf))
14 (define this-module (current-module))
16 (use-modules
17  (guile)
18  (lily))
22 ; simple commands to store and update currentpoint.  This makes the
23 ; other procedures simple rewrites of the PostScript code.
25 (define currentpoint (cons 0 0))
26 (define (showcp) 
27   (string-append (number-pair->string currentpoint) " "))
28 (define (moveto x y)
29   (set! currentpoint (cons x y))
30   (string-append (showcp) "m "))
31 (define (moveto-pair pair)
32   (moveto (car pair) (cdr pair)))
33 (define (rmoveto x y)
34   (moveto (+ x (car currentpoint)) (+ y (cdr currentpoint))))
35 (define (lineto x y)
36   (set! currentpoint (cons x y))
37   (string-append (showcp) "l "))
38 (define (lineto-pair pair)
39   (lineto (car pair) (cdr pair)))
40 (define (rlineto x y)
41   (lineto (+ x (car currentpoint)) (+ y (cdr currentpoint))))
42 (define (curveto x1 y1 x2 y2 x y)
43   (set! currentpoint (cons x y))
44   (string-append (ly:number->string x1) (ly:number->string y1)
45                  (ly:number->string x2) (ly:number->string y2)
46                  (ly:number->string x) (ly:number->string y) "c "))
47 (define (curveto-pairs pt1 pt2 pt)
48   (curveto (car pt1) (cdr pt1) (car pt2) (cdr pt2) (car pt) (cdr pt)))
49 (define (closefill) "h f ")
50 (define (closestroke) "S ")
51 (define (setlinewidth w) (string-append (ly:number->string w) "w "))
52 (define (setgray g) (string-append (ly:number->string g) "g "))
53 (define (setlineparams) "1 j 1 J ")
55 (define (beam width slope thick blot)
56   (let ((ht (* slope width)))
57     (string-append (moveto 0 (- (/ thick 2)))
58                    (rlineto width ht)
59                    (rlineto 0 thick)
60                    (lineto 0 (/ thick 2))
61                    (closefill))))
63 (define (comment s) 
64   (string-append "% " s "\n"))
66 (define (brack-traject pair ds alpha)
67   (let ((alpha-rad (* alpha (/ 3.141592654 180))))
68     (cons (+ (car pair) (* (cos alpha-rad) ds))
69           (+ (cdr pair) (* (sin alpha-rad) ds)))))
71 (define (bracket arch_angle arch_width arch_height height arch_thick thick)
72   (let* ((halfht (+ (/ height 2) thick))
73          (farpt (cons (+ thick arch_height) 
74                       (+ (- halfht arch_thick) arch_width)))
75          (halfbrack 
76           (string-append (moveto 0 0)
77                          (lineto thick 0)
78                          (lineto thick (- halfht arch_thick))
79                          (curveto-pairs
80                           (brack-traject (cons thick 
81                                                (- halfht arch_thick))
82                                          (* 0.4 arch_height) 0)
83                           (brack-traject farpt 
84                                          (* -0.25 arch_height) 
85                                          arch_angle)
86                           farpt)
87                          (curveto-pairs 
88                           (brack-traject farpt
89                                          (* -0.15 arch_height)
90                                          arch_angle)
91                           (brack-traject (cons (/ thick 2) halfht)
92                                          (/ arch_height 2) 0)
93                           (cons 0 halfht))
94                          (lineto 0 0)
95                          (closefill))))
96     (string-append (setlinewidth (/ thick 2))
97                    (setlineparams)
98                    "q 1 0 0 -1 0 0 cm " ; flip coords
99                    halfbrack
100                    "Q " ; grestore
101                    halfbrack)))
103 (define (char i)
104   (invoke-char " show" i))
107 (define (dashed-slur thick dash l)
108   (string-append (setlineparams)
109                  "[ " (ly:number->string dash) " "
110                  (ly:number->string (* 10 thick)) " ] 0 d "
111                  (setlinewidth thick)
112                  (moveto-pair (car l))
113                  (apply curveto (cdr l))
114                  (closestroke)))
116 (define (dashed-line thick on off dx dy)
117   (string-append (setlineparams)
118                  "[ " (ly:number->string on) " "
119                  (ly:number->string off) " ] 0 d "
120                  (setlinewidth thick)
121                  (moveto 0 0)
122                  (lineto dx dy)
123                  (closestroke)))
125 (define (repeat-slash width slope beamthick)
126   (let* ((height (/ beamthick slope))
127          (xwid (sqrt (+ (* beamthick beamthick) (* height height)))))
128     (string-append (moveto 0 0)
129                    (rlineto xwid 0)
130                    (rlineto width (* slope width))
131                    (rlineto (- xwid) 0)
132                    (closefill))))
134 (define (end-output) "")
136 (define (experimental-on) "")
138 (define (filledbox breadth width depth height) 
139   (string-append (ly:number->string (- breadth))
140                  (ly:number->string (- depth))
141                  (ly:number->string (+ breadth width))
142                  (ly:number->string (+ depth height))
143                  " re f "))
145 (define (round-filled-box breadth width depth height blotdiam)
146   (let* ((rad (/ blotdiam 2))
147          (h (- height rad))
148          (d (- depth rad))
149          (w (- width rad))
150          (b (- breadth rad)))
151     (string-append " 0 J "
152                    (setlinewidth blotdiam)
153                    "1 j "
154                    (moveto (- b) (- d))
155                    (rlineto (+ b w) 0)
156                    (rlineto 0 (+ d h))
157                    (rlineto (- (+ b w)) 0)
158                    (rlineto 0 (- (+ d h)))
159                    "b ")))
161 ;; PDF doesn't have the nifty arc operator.  This uses a fast
162 ;; approximation with two curves.  It bulges out a bit more than a
163 ;; true circle should along the 45 degree axes, but most users won't
164 ;; notice.
165 (define (dot x y radius)
166   (string-append (moveto (- x radius) y)
167                  (curveto (- x radius) (+ y (* 1.3333 radius))
168                           (+ x radius) (+ y (* 1.3333 radius))
169                           (+ x radius) y)
170                  (curveto (+ x radius) (- y (* 1.3333 radius))
171                           (- x radius) (- y (* 1.3333 radius))
172                           (- x radius) y)
173                  "f "))
176 (define (round-filled-box breadth width depth height blot) 
177   (filledbox breadth width depth height))
179 (define (font-def i s) "")
181 (define (font-switch i) "")
183 (define (header-end) "")
185 (define (lily-def key val) "")
187 (define (header creator generate) "")
189 (define (invoke-char s i)
190   (string-append 
191    "(\\" (inexact->string i 8) ") " s " " ))
193 (define (placebox x y s) "")
195 (define (bezier-sandwich l thick)
196   (string-append (setlinewidth thick)
197                  (moveto-pair (list-ref l 7))
198                  (curveto-pairs (list-ref l 4)
199                                 (list-ref l 5)
200                                 (list-ref l 6))
201                  (lineto-pair (list-ref l 3))
202                  (curveto-pairs (list-ref l 0)
203                                 (list-ref l 1)
204                                 (list-ref l 2))
205                  "B "
206                  (bezier-ending (list-ref l 3) (list-ref l 0) (list-ref l 5))
207                  (bezier-ending (list-ref l 7) (list-ref l 0) (list-ref l 5))))
209 (define (bezier-ending z0 z1 z2)
210   (let ((x0 (car z0))
211         (y0 (cdr z0))
212         (x1 (car z1))
213         (y1 (cdr z1))
214         (x2 (car z2))
215         (y2 (cdr z2)))
216     (dot x0 y0 
217          (/ (sqrt (+ (* (- x1 x2) (- x1 x2)) 
218                      (* (- y1 y2) (- y1 y2)))) 2))))
221 (define (start-system width height) "")
223 (define (stem breadth width depth height) 
224   (filledbox breadth width depth height))
226 (define (stop-system) "")
228 (define (text s) "")
230 (define (polygon points blotdiameter) "") ;; TODO
232 (define (draw-line thick fx fy tx ty)
233   (string-append (setlineparams)
234                  (setlinewidth thick)
235                  (moveto fx fy)
236                  (lineto tx ty)
237                  "S "))
239 (define (unknown) "\n unknown\n")
241 ; Problem here -- we're using /F18 for the font, but we don't know 
242 ; for sure that that will exist.
244 (define (ez-ball ch letter-col ball-col)
245   (let ((origin (cons 0.45 0)))
246     (string-append (setgray 0)
247                    (setlinewidth 1.1)
248                    (moveto-pair origin) (lineto-pair origin)
249                    (closestroke)
250                    (setgray ball-col)
251                    (setlinewidth 0.9)
252                    (moveto-pair origin) (lineto-pair origin)
253                    (closestroke)
254                    (setgray letter-col)
255                    (moveto-pair origin)
256                    "BT "
257                    "/F18 0.85 Tf "
258                    "-0.28 -0.30 Td " ; move for text block
259                    "[(" ch ")] TJ ET ")))
261 (define (define-origin a b c ) "")
262 (define (no-origin) "")
264 (define-public (pdf-output-expression expr port)
265   (display (eval expr this-module) port) )
268 ; Local Variables:
269 ; scheme-program-name: "guile"
270 ; End: