2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Mono.Cairo / Mono.Cairo / Cairo.cs
1 //
2 // Mono.Cairo.Cairo.cs
3 //
4 // Author: Duncan Mak (duncan@ximian.com)
5 //
6 // (C) Ximian, Inc. 2003
7 //
8 // This is a simplistic binding of the Cairo API to C#. All functions
9 // in cairo.h are transcribed into their C# equivelants and all
10 // enumerations are also listed here.
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Drawing;
36 using System.Runtime.InteropServices;
37
38 namespace Cairo {
39
40         public class CairoAPI
41         {
42                 internal const string CairoImp = "cairo";
43
44                 //
45                 // Manipulating state objects
46                 //
47                 [DllImport (CairoImp)]
48                 public static extern IntPtr cairo_create ();
49
50                 [DllImport (CairoImp)]
51                 public static extern void cairo_reference (IntPtr cr);
52
53                 [DllImport (CairoImp)]
54                 public static extern void cairo_destroy (IntPtr cr);
55
56                 [DllImport (CairoImp)]
57                 public static extern void cairo_save (IntPtr cr);                
58
59                 [DllImport (CairoImp)]
60                 public static extern void cairo_restore (IntPtr cr);
61
62                 [DllImport (CairoImp)]
63                 public static extern void cairo_copy (out IntPtr dest, IntPtr src);
64
65                 //
66                 // Modify state
67                 //
68                 [DllImport (CairoImp)]
69                 public static extern void cairo_set_target_surface (IntPtr cr, IntPtr surface);
70                                 
71                 [DllImport (CairoImp)]
72                 public static extern void cairo_set_target_image (
73                         IntPtr cr, string data, Cairo.Format format, int width, int height, int stride);
74
75                 [DllImport (CairoImp)]
76                 public static extern void cairo_set_target_drawable (
77                                                                      IntPtr ct, IntPtr display, IntPtr drawable);
78
79                 [DllImport (CairoImp)]
80                 public static extern void cairo_set_operator (IntPtr cr, Cairo.Operator op);
81
82                 [DllImport (CairoImp)]
83                 public static extern void cairo_set_rgb_color (IntPtr cr, double red, double green, double blue);
84
85                 [DllImport (CairoImp)]
86                 public static extern void cairo_set_alpha (IntPtr cr, double alpha);
87
88                 [DllImport (CairoImp)]
89                 public static extern void cairo_set_pattern (IntPtr cr, IntPtr pattern);
90
91                 [DllImport (CairoImp)]
92                 public static extern void cairo_set_tolerance (IntPtr cr, double tolerance);
93
94                 [DllImport (CairoImp)]
95                 public static extern void cairo_set_fill_rule (IntPtr cr, Cairo.FillRule fill_rule);
96
97                 [DllImport (CairoImp)]
98                 public static extern void cairo_set_line_width (IntPtr cr, double width);
99
100                 [DllImport (CairoImp)]
101                 public static extern void cairo_set_line_cap (IntPtr cr, Cairo.LineCap line_cap);
102
103                 [DllImport (CairoImp)]
104                 public static extern void cairo_set_line_join (IntPtr cr, Cairo.LineJoin line_join);
105
106                 [DllImport (CairoImp)]
107                 public static extern void cairo_set_dash (IntPtr cr, double [] dashes, int ndash, double offset);
108
109                 [DllImport (CairoImp)]
110                 public static extern void cairo_set_miter_limit (IntPtr cr, double limit);
111
112                 [DllImport (CairoImp)]
113                 public static extern void cairo_translate (IntPtr cr, double tx, double ty);
114
115                 [DllImport (CairoImp)]
116                 public static extern void cairo_scale (IntPtr cr, double sx, double sy);
117
118                 [DllImport (CairoImp)]                
119                 public static extern void cairo_rotate (IntPtr cr, double angle);
120
121                 [DllImport (CairoImp)]
122                 public static extern void cairo_concat_matrix (IntPtr cr, IntPtr matrix);                
123                 
124                 [DllImport (CairoImp)]
125                 public static extern void cairo_set_matrix (IntPtr cr, IntPtr matrix);
126                 
127                 [DllImport (CairoImp)]
128                 public static extern void cairo_default_matrix (IntPtr cr);
129
130                 [DllImport (CairoImp)]
131                 public static extern void cairo_identity_matrix (IntPtr cr);
132
133                 [DllImport (CairoImp)]
134                 public static extern void cairo_transform_point (IntPtr cr, ref double x, ref double y);
135
136                 [DllImport (CairoImp)]
137                 public static extern void cairo_transform_distance (IntPtr cr, ref double dx, ref double dy);
138
139                 [DllImport (CairoImp)]
140                 public static extern void cairo_inverse_transform_point (IntPtr cr, ref double x, ref double y);
141
142                 [DllImport (CairoImp)]
143                 public static extern void cairo_inverse_transform_distance (IntPtr cr, ref double dx, ref double dy);
144
145                 //
146                 // Path creation
147                 //
148                 [DllImport (CairoImp)]
149                 public static extern void cairo_new_path (IntPtr cr);
150
151                 [DllImport (CairoImp)]
152                 public static extern void cairo_move_to (IntPtr cr, double x, double y);
153
154                 [DllImport (CairoImp)]
155                 public static extern void cairo_line_to (IntPtr cr, double x, double y);
156
157                 [DllImport (CairoImp)]
158                 public static extern void cairo_curve_to (
159                         IntPtr cr, double x1, double y1, double x2, double y2, double x3, double y3);
160
161                 [DllImport (CairoImp)]
162                 public static extern void cairo_arc (
163                         IntPtr cr, double xc, double yc, double radius, double angel1, double angel2);
164
165                 [DllImport (CairoImp)]
166                 public static extern void cairo_arc_negative (
167                         IntPtr cr, double xc, double yc, double radius, double angel1, double angel2);
168
169                 [DllImport (CairoImp)]
170                 public static extern void cairo_rel_move_to (IntPtr cr, double dx, double dy);
171
172                 [DllImport (CairoImp)]
173                 public static extern void cairo_rel_line_to (IntPtr cr, double dx, double dy);
174
175                 [DllImport (CairoImp)]
176                 public static extern void cairo_rel_curve_to (
177                         IntPtr cr, double dx1, double dy1, double dx2, double dy2, double dx3, double dy3);
178
179                 [DllImport (CairoImp)]
180                 public static extern void cairo_rectangle (IntPtr cr, double x, double y, double width, double height);
181
182                 [DllImport (CairoImp)]
183                 public static extern void cairo_close_path (IntPtr cr);
184
185                 //
186                 // Painting
187                 //
188                 [DllImport (CairoImp)]
189                 public static extern void cairo_stroke (IntPtr cr);
190
191                 [DllImport (CairoImp)]
192                 public static extern void cairo_fill (IntPtr cr);
193
194                 [DllImport (CairoImp)]
195                 public static extern void cairo_clip (IntPtr cr);
196
197                 //
198                 // Font / Text
199                 //
200                 [DllImport (CairoImp)]
201                 public static extern void cairo_select_font (IntPtr cr, string key, FontSlant slant, FontWeight weight);
202
203                 [DllImport (CairoImp)]
204                 public static extern void cairo_scale_font (IntPtr cr, double scale);
205
206                 [DllImport (CairoImp)]
207                 public static extern void cairo_transform_font (IntPtr cr, IntPtr matrix);
208
209                 [DllImport (CairoImp)]
210                 public static extern void cairo_show_text (IntPtr cr, string utf8);
211
212                 [DllImport (CairoImp)]
213                 public static extern void cairo_font_set_transform (IntPtr font, IntPtr matrix);
214
215                 [DllImport (CairoImp)]
216                 public static extern void cairo_font_current_transform (IntPtr font, IntPtr matrix);
217
218                 [DllImport (CairoImp)]
219                 public static extern void cairo_font_reference (IntPtr font);
220
221                 [DllImport (CairoImp)]
222                 public static extern void cairo_font_destroy (IntPtr font);
223
224                 [DllImport (CairoImp)]
225                 public static extern void cairo_current_font_extents (IntPtr source, ref Extents extents);
226
227                 [DllImport (CairoImp)]
228                 public static extern void cairo_show_glyphs (IntPtr ct, IntPtr glyphs, int num_glyphs);
229
230                 [DllImport (CairoImp)]
231                 public static extern void cairo_text_path  (IntPtr ct, string utf8);
232
233                 [DllImport (CairoImp)]
234                 public static extern void cairo_glyph_path (IntPtr ct, IntPtr glyphs, int num_glyphs);
235
236
237                 // Cairo's font manipulation platform-specific Unix Fontconfig/Freetype interface
238
239                 [DllImport (CairoImp)]
240                 public static extern IntPtr cairo_ft_font_create (IntPtr ft_library, IntPtr ft_pattern);
241
242         
243                 //
244                 // Image
245                 //                
246                 [DllImport (CairoImp)]
247                 public static extern void cairo_show_surface (IntPtr cr, IntPtr surface, int width, int height);
248
249
250
251                 //
252                 // query
253                 //                                
254                 [DllImport (CairoImp)]
255                 public static extern Cairo.Operator cairo_current_operator (IntPtr cr);
256
257                 [DllImport (CairoImp)]
258                 public static extern void cairo_current_rgb_color (
259                         IntPtr cr, out double red, out double green, out double blue);
260
261                 [DllImport (CairoImp)]
262                 public static extern double cairo_current_alpha (IntPtr cr);
263
264                 [DllImport (CairoImp)]
265                 public static extern double cairo_current_tolerance (IntPtr cr);
266
267                 [DllImport (CairoImp)]
268                 public static extern void cairo_current_point (
269                         IntPtr cr, out double x, out double y);
270
271                 [DllImport (CairoImp)]
272                 public static extern Cairo.FillRule cairo_current_fill_rule (IntPtr cr);
273
274                 [DllImport (CairoImp)]
275                 public static extern double cairo_current_line_width (IntPtr cr);
276
277                 [DllImport (CairoImp)]
278                 public static extern LineCap cairo_current_line_cap (IntPtr cr);
279
280                 [DllImport (CairoImp)]
281                 public static extern LineJoin cairo_current_line_join (IntPtr cr);
282
283                 [DllImport (CairoImp)]
284                 public static extern double cairo_current_miter_limit (IntPtr cr);
285
286                 [DllImport (CairoImp)]
287                 public static extern void cairo_current_matrix (IntPtr cr, IntPtr matrix);
288
289                 [DllImport (CairoImp)]
290                 public static extern IntPtr cairo_current_target_surface (IntPtr cr);
291
292                 //
293                 // Error status queries
294                 //
295                 [DllImport (CairoImp)]
296                 public static extern Cairo.Status cairo_status (IntPtr cr);
297
298                 [DllImport (CairoImp, EntryPoint="cairo_status_string")]
299                 static extern IntPtr _cairo_status_string (IntPtr cr);
300
301                 public static string cairo_status_string (IntPtr cr)
302                 {
303                         return Marshal.PtrToStringAnsi (_cairo_status_string (cr));
304                 }
305                 
306                 //
307                 // Surface Manipulation
308                 //
309                 
310                 [DllImport (CairoImp)]                
311                 public static extern IntPtr cairo_surface_create_for_image (
312                         string data, Cairo.Format format, int width, int height, int stride);
313
314                 [DllImport (CairoImp)]
315                 public static extern IntPtr cairo_image_surface_create (Cairo.Format format, int width,
316                         int height);
317
318
319                 [DllImport (CairoImp)]
320                 public static extern IntPtr cairo_surface_create_similar (
321                         IntPtr surface, Cairo.Format format, int width, int height);
322
323                 [DllImport (CairoImp)]                
324                 public static extern IntPtr cairo_surface_create_similar_solid (
325                         IntPtr surface, Cairo.Format format,
326                         int width, int height, double red, double green, double blue, double alpha);
327
328                 [DllImport (CairoImp)]
329                 public static extern void cairo_surface_reference (IntPtr surface);
330
331                 [DllImport (CairoImp)]                
332                 public static extern void cairo_surface_destroy (IntPtr surface);
333
334                 [DllImport (CairoImp)]                
335                 public static extern Cairo.Status cairo_surface_set_repeat (
336                         IntPtr surface, int repeat);
337
338                 [DllImport (CairoImp)]                
339                 public static extern Cairo.Status cairo_surface_set_matrix (
340                         IntPtr surface, IntPtr matrix);
341
342                 [DllImport (CairoImp)]                
343                 public static extern Cairo.Status cairo_surface_get_matrix (
344                         IntPtr surface, out IntPtr matrix);
345
346                 [DllImport (CairoImp)]                
347                 public static extern Cairo.Status cairo_surface_set_filter (
348                         IntPtr surface, Cairo.Filter filter);
349
350                 //
351                 // Matrix
352                 //
353
354                 [DllImport (CairoImp)]                
355                 public static extern IntPtr cairo_matrix_create ();
356
357                 [DllImport (CairoImp)]                
358                 public static extern void cairo_matrix_destroy (IntPtr matrix);
359
360                 [DllImport (CairoImp)]                                
361                 public static extern Cairo.Status cairo_matrix_copy (
362                         IntPtr matrix, out IntPtr other);
363
364                 [DllImport (CairoImp)]                                
365                 public static extern Cairo.Status cairo_matrix_set_identity (IntPtr matrix);
366
367                 [DllImport (CairoImp)]                                
368                 public static extern Cairo.Status cairo_matrix_set_affine (
369                         IntPtr matrix,
370                         double a, double b, double c, double d, double tx, double ty);
371
372                 [DllImport (CairoImp)]                                
373                 public static extern Cairo.Status cairo_matrix_get_affine (
374                         IntPtr matrix,
375                         out double a, out double b, out double c, 
376                         out double d, out double tx, out double ty);
377
378                 [DllImport (CairoImp)]                                
379                 public static extern Cairo.Status cairo_matrix_translate (
380                         IntPtr matrix, double tx, double ty);
381
382                 [DllImport (CairoImp)]                                
383                 public static extern Cairo.Status cairo_matrix_scale (
384                         IntPtr matrix, double sx, double sy);
385
386                 [DllImport (CairoImp)]                                
387                 public static extern Cairo.Status cairo_matrix_rotate (
388                         IntPtr matrix, double radians);
389
390                 [DllImport (CairoImp)]                                
391                 public static extern Cairo.Status cairo_matrix_invert (IntPtr matrix);
392
393                 [DllImport (CairoImp)]                                
394                 public static extern Cairo.Status cairo_matrix_multiply (
395                         out IntPtr result, IntPtr a, IntPtr b);
396
397                 [DllImport (CairoImp)]                                
398                 public static extern Cairo.Status cairo_matrix_transform_distance (
399                         IntPtr matrix, ref double dx, ref double dy);
400
401                 [DllImport (CairoImp)]                                
402                 public static extern Cairo.Status cairo_matrix_transform_point (
403                         IntPtr matrix, ref double x, ref double y);
404
405                 [DllImport (CairoImp)]
406                 public static extern void cairo_set_font (IntPtr ct, IntPtr font);
407
408                 [DllImport (CairoImp)]
409                 public static extern IntPtr cairo_current_font (IntPtr ct);
410
411                 //
412                 // Pattern functions
413                 //
414                 [DllImport (CairoImp)]
415                 public static extern IntPtr cairo_pattern_create_for_surface (IntPtr surface);
416
417                 [DllImport (CairoImp)]
418                 public static extern IntPtr cairo_pattern_create_linear (double x0, double y0,
419                         double x1, double y1);
420
421                 [DllImport (CairoImp)]
422                 public static extern IntPtr cairo_pattern_create_radial (double cx0, double cy0,
423                         double radius0, double cx1, double cy1, double radius1);
424
425                 [DllImport (CairoImp)]
426                 public static extern void cairo_pattern_reference (IntPtr pattern);
427
428                 [DllImport (CairoImp)]
429                 public static extern void cairo_pattern_destroy (IntPtr pattern);
430
431                 [DllImport (CairoImp)]
432                 public static extern Status cairo_pattern_add_color_stop (IntPtr pattern,
433                         double offset, double red, double green, double blue, double alpha);
434
435                 [DllImport (CairoImp)]
436                 public static extern Status cairo_pattern_set_matrix (IntPtr pattern, IntPtr matrix);
437
438                 [DllImport (CairoImp)]
439                 public static extern Status cairo_pattern_get_matrix (IntPtr pattern, out IntPtr matrix);
440
441                 [DllImport (CairoImp)]
442                 public static extern Status cairo_pattern_set_extend (IntPtr pattern, Extend extend);
443
444                 [DllImport (CairoImp)]
445                 public static extern Extend cairo_pattern_get_extend (IntPtr pattern);
446
447                 [DllImport (CairoImp)]
448                 public static extern Status cairo_pattern_set_filter (IntPtr pattern, Filter filter);
449
450                 [DllImport (CairoImp)]
451                 public static extern Filter cairo_pattern_get_filter (IntPtr pattern);
452
453         }
454
455         //
456         // Freetype interface need it by cairo_ft interface calls
457         //
458         public class FreeType
459         {
460                 internal const string FreeTypeImp = "freetype";
461
462                 [DllImport (FreeTypeImp)]
463                 public static extern int FT_Init_FreeType (out IntPtr library);
464
465                 [DllImport (FreeTypeImp)]
466                 public static extern int FT_Set_Char_Size (IntPtr face, long width, long height, uint horz_res, uint vert_res);
467         }
468
469         //
470         // Fontconfig interface need it by cairo_ft interface calls
471         //
472         public class FontConfig
473         {
474                 internal const string FontConfigImp = "fontconfig";
475
476                 public const string FC_FAMILY = "family";
477                 public const string FC_STYLE = "style";
478                 public const string FC_SLANT = "slant";
479                 public const string FC_WEIGHT = "weight";
480
481                 [DllImport (FontConfigImp)]
482                 public static extern bool FcPatternAddString (IntPtr pattern, string obj, string value);
483
484                 [DllImport (FontConfigImp)]
485                 public static extern bool FcPatternAddInteger (IntPtr pattern, string obj, int value);
486
487                 [DllImport (FontConfigImp)]
488                 public static extern IntPtr FcPatternCreate ();
489
490                 [DllImport (FontConfigImp)]
491                 public static extern bool FcPatternDestroy (IntPtr pattern); 
492                 
493         }
494
495
496         //
497         // Enumerations
498         //
499         public enum Format {
500                 ARGB32 = 0,
501                 RGB24 = 1,
502                 A8 = 2,
503                 A1 = 4
504         }
505
506         public enum Operator {
507                 Clear = 0,
508                 Src = 1,
509                 Dst = 2,
510                 Over = 3,
511                 OverReverse = 4,
512                 In = 5,
513                 InReverse = 6,
514                 Out = 7,
515                 OutReverse = 8,
516                 Atop = 9,
517                 AtopReverse = 10,
518                 Xor = 11,
519                 Add = 12,
520                 Saturate = 13,
521                 
522                 DisjointClear = 16,
523                 DisjointSrc = 17,
524                 DisjointDst = 18,
525                 DisjointOver = 19,
526                 DisjointOverReverse = 20,
527                 DisjointIn = 21,
528                 DisjointInReverse = 22,
529                 DisjointOut = 23,
530                 DisjointOutReverse = 24,
531                 DisjointAtop = 25,
532                 DisjointAtopReverse = 26,
533                 DisjointXor = 27,
534
535                 ConjointClear = 32,
536                 ConjointSrc = 33,
537                 ConjointDst = 34,
538                 ConjointOver = 35,
539                 ConjointOverReverse = 36,
540                 ConjointIn = 37,
541                 ConjointInReverse = 38,
542                 ConjointOut = 39,
543                 ConjointOutReverse = 40,
544                 ConjointAtop = 41,
545                 ConjointAtopReverse = 42,
546                 ConjointXor = 43
547         }
548
549         public enum FillRule {
550                 Winding,
551                 EvenOdd
552         }
553
554         public enum LineCap {
555                 Butt, Round, Square
556         }
557
558         public enum LineJoin {
559                 Miter, Round, Bevel
560         }
561
562         public enum Status {
563                 Success = 0,
564                 NoMemory,
565                 InvalidRestore,
566                 InvalidPopGroup,
567                 NoCurrentPoint,
568                 InvalidMatrix
569         }
570
571         public enum Filter {
572                 Fast,
573                 Good,
574                 Best,
575                 Nearest,
576                 Bilinear,
577                 Gaussian,
578         }
579
580         public enum FontSlant {
581                 Normal  = 0,
582                 Italic  = 1,
583                 Oblique = 2
584         }
585
586         public enum FontWeight {
587                 Normal  = 0,
588                 Bold    = 1,
589         }
590
591         public enum Extend {
592                 None,
593                 Repetat,
594                 Reflect,
595         }
596
597        
598         [StructLayout(LayoutKind.Sequential)]
599         public struct Extents
600         {
601                 public  double x_bearing;
602                 public  double y_bearing;
603                 public  double width;
604                 public  double height;
605                 public  double x_advance;
606                 public  double y_advance;
607         }
608
609         [StructLayout(LayoutKind.Sequential)]
610         public struct Glyph
611         {
612                 public  long index;
613                 public  double x;
614                 public  double y;
615         }
616
617
618 }