]> git.donarmstrong.com Git - lilypond.git/blob - ps/music-drawing-routines.ps
Add feature to link to a particular page or a page containing a given label
[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         setlinewidth % w h x y
99         0 setlinecap
100         1 setlinejoin
101
102         rmoveto % w h
103         currentpoint % w h x1 y1
104         4 2 roll % x1 y1 w h
105         4 copy
106         rectfill
107         rectstroke
108 } bind def
109
110 /draw_polygon % fill? x(n) y(n) x(n-1) y(n-1) ... x(0) y(0) n blot
111 {
112         setlinewidth %set to blot
113
114         0 setlinecap
115         1 setlinejoin
116
117         3 1 roll
118         /polygon_x
119         currentpoint
120         /polygon_y exch def
121         def
122         rmoveto % x(0) y(0)
123         { polygon_x polygon_y vector_add lineto } repeat % n times
124         closepath
125         { %fill?
126                 stroke_and_fill
127         }{
128                 stroke
129         } ifelse
130 } bind def
131
132 /draw_repeat_slash % x-width width height draw_repeat_slash
133 {
134         2 index % duplicate x-width
135         1 setlinecap
136         1 setlinejoin
137         
138           0  rlineto % x-width 0
139              rlineto % width height
140         neg 0 rlineto % -x-width 0
141         closepath fill
142 } bind def
143
144 % this is for drawing slurs and barre-indicators.
145 /draw_bezier_sandwich  % x5 y5 x6 y6 x7 y7
146                        % x4 y4
147                        % x1 y1 x2 y2 x3 y3
148                        % x0 y0
149                        % linewidth draw_bezier_sandwich
150 {
151         gsave
152         currentpoint translate
153         % round ending and round beginning
154         1 setlinejoin 1 setlinecap
155         setlinewidth
156         moveto
157         curveto
158         lineto
159         curveto
160         closepath
161         stroke_and_fill
162         grestore
163 } bind def
164
165 /draw_dot % radius x y draw_dot
166 {
167         rmoveto
168         currentpoint
169         3 2 roll
170         0 360 arc closepath stroke_and_fill
171 } bind def
172
173 /draw_circle % filled? radius thickness draw_circle
174 {
175         setlinewidth    % f? r
176         currentpoint    % f? r x0 y0
177         3 2 roll        % f? x0 y0 r
178         dup 0 rmoveto
179         0 360 arc closepath
180                 { stroke_and_fill }
181                 { stroke }
182         ifelse
183 } bind def
184
185 /draw_oval % filled? x-radius y-radius thickness draw_ellipse
186 {
187   setlinewidth % f? x-r y-r
188   /yrad exch def
189   /xrad exch def
190   xrad 0 rmoveto
191   0 yrad -2 xrad mul dup yrad exch 0 rcurveto
192   0 yrad neg dup 2 xrad mul dup 3 1 roll 0 rcurveto
193   closepath
194       { stroke_and_fill}
195       { stroke }
196   ifelse
197 } bind def
198
199 /draw_ellipse % filled? x-radius y-radius thickness draw_ellipse
200 {
201   setlinewidth % f? x-r y-r
202   /savematrix matrix currentmatrix def
203   scale % f?
204   currentpoint
205   1 0 rmoveto
206   1 0 360  arc closepath
207   savematrix setmatrix
208              { stroke_and_fill}
209              { stroke }
210   ifelse
211 } bind def
212
213 /draw_partial_ellipse % filled connect x-radius y-radius startangle endangle thickness draw_partial_ellipse
214 % Note that filled is not boolean to permit for different graylevels (ie for trill keys)
215 {
216   gsave
217   currentpoint translate
218   /thickness exch def
219   /endangle exch def
220   /startangle exch def
221   /y_radius exch def
222   /x_radius exch def
223   /endrad x_radius y_radius mul
224     x_radius x_radius mul
225     endangle cos endangle cos mul mul
226     y_radius y_radius mul
227     endangle sin endangle sin mul mul add sqrt div def
228   /endangle endangle sin endrad mul y_radius div
229     endangle cos endrad mul x_radius div atan def
230   /startrad x_radius y_radius mul
231     x_radius x_radius mul
232       startangle cos startangle cos mul mul
233     y_radius y_radius mul
234       startangle sin startangle sin mul mul add sqrt div def
235   /startangle startangle sin startrad mul y_radius div
236     startangle cos startrad mul x_radius div atan def
237   /connect exch def
238   /filled exch def
239   /savematrix matrix currentmatrix def
240   thickness setlinewidth
241   x_radius y_radius scale
242   startangle cos startangle sin moveto
243   0 0 1 startangle
244     startangle endangle eq { endangle 360 add } { endangle } ifelse
245     arc
246   connect {
247     startangle cos startangle sin moveto endangle cos endangle sin lineto }
248     if
249   savematrix setmatrix filled { stroke_and_fill } { stroke } ifelse
250   grestore
251 } bind def
252
253 /draw_line % dx dy x1 y1 thickness draw_line
254 {
255         setlinewidth % dx dy x1 y1
256         1 setlinecap
257         1 setlinejoin
258         rmoveto % dx dy
259         rlineto
260         stroke
261 } bind def
262
263 /draw_dashed_line % dx dy thickness dashpattern offset draw_dashed_line
264 {
265         1 setlinecap
266         1 setlinejoin
267         setdash % dx dy thickness
268         setlinewidth %dx dy
269         rlineto
270         stroke
271         [] 0 setdash % reset dash pattern
272 } bind def
273
274 /draw_dashed_slur % controls thickness dash
275 {
276 gsave
277         currentpoint translate
278         1 setlinecap
279         1 setlinejoin
280         setdash
281         setlinewidth
282         8 2 roll
283         moveto
284         curveto
285         stroke
286 grestore
287 } bind def
288
289 /print_glyphs % w dx dy glyph print_glyphs
290 {
291         {
292                 currentpoint %w dx dy glyph x0 y0
293                 5 2 roll %w x0 y0 dx dy glyph
294                 3 1 roll %w x0 y0 glyph dx dy
295                 rmoveto %w x0 y0 glyph
296                 glyphshow %w x0 y0
297                 moveto %w
298                 0 rmoveto
299         }repeat
300 }bind def
301 %end music-drawing-routines.ps