]> git.donarmstrong.com Git - lilypond.git/blob - ps/music-drawing-routines.ps
a06e7b158c724e54f4393e1e142657526bbad8c3
[lilypond.git] / ps / music-drawing-routines.ps
1 %!PS-Adobe-1.0: music-drawing-routines.ps
2 %
3 % Functions for direct and embedded PostScript
4
5 % Careful with double % as comment prefix.
6 % Any %%X comment is interpreted as DSC comments.
7
8 % TODO: use dicts or prefixes to prevent namespace pollution.
9
10 /pdfmark where
11 {pop} {userdict /pdfmark /cleartomark load put} ifelse
12
13
14 % llx lly urx ury URI
15 /mark_URI
16 {
17     /uri exch def
18     /ury exch def
19     /urx exch def
20     /lly exch def
21     /llx exch def
22     [
23         /Rect [ llx lly urx ury ]
24         
25         /Border [ 0 0 0 ]
26
27         /Action
28             <<
29                 /Subtype /URI
30                 /URI uri
31             >>
32         /Subtype /Link
33     /ANN
34     pdfmark
35 }
36 bind def
37
38 % llx lly urx ury page
39 /mark_page_link
40 {
41     /page exch def
42     /ury exch def
43     /urx exch def
44     /lly exch def
45     /llx exch def
46     [
47         /Rect [ llx lly urx ury ]
48         /Border [ 0 0 0 ]
49         /Page page
50         /Subtype /Link
51     /ANN
52     pdfmark
53 }
54 bind def
55
56 % from adobe tech note 5002.
57 /BeginEPSF { %def
58     /b4_Inc_state save def % Save state for cleanup
59     /dict_count countdictstack def % Count objects on dict stack
60     /op_count count 1 sub def % Count objects on operand stack
61     userdict begin % Push userdict on dict stack
62     /showpage { } def % Redefine showpage, { } = null proc
63     0 setgray 0 setlinecap % Prepare graphics state
64     1 setlinewidth 0 setlinejoin
65     10 setmiterlimit [ ] 0 setdash newpath
66     /languagelevel where % If level not equal to 1 then
67     {pop languagelevel % set strokeadjust and
68     1 ne % overprint to their defaults.
69       {false setstrokeadjust false setoverprint
70       } if
71     } if
72 } bind def
73
74 /EndEPSF { %def
75   count op_count sub {pop} repeat % Clean up stacks
76   countdictstack dict_count sub {end} repeat
77   b4_Inc_state restore
78 } bind def
79
80 /stroke_and_fill {
81         gsave
82                 stroke
83         grestore
84         fill
85 } bind def
86
87 /vector_add { % x1 y1 x2 y2 vector_add x1+x2 y1+y2
88         exch
89         4 1 roll
90         add
91         3 1 roll
92         add
93         exch
94 } bind def
95
96 /draw_round_box % width height x y blot
97 {
98         dup
99         0.0 gt {
100                 setlinewidth % w h x y
101                 0 setlinecap
102                 1 setlinejoin
103
104                 rmoveto % w h
105                 currentpoint % w h x1 y1
106                 4 2 roll % x1 y1 w h
107                 4 copy
108                 rectfill
109                 rectstroke
110         } {
111                 pop % w h x y
112                 rmoveto % w h
113                 currentpoint % w h x1 y1
114                 4 2 roll % x1 y1 w h
115                 rectfill
116         } ifelse
117 } bind def
118
119 /draw_polygon % fill? x(n) y(n) x(n-1) y(n-1) ... x(0) y(0) n blot
120 {
121         setlinewidth %set to blot
122
123         0 setlinecap
124         1 setlinejoin
125
126         3 1 roll
127         /polygon_x
128         currentpoint
129         /polygon_y exch def
130         def
131         rmoveto % x(0) y(0)
132         { polygon_x polygon_y vector_add lineto } repeat % n times
133         closepath
134         { %fill?
135                 stroke_and_fill
136         }{
137                 stroke
138         } ifelse
139 } bind def
140
141 /draw_repeat_slash % x-width width height draw_repeat_slash
142 {
143         2 index % duplicate x-width
144         1 setlinecap
145         1 setlinejoin
146         
147           0  rlineto % x-width 0
148              rlineto % width height
149         neg 0 rlineto % -x-width 0
150         closepath fill
151 } bind def
152
153 % this is for drawing slurs and barre-indicators.
154 /draw_bezier_sandwich  % x5 y5 x6 y6 x7 y7
155                        % x4 y4
156                        % x1 y1 x2 y2 x3 y3
157                        % x0 y0
158                        % linewidth draw_bezier_sandwich
159 {
160         gsave
161         currentpoint translate
162         % round ending and round beginning
163         1 setlinejoin 1 setlinecap
164         setlinewidth
165         moveto
166         curveto
167         lineto
168         curveto
169         closepath
170         stroke_and_fill
171         grestore
172 } bind def
173
174 /draw_circle % filled? radius thickness draw_circle
175 {
176         setlinewidth    % f? r
177         currentpoint    % f? r x0 y0
178         3 2 roll        % f? x0 y0 r
179         dup 0 rmoveto
180         0 360 arc closepath
181                 { stroke_and_fill }
182                 { stroke }
183         ifelse
184 } bind def
185
186 /draw_oval % filled? x-radius y-radius thickness draw_ellipse
187 {
188   setlinewidth % f? x-r y-r
189   /yrad exch def
190   /xrad exch def
191   xrad 0 rmoveto
192   0 yrad -2 xrad mul dup yrad exch 0 rcurveto
193   0 yrad neg dup 2 xrad mul dup 3 1 roll 0 rcurveto
194   closepath
195       { stroke_and_fill}
196       { stroke }
197   ifelse
198 } bind def
199
200 /draw_ellipse % filled? x-radius y-radius thickness draw_ellipse
201 {
202   setlinewidth % f? x-r y-r
203   /savematrix matrix currentmatrix def
204   scale % f?
205   currentpoint
206   1 0 rmoveto
207   1 0 360  arc closepath
208   savematrix setmatrix
209              { stroke_and_fill}
210              { stroke }
211   ifelse
212 } bind def
213
214 /draw_partial_ellipse % filled connect x-radius y-radius startangle endangle thickness draw_partial_ellipse
215 % Note that filled is not boolean to permit for different graylevels (ie for trill keys)
216 {
217   gsave
218   currentpoint translate
219   /thickness exch def
220   /endangle exch def
221   /startangle exch def
222   /y_radius exch def
223   /x_radius exch def
224   /endrad x_radius y_radius mul
225     x_radius x_radius mul
226     endangle cos endangle cos mul mul
227     y_radius y_radius mul
228     endangle sin endangle sin mul mul add sqrt div def
229   /endangle endangle sin endrad mul y_radius div
230     endangle cos endrad mul x_radius div atan def
231   /startrad x_radius y_radius mul
232     x_radius x_radius mul
233       startangle cos startangle cos mul mul
234     y_radius y_radius mul
235       startangle sin startangle sin mul mul add sqrt div def
236   /startangle startangle sin startrad mul y_radius div
237     startangle cos startrad mul x_radius div atan def
238   /connect exch def
239   /filled exch def
240   /savematrix matrix currentmatrix def
241   thickness setlinewidth
242   x_radius y_radius scale
243   startangle cos startangle sin moveto
244   0 0 1 startangle
245     startangle endangle eq { endangle 360 add } { endangle } ifelse
246     arc
247   connect {
248     startangle cos startangle sin moveto endangle cos endangle sin lineto }
249     if
250   savematrix setmatrix filled { stroke_and_fill } { stroke } ifelse
251   grestore
252 } bind def
253
254 /draw_line % dx dy x1 y1 thickness draw_line
255 {
256         setlinewidth % dx dy x1 y1
257         1 setlinecap
258         1 setlinejoin
259         rmoveto % dx dy
260         rlineto
261         stroke
262 } bind def
263
264 /draw_dashed_line % dx dy thickness dashpattern offset draw_dashed_line
265 {
266         1 setlinecap
267         1 setlinejoin
268         setdash % dx dy thickness
269         setlinewidth %dx dy
270         rlineto
271         stroke
272         [] 0 setdash % reset dash pattern
273 } bind def
274
275 /print_glyphs % w dx dy glyph print_glyphs
276 {
277         {
278                 currentpoint %w dx dy glyph x0 y0
279                 5 2 roll %w x0 y0 dx dy glyph
280                 3 1 roll %w x0 y0 glyph dx dy
281                 rmoveto %w x0 y0 glyph
282                 glyphshow %w x0 y0
283                 moveto %w
284                 0 rmoveto
285         }repeat
286 }bind def
287 %end music-drawing-routines.ps