Skip to content

Commit 7677cfa

Browse files
Merge pull request #265 from SixLabors/js/clipper-convert
Inline Clipper
2 parents 1d451a2 + 208660b commit 7677cfa

File tree

80 files changed

+5138
-7561
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+5138
-7561
lines changed

src/ImageSharp.Drawing/Processing/Processors/Text/RichTextGlyphRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ private Buffer2D<float> Render(IPath path)
509509
0,
510510
size.Height,
511511
subpixelCount,
512-
IntersectionRule.Nonzero,
512+
IntersectionRule.NonZero,
513513
this.memoryAllocator);
514514

515515
try

src/ImageSharp.Drawing/Processing/ShapeOptions.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,25 @@ public ShapeOptions()
1515
{
1616
}
1717

18-
private ShapeOptions(ShapeOptions source) => this.IntersectionRule = source.IntersectionRule;
18+
private ShapeOptions(ShapeOptions source)
19+
{
20+
this.IntersectionRule = source.IntersectionRule;
21+
this.ClippingOperation = source.ClippingOperation;
22+
}
23+
24+
/// <summary>
25+
/// Gets or sets the clipping operation.
26+
/// <para/>
27+
/// Defaults to <see cref="ClippingOperation.Difference"/>.
28+
/// </summary>
29+
public ClippingOperation ClippingOperation { get; set; } = ClippingOperation.Difference;
1930

2031
/// <summary>
21-
/// Gets or sets a value indicating whether antialiasing should be applied.
32+
/// Gets or sets the rule for calculating intersection points.
2233
/// <para/>
23-
/// Defaults to <see cref="IntersectionRule.OddEven"/>.
34+
/// Defaults to <see cref="IntersectionRule.EvenOdd"/>.
2435
/// </summary>
25-
public IntersectionRule IntersectionRule { get; set; } = IntersectionRule.OddEven;
36+
public IntersectionRule IntersectionRule { get; set; } = IntersectionRule.EvenOdd;
2637

2738
/// <inheritdoc/>
2839
public ShapeOptions DeepClone() => new(this);

src/ImageSharp.Drawing/Shapes/ClipPathExtensions.cs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,71 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System.Collections.Generic;
5-
using SixLabors.ImageSharp.Drawing.PolygonClipper;
5+
using SixLabors.ImageSharp.Drawing.Processing;
6+
using SixLabors.ImageSharp.Drawing.Shapes.PolygonClipper;
67

78
namespace SixLabors.ImageSharp.Drawing
89
{
910
/// <summary>
10-
/// Path extensions to clip paths.
11+
/// Provides extension methods to <see cref="IPath"/> that allow the clipping of shapes.
1112
/// </summary>
1213
public static class ClipPathExtensions
1314
{
1415
/// <summary>
15-
/// Clips the specified holes.
16+
/// Clips the specified subject path with the provided clipping paths.
1617
/// </summary>
17-
/// <param name="shape">The shape.</param>
18-
/// <param name="holes">The holes.</param>
19-
/// <returns>Returns a new shape with the holes clipped out of the shape.</returns>
20-
/// <exception cref="ClipperException">Open paths have been disabled.</exception>
21-
public static IPath Clip(this IPath shape, IEnumerable<IPath> holes)
18+
/// <param name="subjectPath">The subject path.</param>
19+
/// <param name="clipPaths">The clipping paths.</param>
20+
/// <returns>The clipped <see cref="IPath"/>.</returns>
21+
/// <exception cref="ClipperException">Thrown when an error occured while attempting to clip the polygon.</exception>
22+
public static IPath Clip(this IPath subjectPath, params IPath[] clipPaths)
23+
=> subjectPath.Clip((IEnumerable<IPath>)clipPaths);
24+
25+
/// <summary>
26+
/// Clips the specified subject path with the provided clipping paths.
27+
/// </summary>
28+
/// <param name="subjectPath">The subject path.</param>
29+
/// <param name="options">The shape options.</param>
30+
/// <param name="clipPaths">The clipping paths.</param>
31+
/// <returns>The clipped <see cref="IPath"/>.</returns>
32+
/// <exception cref="ClipperException">Thrown when an error occured while attempting to clip the polygon.</exception>
33+
public static IPath Clip(
34+
this IPath subjectPath,
35+
ShapeOptions options,
36+
params IPath[] clipPaths)
37+
=> subjectPath.Clip(options, (IEnumerable<IPath>)clipPaths);
38+
39+
/// <summary>
40+
/// Clips the specified subject path with the provided clipping paths.
41+
/// </summary>
42+
/// <param name="subjectPath">The subject path.</param>
43+
/// <param name="clipPaths">The clipping paths.</param>
44+
/// <returns>The clipped <see cref="IPath"/>.</returns>
45+
/// <exception cref="ClipperException">Thrown when an error occured while attempting to clip the polygon.</exception>
46+
public static IPath Clip(this IPath subjectPath, IEnumerable<IPath> clipPaths)
47+
=> subjectPath.Clip(new(), clipPaths);
48+
49+
/// <summary>
50+
/// Clips the specified subject path with the provided clipping paths.
51+
/// </summary>
52+
/// <param name="subjectPath">The subject path.</param>
53+
/// <param name="options">The shape options.</param>
54+
/// <param name="clipPaths">The clipping paths.</param>
55+
/// <returns>The clipped <see cref="IPath"/>.</returns>
56+
/// <exception cref="ClipperException">Thrown when an error occured while attempting to clip the polygon.</exception>
57+
public static IPath Clip(
58+
this IPath subjectPath,
59+
ShapeOptions options,
60+
IEnumerable<IPath> clipPaths)
2261
{
23-
var clipper = new Clipper();
62+
Clipper clipper = new();
2463

25-
clipper.AddPath(shape, ClippingType.Subject);
26-
clipper.AddPaths(holes, ClippingType.Clip);
64+
clipper.AddPath(subjectPath, ClippingType.Subject);
65+
clipper.AddPaths(clipPaths, ClippingType.Clip);
2766

28-
IPath[] result = clipper.GenerateClippedShapes();
67+
IPath[] result = clipper.GenerateClippedShapes(options.ClippingOperation, options.IntersectionRule);
2968

3069
return new ComplexPolygon(result);
3170
}
32-
33-
/// <summary>
34-
/// Clips the specified holes.
35-
/// </summary>
36-
/// <param name="shape">The shape.</param>
37-
/// <param name="holes">The holes.</param>
38-
/// <returns>Returns a new shape with the holes clipped out of the shape.</returns>
39-
/// <exception cref="ClipperException">Open paths have been disabled.</exception>
40-
public static IPath Clip(this IPath shape, params IPath[] holes)
41-
=> shape.Clip((IEnumerable<IPath>)holes);
4271
}
4372
}

0 commit comments

Comments
 (0)