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 Source")]
234 public Pattern Pattern {
244 public Pattern Source {
246 NativeMethods.cairo_set_source (handle, value.Handle);
250 var ptr = NativeMethods.cairo_get_source (handle);
251 return Cairo.Pattern.Lookup (ptr, false);
255 public double MiterLimit {
257 NativeMethods.cairo_set_miter_limit (handle, value);
261 return NativeMethods.cairo_get_miter_limit (handle);
265 public PointD CurrentPoint {
268 NativeMethods.cairo_get_current_point (handle, out x, out y);
269 return new PointD (x, y);
273 public Cairo.Surface Target {
275 if (handle != IntPtr.Zero)
276 NativeMethods.cairo_destroy (handle);
278 handle = NativeMethods.cairo_create (value.Handle);
282 return Surface.Lookup (NativeMethods.cairo_get_target (handle), false);
286 public ScaledFont ScaledFont {
288 NativeMethods.cairo_set_scaled_font (handle, value.Handle);
292 return new ScaledFont (NativeMethods.cairo_get_scaled_font (handle), false);
296 public uint ReferenceCount {
297 get { return NativeMethods.cairo_get_reference_count (handle); }
300 public void SetSourceRGB (double r, double g, double b)
302 NativeMethods.cairo_set_source_rgb (handle, r, g, b);
305 public void SetSourceRGBA (double r, double g, double b, double a)
307 NativeMethods.cairo_set_source_rgba (handle, r, g, b, a);
310 //[Obsolete ("Use SetSource method (with double parameters)")]
311 public void SetSourceSurface (Surface source, int x, int y)
313 NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y);
316 public void SetSource (Surface source, double x, double y)
318 NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y);
321 public void SetSource (Surface source)
323 NativeMethods.cairo_set_source_surface (handle, source.Handle, 0, 0);
328 public void NewPath ()
330 NativeMethods.cairo_new_path (handle);
333 public void NewSubPath ()
335 NativeMethods.cairo_new_sub_path (handle);
338 public void MoveTo (PointD p)
343 public void MoveTo (double x, double y)
345 NativeMethods.cairo_move_to (handle, x, y);
348 public void LineTo (PointD p)
353 public void LineTo (double x, double y)
355 NativeMethods.cairo_line_to (handle, x, y);
358 public void CurveTo (PointD p1, PointD p2, PointD p3)
360 CurveTo (p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
363 public void CurveTo (double x1, double y1, double x2, double y2, double x3, double y3)
365 NativeMethods.cairo_curve_to (handle, x1, y1, x2, y2, x3, y3);
368 public void RelMoveTo (Distance d)
370 RelMoveTo (d.Dx, d.Dy);
373 public void RelMoveTo (double dx, double dy)
375 NativeMethods.cairo_rel_move_to (handle, dx, dy);
378 public void RelLineTo (Distance d)
380 RelLineTo (d.Dx, d.Dy);
383 public void RelLineTo (double dx, double dy)
385 NativeMethods.cairo_rel_line_to (handle, dx, dy);
388 public void RelCurveTo (Distance d1, Distance d2, Distance d3)
390 RelCurveTo (d1.Dx, d1.Dy, d2.Dx, d2.Dy, d3.Dx, d3.Dy);
393 public void RelCurveTo (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
395 NativeMethods.cairo_rel_curve_to (handle, dx1, dy1, dx2, dy2, dx3, dy3);
398 public void Arc (double xc, double yc, double radius, double angle1, double angle2)
400 NativeMethods.cairo_arc (handle, xc, yc, radius, angle1, angle2);
403 public void ArcNegative (double xc, double yc, double radius, double angle1, double angle2)
405 NativeMethods.cairo_arc_negative (handle, xc, yc, radius, angle1, angle2);
408 public void Rectangle (Rectangle rectangle)
410 Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
413 public void Rectangle (PointD p, double width, double height)
415 Rectangle (p.X, p.Y, width, height);
418 public void Rectangle (double x, double y, double width, double height)
420 NativeMethods.cairo_rectangle (handle, x, y, width, height);
423 public void ClosePath ()
425 NativeMethods.cairo_close_path (handle);
428 public Path CopyPath ()
430 return new Path (NativeMethods.cairo_copy_path (handle));
433 public Path CopyPathFlat ()
435 return new Path (NativeMethods.cairo_copy_path_flat (handle));
438 public void AppendPath (Path path)
440 NativeMethods.cairo_append_path (handle, path.Handle);
445 #region Painting Methods
448 NativeMethods.cairo_paint (handle);
451 public void PaintWithAlpha (double alpha)
453 NativeMethods.cairo_paint_with_alpha (handle, alpha);
456 public void Mask (Pattern pattern)
458 NativeMethods.cairo_mask (handle, pattern.Handle);
461 public void MaskSurface (Surface surface, double surface_x, double surface_y)
463 NativeMethods.cairo_mask_surface (handle, surface.Handle, surface_x, surface_y);
466 public void Stroke ()
468 NativeMethods.cairo_stroke (handle);
471 public void StrokePreserve ()
473 NativeMethods.cairo_stroke_preserve (handle);
476 public Rectangle StrokeExtents ()
478 double x1, y1, x2, y2;
479 NativeMethods.cairo_stroke_extents (handle, out x1, out y1, out x2, out y2);
480 return new Rectangle (x1, y1, x2 - x1, y2 - y1);
485 NativeMethods.cairo_fill (handle);
488 public Rectangle FillExtents ()
490 double x1, y1, x2, y2;
491 NativeMethods.cairo_fill_extents (handle, out x1, out y1, out x2, out y2);
492 return new Rectangle (x1, y1, x2 - x1, y2 - y1);
495 public void FillPreserve ()
497 NativeMethods.cairo_fill_preserve (handle);
504 NativeMethods.cairo_clip (handle);
507 public void ClipPreserve ()
509 NativeMethods.cairo_clip_preserve (handle);
512 public void ResetClip ()
514 NativeMethods.cairo_reset_clip (handle);
517 public bool InStroke (double x, double y)
519 return NativeMethods.cairo_in_stroke (handle, x, y);
522 public bool InFill (double x, double y)
524 return NativeMethods.cairo_in_fill (handle, x, y);
527 public Pattern PopGroup ()
529 return Pattern.Lookup (NativeMethods.cairo_pop_group (handle), true);
532 public void PopGroupToSource ()
534 NativeMethods.cairo_pop_group_to_source (handle);
537 public void PushGroup ()
539 NativeMethods.cairo_push_group (handle);
542 public void PushGroup (Content content)
544 NativeMethods.cairo_push_group_with_content (handle, content);
547 public Surface GroupTarget {
549 IntPtr surface = NativeMethods.cairo_get_group_target (handle);
550 return Surface.Lookup (surface, false);
554 public void Rotate (double angle)
556 NativeMethods.cairo_rotate (handle, angle);
559 public void Scale (double sx, double sy)
561 NativeMethods.cairo_scale (handle, sx, sy);
564 public void Translate (double tx, double ty)
566 NativeMethods.cairo_translate (handle, tx, ty);
569 public void Transform (Matrix m)
571 NativeMethods.cairo_transform (handle, m);
574 [Obsolete("Use UserToDevice instead")]
575 public void TransformPoint (ref double x, ref double y)
577 NativeMethods.cairo_user_to_device (handle, ref x, ref y);
580 [Obsolete("Use UserToDeviceDistance instead")]
581 public void TransformDistance (ref double dx, ref double dy)
583 NativeMethods.cairo_user_to_device_distance (handle, ref dx, ref dy);
586 [Obsolete("Use InverseTransformPoint instead")]
587 public void InverseTransformPoint (ref double x, ref double y)
589 NativeMethods.cairo_device_to_user (handle, ref x, ref y);
592 [Obsolete("Use DeviceToUserDistance instead")]
593 public void InverseTransformDistance (ref double dx, ref double dy)
595 NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy);
598 public void UserToDevice (ref double x, ref double y)
600 NativeMethods.cairo_user_to_device (handle, ref x, ref y);
603 public void UserToDeviceDistance (ref double dx, ref double dy)
605 NativeMethods.cairo_user_to_device_distance (handle, ref dx, ref dy);
608 public void DeviceToUser (ref double x, ref double y)
610 NativeMethods.cairo_device_to_user (handle, ref x, ref y);
613 public void DeviceToUserDistance (ref double dx, ref double dy)
615 NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy);
618 public Matrix Matrix {
620 NativeMethods.cairo_set_matrix (handle, value);
624 Matrix m = new Matrix();
625 NativeMethods.cairo_get_matrix (handle, m);
630 public void SetFontSize (double scale)
632 NativeMethods.cairo_set_font_size (handle, scale);
635 public void IdentityMatrix ()
637 NativeMethods.cairo_identity_matrix (handle);
640 [Obsolete ("Use SetFontSize() instead.")]
641 public void FontSetSize (double scale)
646 [Obsolete ("Use SetFontSize() instead.")]
647 public double FontSize {
648 set { SetFontSize (value); }
651 public Matrix FontMatrix {
654 NativeMethods.cairo_get_font_matrix (handle, out m);
657 set { NativeMethods.cairo_set_font_matrix (handle, value); }
660 public FontOptions FontOptions {
662 FontOptions options = new FontOptions ();
663 NativeMethods.cairo_get_font_options (handle, options.Handle);
666 set { NativeMethods.cairo_set_font_options (handle, value.Handle); }
669 [StructLayout(LayoutKind.Sequential)]
670 internal struct NativeGlyph_4byte_longs {
675 public NativeGlyph_4byte_longs (Glyph source)
677 index = (int) source.index;
683 static internal IntPtr FromGlyphToUnManagedMemory(Glyph [] glyphs)
685 IntPtr dest = Marshal.AllocHGlobal (native_glyph_size * glyphs.Length);
686 long pos = dest.ToInt64();
688 if (c_compiler_long_size == 8){
689 foreach (Glyph g in glyphs){
690 Marshal.StructureToPtr (g, (IntPtr)pos, false);
691 pos += native_glyph_size;
694 foreach (Glyph g in glyphs){
695 NativeGlyph_4byte_longs n = new NativeGlyph_4byte_longs (g);
697 Marshal.StructureToPtr (n, (IntPtr)pos, false);
698 pos += native_glyph_size;
705 public void ShowGlyphs (Glyph[] glyphs)
709 ptr = FromGlyphToUnManagedMemory (glyphs);
711 NativeMethods.cairo_show_glyphs (handle, ptr, glyphs.Length);
713 Marshal.FreeHGlobal (ptr);
716 [Obsolete("The matrix argument was never used, use ShowGlyphs(Glyphs []) instead")]
717 public void ShowGlyphs (Matrix matrix, Glyph[] glyphs)
722 [Obsolete("The matrix argument was never used, use GlyphPath(Glyphs []) instead")]
723 public void GlyphPath (Matrix matrix, Glyph[] glyphs)
728 public void GlyphPath (Glyph[] glyphs)
732 ptr = FromGlyphToUnManagedMemory (glyphs);
734 NativeMethods.cairo_glyph_path (handle, ptr, glyphs.Length);
736 Marshal.FreeHGlobal (ptr);
740 public FontExtents FontExtents {
742 FontExtents f_extents;
743 NativeMethods.cairo_font_extents (handle, out f_extents);
748 public void CopyPage ()
750 NativeMethods.cairo_copy_page (handle);
753 [Obsolete ("Use SelectFontFace() instead.")]
754 public void FontFace (string family, FontSlant slant, FontWeight weight)
756 SelectFontFace (family, slant, weight);
759 public FontFace ContextFontFace {
761 return Cairo.FontFace.Lookup (NativeMethods.cairo_get_font_face (handle), false);
765 NativeMethods.cairo_set_font_face (handle, value == null ? IntPtr.Zero : value.Handle);
769 public void SelectFontFace (string family, FontSlant slant, FontWeight weight)
771 NativeMethods.cairo_select_font_face (handle, family, slant, weight);
774 public void ShowPage ()
776 NativeMethods.cairo_show_page (handle);
779 private static byte[] TerminateUtf8(byte[] utf8)
781 if (utf8.Length > 0 && utf8[utf8.Length - 1] == 0)
783 var termedArray = new byte[utf8.Length + 1];
784 Array.Copy(utf8, termedArray, utf8.Length);
785 termedArray[utf8.Length] = 0;
789 private static byte[] TerminateUtf8(string s)
791 // compute the byte count including the trailing \0
792 var byteCount = Encoding.UTF8.GetMaxByteCount(s.Length + 1);
793 var bytes = new byte[byteCount];
794 Encoding.UTF8.GetBytes(s, 0, s.Length, bytes, 0);
798 public void ShowText(string str)
800 NativeMethods.cairo_show_text (handle, TerminateUtf8(str));
803 public void ShowText(byte[] utf8)
805 NativeMethods.cairo_show_text (handle, TerminateUtf8(utf8));
808 public void TextPath(string str)
810 NativeMethods.cairo_text_path (handle, TerminateUtf8(str));
813 public void TextPath(byte[] utf8)
815 NativeMethods.cairo_text_path (handle, TerminateUtf8(utf8));
818 public TextExtents TextExtents(string s)
821 NativeMethods.cairo_text_extents (handle, TerminateUtf8(s), out extents);
825 public TextExtents TextExtents(byte[] utf8)
828 NativeMethods.cairo_text_extents (handle, TerminateUtf8(utf8), out extents);
832 public TextExtents GlyphExtents (Glyph[] glyphs)
834 IntPtr ptr = FromGlyphToUnManagedMemory (glyphs);
838 NativeMethods.cairo_glyph_extents (handle, ptr, glyphs.Length, out extents);
840 Marshal.FreeHGlobal (ptr);