Skip to content

Commit 3142030

Browse files
committed
Add bindings for Cairo.Region
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)
1 parent ce3bceb commit 3142030

3 files changed

Lines changed: 187 additions & 0 deletions

File tree

src/Libs/cairo-1.0/Internal/Region.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,67 @@ namespace Cairo.Internal;
55

66
public partial class Region
77
{
8+
// Not currently wrapped:
9+
// - cairo_region_create_rectangles (needs to be an array of RectangleIntData?)
10+
// - cairo_region_equal (if wrapped, need a hash function to go along with it)
11+
12+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_contains_point")]
13+
public static extern bool ContainsPoint(RegionHandle handle, int x, int y);
14+
15+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_contains_rectangle")]
16+
public static extern RegionOverlap ContainsRectangle(RegionHandle handle, RectangleIntHandle rectangle);
17+
18+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_copy")]
19+
public static extern RegionOwnedHandle Copy(RegionHandle original);
20+
21+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_create")]
22+
public static extern RegionOwnedHandle Create();
23+
24+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_create_rectangle")]
25+
public static extern RegionOwnedHandle CreateRectangle(RectangleIntHandle rectangle);
26+
827
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_destroy")]
928
public static extern void Destroy(IntPtr handle);
29+
30+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_get_extents")]
31+
public static extern void GetExtents(RegionHandle handle, RectangleIntHandle extents);
32+
33+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_get_rectangle")]
34+
public static extern void GetRectangle(RegionHandle handle, int nth, RectangleIntHandle rectangle);
35+
36+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_intersect")]
37+
public static extern Status Intersect(RegionHandle handle, RegionHandle other);
38+
39+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_intersect_rectangle")]
40+
public static extern Status IntersectRectangle(RegionHandle handle, RectangleIntHandle rectangle);
41+
42+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_is_empty")]
43+
public static extern bool IsEmpty(RegionHandle handle);
44+
45+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_num_rectangles")]
46+
public static extern int NumRectangles(RegionHandle handle);
47+
48+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_status")]
49+
public static extern Status Status(RegionHandle handle);
50+
51+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_subtract")]
52+
public static extern Status Subtract(RegionHandle handle, RegionHandle other);
53+
54+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_subtract_rectangle")]
55+
public static extern Status SubtractRectangle(RegionHandle handle, RectangleIntHandle rectangle);
56+
57+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_translate")]
58+
public static extern void Translate(RegionHandle handle, int x, int y);
59+
60+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_union")]
61+
public static extern Status Union(RegionHandle handle, RegionHandle other);
62+
63+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_union_rectangle")]
64+
public static extern Status UnionRectangle(RegionHandle handle, RectangleIntHandle rectangle);
65+
66+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_xor")]
67+
public static extern Status Xor(RegionHandle handle, RegionHandle other);
68+
69+
[DllImport(CairoImportResolver.Library, EntryPoint = "cairo_region_xor_rectangle")]
70+
public static extern Status XorRectangle(RegionHandle handle, RectangleIntHandle rectangle);
1071
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
3+
namespace Cairo;
4+
public partial class Region
5+
{
6+
public Region()
7+
: this(Internal.Region.Create())
8+
{
9+
}
10+
11+
public Region(RectangleInt rectangle)
12+
: this(Internal.Region.CreateRectangle(rectangle.Handle))
13+
{
14+
}
15+
16+
public Region Copy() => new Region(Internal.Region.Copy(Handle));
17+
18+
public Status Status => Internal.Region.Status(Handle);
19+
20+
public bool IsEmpty => Internal.Region.IsEmpty(Handle);
21+
public int NumRectangles => Internal.Region.NumRectangles(Handle);
22+
public void GetRectangle(int i, RectangleInt rectangle)
23+
=> Internal.Region.GetRectangle(Handle, i, rectangle.Handle);
24+
public void GetExtents(RectangleInt extents)
25+
=> Internal.Region.GetExtents(Handle, extents.Handle);
26+
27+
public bool ContainsPoint(int x, int y)
28+
=> Internal.Region.ContainsPoint(Handle, x, y);
29+
30+
public RegionOverlap ContainsRectangle(RectangleInt rectangle)
31+
=> Internal.Region.ContainsRectangle(Handle, rectangle.Handle);
32+
33+
public void Translate(int x, int y)
34+
=> Internal.Region.Translate(Handle, x, y);
35+
36+
public Status Intersect(Region other)
37+
=> Internal.Region.Intersect(Handle, other.Handle);
38+
39+
public Status IntersectRectangle(RectangleInt rectangle)
40+
=> Internal.Region.IntersectRectangle(Handle, rectangle.Handle);
41+
42+
public Status Subtract(Region other)
43+
=> Internal.Region.Subtract(Handle, other.Handle);
44+
45+
public Status SubtractRectangle(RectangleInt rectangle)
46+
=> Internal.Region.SubtractRectangle(Handle, rectangle.Handle);
47+
48+
public Status Union(Region other)
49+
=> Internal.Region.Union(Handle, other.Handle);
50+
51+
public Status UnionRectangle(RectangleInt rectangle)
52+
=> Internal.Region.UnionRectangle(Handle, rectangle.Handle);
53+
54+
public Status Xor(Region other)
55+
=> Internal.Region.Xor(Handle, other.Handle);
56+
57+
public Status XorRectangle(RectangleInt rectangle)
58+
=> Internal.Region.XorRectangle(Handle, rectangle.Handle);
59+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using FluentAssertions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace Cairo.Tests
5+
{
6+
[TestClass, TestCategory("IntegrationTest")]
7+
public class RegionTest : Test
8+
{
9+
[TestMethod]
10+
public void BindingsShouldSucceed()
11+
{
12+
// Default constructor.
13+
var empty_region = new Region();
14+
empty_region.Status.Should().Be(Status.Success);
15+
empty_region.IsEmpty.Should().Be(true);
16+
17+
// Rectangle constructor and accessors.
18+
var rect_init = new RectangleInt(Internal.RectangleIntManagedHandle.Create()) { X = 2, Y = 3, Height = 5, Width = 7 };
19+
var region = new Region(rect_init);
20+
region.Status.Should().Be(Status.Success);
21+
region.IsEmpty.Should().Be(false);
22+
region.NumRectangles.Should().Be(1);
23+
24+
var rect_copy = new RectangleInt(Internal.RectangleIntManagedHandle.Create());
25+
region.GetRectangle(0, rect_copy);
26+
rect_copy.X.Should().Be(2);
27+
rect_copy.Y.Should().Be(3);
28+
rect_copy.Height.Should().Be(5);
29+
rect_copy.Width.Should().Be(7);
30+
31+
rect_copy = new RectangleInt(Internal.RectangleIntManagedHandle.Create());
32+
region.GetExtents(rect_copy);
33+
rect_copy.X.Should().Be(2);
34+
rect_copy.Y.Should().Be(3);
35+
rect_copy.Height.Should().Be(5);
36+
rect_copy.Width.Should().Be(7);
37+
38+
region.Translate(1, 1);
39+
rect_copy = new RectangleInt(Internal.RectangleIntManagedHandle.Create());
40+
region.GetExtents(rect_copy);
41+
rect_copy.X.Should().Be(2);
42+
rect_copy.Y.Should().Be(3);
43+
rect_copy.Height.Should().Be(5);
44+
rect_copy.Width.Should().Be(7);
45+
46+
// Copying
47+
var region_copy = region.Copy();
48+
region_copy.Should().NotBeSameAs(rect_copy);
49+
region_copy.NumRectangles.Should().Be(1);
50+
51+
// Queries
52+
region.ContainsPoint(4, 4).Should().Be(true);
53+
region.ContainsPoint(1, 1).Should().Be(false);
54+
region.ContainsRectangle(rect_init).Should().Be(RegionOverlap.Part);
55+
56+
// Boolean operations
57+
region.Intersect(region_copy).Should().Be(Status.Success);
58+
region.Union(region_copy).Should().Be(Status.Success);
59+
region.Subtract(region_copy).Should().Be(Status.Success);
60+
region.Xor(region_copy).Should().Be(Status.Success);
61+
region.IntersectRectangle(rect_init).Should().Be(Status.Success);
62+
region.UnionRectangle(rect_init).Should().Be(Status.Success);
63+
region.SubtractRectangle(rect_init).Should().Be(Status.Success);
64+
region.XorRectangle(rect_init).Should().Be(Status.Success);
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)