4 // Authors: Duncan Mak (duncan@ximian.com)
5 // Hisham Mardam Bey (hisham.mardambey@gmail.com)
7 // (C) Ximian, Inc. 2003
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.
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
37 using System.Runtime.InteropServices;
43 internal const string CairoImp = "cairo";
46 // Manipulating state objects
48 [DllImport (CairoImp)]
49 public static extern IntPtr cairo_create (IntPtr target);
51 [DllImport (CairoImp)]
52 public static extern void cairo_reference (IntPtr cr);
54 [DllImport (CairoImp)]
55 public static extern void cairo_destroy (IntPtr cr);
57 [DllImport (CairoImp)]
58 public static extern void cairo_save (IntPtr cr);
60 [DllImport (CairoImp)]
61 public static extern void cairo_restore (IntPtr cr);
63 [DllImport (CairoImp)]
64 public static extern void cairo_copy (out IntPtr dest, IntPtr src);
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);
73 [DllImport (CairoImp)]
74 public static extern void cairo_set_operator (IntPtr cr, Cairo.Operator op);
76 [DllImport (CairoImp)]
77 public static extern void cairo_set_source_rgba (IntPtr cr, double red, double green, double blue, double alpha);
79 [DllImport (CairoImp)]
80 public static extern void cairo_set_source_rgb (IntPtr cr, double red, double green, double blue);
82 [DllImport (CairoImp)]
83 public static extern void cairo_set_source (IntPtr cr, IntPtr pattern);
85 [DllImport (CairoImp)]
86 public static extern IntPtr cairo_get_source (IntPtr cr);
88 [DllImport (CairoImp)]
89 public static extern void cairo_set_tolerance (IntPtr cr, double tolerance);
91 [DllImport (CairoImp)]
92 public static extern void cairo_set_fill_rule (IntPtr cr, Cairo.FillRule fill_rule);
94 [DllImport (CairoImp)]
95 public static extern void cairo_set_line_width (IntPtr cr, double width);
97 [DllImport (CairoImp)]
98 public static extern void cairo_set_line_cap (IntPtr cr, Cairo.LineCap line_cap);
100 [DllImport (CairoImp)]
101 public static extern void cairo_set_line_join (IntPtr cr, Cairo.LineJoin line_join);
103 [DllImport (CairoImp)]
104 public static extern void cairo_set_dash (IntPtr cr, double [] dashes, int ndash, double offset);
106 [DllImport (CairoImp)]
107 public static extern void cairo_set_miter_limit (IntPtr cr, double limit);
109 [DllImport (CairoImp)]
110 public static extern void cairo_translate (IntPtr cr, double tx, double ty);
112 [DllImport (CairoImp)]
113 public static extern void cairo_scale (IntPtr cr, double sx, double sy);
115 [DllImport (CairoImp)]
116 public static extern void cairo_rotate (IntPtr cr, double angle);
118 [DllImport (CairoImp)]
119 public static extern void cairo_transform (IntPtr cr, out Matrix_T matrix);
121 [DllImport (CairoImp)]
122 public static extern void cairo_set_matrix (IntPtr cr, Matrix_T matrix);
124 [DllImport (CairoImp)]
125 public static extern void cairo_identity_matrix (IntPtr cr);
127 [DllImport (CairoImp)]
128 public static extern void cairo_user_to_device (IntPtr cr, out double x, out double y);
130 [DllImport (CairoImp)]
131 public static extern void cairo_user_to_device_distance (IntPtr cr, out double dx, out double dy);
133 [DllImport (CairoImp)]
134 public static extern void cairo_device_to_user (IntPtr cr, out double x, out double y);
136 [DllImport (CairoImp)]
137 public static extern void cairo_device_to_user_distance (IntPtr cr, out double dx, out double dy);
142 [DllImport (CairoImp)]
143 public static extern void cairo_new_path (IntPtr cr);
145 public delegate void MoveToCallback (object closure, PointD p);
146 internal delegate void MoveToCallbackPriv (IntPtr closure,
149 public delegate void LineToCallback (object closure, PointD p);
150 internal delegate void LineToCallbackPriv (IntPtr closure,
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);
159 public delegate void ClosePathCallback (object closure);
160 internal delegate void ClosePathCallbackPriv (IntPtr closure);
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,
170 [DllImport (CairoImp)]
171 public static extern void cairo_move_to (IntPtr cr, double x, double y);
173 [DllImport (CairoImp)]
174 public static extern void cairo_line_to (IntPtr cr, double x, double y);
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);
180 [DllImport (CairoImp)]
181 public static extern void cairo_arc (
182 IntPtr cr, double xc, double yc, double radius, double angel1, double angel2);
184 [DllImport (CairoImp)]
185 public static extern void cairo_arc_negative (
186 IntPtr cr, double xc, double yc, double radius, double angel1, double angel2);
188 [DllImport (CairoImp)]
189 public static extern void cairo_arc_to (
190 IntPtr cr, double x1, double y1, double x2, double y2, double radius);
192 [DllImport (CairoImp)]
193 public static extern void cairo_rel_move_to (IntPtr cr, double dx, double dy);
195 [DllImport (CairoImp)]
196 public static extern void cairo_rel_line_to (IntPtr cr, double dx, double dy);
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);
202 [DllImport (CairoImp)]
203 public static extern void cairo_rectangle (IntPtr cr, double x, double y, double width, double height);
205 [DllImport (CairoImp)]
206 public static extern void cairo_close_path (IntPtr cr);
211 [DllImport (CairoImp)]
212 public static extern void cairo_stroke (IntPtr cr);
214 [DllImport (CairoImp)]
215 public static extern void cairo_stroke_preserve (IntPtr cr);
217 [DllImport (CairoImp)]
218 public static extern void cairo_fill (IntPtr cr);
220 [DllImport (CairoImp)]
221 public static extern void cairo_fill_preserve (IntPtr cr);
223 [DllImport (CairoImp)]
224 public static extern void cairo_copy_page (IntPtr cr);
226 [DllImport (CairoImp)]
227 public static extern void cairo_show_page (IntPtr cr);
229 [DllImport (CairoImp)]
230 public static extern void cairo_clip (IntPtr cr);
232 [DllImport (CairoImp)]
233 public static extern void cairo_reset_clip (IntPtr cr);
238 [DllImport (CairoImp)]
239 public static extern void cairo_select_font_face (IntPtr cr,
244 [DllImport (CairoImp)]
245 public static extern void cairo_set_font_size (IntPtr cr,
248 [DllImport (CairoImp)]
249 public static extern void cairo_set_font_matrix (IntPtr cr,
252 [DllImport (CairoImp)]
253 public static extern void cairo_scale_font (IntPtr cr, double scale);
255 [DllImport (CairoImp)]
256 public static extern void cairo_transform_font (IntPtr cr, IntPtr matrix);
258 [DllImport (CairoImp)]
259 public static extern void cairo_show_text (IntPtr cr, string utf8);
261 [DllImport (CairoImp)]
262 public static extern void cairo_font_set_transform (IntPtr font, IntPtr matrix);
264 [DllImport (CairoImp)]
265 public static extern void cairo_font_current_transform (IntPtr font, IntPtr matrix);
267 [DllImport (CairoImp)]
268 public static extern void cairo_font_reference (IntPtr font);
270 [DllImport (CairoImp)]
271 public static extern void cairo_font_destroy (IntPtr font);
273 [DllImport (CairoImp)]
274 public static extern void cairo_font_extents (IntPtr source, ref FontExtents extents);
276 [DllImport (CairoImp)]
277 public static extern void cairo_show_glyphs (IntPtr ct, IntPtr glyphs, int num_glyphs);
279 [DllImport (CairoImp)]
280 public static extern void cairo_text_path (IntPtr ct, string utf8);
282 [DllImport (CairoImp)]
283 public static extern void cairo_text_path (IntPtr ct, string utf8, ref TextExtents extents);
285 [DllImport (CairoImp)]
286 public static extern void cairo_glyph_path (IntPtr ct, IntPtr glyphs, int num_glyphs);
291 [DllImport (CairoImp)]
292 public static extern void cairo_set_source_surface (IntPtr cr, IntPtr surface, int width, int height);
294 [DllImport (CairoImp)]
295 public static extern void cairo_paint (IntPtr cr);
297 [DllImport (CairoImp)]
298 public static extern IntPtr cairo_image_surface_create_from_png (string filename);
300 [DllImport (CairoImp)]
301 public static extern int cairo_image_surface_get_width (IntPtr surface);
303 [DllImport (CairoImp)]
304 public static extern int cairo_image_surface_get_height (IntPtr surface);
309 [DllImport (CairoImp)]
310 public static extern bool cairo_in_stroke (IntPtr cr, double x, double y);
312 [DllImport (CairoImp)]
313 public static extern bool cairo_in_fill (IntPtr cr, double x, double y);
315 [DllImport (CairoImp)]
316 public static extern Cairo.Operator cairo_get_operator (IntPtr cr);
318 [DllImport (CairoImp)]
319 public static extern double cairo_get_tolerance (IntPtr cr);
321 [DllImport (CairoImp)]
322 public static extern void cairo_get_current_point (
323 IntPtr cr, out double x, out double y);
325 [DllImport (CairoImp)]
326 public static extern Cairo.FillRule cairo_get_fill_rule (IntPtr cr);
328 [DllImport (CairoImp)]
329 public static extern double cairo_get_line_width (IntPtr cr);
331 [DllImport (CairoImp)]
332 public static extern LineCap cairo_current_line_cap (IntPtr cr);
334 [DllImport (CairoImp)]
335 public static extern LineJoin cairo_get_line_join (IntPtr cr);
337 [DllImport (CairoImp)]
338 public static extern double cairo_get_miter_limit (IntPtr cr);
340 [DllImport (CairoImp)]
341 public static extern void cairo_get_matrix (IntPtr cr, Matrix_T matrix);
343 [DllImport (CairoImp)]
344 public static extern IntPtr cairo_get_target (IntPtr cr);
347 // Error status queries
349 [DllImport (CairoImp)]
350 public static extern Cairo.Status cairo_status (IntPtr cr);
352 [DllImport (CairoImp, EntryPoint="cairo_status_string")]
353 static extern IntPtr _cairo_status_string (IntPtr cr);
355 public static string cairo_status_string (IntPtr cr)
357 return Marshal.PtrToStringAnsi (_cairo_status_string (cr));
361 // Surface Manipulation
364 [DllImport (CairoImp)]
365 public static extern IntPtr cairo_xlib_surface_create (IntPtr dpi,
366 IntPtr win, IntPtr visual, int w, int h);
368 [DllImport (CairoImp)]
369 public static extern void cairo_xlib_surface_set_size (IntPtr surface, int width, int height);
371 [DllImport (CairoImp)]
372 public static extern Cairo.Status cairo_surface_finish (IntPtr surface);
374 [DllImport (CairoImp)]
375 public static extern void cairo_surface_set_device_offset (IntPtr surface,
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);
382 [DllImport (CairoImp)]
383 public static extern IntPtr cairo_image_surface_create (Cairo.Format format, int width,
387 [DllImport (CairoImp)]
388 public static extern IntPtr cairo_surface_create_similar (
389 IntPtr surface, Cairo.Format format, int width, int height);
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);
396 [DllImport (CairoImp)]
397 public static extern void cairo_surface_reference (IntPtr surface);
399 [DllImport (CairoImp)]
400 public static extern void cairo_surface_destroy (IntPtr surface);
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);
410 [DllImport (CairoImp)]
411 public static extern void cairo_matrix_init_translate (ref Matrix_T matrix,
412 double tx, double ty);
414 [DllImport (CairoImp)]
415 public static extern void cairo_matrix_translate (ref Matrix_T matrix,
416 double tx, double ty);
418 [DllImport (CairoImp)]
419 public static extern void cairo_matrix_init_identity (ref Matrix_T matrix);
421 [DllImport (CairoImp)]
422 public static extern void cairo_matrix_init_scale (ref Matrix_T matrix,
426 [DllImport (CairoImp)]
427 public static extern void cairo_matrix_scale (ref Matrix_T matrix,
431 [DllImport (CairoImp)]
432 public static extern void cairo_matrix_init_rotate (
433 ref Matrix_T matrix, double radians);
435 [DllImport (CairoImp)]
436 public static extern void cairo_matrix_rotate (
437 ref Matrix_T matrix, double radians);
439 [DllImport (CairoImp)]
440 public static extern Cairo.Status cairo_matrix_invert (ref Matrix_T matrix);
442 [DllImport (CairoImp)]
443 public static extern void cairo_matrix_multiply (
444 ref Matrix_T result, ref Matrix_T a, ref Matrix_T b);
446 [DllImport (CairoImp)]
447 public static extern void cairo_matrix_transform_distance (
448 ref Matrix_T matrix, ref double dx, ref double dy);
450 [DllImport (CairoImp)]
451 public static extern void cairo_matrix_transform_point (
452 ref Matrix_T matrix, ref double x, ref double y);
457 [DllImport (CairoImp)]
458 public static extern IntPtr cairo_pattern_create_for_surface (IntPtr surface);
460 [DllImport (CairoImp)]
461 public static extern IntPtr cairo_pattern_create_linear (double x0, double y0,
462 double x1, double y1);
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);
468 [DllImport (CairoImp)]
469 public static extern IntPtr cairo_pattern_create_rgb (double r,
473 [DllImport (CairoImp)]
474 public static extern IntPtr cairo_pattern_create_rgba (double r,
479 [DllImport (CairoImp)]
480 public static extern void cairo_pattern_reference (IntPtr pattern);
482 [DllImport (CairoImp)]
483 public static extern void cairo_pattern_destroy (IntPtr pattern);
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);
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);
493 [DllImport (CairoImp)]
494 public static extern Status cairo_pattern_set_matrix (IntPtr pattern, IntPtr matrix);
496 [DllImport (CairoImp)]
497 public static extern Status cairo_pattern_get_matrix (IntPtr pattern, IntPtr matrix);
499 [DllImport (CairoImp)]
500 public static extern Status cairo_pattern_set_extend (IntPtr pattern, Extend extend);
502 [DllImport (CairoImp)]
503 public static extern Extend cairo_pattern_get_extend (IntPtr pattern);
505 [DllImport (CairoImp)]
506 public static extern Status cairo_pattern_set_filter (IntPtr pattern, Filter filter);
508 [DllImport (CairoImp)]
509 public static extern Filter cairo_pattern_get_filter (IntPtr pattern);
511 [DllImport (CairoImp)]
512 public static extern Status cairo_pattern_status (IntPtr pattern);
526 public enum Operator {
546 DisjointOverReverse = 20,
548 DisjointInReverse = 22,
550 DisjointOutReverse = 24,
552 DisjointAtopReverse = 26,
559 ConjointOverReverse = 36,
561 ConjointInReverse = 38,
563 ConjointOutReverse = 40,
565 ConjointAtopReverse = 42,
569 public enum FillRule {
574 public enum LineCap {
578 public enum LineJoin {
600 public enum FontSlant {
606 public enum FontWeight {
617 [StructLayout(LayoutKind.Sequential)]
618 public struct FontExtents
620 public double ascent;
621 public double descent;
622 public double height;
623 public double max_x_advance;
624 public double max_y_advance;
628 [StructLayout(LayoutKind.Sequential)]
629 public struct TextExtents
631 public double x_bearing;
632 public double y_bearing;
634 public double height;
635 public double x_advance;
636 public double y_advance;
639 [StructLayout(LayoutKind.Sequential)]