Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ImageSharp.Drawing/ImageSharp.Drawing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<ItemGroup>
<PackageReference Include="SixLabors.Fonts" Version="3.0.0-alpha.0.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="4.0.0-alpha.0.44" />
<PackageReference Include="SixLabors.PolygonClipper" Version="1.0.0-alpha.0.48" />
</ItemGroup>
<Import Project="..\..\shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems" Label="Shared" />
</Project>
6 changes: 4 additions & 2 deletions src/ImageSharp.Drawing/Processing/ShapeOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using SixLabors.PolygonClipper;

namespace SixLabors.ImageSharp.Drawing.Processing;

/// <summary>
Expand All @@ -24,9 +26,9 @@ private ShapeOptions(ShapeOptions source)
/// <summary>
/// Gets or sets the clipping operation.
/// <para/>
/// Defaults to <see cref="ClippingOperation.Difference"/>.
/// Defaults to <see cref="BooleanOperation.Difference"/>.
/// </summary>
public ClippingOperation ClippingOperation { get; set; } = ClippingOperation.Difference;
public BooleanOperation ClippingOperation { get; set; } = BooleanOperation.Difference;

/// <summary>
/// Gets or sets the rule for calculating intersection points.
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp.Drawing/Shapes/ClipPathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static IPath Clip(
clipper.AddPath(subjectPath, ClippingType.Subject);
clipper.AddPaths(clipPaths, ClippingType.Clip);

IPath[] result = clipper.GenerateClippedShapes(options.ClippingOperation, options.IntersectionRule);
IPath[] result = clipper.GenerateClippedShapes(options.ClippingOperation);

return new ComplexPolygon(result);
}
Expand Down
38 changes: 0 additions & 38 deletions src/ImageSharp.Drawing/Shapes/ClippingOperation.cs

This file was deleted.

68 changes: 34 additions & 34 deletions src/ImageSharp.Drawing/Shapes/PolygonClipper/Clipper.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,46 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using SixLabors.PolygonClipper;

namespace SixLabors.ImageSharp.Drawing.Shapes.PolygonClipper;

/// <summary>
/// Library to clip polygons.
/// </summary>
internal class Clipper
{
private readonly PolygonClipper polygonClipper;

/// <summary>
/// Initializes a new instance of the <see cref="Clipper"/> class.
/// </summary>
public Clipper()
=> this.polygonClipper = new PolygonClipper();
private SixLabors.PolygonClipper.Polygon? subject;
private SixLabors.PolygonClipper.Polygon? clip;

/// <summary>
/// Generates the clipped shapes from the previously provided paths.
/// </summary>
/// <param name="operation">The clipping operation.</param>
/// <param name="rule">The intersection rule.</param>

Check failure on line 20 in src/ImageSharp.Drawing/Shapes/PolygonClipper/Clipper.cs

View workflow job for this annotation

GitHub Actions / Build (false, macos-13, net9.0, 9.0.x, true, -x64, false)

Check failure on line 20 in src/ImageSharp.Drawing/Shapes/PolygonClipper/Clipper.cs

View workflow job for this annotation

GitHub Actions / Build (false, macos-13, net9.0, 9.0.x, true, -x64, false)

XML comment has a param tag for 'rule', but there is no parameter by that name

Check failure on line 20 in src/ImageSharp.Drawing/Shapes/PolygonClipper/Clipper.cs

View workflow job for this annotation

GitHub Actions / Build (false, macos-13, net9.0, 9.0.x, true, -x64, false)

Check failure on line 20 in src/ImageSharp.Drawing/Shapes/PolygonClipper/Clipper.cs

View workflow job for this annotation

GitHub Actions / Build (false, macos-13, net9.0, 9.0.x, true, -x64, false)

XML comment has a param tag for 'rule', but there is no parameter by that name
/// <returns>The <see cref="T:IPath[]"/>.</returns>
public IPath[] GenerateClippedShapes(ClippingOperation operation, IntersectionRule rule)
public IPath[] GenerateClippedShapes(BooleanOperation operation)
{
PathsF closedPaths = [];
PathsF openPaths = [];
ArgumentNullException.ThrowIfNull(this.subject);
ArgumentNullException.ThrowIfNull(this.clip);

FillRule fillRule = rule == IntersectionRule.EvenOdd ? FillRule.EvenOdd : FillRule.NonZero;
this.polygonClipper.Execute(operation, fillRule, closedPaths, openPaths);
SixLabors.PolygonClipper.PolygonClipper polygonClipper = new(this.subject, this.clip, operation);

IPath[] shapes = new IPath[closedPaths.Count + openPaths.Count];
SixLabors.PolygonClipper.Polygon result = polygonClipper.Run();

Check failure on line 30 in src/ImageSharp.Drawing/Shapes/PolygonClipper/Clipper.cs

View workflow job for this annotation

GitHub Actions / Build (false, macos-13, net9.0, 9.0.x, true, -x64, false)

Check failure on line 30 in src/ImageSharp.Drawing/Shapes/PolygonClipper/Clipper.cs

View workflow job for this annotation

GitHub Actions / Build (false, macos-13, net9.0, 9.0.x, true, -x64, false)

int index = 0;
for (int i = 0; i < closedPaths.Count; i++)
{
PathF path = closedPaths[i];
PointF[] points = new PointF[path.Count];

for (int j = 0; j < path.Count; j++)
{
points[j] = path[j];
}
IPath[] shapes = new IPath[result.Count];

shapes[index++] = new Polygon(points);
}

for (int i = 0; i < openPaths.Count; i++)
int index = 0;
for (int i = 0; i < result.Count; i++)
{
PathF path = openPaths[i];
PointF[] points = new PointF[path.Count];
Contour contour = result[i];
PointF[] points = new PointF[contour.Count];

for (int j = 0; j < path.Count; j++)
for (int j = 0; j < contour.Count; j++)
{
points[j] = path[j];
Vertex vertex = contour[j];
points[j] = new PointF((float)vertex.X, (float)vertex.Y);
}

shapes[index++] = new Polygon(points);
Expand Down Expand Up @@ -100,12 +87,25 @@
internal void AddPath(ISimplePath path, ClippingType clippingType)
{
ReadOnlySpan<PointF> vectors = path.Points.Span;
PathF points = new(vectors.Length);
for (int i = 0; i < vectors.Length; i++)
SixLabors.PolygonClipper.Polygon polygon = [];
Contour contour = new();
polygon.Add(contour);

foreach (PointF point in vectors)
{
points.Add(vectors[i]);
contour.AddVertex(new Vertex(point.X, point.Y));
}

this.polygonClipper.AddPath(points, clippingType, !path.IsClosed);
switch (clippingType)
{
case ClippingType.Clip:
this.clip = polygon;
break;
case ClippingType.Subject:
this.subject = polygon;
break;
default:
throw new ArgumentOutOfRangeException(nameof(clippingType), clippingType, null);
}
}
}
Loading
Loading