2 // Mono.Cairo.Context.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Miguel de Icaza (miguel@novell.com)
7 // Hisham Mardam Bey (hisham.mardambey@gmail.com)
8 // Alp Toker (alp@atoker.com)
10 // (C) Ximian Inc, 2003.
11 // (C) Novell Inc, 2003.
13 // This is an OO wrapper API for the Cairo API.
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System.Runtime.InteropServices;
44 [Obsolete ("Renamed Cairo.Context per suggestion from cairo binding guidelines.")]
45 public class Graphics : Context {
46 public Graphics (IntPtr state) : base (state) {}
47 public Graphics (Surface surface) : base (surface) {}
50 public class Context : IDisposable
52 IntPtr handle = IntPtr.Zero;
54 static int native_glyph_size, c_compiler_long_size;
59 // This is used to determine what kind of structure
60 // we should use to marshal Glyphs, as the public
61 // definition in Cairo uses `long', which can be
62 // 32 bits or 64 bits depending on the platform.
64 // We assume that sizeof(long) == sizeof(void*)
65 // except in the case of Win64 where sizeof(long)
68 int ptr_size = Marshal.SizeOf (typeof (IntPtr));
70 PlatformID platform = Environment.OSVersion.Platform;
71 if (platform == PlatformID.Win32NT ||
72 platform == PlatformID.Win32S ||
73 platform == PlatformID.Win32Windows ||
74 platform == PlatformID.WinCE ||
76 c_compiler_long_size = 4;
77 native_glyph_size = Marshal.SizeOf (typeof (NativeGlyph_4byte_longs));
79 c_compiler_long_size = 8;
80 native_glyph_size = Marshal.SizeOf (typeof (Glyph));
84 public Context (Surface surface) : this (NativeMethods.cairo_create (surface.Handle), true)
89 public Context (IntPtr handle, bool owner)
93 NativeMethods.cairo_reference (handle);
94 if (CairoDebug.Enabled)
95 CairoDebug.OnAllocated (handle);
99 public Context (IntPtr state) : this (state, true)
108 public void Dispose ()
111 GC.SuppressFinalize (this);
114 protected virtual void Dispose (bool disposing)
116 if (!disposing || CairoDebug.Enabled)
117 CairoDebug.OnDisposed<Context> (handle, disposing);
119 if (!disposing|| handle == IntPtr.Zero)
122 NativeMethods.cairo_destroy (handle);
123 handle = IntPtr.Zero;
129 NativeMethods.cairo_save (handle);
132 public void Restore ()
134 NativeMethods.cairo_restore (handle);
137 public Antialias Antialias {
138 get { return NativeMethods.cairo_get_antialias (handle); }
139 set { NativeMethods.cairo_set_antialias (handle, value); }
142 public Cairo.Status Status {
144 return NativeMethods.cairo_status (handle);
148 public IntPtr Handle {
154 public Operator Operator {
156 NativeMethods.cairo_set_operator (handle, value);
160 return NativeMethods.cairo_get_operator (handle);
164 [Obsolete ("Use SetSourceRGBA method")]
167 NativeMethods.cairo_set_source_rgba (handle, value.R, value.G, value.B, value.A);
171 [Obsolete ("Use SetSourceRGBA method")]
172 public Cairo.Color ColorRgb {
174 Color = new Color (value.R, value.G, value.B);
178 public double Tolerance {
180 return NativeMethods.cairo_get_tolerance (handle);
184 NativeMethods.cairo_set_tolerance (handle, value);
188 public Cairo.FillRule FillRule {
190 NativeMethods.cairo_set_fill_rule (handle, value);
194 return NativeMethods.cairo_get_fill_rule (handle);
198 public double LineWidth {
200 NativeMethods.cairo_set_line_width (handle, value);
204 return NativeMethods.cairo_get_line_width (handle);
208 public Cairo.LineCap LineCap {
210 NativeMethods.cairo_set_line_cap (handle, value);
214 return NativeMethods.cairo_get_line_cap (handle);
218 public Cairo.LineJoin LineJoin {
220 NativeMethods.cairo_set_line_join (handle, value);
224 return NativeMethods.cairo_get_line_join (handle);
228 public void SetDash (double [] dashes, double offset)
230 NativeMethods.cairo_set_dash (handle, dashes, dashes.Length, offset);
233 [Obsolete("Use GetSource/GetSource")]
234 public Pattern Pattern {
243 //This is obsolete because it wasn't obvious it needed to be disposed
244 [Obsolete("Use GetSource/GetSource")]
245 public Pattern Source {
254 public void SetSource (Pattern source)
256 NativeMethods.cairo_set_source (handle, source.Handle);
259 public Pattern GetSource ()
261 var ptr = NativeMethods.cairo_get_source (handle);
262 return Cairo.Pattern.Lookup (ptr, false);
265 public double MiterLimit {
267 NativeMethods.cairo_set_miter_limit (handle, value);
271 return NativeMethods.cairo_get_miter_limit (handle);
275 public PointD CurrentPoint {
278 NativeMethods.cairo_get_current_point (handle, out x, out y);
279 return new PointD (x, y);
283 [Obsolete ("Use GetTarget/SetTarget")]
284 public Cairo.Surface Target {
286 if (handle != IntPtr.Zero)
287 NativeMethods.cairo_destroy (handle);
289 handle = NativeMethods.cairo_create (value.Handle);
297 public Surface GetTarget ()
299 return Surface.Lookup (NativeMethods.cairo_get_target (handle), false);
302 public void SetTarget (Surface target)
304 if (handle != IntPtr.Zero)
305 NativeMethods.cairo_destroy (handle);
306 handle = NativeMethods.cairo_create (target.Handle);
309 [Obsolete("Use GetScaledFont/SetScaledFont")]
310 public ScaledFont ScaledFont {
312 SetScaledFont (value);
316 return GetScaledFont ();
320 public ScaledFont GetScaledFont ()
322 return new ScaledFont (NativeMethods.cairo_get_scaled_font (handle), false);
325 public void SetScaledFont (ScaledFont font)
327 NativeMethods.cairo_set_scaled_font (handle, font.Handle);
330 public uint ReferenceCount {
331 get { return NativeMethods.cairo_get_reference_count (handle); }
334 public void SetSourceRGB (double r, double g, double b)
336 NativeMethods.cairo_set_source_rgb (handle, r, g, b);
339 public void SetSourceRGBA (double r, double g, double b, double a)
341 NativeMethods.cairo_set_source_rgba (handle, r, g, b, a);
344 //[Obsolete ("Use SetSource method (with double parameters)")]
345 public void SetSourceSurface (Surface source, int x, int y)
347 NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y);
350 public void SetSource (Surface source, double x, double y)
352 NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y);
355 public void SetSource (Surface source)
357 NativeMethods.cairo_set_source_surface (handle, source.Handle, 0, 0);
362 public void NewPath ()
364 NativeMethods.cairo_new_path (handle);
367 public void NewSubPath ()
369 NativeMethods.cairo_new_sub_path (handle);
372 public void MoveTo (PointD p)
377 public void MoveTo (double x, double y)
379 NativeMethods.cairo_move_to (handle, x, y);
382 public void LineTo (PointD p)
387 public void LineTo (double x, double y)
389 NativeMethods.cairo_line_to (handle, x, y);
392 public void CurveTo (PointD p1, PointD p2, PointD p3)
394 CurveTo (p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
397 public void CurveTo (double x1, double y1, double x2, double y2, double x3, double y3)
399 NativeMethods.cairo_curve_to (handle, x1, y1, x2, y2, x3, y3);
402 public void RelMoveTo (Distance d)
404 RelMoveTo (d.Dx, d.Dy);
407 public void RelMoveTo (double dx, double dy)
409 NativeMethods.cairo_rel_move_to (handle, dx, dy);
412 public void RelLineTo (Distance d)
414 RelLineTo (d.Dx, d.Dy);
417 public void RelLineTo (double dx, double dy)
419 NativeMethods.cairo_rel_line_to (handle, dx, dy);
422 public void RelCurveTo (Distance d1, Distance d2, Distance d3)
424 RelCurveTo (d1.Dx, d1.Dy, d2.Dx, d2.Dy, d3.Dx, d3.Dy);
427 public void RelCurveTo (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
429 NativeMethods.cairo_rel_curve_to (handle, dx1, dy1, dx2, dy2, dx3, dy3);
432 public void Arc (double xc, double yc, double radius, double angle1, double angle2)
434 NativeMethods.cairo_arc (handle, xc, yc, radius, angle1, angle2);
437 public void ArcNegative (double xc, double yc, double radius, double angle1, double angle2)
439 NativeMethods.cairo_arc_negative (handle, xc, yc, radius, angle1, angle2);
442 public void Rectangle (Rectangle rectangle)
444 Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
447 public void Rectangle (PointD p, double width, double height)
449 Rectangle (p.X, p.Y, width, height);
452 public void Rectangle (double x, double y, double width, double height)
454 NativeMethods.cairo_rectangle (handle, x, y, width, height);
457 public void ClosePath ()
459 NativeMethods.cairo_close_path (handle);
462 public Path CopyPath ()
464 return new Path (NativeMethods.cairo_copy_path (handle));
467 public Path CopyPathFlat ()
469 return new Path (NativeMethods.cairo_copy_path_flat (handle));
472 public void AppendPath (Path path)
474 NativeMethods.cairo_append_path (handle, path.Handle);
479 #region Painting Methods
482 NativeMethods.cairo_paint (handle);
485 public void PaintWithAlpha (double alpha)
487 NativeMethods.cairo_paint_with_alpha (handle, alpha);
490 public void Mask (Pattern pattern)
492 NativeMethods.cairo_mask (handle, pattern.Handle);
495 public void MaskSurface (Surface surface, double surface_x, double surface_y)
497 NativeMethods.cairo_mask_surface (handle, surface.Handle, surface_x, surface_y);
500 public void Stroke ()
502 NativeMethods.cairo_stroke (handle);
505 public void StrokePreserve ()
507 NativeMethods.cairo_stroke_preserve (handle);
510 public Rectangle StrokeExtents ()
512 double x1, y1, x2, y2;
513 NativeMethods.cairo_stroke_extents (handle, out x1, out y1, out x2, out y2);
514 return new Rectangle (x1, y1, x2 - x1, y2 - y1);
519 NativeMethods.cairo_fill (handle);
522 public Rectangle FillExtents ()
524 double x1, y1, x2, y2;
525 NativeMethods.cairo_fill_extents (handle, out x1, out y1, out x2, out y2);
526 return new Rectangle (x1, y1, x2 - x1, y2 - y1);
529 public void FillPreserve ()
531 NativeMethods.cairo_fill_preserve (handle);
538 NativeMethods.cairo_clip (handle);
541 public void ClipPreserve ()
543 NativeMethods.cairo_clip_preserve (handle);
546 public void ResetClip ()
548 NativeMethods.cairo_reset_clip (handle);
551 public bool InStroke (double x, double y)
553 return NativeMethods.cairo_in_stroke (handle, x, y);
556 public bool InFill (double x, double y)
558 return NativeMethods.cairo_in_fill (handle, x, y);
561 public Pattern PopGroup ()
563 return Pattern.Lookup (NativeMethods.cairo_pop_group (handle), true);
566 public void PopGroupToSource ()
568 NativeMethods.cairo_pop_group_to_source (handle);
571 public void PushGroup ()
573 NativeMethods.cairo_push_group (handle);
576 public void PushGroup (Content content)
578 NativeMethods.cairo_push_group_with_content (handle, content);
581 [Obsolete ("Use GetGroupTarget()")]
582 public Surface GroupTarget {
584 return GetGroupTarget ();
588 public Surface GetGroupTarget ()
590 IntPtr surface = NativeMethods.cairo_get_group_target (handle);
591 return Surface.Lookup (surface, false);
594 public void Rotate (double angle)
596 NativeMethods.cairo_rotate (handle, angle);
599 public void Scale (double sx, double sy)
601 NativeMethods.cairo_scale (handle, sx, sy);
604 public void Translate (double tx, double ty)
606 NativeMethods.cairo_translate (handle, tx, ty);
609 public void Transform (Matrix m)
611 NativeMethods.cairo_transform (handle, m);
614 [Obsolete("Use UserToDevice instead")]
615 public void TransformPoint (ref double x, ref double y)
617 NativeMethods.cairo_user_to_device (handle, ref x, ref y);
620 [Obsolete("Use UserToDeviceDistance instead")]
621 public void TransformDistance (ref double dx, ref double dy)
623 NativeMethods.cairo_user_to_device_distance (handle, ref dx, ref dy);
626 [Obsolete("Use InverseTransformPoint instead")]
627 public void InverseTransformPoint (ref double x, ref double y)
629 NativeMethods.cairo_device_to_user (handle, ref x, ref y);
632 [Obsolete("Use DeviceToUserDistance instead")]
633 public void InverseTransformDistance (ref double dx, ref double dy)
635 NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy);
638 public void UserToDevice (ref double x, ref double y)
640 NativeMethods.cairo_user_to_device (handle, ref x, ref y);
643 public void UserToDeviceDistance (ref double dx, ref double dy)
645 NativeMethods.cairo_user_to_device_distance (handle, ref dx, ref dy);
648 public void DeviceToUser (ref double x, ref double y)
650 NativeMethods.cairo_device_to_user (handle, ref x, ref y);
653 public void DeviceToUserDistance (ref double dx, ref double dy)
655 NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy);
658 public Matrix Matrix {
660 NativeMethods.cairo_set_matrix (handle, value);
664 Matrix m = new Matrix();
665 NativeMethods.cairo_get_matrix (handle, m);
670 public void SetFontSize (double scale)
672 NativeMethods.cairo_set_font_size (handle, scale);
675 public void IdentityMatrix ()
677 NativeMethods.cairo_identity_matrix (handle);
680 [Obsolete ("Use SetFontSize() instead.")]
681 public void FontSetSize (double scale)
686 [Obsolete ("Use SetFontSize() instead.")]
687 public double FontSize {
688 set { SetFontSize (value); }
691 public Matrix FontMatrix {
694 NativeMethods.cairo_get_font_matrix (handle, out m);
697 set { NativeMethods.cairo_set_font_matrix (handle, value); }
700 public FontOptions FontOptions {
702 FontOptions options = new FontOptions ();
703 NativeMethods.cairo_get_font_options (handle, options.Handle);
706 set { NativeMethods.cairo_set_font_options (handle, value.Handle); }
709 [StructLayout(LayoutKind.Sequential)]
710 internal struct NativeGlyph_4byte_longs {
715 public NativeGlyph_4byte_longs (Glyph source)
717 index = (int) source.index;
723 static internal IntPtr FromGlyphToUnManagedMemory(Glyph [] glyphs)
725 IntPtr dest = Marshal.AllocHGlobal (native_glyph_size * glyphs.Length);
726 long pos = dest.ToInt64();
728 if (c_compiler_long_size == 8){
729 foreach (Glyph g in glyphs){
730 Marshal.StructureToPtr (g, (IntPtr)pos, false);
731 pos += native_glyph_size;
734 foreach (Glyph g in glyphs){
735 NativeGlyph_4byte_longs n = new NativeGlyph_4byte_longs (g);
737 Marshal.StructureToPtr (n, (IntPtr)pos, false);
738 pos += native_glyph_size;
745 public void ShowGlyphs (Glyph[] glyphs)
749 ptr = FromGlyphToUnManagedMemory (glyphs);
751 NativeMethods.cairo_show_glyphs (handle, ptr, glyphs.Length);
753 Marshal.FreeHGlobal (ptr);
756 [Obsolete("The matrix argument was never used, use ShowGlyphs(Glyphs []) instead")]
757 public void ShowGlyphs (Matrix matrix, Glyph[] glyphs)
762 [Obsolete("The matrix argument was never used, use GlyphPath(Glyphs []) instead")]
763 public void GlyphPath (Matrix matrix, Glyph[] glyphs)
768 public void GlyphPath (Glyph[] glyphs)
772 ptr = FromGlyphToUnManagedMemory (glyphs);
774 NativeMethods.cairo_glyph_path (handle, ptr, glyphs.Length);
776 Marshal.FreeHGlobal (ptr);
780 public FontExtents FontExtents {
782 FontExtents f_extents;
783 NativeMethods.cairo_font_extents (handle, out f_extents);
788 public void CopyPage ()
790 NativeMethods.cairo_copy_page (handle);
793 [Obsolete ("Use SelectFontFace() instead.")]
794 public void FontFace (string family, FontSlant slant, FontWeight weight)
796 SelectFontFace (family, slant, weight);
799 [Obsolete("Use GetFontFace/SetFontFace")]
800 public FontFace ContextFontFace {
802 return GetContextFontFace ();
805 SetContextFontFace (value);
809 public FontFace GetContextFontFace ()
811 return Cairo.FontFace.Lookup (NativeMethods.cairo_get_font_face (handle), false);
814 public void SetContextFontFace (FontFace value)
816 NativeMethods.cairo_set_font_face (handle, value == null ? IntPtr.Zero : value.Handle);
819 public void SelectFontFace (string family, FontSlant slant, FontWeight weight)
821 NativeMethods.cairo_select_font_face (handle, family, slant, weight);
824 public void ShowPage ()
826 NativeMethods.cairo_show_page (handle);
829 private static byte[] TerminateUtf8(byte[] utf8)
831 if (utf8.Length > 0 && utf8[utf8.Length - 1] == 0)
833 var termedArray = new byte[utf8.Length + 1];
834 Array.Copy(utf8, termedArray, utf8.Length);
835 termedArray[utf8.Length] = 0;
839 private static byte[] TerminateUtf8(string s)
841 // compute the byte count including the trailing \0
842 var byteCount = Encoding.UTF8.GetMaxByteCount(s.Length + 1);
843 var bytes = new byte[byteCount];
844 Encoding.UTF8.GetBytes(s, 0, s.Length, bytes, 0);
848 public void ShowText(string str)
850 NativeMethods.cairo_show_text (handle, TerminateUtf8(str));
853 public void ShowText(byte[] utf8)
855 NativeMethods.cairo_show_text (handle, TerminateUtf8(utf8));
858 public void TextPath(string str)
860 NativeMethods.cairo_text_path (handle, TerminateUtf8(str));
863 public void TextPath(byte[] utf8)
865 NativeMethods.cairo_text_path (handle, TerminateUtf8(utf8));
868 public TextExtents TextExtents(string s)
871 NativeMethods.cairo_text_extents (handle, TerminateUtf8(s), out extents);
875 public TextExtents TextExtents(byte[] utf8)
878 NativeMethods.cairo_text_extents (handle, TerminateUtf8(utf8), out extents);
882 public TextExtents GlyphExtents (Glyph[] glyphs)
884 IntPtr ptr = FromGlyphToUnManagedMemory (glyphs);
888 NativeMethods.cairo_glyph_extents (handle, ptr, glyphs.Length, out extents);
890 Marshal.FreeHGlobal (ptr);