using System; using System.Reflection; using Cairo; namespace Cairo.Snippets { public class Snippets { public static string[] snippets = { "arc", "arc_negative", "clip", "clip_image", "curve_rectangle", "curve_to", "fill_and_stroke", "fill_and_stroke2", "gradient", "image", "imagepattern", "path", "set_line_cap", "set_line_join", "text", "text_align_center", "text_extents", "xxx_clip_rectangle", "xxx_dash", "xxx_long_lines", "xxx_multi_segment_caps", "xxx_self_intersect" }; static Type[] types = new Type[] {typeof (Context), typeof (int), typeof (int)}; public static void InvokeSnippet (Snippets snip, string snippet, Context cr, int width, int height) { MethodInfo m = snip.GetType ().GetMethod(snippet, types); m.Invoke (snip, new Object[] {cr, width, height}); } public void Normalize (Context cr, int width, int height) { cr.Scale (width, height); cr.LineWidth = 0.04; } public void arc(Context cr, int width, int height) { PointD c = new PointD (0.5, 0.5); double radius = 0.4; double angle1 = 45.0 * (Math.PI/180.0); /* angles are specified */ double angle2 = 180.0 * (Math.PI/180.0); /* in radians */ Normalize(cr, width, height); cr.Arc(c.X, c.Y, radius, angle1, angle2); cr.Stroke(); // draw helping lines cr.Color = new Color (1, 0.2, 0.2, 0.6); cr.Arc(c.X, c.Y, 0.05, 0, 2*Math.PI); cr.Fill(); cr.LineWidth = 0.03; cr.Arc(c.X, c.Y, radius, angle1, angle1); cr.LineTo(c); cr.Arc(c.X, c.Y, radius, angle2, angle2); cr.LineTo(c); cr.Stroke(); } public void arc_negative(Context cr, int width, int height) { PointD c = new PointD(0.5, 0.5); double radius = 0.4; double angle1 = 45.0 * (Math.PI/180.0); /* angles are specified */ double angle2 = 180.0 * (Math.PI/180.0); /* in radians */ Normalize(cr, width, height); cr.ArcNegative(c.X, c.Y, radius, angle1, angle2); cr.Stroke(); // draw helping lines cr.Color = new Color (1, 0.2, 0.2, 0.6); cr.Arc(c.X, c.Y, 0.05, 0, 2*Math.PI); cr.Fill(); cr.LineWidth = 0.03; cr.Arc(c.X, c.Y, radius, angle1, angle1); cr.LineTo(c); cr.Arc(c.X, c.Y, radius, angle2, angle2); cr.LineTo(c); cr.Stroke(); } public void clip(Context cr, int width, int height) { Normalize (cr, width, height); cr.Arc(0.5, 0.5, 0.3, 0, 2 * Math.PI); cr.Clip(); cr.NewPath(); // current path is not consumed by cairo_clip() cr.Rectangle(0, 0, 1, 1); cr.Fill(); cr.Color = new Color (0, 1, 0); cr.MoveTo(0, 0); cr.LineTo(1, 1); cr.MoveTo(1, 0); cr.LineTo(0, 1); cr.Stroke(); } public void clip_image(Context cr, int width, int height) { Normalize (cr, width, height); cr.Arc (0.5, 0.5, 0.3, 0, 2*Math.PI); cr.Clip (); cr.NewPath (); // path not consumed by clip() ImageSurface image = new ImageSurface ("data/romedalen.png"); int w = image.Width; int h = image.Height; cr.Scale (1.0/w, 1.0/h); cr.SetSourceSurface (image, 0, 0); cr.Paint (); image.Destroy (); } public void curve_to(Context cr, int width, int height) { double x=0.1, y=0.5; double x1=0.4, y1=0.9, x2=0.6, y2=0.1, x3=0.9, y3=0.5; Normalize (cr, width, height); cr.MoveTo(x, y); cr.CurveTo(x1, y1, x2, y2, x3, y3); cr.Stroke(); cr.Color = new Color (1, 0.2, 0.2, 0.6); cr.LineWidth = 0.03; cr.MoveTo(x,y); cr.LineTo(x1,y1); cr.MoveTo(x2,y2); cr.LineTo(x3,y3); cr.Stroke(); } public void curve_rectangle(Context cr, int width, int height) { // a custom shape, that could be wrapped in a function double x0 = 0.1, //< parameters like cairo_rectangle y0 = 0.1, rect_width = 0.8, rect_height = 0.8, radius = 0.4; //< and an approximate curvature radius double x1,y1; Normalize(cr, width, height); x1=x0+rect_width; y1=y0+rect_height; if (rect_width/2