2 // Mono.Cairo.Graphics.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Miguel de Icaza (miguel@novell.com)
8 // (C) Ximian Inc, 2003.
9 // (C) Novell Inc, 2003.
11 // This is an OO wrapper API for the Cairo API.
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;
42 public class Graphics : IDisposable
44 internal IntPtr state = IntPtr.Zero;
48 state = CairoAPI.cairo_create ();
51 private Graphics (IntPtr state)
58 Console.WriteLine ("Cairo not thread safe, you might want to call IDisposable.Dispose on Cairo.Surface");
62 void IDisposable.Dispose ()
65 GC.SuppressFinalize (this);
68 protected virtual void Dispose (bool disposing)
71 Console.WriteLine ("Cairo.Graphics: called from thread");
75 if (state == (IntPtr) 0)
78 CairoAPI.cairo_destroy (state);
82 public Cairo.Graphics Copy ()
85 CairoAPI.cairo_copy (out dest, state);
86 return new Cairo.Graphics (dest);
91 CairoAPI.cairo_save (state);
94 public void Restore ()
96 CairoAPI.cairo_restore (state);
99 public Cairo.Status Status {
101 return CairoAPI.cairo_status (state);
105 public string StatusString {
107 return CairoAPI.cairo_status_string (state);
111 public IntPtr Handle {
117 public Cairo.Operator Operator {
119 CairoAPI.cairo_set_operator (state, value);
123 return CairoAPI.cairo_current_operator (state);
127 public void SetRGBColor (double r, double g, double b)
129 CairoAPI.cairo_set_rgb_color (state, r, g, b);
132 public double Tolerance {
134 CairoAPI.cairo_set_tolerance (state, value);
138 return CairoAPI.cairo_current_tolerance (state);
142 public double Alpha {
144 CairoAPI.cairo_set_alpha (state, value);
148 return CairoAPI.cairo_current_alpha (state);
152 public Cairo.FillRule FillRule {
154 CairoAPI.cairo_set_fill_rule (state, value);
158 return CairoAPI.cairo_current_fill_rule (state);
162 public double LineWidth {
164 CairoAPI.cairo_set_line_width (state, value);
168 return CairoAPI.cairo_current_line_width (state);
172 public Cairo.LineCap LineCap {
174 CairoAPI.cairo_set_line_cap (state, value);
178 return CairoAPI.cairo_current_line_cap (state);
182 public Cairo.LineJoin LineJoin {
184 CairoAPI.cairo_set_line_join (state, value);
188 return CairoAPI.cairo_current_line_join (state);
192 public void SetDash (double [] dashes, int ndash, double offset)
194 CairoAPI.cairo_set_dash (state, dashes, ndash, offset);
197 public Cairo.Surface Pattern {
199 CairoAPI.cairo_set_pattern (state, value.Handle);
203 public double MiterLimit {
205 CairoAPI.cairo_set_miter_limit (state, value);
209 return CairoAPI.cairo_current_miter_limit (state);
213 public void GetCurrentPoint (out double x, out double y)
215 CairoAPI.cairo_current_point (state, out x, out y);
218 public Point CurrentPoint {
221 CairoAPI.cairo_current_point (state, out x, out y);
222 return new Point ((int) x, (int) y);
226 public Cairo.Surface TargetSurface {
228 CairoAPI.cairo_set_target_surface (state, value.Handle);
232 return Cairo.Surface.LookupExternalSurface (
233 CairoAPI.cairo_current_target_surface (state));
239 public void NewPath ()
241 CairoAPI.cairo_new_path (state);
244 public void MoveTo (double x, double y)
246 CairoAPI.cairo_move_to (state, x, y);
249 public void LineTo (double x, double y)
251 CairoAPI.cairo_line_to (state, x, y);
254 public void CurveTo (double x1, double y1, double x2, double y2, double x3, double y3)
256 CairoAPI.cairo_curve_to (state, x1, y1, x2, y2, x3, y3);
259 public void CurveTo (Point p1, Point p2, Point p3)
261 CairoAPI.cairo_curve_to (state, p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
264 public void RelMoveTo (double dx, double dy)
266 CairoAPI.cairo_rel_move_to (state, dx, dy);
269 public void RelLineTo (double dx, double dy)
271 CairoAPI.cairo_rel_line_to (state, dx, dy);
274 public void RelCurveTo (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
276 CairoAPI.cairo_rel_curve_to (state, dx1, dy1, dx2, dy2, dx3, dy3);
279 public void Arc (double xc, double yc, double radius, double angel1, double angel2)
281 CairoAPI.cairo_arc (state, xc, yc, radius, angel1, angel2);
284 public void ArcNegative (double xc, double yc, double radius, double angel1, double angel2)
286 CairoAPI.cairo_arc_negative (state, xc, yc, radius, angel1, angel2);
289 public void Rectangle (double x, double y, double width, double height)
291 CairoAPI.cairo_rectangle (state, x, y, width, height);
294 public void ClosePath ()
296 CairoAPI.cairo_close_path (state);
300 #region Painting Methods
302 public void Stroke ()
304 CairoAPI.cairo_stroke (state);
309 CairoAPI.cairo_fill (state);
316 CairoAPI.cairo_clip (state);
319 #region Modified state
321 public void SetTargetImage (
322 string data, Cairo.Format format, int width, int height, int stride)
324 CairoAPI.cairo_set_target_image (state, data, format, width, height, stride);
327 public void SetTargetDrawable (IntPtr dpy, IntPtr drawable)
329 CairoAPI.cairo_set_target_drawable (state, dpy, drawable);
332 public void SetPattern (Pattern pattern)
334 CairoAPI.cairo_set_pattern (state, pattern.Pointer);
338 public void Rotate (double angle)
340 CairoAPI.cairo_rotate (state, angle);
343 public void Scale (double sx, double sy)
345 CairoAPI.cairo_scale (state, sx, sy);
348 public void Translate (double tx, double ty)
350 CairoAPI.cairo_translate (state, tx, ty);
353 public void TransformPoint (ref double x, ref double y)
355 CairoAPI.cairo_transform_point (state, ref x, ref y);
358 public void TransformDistance (ref double dx, ref double dy)
360 CairoAPI.cairo_transform_distance (state, ref dx, ref dy);
363 public void InverseTransformPoint (ref double x, ref double y)
365 CairoAPI.cairo_inverse_transform_point (state, ref x, ref y);
368 public void InverseTransformDistance (ref double dx, ref double dy)
370 CairoAPI.cairo_inverse_transform_distance (state, ref dx, ref dy);
373 public void ConcatMatrix (Cairo.Matrix matrix)
375 CairoAPI.cairo_concat_matrix (state, matrix.Pointer);
378 public Cairo.Matrix Matrix {
380 CairoAPI.cairo_set_matrix (state, value.Pointer);
384 IntPtr p = CairoAPI.cairo_matrix_create ();
385 CairoAPI.cairo_current_matrix (state, p);
386 Matrix mat = new Cairo.Matrix (p);
393 CairoAPI.cairo_set_font (state, value.Pointer);
398 IntPtr fnt = IntPtr.Zero;
400 fnt = CairoAPI.cairo_current_font (state);
402 return new Font (fnt);
407 public void ScaleFont (double scale)
409 CairoAPI.cairo_scale_font (state, scale);
412 public void SetText (string str)
414 /* Use UTF8 encoding*/
415 CairoAPI.cairo_show_text (state, str);
418 public void TransformFont (Matrix matrix)
420 CairoAPI.cairo_transform_font (state, matrix.Pointer);
425 static internal IntPtr FromGlyphToUnManagedMemory(Glyph [] glyphs)
427 int size = Marshal.SizeOf (glyphs[0]);
428 IntPtr dest = Marshal.AllocHGlobal (size * glyphs.Length);
429 int pos = dest.ToInt32();
431 for (int i=0; i < glyphs.Length; i++, pos += size)
432 Marshal.StructureToPtr (glyphs[i], (IntPtr)pos, false);
438 public void ShowGlyphs (Matrix matrix, Glyph[] glyphs)
443 ptr = FromGlyphToUnManagedMemory (glyphs);
445 CairoAPI.cairo_show_glyphs (state, ptr, glyphs.Length);
447 Marshal.FreeHGlobal (ptr);
451 public void TextPath (string str)
453 CairoAPI.cairo_text_path (state, str);
456 public void GlyphPath (Matrix matrix, Glyph[] glyphs)
461 ptr = FromGlyphToUnManagedMemory (glyphs);
463 CairoAPI.cairo_glyph_path (state, ptr, glyphs.Length);
465 Marshal.FreeHGlobal (ptr);
469 public void SelectFont (string key, FontSlant slant, FontWeight weight)
471 CairoAPI.cairo_select_font (state, key, slant, weight);
476 public Extents Extents {
479 Extents extents = new Extents();
480 CairoAPI.cairo_current_font_extents (state, ref extents);