Skip to content
This repository was archived by the owner on Jan 18, 2020. It is now read-only.

Added: ability to change fill type #68

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/SixLabors.Shapes/ClipperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ public static class ClipperExtensions
/// </summary>
/// <param name="shape">The shape.</param>
/// <param name="holes">The holes.</param>
/// <param name="fillType">The fillType.</param>
/// <returns>Returns a new shape with the holes cliped out out the shape.</returns>
public static IPath Clip(this IPath shape, IEnumerable<IPath> holes)
public static IPath Clip(this IPath shape, IEnumerable<IPath> holes, FillType fillType = FillType.EvenOdd)
{
var clipper = new Clipper();

clipper.AddPath(shape, ClippingType.Subject);
clipper.AddPaths(holes, ClippingType.Clip);

IPath[] result = clipper.GenerateClippedShapes();
IPath[] result = clipper.GenerateClippedShapes(fillType);

return new ComplexPolygon(result);
}
Expand Down
31 changes: 31 additions & 0 deletions src/SixLabors.Shapes/FillType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

namespace SixLabors.Shapes
{
/// <summary>
/// The fill type method used when filling a polygon.
/// </summary>
public enum FillType
{
/// <summary>
/// Even-Odd rule.
/// </summary>
EvenOdd = 0,

/// <summary>
/// Non-Zero rule.
/// </summary>
NonZero = 1,

/// <summary>
/// Positive rule.
/// </summary>
Positive = 2,

/// <summary>
/// Negative rule.
/// </summary>
Negative = 3,
}
}
21 changes: 19 additions & 2 deletions src/SixLabors.Shapes/PolygonClipper/Clipper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ public Clipper(params ClippablePath[] shapes)
/// <summary>
/// Executes the specified clip type.
/// </summary>
/// <param name="fillType">The fillType.</param>
/// <returns>
/// Returns the <see cref="IPath" /> array containing the converted polygons.
/// </returns>
public IPath[] GenerateClippedShapes()
public IPath[] GenerateClippedShapes(FillType fillType = FillType.EvenOdd)
{
var results = new List<PolyNode>();

lock (this.syncRoot)
{
this.innerClipper.Execute(ClipType.ctDifference, results);
this.innerClipper.Execute(ClipType.ctDifference, results, Convert(fillType));
}

var shapes = new IPath[results.Count];
Expand Down Expand Up @@ -153,5 +154,21 @@ internal void AddPath(ISimplePath path, ClippingType clippingType)
this.innerClipper.AddPath(points, type, path.IsClosed);
}
}

private static PolyFillType Convert(FillType type)
{
switch (type)
{
case FillType.NonZero:
return PolyFillType.pftNonZero;
case FillType.Positive:
return PolyFillType.pftPositive;
case FillType.Negative:
return PolyFillType.pftNegative;
case FillType.EvenOdd:
default:
return PolyFillType.pftEvenOdd;
}
}
}
}