diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2021-11-24 03:00:25 +0100 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2021-11-24 03:00:25 +0100 |
| commit | f005d76468dc4c6a368b6f1f6eca69da25214e80 (patch) | |
| tree | 14a70c7fb9a58be9083dbd7492ecd94d030c18a9 /Backends/CairoBackend/src | |
| parent | 362d83b9556355b1d3dfb6cb892896ed33371521 (diff) | |
default Crow.CairoBackend ok
Diffstat (limited to 'Backends/CairoBackend/src')
22 files changed, 472 insertions, 623 deletions
diff --git a/Backends/CairoBackend/src/Context.cs b/Backends/CairoBackend/src/Context.cs index 965297be..a28c7b0c 100644 --- a/Backends/CairoBackend/src/Context.cs +++ b/Backends/CairoBackend/src/Context.cs @@ -139,52 +139,24 @@ namespace Crow.CairoBackend } } - public IntPtr Handle { - get { - return handle; - } - } + public IntPtr Handle => handle; public Operator Operator { - set { - NativeMethods.cairo_set_operator (handle, value); - } - - get { - return NativeMethods.cairo_get_operator (handle); - } + set => NativeMethods.cairo_set_operator (handle, value); + get => NativeMethods.cairo_get_operator (handle); } - public double Tolerance { - get { - return NativeMethods.cairo_get_tolerance (handle); - } - - set { - NativeMethods.cairo_set_tolerance (handle, value); - } + get => NativeMethods.cairo_get_tolerance (handle); + set => NativeMethods.cairo_set_tolerance (handle, value); } - public FillRule FillRule { - set { - NativeMethods.cairo_set_fill_rule (handle, value); - } - - get { - return NativeMethods.cairo_get_fill_rule (handle); - } + set => NativeMethods.cairo_set_fill_rule (handle, value); + get => NativeMethods.cairo_get_fill_rule (handle); } - public double LineWidth { - set { - NativeMethods.cairo_set_line_width (handle, value); - } - - get { - return NativeMethods.cairo_get_line_width (handle); - } + set => NativeMethods.cairo_set_line_width (handle, value); + get => NativeMethods.cairo_get_line_width (handle); } - public LineCap LineCap { set { NativeMethods.cairo_set_line_cap (handle, value); @@ -233,7 +205,7 @@ namespace Crow.CairoBackend public void SetSource (Pattern source) { - NativeMethods.cairo_set_source (handle, source.Handle); + NativeMethods.cairo_set_source (handle, source.handle); } public Pattern GetSource () @@ -265,63 +237,20 @@ namespace Crow.CairoBackend return NativeMethods.cairo_has_current_point (handle); } } - - [Obsolete ("Use GetTarget/SetTarget")] - public Surface Target { - set { - if (handle != IntPtr.Zero) - NativeMethods.cairo_destroy (handle); - - handle = NativeMethods.cairo_create (value.Handle); - } - - get { - return GetTarget (); - } - } - - public Surface GetTarget () - { - return Surface.Lookup (NativeMethods.cairo_get_target (handle), false); - } - + public Surface GetTarget () => Surface.Lookup (NativeMethods.cairo_get_target (handle), false); public void SetTarget (Surface target) { if (handle != IntPtr.Zero) NativeMethods.cairo_destroy (handle); handle = NativeMethods.cairo_create (target.Handle); } - - [Obsolete("Use GetScaledFont/SetScaledFont")] - public ScaledFont ScaledFont { - set { - SetScaledFont (value); - } - - get { - return GetScaledFont (); - } - } - public ScaledFont GetScaledFont () - { - return new ScaledFont (NativeMethods.cairo_get_scaled_font (handle), false); - } - + => new ScaledFont (NativeMethods.cairo_get_scaled_font (handle), false); public void SetScaledFont (ScaledFont font) - { - NativeMethods.cairo_set_scaled_font (handle, font.Handle); - } - - public uint ReferenceCount { - get { return NativeMethods.cairo_get_reference_count (handle); } - } - + => NativeMethods.cairo_set_scaled_font (handle, font.Handle); + public uint ReferenceCount => NativeMethods.cairo_get_reference_count (handle); public void SetSource (Color color) - { - NativeMethods.cairo_set_source_rgba (handle, color.R / 255.0, color.G / 255.0, color.B / 255.0, color.A / 255.0); - } - + => NativeMethods.cairo_set_source_rgba (handle, color.R / 255.0, color.G / 255.0, color.B / 255.0, color.A / 255.0); public void SetSource (double r, double g, double b) { NativeMethods.cairo_set_source_rgb (handle, r, g, b); @@ -348,168 +277,54 @@ namespace Crow.CairoBackend NativeMethods.cairo_set_source_surface (handle, source.Handle, 0, 0); } -#region Path methods - - public void NewPath () - { - NativeMethods.cairo_new_path (handle); - } - - public void NewSubPath () - { - NativeMethods.cairo_new_sub_path (handle); - } - - public void MoveTo (PointD p) - { - MoveTo (p.X, p.Y); - } - - public void MoveTo (double x, double y) - { - NativeMethods.cairo_move_to (handle, x, y); - } - - public void LineTo (PointD p) - { - LineTo (p.X, p.Y); - } - - public void LineTo (double x, double y) - { - NativeMethods.cairo_line_to (handle, x, y); - } - - public void CurveTo (PointD p1, PointD p2, PointD p3) - { - CurveTo (p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y); - } - + #region Path methods + public void NewPath () => NativeMethods.cairo_new_path (handle); + public void NewSubPath () => NativeMethods.cairo_new_sub_path (handle); + public void MoveTo (PointD p) => MoveTo (p.X, p.Y); + public void MoveTo (double x, double y) => NativeMethods.cairo_move_to (handle, x, y); + public void LineTo (PointD p) => LineTo (p.X, p.Y); + public void LineTo (double x, double y) => NativeMethods.cairo_line_to (handle, x, y); + public void CurveTo (PointD p1, PointD p2, PointD p3) => CurveTo (p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y); public void CurveTo (double x1, double y1, double x2, double y2, double x3, double y3) - { - NativeMethods.cairo_curve_to (handle, x1, y1, x2, y2, x3, y3); - } - - public void RelMoveTo (Distance d) - { - RelMoveTo (d.Dx, d.Dy); - } - - public void RelMoveTo (double dx, double dy) - { - NativeMethods.cairo_rel_move_to (handle, dx, dy); - } - - public void RelLineTo (Distance d) - { - RelLineTo (d.Dx, d.Dy); - } - - public void RelLineTo (double dx, double dy) - { - NativeMethods.cairo_rel_line_to (handle, dx, dy); - } - + => NativeMethods.cairo_curve_to (handle, x1, y1, x2, y2, x3, y3); + public void RelMoveTo (Distance d) => RelMoveTo (d.Dx, d.Dy); + public void RelMoveTo (double dx, double dy) => NativeMethods.cairo_rel_move_to (handle, dx, dy); + public void RelLineTo (Distance d) => RelLineTo (d.Dx, d.Dy); + public void RelLineTo (double dx, double dy) => NativeMethods.cairo_rel_line_to (handle, dx, dy); public void RelCurveTo (Distance d1, Distance d2, Distance d3) - { - RelCurveTo (d1.Dx, d1.Dy, d2.Dx, d2.Dy, d3.Dx, d3.Dy); - } - + => RelCurveTo (d1.Dx, d1.Dy, d2.Dx, d2.Dy, d3.Dx, d3.Dy); public void RelCurveTo (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3) - { - NativeMethods.cairo_rel_curve_to (handle, dx1, dy1, dx2, dy2, dx3, dy3); - } - + => NativeMethods.cairo_rel_curve_to (handle, dx1, dy1, dx2, dy2, dx3, dy3); public void Arc (double xc, double yc, double radius, double angle1, double angle2) - { - NativeMethods.cairo_arc (handle, xc, yc, radius, angle1, angle2); - } + => NativeMethods.cairo_arc (handle, xc, yc, radius, angle1, angle2); public void Arc (PointD center, double radius, double angle1, double angle2) - { - NativeMethods.cairo_arc (handle, center.X, center.Y, radius, angle1, angle2); - } - + => NativeMethods.cairo_arc (handle, center.X, center.Y, radius, angle1, angle2); public void ArcNegative (double xc, double yc, double radius, double angle1, double angle2) - { - NativeMethods.cairo_arc_negative (handle, xc, yc, radius, angle1, angle2); - } + => NativeMethods.cairo_arc_negative (handle, xc, yc, radius, angle1, angle2); public void ArcNegative (PointD center, double radius, double angle1, double angle2) - { - NativeMethods.cairo_arc_negative (handle, center.X, center.Y, radius, angle1, angle2); - } - + => NativeMethods.cairo_arc_negative (handle, center.X, center.Y, radius, angle1, angle2); public void Rectangle (Rectangle rectangle) - { - Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); - } + => Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); public void Rectangle (RectangleD rectangle) - { - Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); - } - + => Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); public void Rectangle (PointD p, double width, double height) - { - Rectangle (p.X, p.Y, width, height); - } - + => Rectangle (p.X, p.Y, width, height); public void Rectangle (double x, double y, double width, double height) - { - NativeMethods.cairo_rectangle (handle, x, y, width, height); - } - - public void ClosePath () - { - NativeMethods.cairo_close_path (handle); - } - - public Path CopyPath () - { - return new Path (NativeMethods.cairo_copy_path (handle)); - } - - public Path CopyPathFlat () - { - return new Path (NativeMethods.cairo_copy_path_flat (handle)); - } - - public void AppendPath (Path path) - { - NativeMethods.cairo_append_path (handle, path.Handle); - } - -#endregion - -#region Painting Methods - public void Paint () - { - NativeMethods.cairo_paint (handle); - } - - public void PaintWithAlpha (double alpha) - { - NativeMethods.cairo_paint_with_alpha (handle, alpha); - } - - public void Mask (Pattern pattern) - { - NativeMethods.cairo_mask (handle, pattern.Handle); - } + => NativeMethods.cairo_rectangle (handle, x, y, width, height); + public void ClosePath () => NativeMethods.cairo_close_path (handle); + public Path CopyPath () => new Path (NativeMethods.cairo_copy_path (handle)); + public Path CopyPathFlat () => new Path (NativeMethods.cairo_copy_path_flat (handle)); + public void AppendPath (Path path) => NativeMethods.cairo_append_path (handle, path.Handle); + #endregion + #region Painting Methods + public void Paint () => NativeMethods.cairo_paint (handle); + public void PaintWithAlpha (double alpha) => NativeMethods.cairo_paint_with_alpha (handle, alpha); + public void Mask (Pattern pattern) => NativeMethods.cairo_mask (handle, pattern.handle); public void MaskSurface (Surface surface, double surface_x, double surface_y) - { - NativeMethods.cairo_mask_surface (handle, surface.Handle, surface_x, surface_y); - } - - public void Stroke () - { - NativeMethods.cairo_stroke (handle); - } - - public void StrokePreserve () - { - NativeMethods.cairo_stroke_preserve (handle); - } - + => NativeMethods.cairo_mask_surface (handle, surface.Handle, surface_x, surface_y); + public void Stroke () => NativeMethods.cairo_stroke (handle); + public void StrokePreserve () => NativeMethods.cairo_stroke_preserve (handle); public Rectangle StrokeExtents () { double x1, y1, x2, y2; @@ -517,11 +332,8 @@ namespace Crow.CairoBackend return new Rectangle ((int)x1, (int)y1, (int)(x2 - x1), (int)(y2 - y1)); } - public void Fill () - { - NativeMethods.cairo_fill (handle); - } - + public void Fill () => NativeMethods.cairo_fill (handle); + public void FillPreserve () => NativeMethods.cairo_fill_preserve (handle); public Rectangle FillExtents () { double x1, y1, x2, y2; @@ -529,102 +341,36 @@ namespace Crow.CairoBackend return new Rectangle ((int)x1, (int)y1, (int)(x2 - x1), (int)(y2 - y1)); } - public void FillPreserve () - { - NativeMethods.cairo_fill_preserve (handle); - } - -#endregion - - public void Clip () - { - NativeMethods.cairo_clip (handle); - } - - public void ClipPreserve () - { - NativeMethods.cairo_clip_preserve (handle); - } - - public void ResetClip () - { - NativeMethods.cairo_reset_clip (handle); - } + #endregion - public bool InClip (double x, double y) - { - return NativeMethods.cairo_in_clip (handle, x, y); - } - public RectangleList GetClipRectangles (){ + public void Clip () => NativeMethods.cairo_clip (handle); + public void ClipPreserve () => NativeMethods.cairo_clip_preserve (handle); + public void ResetClip () => NativeMethods.cairo_reset_clip (handle); + public bool InClip (double x, double y) => NativeMethods.cairo_in_clip (handle, x, y); + /*public RectangleList GetClipRectangles (){ return (RectangleList)Marshal.PtrToStructure (NativeMethods.cairo_copy_clip_rectangle_list (handle), typeof(RectangleList)); - } + }*/ public void ClipExtendRectangle (){ double x1, y1, x2, y2; NativeMethods.cairo_clip_extents (handle, out x1, out y1, out x2, out y2); - NativeMethods.cairo_rectangle (handle, x1, y1, x2 - x1, y2 - y1); - } - public bool InStroke (double x, double y) - { - return NativeMethods.cairo_in_stroke (handle, x, y); - } - - public bool InFill (double x, double y) - { - return NativeMethods.cairo_in_fill (handle, x, y); - } - - public Pattern PopGroup () - { - return Pattern.Lookup (NativeMethods.cairo_pop_group (handle), true); - } - - public void PopGroupToSource () - { - NativeMethods.cairo_pop_group_to_source (handle); - } - - public void PushGroup () - { - NativeMethods.cairo_push_group (handle); + NativeMethods.cairo_rectangle (handle, x1, y1, x2 - x1, y2 - y1); } - - public void PushGroup (Content content) - { - NativeMethods.cairo_push_group_with_content (handle, content); - } - - [Obsolete ("Use GetGroupTarget()")] - public Surface GroupTarget { - get { - return GetGroupTarget (); - } - } - + public bool InStroke (double x, double y) => NativeMethods.cairo_in_stroke (handle, x, y); + public bool InFill (double x, double y) => NativeMethods.cairo_in_fill (handle, x, y); + public Pattern PopGroup () => Pattern.Lookup (NativeMethods.cairo_pop_group (handle), true); + public void PopGroupToSource () => NativeMethods.cairo_pop_group_to_source (handle); + public void PushGroup () => NativeMethods.cairo_push_group (handle); + public void PushGroup (Content content) => NativeMethods.cairo_push_group_with_content (handle, content); public Surface GetGroupTarget () { IntPtr surface = NativeMethods.cairo_get_group_target (handle); return Surface.Lookup (surface, false); } - public void Rotate (double angle) - { - NativeMethods.cairo_rotate (handle, angle); - } - - public void Scale (double sx, double sy) - { - NativeMethods.cairo_scale (handle, sx, sy); - } - - public void Translate (double tx, double ty) - { - NativeMethods.cairo_translate (handle, tx, ty); - } - - public void Transform (Matrix m) - { - NativeMethods.cairo_transform (handle, m); - } + public void Rotate (double angle) => NativeMethods.cairo_rotate (handle, angle); + public void Scale (double sx, double sy) => NativeMethods.cairo_scale (handle, sx, sy); + public void Translate (double tx, double ty) => NativeMethods.cairo_translate (handle, tx, ty); + public void Transform (Matrix m) => NativeMethods.cairo_transform (handle, m); [Obsolete("Use UserToDevice instead")] public void TransformPoint (ref double x, ref double y) @@ -670,28 +416,12 @@ namespace Crow.CairoBackend NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy); } - public Matrix Matrix { - set { - NativeMethods.cairo_set_matrix (handle, value); - } - - get { - Matrix m = new Matrix(); - NativeMethods.cairo_get_matrix (handle, m); - return m; - } - } - public void SetFontSize (double scale) { NativeMethods.cairo_set_font_size (handle, scale); } - public void IdentityMatrix () - { - NativeMethods.cairo_identity_matrix (handle); - } - + public void IdentityMatrix () => NativeMethods.cairo_identity_matrix (handle); public Matrix FontMatrix { get { Matrix m; @@ -853,18 +583,18 @@ namespace Crow.CairoBackend public void ShowText (ReadOnlySpan<char> s, int tabSize) { int size = s.Length * 4 + 1; Span<byte> bytes = size > 512 ? new byte[size] : stackalloc byte[size]; - int encodedBytes = Text.Encoding.ToUtf8 (s, bytes, tabSize); + int encodedBytes = s.ToUtf8 (bytes, tabSize); bytes[encodedBytes] = 0; ShowText (bytes.Slice (0, encodedBytes + 1)); } public TextExtents TextExtents (ReadOnlySpan<char> s, int tabSize) { - TextExtents (s, tabSize, out TextExtents extents); + TextExtents (s, tabSize, out TextExtents extents); return extents; } public void TextExtents (ReadOnlySpan<char> s, int tabSize, out TextExtents extents) { int size = s.Length * 4 + 1; Span<byte> bytes = size > 512 ? new byte[size] : stackalloc byte[size]; - int encodedBytes = Text.Encoding.ToUtf8 (s, bytes, tabSize); + int encodedBytes = s.ToUtf8 (bytes, tabSize); bytes[encodedBytes] = 0; TextExtents (bytes.Slice (0, encodedBytes + 1), out extents); } @@ -887,5 +617,106 @@ namespace Crow.CairoBackend return extents; } + + public void Flush() + { + throw new NotImplementedException(); + } + + public void Clear() + { + throw new NotImplementedException(); + } + public void MoveTo(Point p) + { + throw new NotImplementedException(); + } + public void LineTo(Point p) + { + throw new NotImplementedException(); + } + +/* public void Arc(float xc, float yc, float radius, float a1, float a2) + { + throw new NotImplementedException(); + } + public void ArcNegative(float xc, float yc, float radius, float a1, float a2) + { + throw new NotImplementedException(); + } + + public void MoveTo(float x, float y) + { + throw new NotImplementedException(); + } + + public void RelMoveTo(float x, float y) + { + throw new NotImplementedException(); + } + + public void LineTo(float x, float y) + { + throw new NotImplementedException(); + } + public void RelLineTo(float x, float y) + { + throw new NotImplementedException(); + } + + public void CurveTo(float x1, float y1, float x2, float y2, float x3, float y3) + { + throw new NotImplementedException(); + } + + public void RelCurveTo(float x1, float y1, float x2, float y2, float x3, float y3) + { + throw new NotImplementedException(); + } + + public void Scale(float sx, float sy) + { + throw new NotImplementedException(); + } + + public void Translate(float dx, float dy) + { + throw new NotImplementedException(); + } + public void Rotate(float alpha) + { + throw new NotImplementedException(); + } + public void SetSource(float r, float g, float b, float a = 1) + { + throw new NotImplementedException(); + } + + public void SetSource(ISurface surf, float x = 0, float y = 0) + { + throw new NotImplementedException(); + } + + public void SetSourceSurface(ISurface surf, float x = 0, float y = 0) + { + throw new NotImplementedException(); + } +*/ + public void Translate(PointD p) + { + throw new NotImplementedException(); + } + public void RenderSvg(IntPtr svgNativeHandle, string subId = null) + { + throw new NotImplementedException(); + } + Matrix savedMat; + public void SaveTransformations() + => NativeMethods.cairo_get_matrix (handle, out savedMat); + public void RestoreTransformations() + => NativeMethods.cairo_set_matrix (handle, ref savedMat); + public void SetSource(IPattern pat) => NativeMethods.cairo_set_source (handle, pat.Handle); + public void SetSource(ISurface surf, double x = 0, double y = 0) + => NativeMethods.cairo_set_source_surface (handle, surf.Handle, x, y); } } diff --git a/Backends/CairoBackend/src/DRMDevice.cs b/Backends/CairoBackend/src/DRMDevice.cs index 7bca9e6a..69009fc7 100644 --- a/Backends/CairoBackend/src/DRMDevice.cs +++ b/Backends/CairoBackend/src/DRMDevice.cs @@ -31,7 +31,7 @@ using System; namespace Crow.CairoBackend { - public class DRMDevice : Device + public class DRMDevice : CairoDevice { public DRMDevice () : base (NativeMethods.cairo_drm_device_default (), true) { @@ -44,10 +44,10 @@ namespace Crow.CairoBackend } public int FileDescriptor { - get { return NativeMethods.cairo_drm_device_get_fd (Handle); } + get { return NativeMethods.cairo_drm_device_get_fd (handle); } } - public void DeviceThrottle () { NativeMethods.cairo_drm_device_throttle (Handle);} + public void DeviceThrottle () { NativeMethods.cairo_drm_device_throttle (handle);} } } diff --git a/Backends/CairoBackend/src/Device.cs b/Backends/CairoBackend/src/Device.cs index 5191a23d..4cbb180f 100644 --- a/Backends/CairoBackend/src/Device.cs +++ b/Backends/CairoBackend/src/Device.cs @@ -28,33 +28,17 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; +using System.IO; +using System.Runtime.InteropServices; using Drawing2D; using Glfw; namespace Crow.CairoBackend { - public class Device : IDevice - { - IntPtr handle = IntPtr.Zero; - /// <summary> Global font rendering settings for Cairo </summary> - FontOptions FontRenderingOptions; - /// <summary> Global font rendering settings for Cairo </summary> - Antialias Antialias = Antialias.Subpixel; - - protected Device() - { - FontRenderingOptions = new FontOptions (); - FontRenderingOptions.Antialias = Antialias.Subpixel; - FontRenderingOptions.HintMetrics = HintMetrics.On; - FontRenderingOptions.HintStyle = HintStyle.Full; - FontRenderingOptions.SubpixelOrder = SubpixelOrder.Default; - } - - protected Device (IntPtr ptr) : this (ptr, true) - { - } - - protected Device (IntPtr handle, bool owner) + public abstract class CairoDevice : Device { + protected IntPtr handle = IntPtr.Zero; + public IntPtr Handle => handle; + protected CairoDevice (IntPtr handle, bool owner = true) { this.handle = handle; if (!owner) @@ -62,25 +46,11 @@ namespace Crow.CairoBackend if (CairoDebug.Enabled) CairoDebug.OnAllocated (handle); } - - ~Device () - { - Dispose (false); - } - - public IntPtr Handle { - get { - return handle; - } - } public string Status { get { return System.Runtime.InteropServices.Marshal.PtrToStringAuto(NativeMethods.cairo_status_to_string (NativeMethods.cairo_device_status (handle))); } } - public void SetThreadAware (bool value){ - NativeMethods.cairo_gl_device_set_thread_aware (handle, value ? 1 : 0); - } public Status Acquire() { return NativeMethods.cairo_device_acquire (handle); @@ -90,14 +60,10 @@ namespace Crow.CairoBackend NativeMethods.cairo_device_release (handle); } - public void Dispose () + protected override void Dispose (bool disposing) { - Dispose (true); - GC.SuppressFinalize (this); - } + base.Dispose (disposing); - protected virtual void Dispose (bool disposing) - { if (!disposing || CairoDebug.Enabled) CairoDebug.OnDisposed<Device> (handle, disposing); @@ -106,24 +72,46 @@ namespace Crow.CairoBackend NativeMethods.cairo_device_destroy (handle); - FontRenderingOptions.Dispose (); handle = IntPtr.Zero; } + } + public class Device : IDevice + { + /// <summary> Global font rendering settings for Cairo </summary> + FontOptions FontRenderingOptions; + /// <summary> Global font rendering settings for Cairo </summary> + Antialias Antialias = Antialias.Subpixel; - public void GetDpy(out int hdpy, out int vdpy) + public Device() { - throw new NotImplementedException(); + FontRenderingOptions = new FontOptions (); + FontRenderingOptions.Antialias = Antialias.Subpixel; + FontRenderingOptions.HintMetrics = HintMetrics.On; + FontRenderingOptions.HintStyle = HintStyle.Full; + FontRenderingOptions.SubpixelOrder = SubpixelOrder.Default; } - public void SetDpy(int hdpy, int vdpy) + ~Device () + { + Dispose (false); + } + + #region IDevice implementation + public void GetDpy(out int hdpy, out int vdpy) { throw new NotImplementedException(); } - public virtual ISurface CreateSurface(int width, int height) + public void SetDpy(int hdpy, int vdpy) { throw new NotImplementedException(); } + public IRegion CreateRegion () => new Region (); + public virtual ISurface CreateSurface(int width, int height) + => new ImageSurface (Format.ARGB32, width, height); + public virtual ISurface CreateSurface(byte[] data, int width, int height) + => new ImageSurface (data, Format.ARGB32, width, height, 4 * width); + public ISurface CreateSurface (IntPtr nativeWindoPointer, int width, int height) { switch (Environment.OSVersion.Platform) { case PlatformID.Unix: @@ -147,8 +135,73 @@ namespace Crow.CairoBackend Context gr = new Context (surf); gr.FontOptions = FontRenderingOptions; gr.Antialias = Antialias; - throw new NotImplementedException(); + return gr; + } + public byte[] LoadBitmap (Stream stream, out Size dimensions) { + byte[] image; +#if STB_SHARP + StbImageSharp.ImageResult stbi = StbImageSharp.ImageResult.FromStream (stream, StbImageSharp.ColorComponents.RedGreenBlueAlpha); + image = new byte[stbi.Data.Length]; + //rgba to argb for cairo. + for (int i = 0; i < stbi.Data.Length; i += 4) { + image[i] = stbi.Data[i + 2]; + image[i + 1] = stbi.Data[i + 1]; + image[i + 2] = stbi.Data[i]; + image[i + 3] = stbi.Data[i + 3]; + } + dimensions = new Size (stbi.Width, stbi.Height); +#else + using (StbImage stbi = new StbImage (stream)) { + image = new byte [stbi.Size]; + for (int i = 0; i < stbi.Size; i+=4) { + //rgba to argb for cairo. ???? looks like bgra to me. + image [i] = Marshal.ReadByte (stbi.Handle, i + 2); + image [i + 1] = Marshal.ReadByte (stbi.Handle, i + 1); + image [i + 2] = Marshal.ReadByte (stbi.Handle, i); + image [i + 3] = Marshal.ReadByte (stbi.Handle, i + 3); + } + dimensions = new Size (stbi.Width, stbi.Height); + } +#endif + return image; + } + public ISvgHandle LoadSvg(Stream stream) + { + using (BinaryReader sr = new BinaryReader (stream)) + return new SvgHandle (sr.ReadBytes ((int)stream.Length)); + } + + public ISvgHandle LoadSvg(string svgFragment) => + new SvgHandle (System.Text.Encoding.Unicode.GetBytes (svgFragment)); + + public IGradient CreateGradient (GradientType gradientType, Rectangle bounds) { + switch (gradientType) { + case GradientType.Vertical: + return new LinearGradient (bounds.Left, bounds.Top, bounds.Left, bounds.Bottom); + case GradientType.Horizontal: + return new LinearGradient (bounds.Left, bounds.Top, bounds.Right, bounds.Top); + case GradientType.Oblic: + return new LinearGradient (bounds.Left, bounds.Top, bounds.Right, bounds.Bottom); + case GradientType.Radial: + throw new NotImplementedException (); + } + return null; + } + #endregion + + #region IDispose implementation + public void Dispose () + { + Dispose (true); + GC.SuppressFinalize (this); + } + + protected virtual void Dispose (bool disposing) + { + if (disposing) + FontRenderingOptions.Dispose (); } + #endregion } } diff --git a/Backends/CairoBackend/src/EGLDevice.cs b/Backends/CairoBackend/src/EGLDevice.cs index 1f73ef4f..5305298b 100644 --- a/Backends/CairoBackend/src/EGLDevice.cs +++ b/Backends/CairoBackend/src/EGLDevice.cs @@ -31,7 +31,7 @@ using System; namespace Crow.CairoBackend { - public class EGLDevice : Device + public class EGLDevice : GLDevice { public EGLDevice (IntPtr dpy, IntPtr gl_ctx) : base (NativeMethods.cairo_egl_device_create (dpy, gl_ctx), true) { diff --git a/Backends/CairoBackend/src/GLDevice.cs b/Backends/CairoBackend/src/GLDevice.cs new file mode 100644 index 00000000..c7992b74 --- /dev/null +++ b/Backends/CairoBackend/src/GLDevice.cs @@ -0,0 +1,17 @@ +// Copyright (c) 2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +using System; + +namespace Crow.CairoBackend +{ + public abstract class GLDevice : CairoDevice + { + protected GLDevice (IntPtr handle, bool owner = true) : base (handle, owner) {} + public void SetThreadAware (bool value) { + NativeMethods.cairo_gl_device_set_thread_aware (handle, value ? 1 : 0); + } + + } +} + diff --git a/Backends/CairoBackend/src/GLSurface.cs b/Backends/CairoBackend/src/GLSurface.cs index 117bd60f..ddbf0a4f 100644 --- a/Backends/CairoBackend/src/GLSurface.cs +++ b/Backends/CairoBackend/src/GLSurface.cs @@ -37,7 +37,7 @@ namespace Crow.CairoBackend { public GLSurface (IntPtr ptr, bool own) : base (ptr, own) {} - public GLSurface (Device device, Content content, uint tex, int width, int height) + public GLSurface (CairoDevice device, Content content, uint tex, int width, int height) : base (NativeMethods.cairo_gl_surface_create_for_texture (device.Handle, (uint)content, tex, width, height), true) {} diff --git a/Backends/CairoBackend/src/GLXDevice.cs b/Backends/CairoBackend/src/GLXDevice.cs index 8c080df5..4a788c99 100644 --- a/Backends/CairoBackend/src/GLXDevice.cs +++ b/Backends/CairoBackend/src/GLXDevice.cs @@ -31,18 +31,18 @@ using System; namespace Crow.CairoBackend { - public class GLXDevice : Device + public class GLXDevice : CairoDevice { public GLXDevice (IntPtr dpy, IntPtr gl_ctx) : base (NativeMethods.cairo_glx_device_create (dpy, gl_ctx), true) { } public IntPtr Display { - get { return NativeMethods.cairo_glx_device_get_display (Handle); } + get { return NativeMethods.cairo_glx_device_get_display (handle); } } public IntPtr Context { - get { return NativeMethods.cairo_glx_device_get_context (Handle); } + get { return NativeMethods.cairo_glx_device_get_context (handle); } } } } diff --git a/Backends/CairoBackend/src/Gradient.cs b/Backends/CairoBackend/src/Gradient.cs index 3dd0d9d2..74ac7d48 100644 --- a/Backends/CairoBackend/src/Gradient.cs +++ b/Backends/CairoBackend/src/Gradient.cs @@ -28,11 +28,11 @@ // using System; -using Color = Drawing2D.Color; +using Drawing2D; namespace Crow.CairoBackend { - public class Gradient : Pattern + public class Gradient : Pattern, IGradient { protected Gradient (IntPtr handle, bool owned) : base (handle, owned) { @@ -41,20 +41,20 @@ namespace Crow.CairoBackend { public int ColorStopCount { get { int cnt; - NativeMethods.cairo_pattern_get_color_stop_count (Handle, out cnt); + NativeMethods.cairo_pattern_get_color_stop_count (handle, out cnt); return cnt; } } - - public Status AddColorStop (double offset, Color c) - { - NativeMethods.cairo_pattern_add_color_stop_rgba (Handle, offset, c.R / 255.0, c.G / 255.0, c.B / 255.0, c.A / 255.0); - return Status; - } + #region IGradient implementation + public void AddColorStop (double offset, Color c) + => NativeMethods.cairo_pattern_add_color_stop_rgba (handle, offset, c.R / 255.0, c.G / 255.0, c.B / 255.0, c.A / 255.0); + public void AddColorStop(float offset, float r, float g, float b, float a = 1) + => NativeMethods.cairo_pattern_add_color_stop_rgba (handle, offset, r, g, b, a); + #endregion public Status AddColorStopRgb (double offset, Color c) { - NativeMethods.cairo_pattern_add_color_stop_rgb (Handle, offset, c.R / 255.0, c.G / 255.0 / 255.0, c.B / 255.0); + NativeMethods.cairo_pattern_add_color_stop_rgb (handle, offset, c.R / 255.0, c.G / 255.0 / 255.0, c.B / 255.0); return Status; } } diff --git a/Backends/CairoBackend/src/LinearGradient.cs b/Backends/CairoBackend/src/LinearGradient.cs index 32b83db2..49e423ed 100644 --- a/Backends/CairoBackend/src/LinearGradient.cs +++ b/Backends/CairoBackend/src/LinearGradient.cs @@ -48,7 +48,7 @@ namespace Crow.CairoBackend { double x0, y0, x1, y1; PointD[] points = new PointD [2]; - NativeMethods.cairo_pattern_get_linear_points (Handle, out x0, out y0, out x1, out y1); + NativeMethods.cairo_pattern_get_linear_points (handle, out x0, out y0, out x1, out y1); points[0] = new PointD (x0, y0); points[1] = new PointD (x1, y1); diff --git a/Backends/CairoBackend/src/Matrix.cs b/Backends/CairoBackend/src/Matrix.cs index b1925688..b565edcc 100644 --- a/Backends/CairoBackend/src/Matrix.cs +++ b/Backends/CairoBackend/src/Matrix.cs @@ -37,7 +37,7 @@ using System.Runtime.InteropServices; namespace Crow.CairoBackend { [StructLayout(LayoutKind.Sequential)] - public class Matrix //: ICloneable + public struct Matrix //: ICloneable { public double Xx; public double Yx; @@ -52,12 +52,6 @@ namespace Crow.CairoBackend { this.Xx = xx; this.Yx = yx; this.Xy = xy; this.Yy = yy; this.X0 = x0; this.Y0 = y0; } - - public Matrix () - { - this.InitIdentity (); - } - public bool IsIdentity () { return (this == new Matrix ()); diff --git a/Backends/CairoBackend/src/MeshPattern.cs b/Backends/CairoBackend/src/MeshPattern.cs index f2576705..39fdd2af 100644 --- a/Backends/CairoBackend/src/MeshPattern.cs +++ b/Backends/CairoBackend/src/MeshPattern.cs @@ -45,64 +45,64 @@ namespace Crow.CairoBackend { //no idea why this is here, the base one is identical, but we can't remove it now public new Extend Extend { - set { NativeMethods.cairo_pattern_set_extend (Handle, value); } - get { return NativeMethods.cairo_pattern_get_extend (Handle); } + set { NativeMethods.cairo_pattern_set_extend (handle, value); } + get { return NativeMethods.cairo_pattern_get_extend (handle); } } public Filter Filter { - set { NativeMethods.cairo_pattern_set_filter (Handle, value); } - get { return NativeMethods.cairo_pattern_get_filter (Handle); } + set { NativeMethods.cairo_pattern_set_filter (handle, value); } + get { return NativeMethods.cairo_pattern_get_filter (handle); } } public void BeginPatch(){ - NativeMethods.cairo_mesh_pattern_begin_patch (Handle); + NativeMethods.cairo_mesh_pattern_begin_patch (handle); } public void EndPatch(){ - NativeMethods.cairo_mesh_pattern_end_patch (Handle); + NativeMethods.cairo_mesh_pattern_end_patch (handle); } public void MoveTo(double x, double y){ - NativeMethods.cairo_mesh_pattern_move_to (Handle, x, y); + NativeMethods.cairo_mesh_pattern_move_to (handle, x, y); } public void MoveTo (PointD p) { - NativeMethods.cairo_mesh_pattern_move_to (Handle, p.X, p.Y); + NativeMethods.cairo_mesh_pattern_move_to (handle, p.X, p.Y); } public void LineTo(double x, double y){ - NativeMethods.cairo_mesh_pattern_line_to (Handle, x, y); + NativeMethods.cairo_mesh_pattern_line_to (handle, x, y); } public void LineTo (PointD p) { - NativeMethods.cairo_mesh_pattern_line_to (Handle, p.X, p.Y); + NativeMethods.cairo_mesh_pattern_line_to (handle, p.X, p.Y); } public void CurveTo(double x1, double y1, double x2, double y2, double x3, double y3) { - NativeMethods.cairo_mesh_pattern_curve_to (Handle, x1, y1, x2, y2, x3, y3); + NativeMethods.cairo_mesh_pattern_curve_to (handle, x1, y1, x2, y2, x3, y3); } public void SetControlPoint(uint point_num, double x, double y){ - NativeMethods.cairo_mesh_pattern_set_control_point (Handle, point_num, x, y); + NativeMethods.cairo_mesh_pattern_set_control_point (handle, point_num, x, y); } public void SetControlPoint (uint point_num, PointD p) { - NativeMethods.cairo_mesh_pattern_set_control_point (Handle, point_num, p.X, p.Y); + NativeMethods.cairo_mesh_pattern_set_control_point (handle, point_num, p.X, p.Y); } public void SetCornerColorRGB(uint corner_num, double r, double g, double b){ - NativeMethods.cairo_mesh_pattern_set_corner_color_rgb (Handle, corner_num, r, g, b); + NativeMethods.cairo_mesh_pattern_set_corner_color_rgb (handle, corner_num, r, g, b); } public void SetCornerColorRGBA(uint corner_num, double r, double g, double b, double a){ - NativeMethods.cairo_mesh_pattern_set_corner_color_rgba (Handle, corner_num, r, g, b, a); + NativeMethods.cairo_mesh_pattern_set_corner_color_rgba (handle, corner_num, r, g, b, a); } public void SetCornerColor (uint corner_num, Color c) { - NativeMethods.cairo_mesh_pattern_set_corner_color_rgba (Handle, corner_num, c.R, c.G, c.B, c.A); + NativeMethods.cairo_mesh_pattern_set_corner_color_rgba (handle, corner_num, c.R, c.G, c.B, c.A); } public uint PatchCount { get { uint count = 0; - NativeMethods.cairo_mesh_pattern_get_patch_count(Handle, out count); + NativeMethods.cairo_mesh_pattern_get_patch_count(handle, out count); return count; } } public Path GetPath(uint patch_num){ - return new Path(NativeMethods.cairo_mesh_pattern_get_path(Handle, patch_num)); + return new Path(NativeMethods.cairo_mesh_pattern_get_path(handle, patch_num)); } public PointD GetControlPoint(uint point_num, uint patch_num = 0) { - NativeMethods.cairo_mesh_pattern_get_control_point (Handle, patch_num, point_num, out double x, out double y); + NativeMethods.cairo_mesh_pattern_get_control_point (handle, patch_num, point_num, out double x, out double y); return new PointD (x, y); } public void GetCornerColorRGBA(){ diff --git a/Backends/CairoBackend/src/NativeMethods.cs b/Backends/CairoBackend/src/NativeMethods.cs index 27f61154..7b6053ac 100644 --- a/Backends/CairoBackend/src/NativeMethods.cs +++ b/Backends/CairoBackend/src/NativeMethods.cs @@ -233,7 +233,7 @@ namespace Crow.CairoBackend internal static extern double cairo_get_line_width (IntPtr cr); [DllImport (cairo, CallingConvention=CallingConvention.Cdecl)] - internal static extern void cairo_get_matrix (IntPtr cr, Matrix matrix); + internal static extern void cairo_get_matrix (IntPtr cr, out Matrix matrix); [DllImport (cairo, CallingConvention=CallingConvention.Cdecl)] internal static extern double cairo_get_miter_limit (IntPtr cr); @@ -723,7 +723,7 @@ namespace Crow.CairoBackend internal static extern void cairo_set_line_width (IntPtr cr, double width); [DllImport (cairo, CallingConvention=CallingConvention.Cdecl)] - internal static extern void cairo_set_matrix (IntPtr cr, Matrix matrix); + internal static extern void cairo_set_matrix (IntPtr cr, ref Matrix matrix); [DllImport (cairo, CallingConvention=CallingConvention.Cdecl)] internal static extern void cairo_set_miter_limit (IntPtr cr, double limit); diff --git a/Backends/CairoBackend/src/Pattern.cs b/Backends/CairoBackend/src/Pattern.cs index 545dda36..137f67fe 100644 --- a/Backends/CairoBackend/src/Pattern.cs +++ b/Backends/CairoBackend/src/Pattern.cs @@ -14,10 +14,10 @@ // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: -// +// // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -28,21 +28,19 @@ // using System; -using System.Collections; using Drawing2D; -namespace Crow.CairoBackend { - - public class Pattern : IDisposable +namespace Crow.CairoBackend +{ + public class Pattern : IPattern { - [Obsolete] - protected IntPtr pattern = IntPtr.Zero; - + internal IntPtr handle; + public IntPtr Handle => handle; public static Pattern Lookup (IntPtr pattern, bool owner) { if (pattern == IntPtr.Zero) return null; - + PatternType pt = NativeMethods.cairo_pattern_get_type (pattern); switch (pt) { case PatternType.Solid: @@ -58,98 +56,73 @@ namespace Crow.CairoBackend { } } - [Obsolete] - protected Pattern () - { - } - internal Pattern (IntPtr handle, bool owned) { - Handle = handle; + this.handle = handle; if (!owned) NativeMethods.cairo_pattern_reference (handle); if (CairoDebug.Enabled) CairoDebug.OnAllocated (handle); } - ~Pattern () - { - Dispose (false); - } - [Obsolete ("Use the SurfacePattern constructor")] public Pattern (Surface surface) : this ( NativeMethods.cairo_pattern_create_for_surface (surface.Handle), true) { } - + [Obsolete] protected void Reference () { - NativeMethods.cairo_pattern_reference (pattern); - } - - public void Dispose () - { - Dispose (true); - GC.SuppressFinalize (this); - } - - protected virtual void Dispose (bool disposing) - { - if (!disposing || CairoDebug.Enabled) - CairoDebug.OnDisposed<Pattern> (Handle, disposing); - - if (!disposing|| Handle == IntPtr.Zero) - return; - - NativeMethods.cairo_pattern_destroy (Handle); - Handle = IntPtr.Zero; + NativeMethods.cairo_pattern_reference (handle); } - [Obsolete ("Use Dispose()")] - public void Destroy () - { - Dispose (); - } - public Status Status - { - get { return NativeMethods.cairo_pattern_status (Handle); } - } - public Extend Extend - { - get { return NativeMethods.cairo_pattern_get_extend (Handle); } - set { NativeMethods.cairo_pattern_set_extend (Handle, value); } - } + public Status Status => NativeMethods.cairo_pattern_status (handle); public Matrix Matrix { - set { - NativeMethods.cairo_pattern_set_matrix (Handle, value); - } - + set => NativeMethods.cairo_pattern_set_matrix (handle, value); get { Matrix m = new Matrix (); - NativeMethods.cairo_pattern_get_matrix (Handle, m); + NativeMethods.cairo_pattern_get_matrix (handle, m); return m; } } -#pragma warning disable 612 - public IntPtr Handle { - get { return pattern; } - private set { pattern = value; } + public PatternType PatternType => NativeMethods.cairo_pattern_get_type (handle); + + #region IPattern implementation + public Extend Extend + { + get { return NativeMethods.cairo_pattern_get_extend (handle); } + set { NativeMethods.cairo_pattern_set_extend (handle, value); } + } + public Filter Filter { + get => NativeMethods.cairo_pattern_get_filter (handle); + set => NativeMethods.cairo_pattern_set_filter (handle, value); } -#pragma warning restore 612 + #endregion - [Obsolete] - public IntPtr Pointer { - get { return pattern; } + ~Pattern () + { + Dispose (false); } + public void Dispose () + { + Dispose (true); + GC.SuppressFinalize (this); + } + protected virtual void Dispose (bool disposing) + { + if (!disposing || CairoDebug.Enabled) + CairoDebug.OnDisposed<Pattern> (handle, disposing); + + if (!disposing|| handle == IntPtr.Zero) + return; - public PatternType PatternType { - get { return NativeMethods.cairo_pattern_get_type (Handle); } + NativeMethods.cairo_pattern_destroy (handle); + handle = IntPtr.Zero; } } } diff --git a/Backends/CairoBackend/src/Region.cs b/Backends/CairoBackend/src/Region.cs index 5466b121..139ebc8e 100644 --- a/Backends/CairoBackend/src/Region.cs +++ b/Backends/CairoBackend/src/Region.cs @@ -21,34 +21,25 @@ // using System; +using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using Drawing2D; namespace Crow.CairoBackend { [StructLayout(LayoutKind.Sequential)] - public struct RectangleList { + struct RectangleList { public Status Status; public IntPtr Rectangles; public int NumRectangles; } - public enum RegionOverlap { - In, - Out, - Part, - } - public class Region : IDisposable { + public class Region : IRegion { IntPtr handle; - public IntPtr Handle { - get { return handle; } - } - - [Obsolete] - public Region (IntPtr handle) : this (handle, false) {} + #region CTOR public Region (IntPtr handle, bool owned) { this.handle = handle; @@ -57,148 +48,127 @@ namespace Crow.CairoBackend if (CairoDebug.Enabled) CairoDebug.OnAllocated (handle); } - public Region () : this (NativeMethods.cairo_region_create () , true) - { - } - - public Region (Rectangle rect) - { + {} + public Region (Rectangle rect) { handle = NativeMethods.cairo_region_create_rectangle (ref rect); } - - public Region (RectangleList rects) - { - handle = NativeMethods.cairo_region_create_rectangles (rects.Rectangles, rects.NumRectangles); - } - - public Region Copy () - { - return new Region (NativeMethods.cairo_region_copy (Handle), true); - } - - #region IDisposable - ~Region () - { - Dispose (false); - } - - public void Dispose () - { - Dispose (true); - GC.SuppressFinalize (this); - } - - protected virtual void Dispose (bool disposing) - { - if (!disposing || CairoDebug.Enabled) - CairoDebug.OnDisposed<Region> (handle, disposing); - - if (!disposing|| handle == IntPtr.Zero) - return; - - NativeMethods.cairo_region_destroy (Handle); - handle = IntPtr.Zero; - } #endregion - public override bool Equals (object obj) - { - return (obj is Region) && NativeMethods.cairo_region_equal (Handle, (obj as Region).Handle); - } + public Region Copy () => new Region (NativeMethods.cairo_region_copy (handle), true); - public override int GetHashCode () - { - return Handle.GetHashCode (); - } public Status Status { - get { return NativeMethods.cairo_region_status (Handle); } + get { return NativeMethods.cairo_region_status (handle); } } public Rectangle Extents { get { Rectangle result; - NativeMethods.cairo_region_get_extents (Handle, out result); + NativeMethods.cairo_region_get_extents (handle, out result); return result; } } - public int NumRectangles { - get { return NativeMethods.cairo_region_num_rectangles (Handle); } - } - - public Rectangle GetRectangle (int nth) - { - Rectangle val; - NativeMethods.cairo_region_get_rectangle (Handle, nth, out val); - return val; - } - - public bool IsEmpty { - get { return NativeMethods.cairo_region_is_empty (Handle); } - } - - public RegionOverlap Contains (Rectangle rectangle) - { - return NativeMethods.cairo_region_contains_rectangle (Handle, ref rectangle); - } - public bool Contains (int x, int y) { - return NativeMethods.cairo_region_contains_point (Handle, x, y); + return NativeMethods.cairo_region_contains_point (handle, x, y); } public void Translate (int dx, int dy) { - NativeMethods.cairo_region_translate (Handle, dx, dy); + NativeMethods.cairo_region_translate (handle, dx, dy); } public Status Subtract (Region other) { - return NativeMethods.cairo_region_subtract (Handle, other.Handle); + return NativeMethods.cairo_region_subtract (handle, other.handle); } public Status SubtractRectangle (Rectangle rectangle) { - return NativeMethods.cairo_region_subtract_rectangle (Handle, ref rectangle); + return NativeMethods.cairo_region_subtract_rectangle (handle, ref rectangle); } public Status Intersect (Region other) { - return NativeMethods.cairo_region_intersect (Handle, other.Handle); + return NativeMethods.cairo_region_intersect (handle, other.handle); } public Status IntersectRectangle (Rectangle rectangle) { - return NativeMethods.cairo_region_intersect_rectangle (Handle, ref rectangle); + return NativeMethods.cairo_region_intersect_rectangle (handle, ref rectangle); } public Status Union (Region other) { - return NativeMethods.cairo_region_union (Handle, other.Handle); + return NativeMethods.cairo_region_union (handle, other.handle); } - public Status UnionRectangle (Rectangle rectangle) - { - return NativeMethods.cairo_region_union_rectangle (Handle, ref rectangle); - } public Status Xor (Region other) { - return NativeMethods.cairo_region_xor (Handle, other.Handle); + return NativeMethods.cairo_region_xor (handle, other.handle); } public Status XorRectangle (Rectangle rectangle) { - return NativeMethods.cairo_region_xor_rectangle (Handle, ref rectangle); + return NativeMethods.cairo_region_xor_rectangle (handle, ref rectangle); + } + + #region IRegion implementation + public bool IsEmpty => NativeMethods.cairo_region_is_empty (handle); + public int NumRectangles => NativeMethods.cairo_region_num_rectangles (handle); + public Rectangle GetRectangle (int nth) + { + Rectangle val; + NativeMethods.cairo_region_get_rectangle (handle, nth, out val); + return val; } + public void UnionRectangle (Rectangle rectangle) + => NativeMethods.cairo_region_union_rectangle (handle, ref rectangle); + public bool OverlapOut (Rectangle rectangle) => Contains (rectangle) == RegionOverlap.Out; + public RegionOverlap Contains (Rectangle rectangle) + => NativeMethods.cairo_region_contains_rectangle (handle, ref rectangle); public void Reset () { if (IsEmpty) return; - NativeMethods.cairo_region_destroy (Handle); + NativeMethods.cairo_region_destroy (handle); handle = NativeMethods.cairo_region_create (); } - public bool OverlapOut (Rectangle rectangle) => Contains (rectangle) == RegionOverlap.Out; + + public bool Equals(IRegion other) + => other is Region r ? NativeMethods.cairo_region_equal (handle, r.handle) : false; + #endregion + + public override bool Equals (object obj) + => obj is Region r ? NativeMethods.cairo_region_equal (handle, r.handle) : false; + + public override int GetHashCode () => handle.GetHashCode (); + + #region IDisposable + ~Region () + { + Dispose (false); + } + + public void Dispose () + { + Dispose (true); + GC.SuppressFinalize (this); + } + + protected virtual void Dispose (bool disposing) + { + if (!disposing || CairoDebug.Enabled) + CairoDebug.OnDisposed<Region> (handle, disposing); + + if (!disposing|| handle == IntPtr.Zero) + return; + + NativeMethods.cairo_region_destroy (handle); + handle = IntPtr.Zero; + } + #endregion } } diff --git a/Backends/CairoBackend/src/SolidPattern.cs b/Backends/CairoBackend/src/SolidPattern.cs index 8d52fb01..65e86a01 100644 --- a/Backends/CairoBackend/src/SolidPattern.cs +++ b/Backends/CairoBackend/src/SolidPattern.cs @@ -28,7 +28,8 @@ // using System; -using Color = Drawing2D.Color; +using Drawing2D; + namespace Crow.CairoBackend { public class SolidPattern : Pattern @@ -63,7 +64,7 @@ namespace Crow.CairoBackend { public Color Color { get { double red, green, blue, alpha; - NativeMethods.cairo_pattern_get_rgba (Handle, out red, out green, out blue, out alpha); + NativeMethods.cairo_pattern_get_rgba (handle, out red, out green, out blue, out alpha); return new Color (red, green, blue, alpha); } } diff --git a/Backends/CairoBackend/src/Surface.cs b/Backends/CairoBackend/src/Surface.cs index b41af1a4..2d6fac10 100644 --- a/Backends/CairoBackend/src/Surface.cs +++ b/Backends/CairoBackend/src/Surface.cs @@ -34,14 +34,13 @@ // using System; -using System.Collections; using Drawing2D; namespace Crow.CairoBackend { public class Surface : ISurface { - IntPtr handle = IntPtr.Zero; + protected IntPtr handle = IntPtr.Zero; [Obsolete] protected Surface() @@ -144,7 +143,7 @@ namespace Crow.CairoBackend { NativeMethods.cairo_surface_destroy (handle); handle = IntPtr.Zero; } - public virtual void SetSize (int width, int height) { + public virtual void Resize (int width, int height) { } public Status Finish () diff --git a/Backends/CairoBackend/src/SurfacePattern.cs b/Backends/CairoBackend/src/SurfacePattern.cs index 7b06265f..83b98a8c 100644 --- a/Backends/CairoBackend/src/SurfacePattern.cs +++ b/Backends/CairoBackend/src/SurfacePattern.cs @@ -45,13 +45,13 @@ namespace Crow.CairoBackend { //no idea why this is here, the base one is identical, but we can't remove it now public new Extend Extend { - set { NativeMethods.cairo_pattern_set_extend (Handle, value); } - get { return NativeMethods.cairo_pattern_get_extend (Handle); } + set { NativeMethods.cairo_pattern_set_extend (handle, value); } + get { return NativeMethods.cairo_pattern_get_extend (handle); } } public Filter Filter { - set { NativeMethods.cairo_pattern_set_filter (Handle, value); } - get { return NativeMethods.cairo_pattern_get_filter (Handle); } + set { NativeMethods.cairo_pattern_set_filter (handle, value); } + get { return NativeMethods.cairo_pattern_get_filter (handle); } } } } diff --git a/Backends/CairoBackend/src/WGLDevice.cs b/Backends/CairoBackend/src/WGLDevice.cs index 18179948..4d7773e5 100644 --- a/Backends/CairoBackend/src/WGLDevice.cs +++ b/Backends/CairoBackend/src/WGLDevice.cs @@ -31,14 +31,14 @@ using System; namespace Crow.CairoBackend { - public class WGLDevice : Device + public class WGLDevice : GLDevice { public WGLDevice (IntPtr hglrc) : base (NativeMethods.cairo_wgl_device_create (hglrc), true) { } public IntPtr Context { - get { return NativeMethods.cairo_wgl_device_get_context (Handle); } + get { return NativeMethods.cairo_wgl_device_get_context (handle); } } } } diff --git a/Backends/CairoBackend/src/Win32Surface.cs b/Backends/CairoBackend/src/Win32Surface.cs index f620673e..a10a9cf2 100644 --- a/Backends/CairoBackend/src/Win32Surface.cs +++ b/Backends/CairoBackend/src/Win32Surface.cs @@ -32,6 +32,7 @@ namespace Crow.CairoBackend { public class Win32Surface : Surface { + IntPtr hdc; internal Win32Surface (IntPtr handle, bool owns) : base (handle, owns) { } @@ -39,6 +40,16 @@ namespace Crow.CairoBackend { public Win32Surface (IntPtr hdc) : base (NativeMethods.cairo_win32_surface_create (hdc), true) { + this.hdc = hdc; + } + public override void Resize(int width, int height) + { + if (hdc == IntPtr.Zero) + base.Resize (width, height); + else { + NativeMethods.cairo_surface_destroy (handle); + handle = NativeMethods.cairo_win32_surface_create (hdc); + } } } } diff --git a/Backends/CairoBackend/src/XcbSurface.cs b/Backends/CairoBackend/src/XcbSurface.cs index affc46f8..bb198a71 100644 --- a/Backends/CairoBackend/src/XcbSurface.cs +++ b/Backends/CairoBackend/src/XcbSurface.cs @@ -45,7 +45,7 @@ namespace Crow.CairoBackend { IntPtr ptr = NativeMethods.cairo_xcb_surface_create_for_bitmap (connection, bitmap, screen, width, height); return new XcbSurface (ptr, true); } - public override void SetSize (int width, int height) + public override void Resize (int width, int height) { NativeMethods.cairo_xcb_surface_set_size (Handle, width, height); } diff --git a/Backends/CairoBackend/src/XlibSurface.cs b/Backends/CairoBackend/src/XlibSurface.cs index 602400e5..df4b2619 100644 --- a/Backends/CairoBackend/src/XlibSurface.cs +++ b/Backends/CairoBackend/src/XlibSurface.cs @@ -60,7 +60,7 @@ namespace Crow.CairoBackend { NativeMethods.cairo_xlib_surface_set_drawable (Handle, drawable, width, height); } - public override void SetSize (int width, int height) + public override void Resize (int width, int height) { NativeMethods.cairo_xlib_surface_set_size (Handle, width, height); } diff --git a/Backends/CairoBackend/src/rsvg/SvgHandle.cs b/Backends/CairoBackend/src/rsvg/SvgHandle.cs index 1622ecfa..6db657fd 100644 --- a/Backends/CairoBackend/src/rsvg/SvgHandle.cs +++ b/Backends/CairoBackend/src/rsvg/SvgHandle.cs @@ -6,7 +6,7 @@ using Drawing2D; namespace Crow.CairoBackend { - public sealed class SvgHandle : IDisposable { + public sealed class SvgHandle : ISvgHandle { const string lib = "rsvg-2.40"; public IntPtr Raw; @@ -58,10 +58,10 @@ namespace Crow.CairoBackend { public void SetDpiXY (double dpi_x, double dpi_y) => rsvg_handle_set_dpi_x_y (Raw, dpi_x, dpi_y); - public void Render(Context cr) => + public void Render(IContext cr) => rsvg_handle_render_cairo (Raw, cr == null ? IntPtr.Zero : cr.Handle); - public void Render (Context cr, string id) => + public void Render (IContext cr, string id) => rsvg_handle_render_cairo_sub (Raw, cr == null ? IntPtr.Zero : cr.Handle, id); [StructLayout(LayoutKind.Sequential)] |