2 // Mono.Cairo.Graphics.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Miguel de Icaza (miguel@novell.com)
7 // Hisham Mardam Bey (hisham.mardambey@gmail.com)
9 // (C) Ximian Inc, 2003.
10 // (C) Novell Inc, 2003.
12 // This is an OO wrapper API for the Cairo API.
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.Runtime.InteropServices;
47 public Point (int x, int y)
59 public PointD (double x, double y)
72 public Distance (double x, double y)
86 public Color(double r, double g, double b, double a)
95 public class Graphics : IDisposable
97 internal IntPtr state = IntPtr.Zero;
98 //private Surface surface;
100 public Graphics (Surface surface)
102 state = CairoAPI.cairo_create (surface.Pointer);
105 public Graphics (IntPtr state)
115 void IDisposable.Dispose ()
118 GC.SuppressFinalize (this);
121 protected virtual void Dispose (bool disposing)
124 //Console.WriteLine ("Cairo.Graphics: called from thread");
128 if (state == IntPtr.Zero)
131 CairoAPI.cairo_destroy (state);
135 public Cairo.Graphics Copy ()
138 CairoAPI.cairo_copy (out dest, state);
139 return new Cairo.Graphics (dest);
144 CairoAPI.cairo_save (state);
147 public void Restore ()
149 CairoAPI.cairo_restore (state);
152 public Antialias Antialias {
153 get { return CairoAPI.cairo_get_antialias (state); }
154 set { CairoAPI.cairo_set_antialias (state, value); }
157 public Cairo.Status Status {
159 return CairoAPI.cairo_status (state);
164 public string StatusString {
166 return CairoAPI.cairo_status_to_string (state);
170 public IntPtr Handle {
176 public Cairo.Operator Operator {
178 CairoAPI.cairo_set_operator (state, value);
182 return CairoAPI.cairo_get_operator (state);
186 public Cairo.Color Color {
188 CairoAPI.cairo_set_source_rgba (state, value.R,
194 public Cairo.Color ColorRgb {
196 CairoAPI.cairo_set_source_rgb (state, value.R,
201 public double Tolerance {
203 CairoAPI.cairo_set_tolerance (state, value);
207 public Cairo.FillRule FillRule {
209 CairoAPI.cairo_set_fill_rule (state, value);
213 return CairoAPI.cairo_get_fill_rule (state);
217 public double LineWidth {
219 CairoAPI.cairo_set_line_width (state, value);
223 return CairoAPI.cairo_get_line_width (state);
227 public Cairo.LineCap LineCap {
229 CairoAPI.cairo_set_line_cap (state, value);
233 return CairoAPI.cairo_current_line_cap (state);
237 public Cairo.LineJoin LineJoin {
239 CairoAPI.cairo_set_line_join (state, value);
243 return CairoAPI.cairo_get_line_join (state);
247 public void SetDash (double [] dashes, int ndash, double offset)
249 CairoAPI.cairo_set_dash (state, dashes, ndash, offset);
252 public Pattern Pattern {
254 CairoAPI.cairo_set_source (state, value.Pointer);
258 return new Pattern (CairoAPI.cairo_get_source (state));
262 public Pattern Source {
264 CairoAPI.cairo_set_source (state, value.Pointer);
268 return new Pattern (CairoAPI.cairo_get_source (state));
272 public double MiterLimit {
274 CairoAPI.cairo_set_miter_limit (state, value);
278 return CairoAPI.cairo_get_miter_limit (state);
282 public PointD CurrentPoint {
285 CairoAPI.cairo_get_current_point (state, out x, out y);
286 return new PointD (x, y);
290 public Cairo.Surface TargetSurface {
292 state = CairoAPI.cairo_create (value.Pointer);
293 //CairoAPI.cairo_set_target_surface (state, value.Handle);
297 return Cairo.Surface.LookupExternalSurface (
298 CairoAPI.cairo_get_target (state));
304 public void NewPath ()
306 CairoAPI.cairo_new_path (state);
309 public void CurrentPath (MoveToCallback move_to,
310 LineToCallback line_to,
311 CurveToCallback curve_to,
312 ClosePathCallback close_path,
318 public void MoveTo (PointD p)
323 public void MoveTo (double x, double y)
325 CairoAPI.cairo_move_to (state, x, y);
328 public void LineTo (PointD p)
333 public void LineTo (double x, double y)
335 CairoAPI.cairo_line_to (state, x, y);
338 public void CurveTo (PointD p1, PointD p2, PointD p3)
340 CurveTo (p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
343 public void CurveTo (double x1, double y1, double x2, double y2, double x3, double y3)
345 CairoAPI.cairo_curve_to (state, x1, y1, x2, y2, x3, y3);
348 public void RelMoveTo (PointD p)
350 RelMoveTo (p.X, p.Y);
353 public void RelMoveTo (double x, double y)
355 CairoAPI.cairo_rel_move_to (state, x, y);
358 public void RelLineTo (PointD p)
360 RelLineTo (p.X, p.Y);
363 public void RelLineTo (double x, double y)
365 CairoAPI.cairo_rel_line_to (state, x, y);
368 public void RelCurveTo (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
370 CairoAPI.cairo_rel_curve_to (state, dx1, dy1, dx2, dy2, dx3, dy3);
373 public void Arc (double xc, double yc, double radius, double angel1, double angel2)
375 CairoAPI.cairo_arc (state, xc, yc, radius, angel1, angel2);
378 public void ArcNegative (double xc, double yc, double radius, double angel1, double angel2)
380 CairoAPI.cairo_arc_negative (state, xc, yc, radius, angel1, angel2);
383 public void ArcTo (PointD p1, PointD p2, double radius)
385 ArcTo (p1.X, p1.Y, p2.X, p2.Y, radius);
388 public void ArcTo (double x1, double y1, double x2, double y2, double radius)
390 CairoAPI.cairo_arc_to (state, x1, y1, x2, y2, radius);
393 public void Rectangle (PointD p, double width, double height)
395 Rectangle (p.X, p.Y, width, height);
398 public void Rectangle (double x, double y, double width, double height)
400 CairoAPI.cairo_rectangle (state, x, y, width, height);
403 public void ClosePath ()
405 CairoAPI.cairo_close_path (state);
409 #region Painting Methods
411 public void Stroke ()
413 CairoAPI.cairo_stroke (state);
416 public void StrokePreserve ()
418 CairoAPI.cairo_stroke_preserve (state);
423 CairoAPI.cairo_fill (state);
426 public void FillPreserve ()
428 CairoAPI.cairo_fill_preserve (state);
435 CairoAPI.cairo_clip (state);
438 public void ClipReset ()
440 CairoAPI.cairo_reset_clip (state);
443 public bool InStroke (double x, double y)
445 return CairoAPI.cairo_in_stroke (state, x, y);
448 public bool InFill (double x, double y)
450 return CairoAPI.cairo_in_fill (state, x, y);
454 #region Modified state
456 public void SetTargetImage (
457 string data, Cairo.Format format, int width, int height, int stride)
459 CairoAPI.cairo_image_surface_create_for_data (data, format, width, height, stride);
462 public void SetTargetDrawable (IntPtr dpy, IntPtr drawable, IntPtr visual, int width, int height)
464 CairoAPI.cairo_xlib_surface_create (dpy, drawable, visual, width, height);
468 public void Rotate (double angle)
470 CairoAPI.cairo_rotate (state, angle);
473 public void Scale (double sx, double sy)
475 CairoAPI.cairo_scale (state, sx, sy);
478 public void Translate (double tx, double ty)
480 CairoAPI.cairo_translate (state, tx, ty);
483 public PointD TransformPoint
487 CairoAPI.cairo_user_to_device (state, out x, out y);
488 return new PointD(x, y);
492 public Distance TransformDistance
495 double dx; double dy;
496 CairoAPI.cairo_user_to_device_distance (state, out dx, out dy);
497 return new Distance(dx, dy);
501 public PointD InverseTransformPoint
505 CairoAPI.cairo_device_to_user (state, out x, out y);
506 return new PointD (x, y);
510 public Distance InverseTransformDistance
513 double dx; double dy;
514 CairoAPI.cairo_device_to_user_distance (state, out dx, out dy);
515 return new Distance (dx, dy);
519 public Cairo.Matrix Matrix {
521 CairoAPI.cairo_set_matrix (state, value.Pointer);
525 Matrix_T m = new Matrix_T ();
526 CairoAPI.cairo_get_matrix (state, m);
527 return new Matrix (m);
533 CairoAPI.cairo_set_font (state, value.Pointer);
538 IntPtr fnt = IntPtr.Zero;
540 fnt = CairoAPI.cairo_current_font (state);
542 return new Font (fnt);
547 public void FontSetSize (double size)
549 CairoAPI.cairo_set_font_size (state, size);
552 public void FontSetMatrix (Matrix m)
554 CairoAPI.cairo_set_font_matrix (state, m.Pointer);
558 public void TransformFont (Matrix matrix)
560 CairoAPI.cairo_transform_font (state, matrix.Pointer);
565 static internal IntPtr FromGlyphToUnManagedMemory(Glyph [] glyphs)
567 int size = Marshal.SizeOf (glyphs[0]);
568 IntPtr dest = Marshal.AllocHGlobal (size * glyphs.Length);
569 int pos = dest.ToInt32();
571 for (int i=0; i < glyphs.Length; i++, pos += size)
572 Marshal.StructureToPtr (glyphs[i], (IntPtr)pos, false);
578 public void ShowGlyphs (Matrix matrix, Glyph[] glyphs)
583 ptr = FromGlyphToUnManagedMemory (glyphs);
585 CairoAPI.cairo_show_glyphs (state, ptr, glyphs.Length);
587 Marshal.FreeHGlobal (ptr);
591 public void GlyphPath (Matrix matrix, Glyph[] glyphs)
596 ptr = FromGlyphToUnManagedMemory (glyphs);
598 CairoAPI.cairo_glyph_path (state, ptr, glyphs.Length);
600 Marshal.FreeHGlobal (ptr);
604 public FontExtents FontExtents {
607 FontExtents f_extents = new FontExtents();
608 CairoAPI.cairo_font_extents (state, ref f_extents);
613 public void FontFace (string family, FontSlant s, FontWeight w)
615 CairoAPI.cairo_select_font_face (state, family, s, w);
618 public double FontSize {
619 set { CairoAPI.cairo_set_font_size (state, value); }
622 public void ShowText (string str)
624 CairoAPI.cairo_show_text (state, str);
627 public void TextPath (string str)
629 CairoAPI.cairo_text_path (state, str);