1 // 2 // 3 // Mono.Cairo drawing samples using image (png) as drawing surface 4 // Author: Hisham Mardam Bey <hisham@hisham.cc> 5 // 6 7 // 8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com) 9 // 10 // Permission is hereby granted, free of charge, to any person obtaining 11 // a copy of this software and associated documentation files (the 12 // "Software"), to deal in the Software without restriction, including 13 // without limitation the rights to use, copy, modify, merge, publish, 14 // distribute, sublicense, and/or sell copies of the Software, and to 15 // permit persons to whom the Software is furnished to do so, subject to 16 // the following conditions: 17 // 18 // The above copyright notice and this permission notice shall be 19 // included in all copies or substantial portions of the Software. 20 // 21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 // 29 30 using System; 31 using Cairo; 32 33 public class CairoTest 34 { 35 draw(Cairo.Context gr, int width, int height)36 static void draw (Cairo.Context gr, int width, int height) 37 { 38 double x0 = 0.1; 39 double y0 = 0.1; 40 double rect_width = 0.8; 41 double rect_height = 0.8; 42 double radius = 0.4; 43 44 double x1,y1; 45 46 gr.Scale (width, height); 47 gr.LineWidth = 0.04; 48 49 x1=x0+rect_width; 50 y1=y0+rect_height; 51 if (rect_width == 0 || rect_height == 0) 52 return; 53 54 if (rect_width/2<radius) { 55 if (rect_height/2<radius) { 56 gr.MoveTo( new PointD(x0, (y0 + y1)/2) ); 57 58 gr.CurveTo ( new PointD (x0 ,y0), 59 new PointD (x0, y0), 60 new PointD ((x0 + x1)/2, y0) 61 ); 62 63 gr.CurveTo ( new PointD (x1, y0 ), 64 new PointD (x1, y0 ), 65 new PointD (x1, (y0 + y1)/2) 66 ); 67 68 gr.CurveTo ( new PointD (x1, y1), 69 new PointD (x1, y1), 70 new PointD ((x1 + x0)/2, y1) 71 ); 72 73 gr.CurveTo ( new PointD (x0, y1), 74 new PointD (x0, y1), 75 new PointD (x0, (y0 + y1)/2) 76 ); 77 } 78 else { 79 gr.MoveTo ( new PointD (x0, y0 + radius) ); 80 81 gr.CurveTo ( new PointD (x0 ,y0), 82 new PointD (x0, y0), 83 new PointD ((x0 + x1)/2, y0) 84 ); 85 86 gr.CurveTo ( new PointD (x1, y0), 87 new PointD (x1, y0), 88 new PointD (x1, y0 + radius) 89 ); 90 91 gr.LineTo ( new PointD (x1 , y1 - radius) ); 92 93 gr.CurveTo ( new PointD (x1, y1), 94 new PointD (x1, y1), 95 new PointD ((x1 + x0)/2, y1) 96 ); 97 98 gr.CurveTo ( new PointD (x0, y1), 99 new PointD (x0, y1), 100 new PointD (x0, y1- radius) 101 ); 102 } 103 } 104 else { 105 if (rect_height/2<radius) { 106 gr.MoveTo ( new PointD (x0, (y0 + y1)/2) ); 107 108 gr.CurveTo ( new PointD (x0 , y0), 109 new PointD (x0 , y0), 110 new PointD (x0 + radius, y0) 111 ); 112 113 gr.LineTo ( new PointD (x1 - radius, y0) ); 114 115 gr.CurveTo ( new PointD (x1, y0), 116 new PointD (x1, y0), 117 new PointD (x1, (y0 + y1)/2) 118 ); 119 120 gr.CurveTo ( new PointD (x1, y1), 121 new PointD (x1, y1), 122 new PointD (x1 - radius, y1) 123 ); 124 125 gr.LineTo ( new PointD (x0 + radius, y1) ); 126 127 gr.CurveTo ( new PointD ( x0, y1), 128 new PointD (x0, y1), 129 new PointD (x0, (y0 + y1)/2) 130 ); 131 } 132 else { 133 gr.MoveTo ( new PointD (x0, y0 + radius) ); 134 135 gr.CurveTo ( new PointD (x0 , y0), 136 new PointD (x0 , y0), 137 new PointD (x0 + radius, y0) 138 ); 139 140 gr.LineTo ( new PointD (x1 - radius, y0) ); 141 142 gr.CurveTo ( new PointD (x1, y0), 143 new PointD (x1, y0), 144 new PointD (x1, y0 + radius) 145 ); 146 147 gr.LineTo ( new PointD (x1 , y1 - radius) ); 148 149 gr.CurveTo ( new PointD ( x1, y1), 150 new PointD (x1, y1), 151 new PointD (x1 - radius, y1) 152 ); 153 154 gr.LineTo ( new PointD (x0 + radius, y1) ); 155 gr.CurveTo ( new PointD ( x0, y1), 156 new PointD (x0, y1), 157 new PointD (x0, y1- radius) 158 ); 159 } 160 } 161 162 gr.Color = new Color (0.5,0.5,1, 1); 163 gr.FillPreserve (); 164 gr.Color = new Color(0.5, 0, 0, 0.5); 165 gr.Stroke (); 166 } 167 Main()168 static void Main () 169 { 170 Surface s = new ImageSurface (Format.ARGB32, 500, 500); 171 Cairo.Context g = new Cairo.Context (s); 172 173 draw (g, 500, 500); 174 175 s.WriteToPng ("curve_rect.png"); 176 } 177 } 178