2 using System.Reflection;
5 namespace Cairo.Snippets
9 public static string[] snippets = {
30 "xxx_multi_segment_caps",
34 static Type[] types = new Type[] {typeof (Context), typeof (int), typeof (int)};
35 public static void InvokeSnippet (Snippets snip, string snippet, Context cr, int width, int height)
37 MethodInfo m = snip.GetType ().GetMethod(snippet, types);
38 m.Invoke (snip, new Object[] {cr, width, height});
41 public void Normalize (Context cr, int width, int height)
43 cr.Scale (width, height);
47 public void arc(Context cr, int width, int height)
49 PointD c = new PointD (0.5, 0.5);
51 double angle1 = 45.0 * (Math.PI/180.0); /* angles are specified */
52 double angle2 = 180.0 * (Math.PI/180.0); /* in radians */
54 Normalize(cr, width, height);
56 cr.Arc(c.X, c.Y, radius, angle1, angle2);
60 cr.Color = new Color (1, 0.2, 0.2, 0.6);
61 cr.Arc(c.X, c.Y, 0.05, 0, 2*Math.PI);
64 cr.Arc(c.X, c.Y, radius, angle1, angle1);
66 cr.Arc(c.X, c.Y, radius, angle2, angle2);
71 public void arc_negative(Context cr, int width, int height)
73 PointD c = new PointD(0.5, 0.5);
75 double angle1 = 45.0 * (Math.PI/180.0); /* angles are specified */
76 double angle2 = 180.0 * (Math.PI/180.0); /* in radians */
78 Normalize(cr, width, height);
80 cr.ArcNegative(c.X, c.Y, radius, angle1, angle2);
84 cr.Color = new Color (1, 0.2, 0.2, 0.6);
85 cr.Arc(c.X, c.Y, 0.05, 0, 2*Math.PI);
88 cr.Arc(c.X, c.Y, radius, angle1, angle1);
90 cr.Arc(c.X, c.Y, radius, angle2, angle2);
95 public void clip(Context cr, int width, int height)
97 Normalize (cr, width, height);
99 cr.Arc(0.5, 0.5, 0.3, 0, 2 * Math.PI);
102 cr.NewPath(); // current path is not consumed by cairo_clip()
103 cr.Rectangle(0, 0, 1, 1);
105 cr.Color = new Color (0, 1, 0);
113 public void clip_image(Context cr, int width, int height)
115 Normalize (cr, width, height);
116 cr.Arc (0.5, 0.5, 0.3, 0, 2*Math.PI);
118 cr.NewPath (); // path not consumed by clip()
120 ImageSurface image = new ImageSurface ("data/romedalen.png");
122 int h = image.Height;
124 cr.Scale (1.0/w, 1.0/h);
126 cr.SetSourceSurface (image, 0, 0);
132 public void curve_to(Context cr, int width, int height)
135 double x1=0.4, y1=0.9, x2=0.6, y2=0.1, x3=0.9, y3=0.5;
137 Normalize (cr, width, height);
140 cr.CurveTo(x1, y1, x2, y2, x3, y3);
144 cr.Color = new Color (1, 0.2, 0.2, 0.6);
153 public void curve_rectangle(Context cr, int width, int height)
155 // a custom shape, that could be wrapped in a function
156 double x0 = 0.1, //< parameters like cairo_rectangle
160 radius = 0.4; //< and an approximate curvature radius
164 Normalize(cr, width, height);
169 if (rect_width/2<radius) {
170 if (rect_height/2<radius) {
171 cr.MoveTo(x0, (y0 + y1)/2);
172 cr.CurveTo(x0 ,y0, x0, y0, (x0 + x1)/2, y0);
173 cr.CurveTo(x1, y0, x1, y0, x1, (y0 + y1)/2);
174 cr.CurveTo(x1, y1, x1, y1, (x1 + x0)/2, y1);
175 cr.CurveTo(x0, y1, x0, y1, x0, (y0 + y1)/2);
177 cr.MoveTo(x0, y0 + radius);
178 cr.CurveTo(x0 ,y0, x0, y0, (x0 + x1)/2, y0);
179 cr.CurveTo(x1, y0, x1, y0, x1, y0 + radius);
180 cr.LineTo(x1 , y1 - radius);
181 cr.CurveTo(x1, y1, x1, y1, (x1 + x0)/2, y1);
182 cr.CurveTo(x0, y1, x0, y1, x0, y1- radius);
185 if (rect_height/2<radius) {
186 cr.MoveTo(x0, (y0 + y1)/2);
187 cr.CurveTo(x0 , y0, x0 , y0, x0 + radius, y0);
188 cr.LineTo(x1 - radius, y0);
189 cr.CurveTo(x1, y0, x1, y0, x1, (y0 + y1)/2);
190 cr.CurveTo(x1, y1, x1, y1, x1 - radius, y1);
191 cr.LineTo(x0 + radius, y1);
192 cr.CurveTo(x0, y1, x0, y1, x0, (y0 + y1)/2);
194 cr.MoveTo(x0, y0 + radius);
195 cr.CurveTo(x0 , y0, x0 , y0, x0 + radius, y0);
196 cr.LineTo(x1 - radius, y0);
197 cr.CurveTo(x1, y0, x1, y0, x1, y0 + radius);
198 cr.LineTo(x1 , y1 - radius);
199 cr.CurveTo(x1, y1, x1, y1, x1 - radius, y1);
200 cr.LineTo(x0 + radius, y1);
201 cr.CurveTo(x0, y1, x0, y1, x0, y1- radius);
206 // and fill/stroke it
207 cr.Color = new Color (0.5, 0.5, 1);
209 cr.Color = new Color (0.5, 0, 0, 0.5);
213 public void fill_and_stroke(Context cr, int width, int height)
215 Normalize(cr, width, height);
219 cr.RelLineTo(-0.4, 0.0);
220 cr.CurveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);
223 cr.Color = new Color (0, 0, 1);
225 cr.Color = new Color (0, 0, 0);
230 public void fill_and_stroke2(Context cr, int width, int height)
232 Normalize (cr, width, height);
236 cr.RelLineTo(-0.4, 0.0);
237 cr.CurveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);
240 cr.MoveTo(0.25, 0.1);
241 cr.RelLineTo(0.2, 0.2);
242 cr.RelLineTo(-0.2, 0.2);
243 cr.RelLineTo(-0.2, -0.2);
246 cr.Color = new Color (0, 0, 1);
248 cr.Color = new Color (0, 0, 0);
253 public void gradient(Context cr, int width, int height)
255 Normalize (cr, width, height);
257 LinearGradient lg = new LinearGradient(0.0, 0.0, 0.0, 1.0);
258 lg.AddColorStop(1, new Color(0, 0, 0, 1));
259 lg.AddColorStop(0, new Color(1, 1, 1, 1));
260 cr.Rectangle(0,0,1,1);
264 RadialGradient rg = new RadialGradient(0.45, 0.4, 0.1, 0.4, 0.4, 0.5);
265 rg.AddColorStop(0, new Color (1, 1, 1, 1));
266 rg.AddColorStop(1, new Color (0, 0, 0, 1));
268 cr.Arc(0.5, 0.5, 0.3, 0, 2 * Math.PI);
272 public void image(Context cr, int width, int height)
274 Normalize (cr, width, height);
275 ImageSurface image = new ImageSurface ("data/romedalen.png");
277 int h = image.Height;
279 cr.Translate (0.5, 0.5);
280 cr.Rotate (45* Math.PI/180);
281 cr.Scale (1.0/w, 1.0/h);
282 cr.Translate (-0.5*w, -0.5*h);
284 cr.SetSourceSurface (image, 0, 0);
289 public void imagepattern(Context cr, int width, int height)
291 Normalize (cr, width, height);
293 ImageSurface image = new ImageSurface ("data/romedalen.png");
295 int h = image.Height;
297 SurfacePattern pattern = new SurfacePattern (image);
298 pattern.Extend = Extend.Repeat;
300 cr.Translate (0.5, 0.5);
301 cr.Rotate (Math.PI / 4);
302 cr.Scale (1 / Math.Sqrt (2), 1 / Math.Sqrt (2));
303 cr.Translate (- 0.5, - 0.5);
305 Matrix matrix = new Matrix ();
306 matrix.InitScale (w * 5.0, h * 5.0);
307 pattern.Matrix = matrix;
311 cr.Rectangle (0, 0, 1.0, 1.0);
318 public void path(Context cr, int width, int height)
320 Normalize(cr, width, height);
323 cr.RelLineTo(-0.4, 0.0);
324 cr.CurveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);
329 public void set_line_cap(Context cr, int width, int height)
331 Normalize(cr, width, height);
333 cr.LineCap = LineCap.Butt; /* default */
334 cr.MoveTo(0.25, 0.2);
335 cr.LineTo(0.25, 0.8);
337 cr.LineCap = LineCap.Round;
341 cr.LineCap = LineCap.Square;
342 cr.MoveTo(0.75, 0.2);
343 cr.LineTo(0.75, 0.8);
346 // draw helping lines
347 cr.Color = new Color (1,0.2,0.2);
349 cr.MoveTo(0.25, 0.2);
350 cr.LineTo(0.25, 0.8);
353 cr.MoveTo(0.75, 0.2);
354 cr.LineTo(0.75, 0.8);
358 public void set_line_join(Context cr, int width, int height)
360 Normalize(cr, width, height);
362 cr.MoveTo(0.3, 0.33);
363 cr.RelLineTo(0.2, -0.2);
364 cr.RelLineTo(0.2, 0.2);
365 cr.LineJoin = LineJoin.Miter; // default
368 cr.MoveTo(0.3, 0.63);
369 cr.RelLineTo(0.2, -0.2);
370 cr.RelLineTo(0.2, 0.2);
371 cr.LineJoin = LineJoin.Bevel;
374 cr.MoveTo(0.3, 0.93);
375 cr.RelLineTo(0.2, -0.2);
376 cr.RelLineTo(0.2, 0.2);
377 cr.LineJoin = LineJoin.Round;
381 public void text(Context cr, int width, int height)
383 Normalize (cr, width, height);
384 cr.SelectFontFace("Sans", FontSlant.Normal, FontWeight.Bold);
385 cr.SetFontSize(0.35);
387 cr.MoveTo(0.04, 0.53);
388 cr.ShowText("Hello");
390 cr.MoveTo(0.27, 0.65);
393 cr.Color = new Color (0.5,0.5,1);
399 // draw helping lines
400 cr.Color = new Color (1.0, 0.2, 0.2, 0.6);
401 cr.Arc(0.04, 0.53, 0.02, 0, 2*Math.PI);
402 cr.Arc(0.27, 0.65, 0.02, 0, 2*Math.PI);
406 public void text_align_center(Context cr, int width, int height)
408 Normalize (cr, width, height);
410 cr.SelectFontFace("Sans", FontSlant.Normal, FontWeight.Normal);
412 TextExtents extents = cr.TextExtents("cairo");
413 double x = 0.5 -((extents.Width/2.0) + extents.XBearing);
414 double y = 0.5 -((extents.Height/2.0) + extents.YBearing);
417 cr.ShowText("cairo");
419 // draw helping lines
420 cr.Color = new Color (1, 0.2, 0.2, 0.6);
421 cr.Arc(x, y, 0.05, 0, 2*Math.PI);
430 public void text_extents(Context cr, int width, int height)
434 string utf8 = "cairo";
435 Normalize (cr, width, height);
437 cr.SelectFontFace("Sans", FontSlant.Normal, FontWeight.Normal);
440 TextExtents extents = cr.TextExtents(utf8);
445 // draw helping lines
446 cr.Color = new Color (1, 0.2, 0.2, 0.6);
447 cr.Arc(x, y, 0.05, 0, 2*Math.PI);
450 cr.RelLineTo(0, -extents.Height);
451 cr.RelLineTo(extents.Width, 0);
452 cr.RelLineTo(extents.XBearing, -extents.YBearing);
456 public void xxx_clip_rectangle(Context cr, int width, int height)
458 Normalize (cr, width, height);
475 public void xxx_dash(Context cr, int width, int height)
477 double[] dashes = new double[] {
483 double offset = -0.2;
485 Normalize(cr, width, height);
487 cr.SetDash(dashes, offset);
491 cr.RelLineTo(-0.4, 0.0);
492 cr.CurveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);
496 public void xxx_long_lines(Context cr, int width, int height)
498 Normalize(cr, width, height);
502 cr.Color = new Color (1, 0 ,0);
507 cr.Color = new Color (1, 1 ,0);
512 cr.Color = new Color (0, 1 ,0);
517 cr.Color = new Color (0, 0 ,1);
521 public void xxx_multi_segment_caps(Context cr, int width, int height)
523 Normalize(cr, width, height);
535 cr.LineCap = LineCap.Round;
539 public void xxx_self_intersect(Context cr, int width, int height)
541 Normalize(cr, width, height);
550 cr.LineCap = LineCap.Round;
551 cr.LineJoin = LineJoin.Round;