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