* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Mono.Cairo / snippets / Snippets.cs
1 using System;
2 using System.Reflection;
3 using Cairo;
4
5 namespace Cairo.Snippets
6 {
7         public class Snippets
8         {
9                 public static string[] snippets = {
10                         "arc",
11                         "arc_negative",
12                         "clip",
13                         "clip_image",
14                         "curve_rectangle",
15                         "curve_to",
16                         "fill_and_stroke",
17                         "fill_and_stroke2",
18                         "gradient",
19                         "image",
20                         "imagepattern",
21                         "path",
22                         "set_line_cap",
23                         "set_line_join",
24                         "text",
25                         "text_align_center",
26                         "text_extents",
27                         "xxx_clip_rectangle",
28                         "xxx_dash",
29                         "xxx_long_lines",
30                         "xxx_multi_segment_caps",
31                         "xxx_self_intersect"
32                 };
33         
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)
36                 {
37                         MethodInfo m = snip.GetType ().GetMethod(snippet, types);
38                         m.Invoke (snip, new Object[] {cr, width, height});
39                 }
40
41                 public void Normalize (Context cr, int width, int height)
42                 {
43                         cr.Scale (width, height);
44                         cr.LineWidth = 0.04;
45                 }
46         
47                 public void arc(Context cr, int width, int height)
48                 {
49                         PointD c = new PointD (0.5, 0.5);
50                         double radius = 0.4;
51                         double angle1 = 45.0  * (Math.PI/180.0);  /* angles are specified */
52                         double angle2 = 180.0 * (Math.PI/180.0);  /* in radians           */
53
54                         Normalize(cr, width, height);
55
56                         cr.Arc(c.X, c.Y, radius, angle1, angle2);
57                         cr.Stroke();
58
59                         // draw helping lines
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);
62                         cr.Fill();
63                         cr.LineWidth = 0.03;
64                         cr.Arc(c.X, c.Y, radius, angle1, angle1);
65                         cr.LineTo(c);
66                         cr.Arc(c.X, c.Y, radius, angle2, angle2);
67                         cr.LineTo(c);
68                         cr.Stroke();
69                 }
70         
71                 public void arc_negative(Context cr, int width, int height)
72                 {
73                         PointD c = new PointD(0.5, 0.5);
74                         double radius = 0.4;
75                         double angle1 = 45.0  * (Math.PI/180.0);  /* angles are specified */
76                         double angle2 = 180.0 * (Math.PI/180.0);  /* in radians           */
77
78                         Normalize(cr, width, height);
79
80                         cr.ArcNegative(c.X, c.Y, radius, angle1, angle2);
81                         cr.Stroke();
82
83                         // draw helping lines
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);
86                         cr.Fill();
87                         cr.LineWidth = 0.03;
88                         cr.Arc(c.X, c.Y, radius, angle1, angle1);
89                         cr.LineTo(c);
90                         cr.Arc(c.X, c.Y, radius, angle2, angle2);
91                         cr.LineTo(c);
92                         cr.Stroke();
93                 }
94         
95                 public void clip(Context cr, int width, int height)
96                 {
97                         Normalize (cr, width, height);
98
99                         cr.Arc(0.5, 0.5, 0.3, 0, 2 * Math.PI);
100                         cr.Clip();
101
102                         cr.NewPath();  // current path is not consumed by cairo_clip()
103                         cr.Rectangle(0, 0, 1, 1);
104                         cr.Fill();
105                         cr.Color = new Color (0, 1, 0);
106                         cr.MoveTo(0, 0);
107                         cr.LineTo(1, 1);
108                         cr.MoveTo(1, 0);
109                         cr.LineTo(0, 1);
110                         cr.Stroke();
111                 }
112
113                 public void clip_image(Context cr, int width, int height)
114                 {
115                         Normalize (cr, width, height);
116                         cr.Arc (0.5, 0.5, 0.3, 0, 2*Math.PI);
117                         cr.Clip ();
118                         cr.NewPath (); // path not consumed by clip()
119
120                         ImageSurface image = new ImageSurface ("data/romedalen.png");
121                         int w = image.Width;
122                         int h = image.Height;
123
124                         cr.Scale (1.0/w, 1.0/h);
125
126                         cr.SetSourceSurface (image, 0, 0);
127                         cr.Paint ();
128
129                         image.Destroy ();
130                 }
131
132                 public void curve_to(Context cr, int width, int height)
133                 {
134                         double x=0.1,  y=0.5;
135                         double x1=0.4, y1=0.9, x2=0.6, y2=0.1, x3=0.9, y3=0.5;
136
137                         Normalize (cr, width, height);
138
139                         cr.MoveTo(x, y);
140                         cr.CurveTo(x1, y1, x2, y2, x3, y3);
141
142                         cr.Stroke();
143
144                         cr.Color = new Color (1, 0.2, 0.2, 0.6);
145                         cr.LineWidth = 0.03;
146                         cr.MoveTo(x,y);
147                         cr.LineTo(x1,y1);
148                         cr.MoveTo(x2,y2);
149                         cr.LineTo(x3,y3);
150                         cr.Stroke();
151                 }
152
153                 public void curve_rectangle(Context cr, int width, int height)
154                 {
155                         // a custom shape, that could be wrapped in a function
156                         double x0          = 0.1,   //< parameters like cairo_rectangle
157                                y0          = 0.1,
158                                rect_width  = 0.8,
159                            rect_height = 0.8,
160                                    radius = 0.4;   //< and an approximate curvature radius
161
162                         double x1,y1;
163
164                         Normalize(cr, width, height);
165
166                         x1=x0+rect_width;
167                         y1=y0+rect_height;
168
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);
176                                 } else {
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);
183                                 }
184                         } else {
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);
193                                 } else {
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);
202                                 }
203                         }
204                         cr.ClosePath();
205
206                         // and fill/stroke it
207                         cr.Color = new Color (0.5, 0.5, 1);
208                         cr.FillPreserve();
209                         cr.Color = new Color (0.5, 0, 0, 0.5);
210                         cr.Stroke();
211                 }
212
213                 public void fill_and_stroke(Context cr, int width, int height)
214                 {
215                         Normalize(cr, width, height);
216
217                         cr.MoveTo(0.5, 0.1);
218                         cr.LineTo(0.9, 0.9);
219                         cr.RelLineTo(-0.4, 0.0);
220                         cr.CurveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);
221                         cr.ClosePath();
222
223                         cr.Color = new Color (0, 0, 1);
224                         cr.FillPreserve();
225                         cr.Color = new Color (0, 0, 0);
226
227                         cr.Stroke();
228                 }
229
230                 public void fill_and_stroke2(Context cr, int width, int height)
231                 {
232                         Normalize (cr, width, height);
233
234                         cr.MoveTo(0.5, 0.1);
235                         cr.LineTo(0.9, 0.9);
236                         cr.RelLineTo(-0.4, 0.0);
237                         cr.CurveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);
238                         cr.ClosePath();
239
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);
244                         cr.ClosePath();
245
246                         cr.Color = new Color (0, 0, 1);
247                         cr.FillPreserve();
248                         cr.Color = new Color (0, 0, 0);
249
250                         cr.Stroke();
251                 }
252
253                 public void gradient(Context cr, int width, int height)
254                 {
255                         Normalize (cr, width, height);
256
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);
261                         cr.Source = lg;
262                         cr.Fill();
263
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));
267                         cr.Source = rg;
268                         cr.Arc(0.5, 0.5, 0.3, 0, 2 * Math.PI);
269                         cr.Fill();
270                 }
271
272                 public void image(Context cr, int width, int height)
273                 {
274                         Normalize (cr, width, height);
275                         ImageSurface image = new ImageSurface ("data/romedalen.png");
276                         int w = image.Width;
277                         int h = image.Height;
278
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);
283
284                         cr.SetSourceSurface (image, 0, 0);
285                         cr.Paint ();
286                         image.Destroy ();
287                 }
288                 
289                 public void imagepattern(Context cr, int width, int height)
290                 {
291                         Normalize (cr, width, height);
292                         
293                         ImageSurface image = new ImageSurface ("data/romedalen.png");
294                         int w = image.Width;
295                         int h = image.Height;
296
297                         SurfacePattern pattern = new SurfacePattern (image);
298                         pattern.Extend = Extend.Repeat;
299
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);
304
305                         Matrix matrix = new Matrix ();
306                         matrix.InitScale (w * 5.0, h * 5.0);
307                         pattern.Matrix = matrix;
308
309                         cr.Source = pattern;
310
311                         cr.Rectangle (0, 0, 1.0, 1.0);
312                         cr.Fill ();
313
314                         pattern.Destroy ();
315                         image.Destroy ();
316                 }
317                 
318                 public void path(Context cr, int width, int height)
319                 {
320                         Normalize(cr, width, height);
321                         cr.MoveTo(0.5, 0.1);
322                         cr.LineTo(0.9, 0.9);
323                         cr.RelLineTo(-0.4, 0.0);
324                         cr.CurveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);
325
326                         cr.Stroke();
327                 }
328
329                 public void set_line_cap(Context cr, int width, int height)
330                 {
331                         Normalize(cr, width, height);
332                         cr.LineWidth = 0.12;
333                         cr.LineCap = LineCap.Butt; /* default */
334                         cr.MoveTo(0.25, 0.2); 
335                         cr.LineTo(0.25, 0.8);
336                         cr.Stroke();
337                         cr.LineCap = LineCap.Round;
338                         cr.MoveTo(0.5, 0.2); 
339                         cr.LineTo(0.5, 0.8);
340                         cr.Stroke();
341                         cr.LineCap = LineCap.Square;
342                         cr.MoveTo(0.75, 0.2); 
343                         cr.LineTo(0.75, 0.8);
344                         cr.Stroke();
345
346                         // draw helping lines
347                         cr.Color = new Color (1,0.2,0.2);
348                         cr.LineWidth = 0.01;
349                         cr.MoveTo(0.25, 0.2); 
350                         cr.LineTo(0.25, 0.8);
351                         cr.MoveTo(0.5, 0.2);  
352                         cr.LineTo(0.5, 0.8);
353                         cr.MoveTo(0.75, 0.2); 
354                         cr.LineTo(0.75, 0.8);
355                         cr.Stroke();
356                 }
357
358                 public void set_line_join(Context cr, int width, int height)
359                 {
360                         Normalize(cr, width, height);
361                         cr.LineWidth = 0.16;
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
366                         cr.Stroke();
367
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;
372                         cr.Stroke();
373
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;
378                         cr.Stroke();
379                 }
380
381                 public void text(Context cr, int width, int height)
382                 {
383                         Normalize (cr, width, height);
384                         cr.SelectFontFace("Sans", FontSlant.Normal, FontWeight.Bold);
385                         cr.SetFontSize(0.35);
386
387                         cr.MoveTo(0.04, 0.53);
388                         cr.ShowText("Hello");
389
390                         cr.MoveTo(0.27, 0.65);
391                         cr.TextPath("void");
392                         cr.Save();
393                         cr.Color = new Color (0.5,0.5,1);
394                         cr.Fill();
395                         cr.Restore();
396                         cr.LineWidth = 0.01;
397                         cr.Stroke();
398
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);
403                         cr.Fill();
404                 }
405
406                 public void text_align_center(Context cr, int width, int height)
407                 {
408                         Normalize (cr, width, height);
409
410                         cr.SelectFontFace("Sans", FontSlant.Normal, FontWeight.Normal);
411                         cr.SetFontSize(0.2);
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);
415
416                         cr.MoveTo(x, y);
417                         cr.ShowText("cairo");
418
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);
422                         cr.Fill();
423                         cr.MoveTo(0.5, 0);
424                         cr.RelLineTo(0, 1);
425                         cr.MoveTo(0, 0.5);
426                         cr.RelLineTo(1, 0);
427                         cr.Stroke();
428                 }
429         
430                 public void text_extents(Context cr, int width, int height)
431                 {
432                         double x=0.1;
433                         double y=0.6;
434                         string utf8 = "cairo";
435                         Normalize (cr, width, height);
436
437                         cr.SelectFontFace("Sans", FontSlant.Normal, FontWeight.Normal);
438
439                         cr.SetFontSize(0.4);
440                         TextExtents extents = cr.TextExtents(utf8);
441
442                         cr.MoveTo(x,y);
443                         cr.ShowText(utf8);
444
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);
448                         cr.Fill();
449                         cr.MoveTo(x,y);
450                         cr.RelLineTo(0, -extents.Height);
451                         cr.RelLineTo(extents.Width, 0);
452                         cr.RelLineTo(extents.XBearing, -extents.YBearing);
453                         cr.Stroke();
454                 }
455
456                 public void xxx_clip_rectangle(Context cr, int width, int height)
457                 {
458                         Normalize (cr, width, height);
459
460                         cr.NewPath();
461                         cr.MoveTo(.25, .25);
462                         cr.LineTo(.25, .75);
463                         cr.LineTo(.75, .75);
464                         cr.LineTo(.75, .25);
465                         cr.LineTo(.25, .25);
466                         cr.ClosePath();
467
468                         cr.Clip();
469
470                         cr.MoveTo(0, 0);
471                         cr.LineTo(1, 1);
472                         cr.Stroke();
473                 }
474
475                 public void xxx_dash(Context cr, int width, int height)
476                 {
477                         double[] dashes = new double[] {
478                                 0.20,  // ink
479                                 0.05,  // skip
480                                 0.05,  // ink
481                                 0.05   // skip 
482                         };
483                         double offset = -0.2; 
484
485                         Normalize(cr, width, height);
486
487                         cr.SetDash(dashes, offset);
488
489                         cr.MoveTo(0.5, 0.1);
490                         cr.LineTo(0.9, 0.9);
491                         cr.RelLineTo(-0.4, 0.0);
492                         cr.CurveTo(0.2, 0.9, 0.2, 0.5, 0.5, 0.5);
493                         cr.Stroke();
494                 }
495
496                 public void xxx_long_lines(Context cr, int width, int height)
497                 {
498                         Normalize(cr, width, height);
499
500                         cr.MoveTo(0.1, -50);
501                         cr.LineTo(0.1,  50);
502                         cr.Color = new Color (1, 0 ,0);
503                         cr.Stroke();
504
505                         cr.MoveTo(0.2, -60);
506                         cr.LineTo(0.2,  60);
507                         cr.Color = new Color (1, 1 ,0);
508                         cr.Stroke();
509
510                         cr.MoveTo(0.3, -70);
511                         cr.LineTo(0.3,  70);
512                         cr.Color = new Color (0, 1 ,0);
513                         cr.Stroke();
514
515                         cr.MoveTo(0.4, -80);
516                         cr.LineTo(0.4,  80);
517                         cr.Color = new Color (0, 0 ,1);
518                         cr.Stroke();
519                 }
520
521                 public void xxx_multi_segment_caps(Context cr, int width, int height)
522                 {
523                         Normalize(cr, width, height);
524
525                         cr.MoveTo(0.2, 0.3);
526                         cr.LineTo(0.8, 0.3);
527
528                         cr.MoveTo(0.2, 0.5);
529                         cr.LineTo(0.8, 0.5);
530
531                         cr.MoveTo(0.2, 0.7);
532                         cr.LineTo(0.8, 0.7);
533
534                         cr.LineWidth = 0.12;
535                         cr.LineCap = LineCap.Round;
536                         cr.Stroke();
537                 }
538
539                 public void xxx_self_intersect(Context cr, int width, int height)
540                 {
541                         Normalize(cr, width, height);
542
543                         cr.MoveTo(0.3, 0.3);
544                         cr.LineTo(0.7, 0.3);
545
546                         cr.LineTo(0.5, 0.3);
547                         cr.LineTo(0.5, 0.7);
548
549                         cr.LineWidth = 0.22;
550                         cr.LineCap = LineCap.Round;
551                         cr.LineJoin = LineJoin.Round;
552                         cr.Stroke();
553                 }
554         }
555 }