Skip to content

Commit

Permalink
Add bindings for Cairo.Region
Browse files Browse the repository at this point in the history
This also adds some additional bindings for Cairo.RectangleInt in order to implement the unit tests (e.g. constructing a rectangle, comparing equality, and field accessors)
  • Loading branch information
cameronwhite committed Jan 15, 2024
1 parent ce3bceb commit b772dfb
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Libs/cairo-1.0/Internal/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,67 @@ namespace Cairo.Internal;

public partial class Region
{
// Not currently wrapped:
// - cairo_region_create_rectangles (needs to be an array of RectangleIntData?)
// - cairo_region_equal (if wrapped, need a hash function to go along with it)

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_contains_point")]
public static extern bool ContainsPoint(RegionHandle handle, int x, int y);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_contains_rectangle")]
public static extern RegionOverlap ContainsRectangle(RegionHandle handle, RectangleIntHandle rectangle);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_copy")]
public static extern RegionOwnedHandle Copy(RegionHandle original);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_create")]
public static extern RegionOwnedHandle Create();

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_create_rectangle")]
public static extern RegionOwnedHandle CreateRectangle(RectangleIntHandle rectangle);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_destroy")]
public static extern void Destroy(IntPtr handle);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_get_extents")]
public static extern void GetExtents(RegionHandle handle, RectangleIntHandle extents);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_get_rectangle")]
public static extern void GetRectangle(RegionHandle handle, int nth, RectangleIntHandle rectangle);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_intersect")]
public static extern Status Intersect(RegionHandle handle, RegionHandle other);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_intersect_rectangle")]
public static extern Status IntersectRectangle(RegionHandle handle, RectangleIntHandle rectangle);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_is_empty")]
public static extern bool IsEmpty(RegionHandle handle);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_num_rectangles")]
public static extern int NumRectangles(RegionHandle handle);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_status")]
public static extern Status Status(RegionHandle handle);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_subtract")]
public static extern Status Subtract(RegionHandle handle, RegionHandle other);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_subtract_rectangle")]
public static extern Status SubtractRectangle(RegionHandle handle, RectangleIntHandle rectangle);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_translate")]
public static extern void Translate(RegionHandle handle, int x, int y);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_union")]
public static extern Status Union(RegionHandle handle, RegionHandle other);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_union_rectangle")]
public static extern Status UnionRectangle(RegionHandle handle, RectangleIntHandle rectangle);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_xor")]
public static extern Status Xor(RegionHandle handle, RegionHandle other);

[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_xor_rectangle")]
public static extern Status XorRectangle(RegionHandle handle, RectangleIntHandle rectangle);
}
59 changes: 59 additions & 0 deletions src/Libs/cairo-1.0/Public/Region.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;

namespace Cairo;
public partial class Region
{
public Region()
: this(Internal.Region.Create())
{
}

public Region(RectangleInt rectangle)
: this(Internal.Region.CreateRectangle(rectangle.Handle))
{
}

public Region Copy() => new Region(Internal.Region.Copy(Handle));

public Status Status => Internal.Region.Status(Handle);

public bool IsEmpty => Internal.Region.IsEmpty(Handle);
public int NumRectangles => Internal.Region.NumRectangles(Handle);
public void GetRectangle(int i, RectangleInt rectangle)
=> Internal.Region.GetRectangle(Handle, i, rectangle.Handle);
public void GetExtents(RectangleInt extents)
=> Internal.Region.GetExtents(Handle, extents.Handle);

public bool ContainsPoint(int x, int y)
=> Internal.Region.ContainsPoint(Handle, x, y);

public RegionOverlap ContainsRectangle(RectangleInt rectangle)
=> Internal.Region.ContainsRectangle(Handle, rectangle.Handle);

public void Translate(int x, int y)
=> Internal.Region.Translate(Handle, x, y);

public Status Intersect(Region other)
=> Internal.Region.Intersect(Handle, other.Handle);

public Status IntersectRectangle(RectangleInt rectangle)
=> Internal.Region.IntersectRectangle(Handle, rectangle.Handle);

public Status Subtract(Region other)
=> Internal.Region.Subtract(Handle, other.Handle);

public Status SubtractRectangle(RectangleInt rectangle)
=> Internal.Region.SubtractRectangle(Handle, rectangle.Handle);

public Status Union(Region other)
=> Internal.Region.Union(Handle, other.Handle);

public Status UnionRectangle(RectangleInt rectangle)
=> Internal.Region.UnionRectangle(Handle, rectangle.Handle);

public Status Xor(Region other)
=> Internal.Region.Xor(Handle, other.Handle);

public Status XorRectangle(RectangleInt rectangle)
=> Internal.Region.XorRectangle(Handle, rectangle.Handle);
}
67 changes: 67 additions & 0 deletions src/Tests/Libs/Cairo-1.0.Tests/RegionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cairo.Tests
{
[TestClass, TestCategory("IntegrationTest")]
public class RegionTest : Test
{
[TestMethod]
public void BindingsShouldSucceed()
{
// Default constructor.
var empty_region = new Region();
empty_region.Status.Should().Be(Status.Success);
empty_region.IsEmpty.Should().Be(true);

// Rectangle constructor and accessors.
var rect_init = new RectangleInt(Internal.RectangleIntManagedHandle.Create()) { X = 2, Y = 3, Height = 5, Width = 7 };
var region = new Region(rect_init);
region.Status.Should().Be(Status.Success);
region.IsEmpty.Should().Be(false);
region.NumRectangles.Should().Be(1);

var rect_copy = new RectangleInt(Internal.RectangleIntManagedHandle.Create());
region.GetRectangle(0, rect_copy);
rect_copy.X.Should().Be(2);
rect_copy.Y.Should().Be(4);
rect_copy.Height.Should().Be(5);
rect_copy.Width.Should().Be(7);

rect_copy = new RectangleInt(Internal.RectangleIntManagedHandle.Create());
region.GetExtents(rect_copy);
rect_copy.X.Should().Be(2);
rect_copy.Y.Should().Be(4);
rect_copy.Height.Should().Be(5);
rect_copy.Width.Should().Be(7);

region.Translate(1, 1);
rect_copy = new RectangleInt(Internal.RectangleIntManagedHandle.Create());
region.GetExtents(rect_copy);
rect_copy.X.Should().Be(2);
rect_copy.Y.Should().Be(4);
rect_copy.Height.Should().Be(5);
rect_copy.Width.Should().Be(7);

// Copying
var region_copy = region.Copy();
region_copy.Should().NotBeSameAs(rect_copy);
region_copy.NumRectangles.Should().Be(1);

// Queries
region.ContainsPoint(4, 4).Should().Be(true);
region.ContainsPoint(1, 1).Should().Be(false);
region.ContainsRectangle(rect_init).Should().Be(RegionOverlap.Part);

// Boolean operations
region.Intersect(region_copy).Should().Be(Status.Success);
region.Union(region_copy).Should().Be(Status.Success);
region.Subtract(region_copy).Should().Be(Status.Success);
region.Xor(region_copy).Should().Be(Status.Success);
region.IntersectRectangle(rect_init).Should().Be(Status.Success);
region.UnionRectangle(rect_init).Should().Be(Status.Success);
region.SubtractRectangle(rect_init).Should().Be(Status.Success);
region.XorRectangle(rect_init).Should().Be(Status.Success);
}
}
}

0 comments on commit b772dfb

Please sign in to comment.