Skip to content

Commit 53ebfd1

Browse files
committed
Translate ClipperLib internal exception
1 parent 6072005 commit 53ebfd1

6 files changed

Lines changed: 77 additions & 18 deletions

File tree

src/ImageSharp.Drawing/Shapes/ClipPathExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static class ClipPathExtensions
1717
/// <param name="shape">The shape.</param>
1818
/// <param name="holes">The holes.</param>
1919
/// <returns>Returns a new shape with the holes clipped out of the shape.</returns>
20+
/// <exception cref="ClipperException">GenerateClippedShapes: Open paths have been disabled.</exception>
2021
public static IPath Clip(this IPath shape, IEnumerable<IPath> holes)
2122
{
2223
var clipper = new Clipper();
@@ -35,6 +36,7 @@ public static IPath Clip(this IPath shape, IEnumerable<IPath> holes)
3536
/// <param name="shape">The shape.</param>
3637
/// <param name="holes">The holes.</param>
3738
/// <returns>Returns a new shape with the holes clipped out of the shape.</returns>
39+
/// <exception cref="ClipperException">GenerateClippedShapes: Open paths have been disabled.</exception>
3840
public static IPath Clip(this IPath shape, params IPath[] holes)
3941
=> shape.Clip((IEnumerable<IPath>)holes);
4042
}

src/ImageSharp.Drawing/Shapes/OutlinePathExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Numerics;
7-
using SixLabors.ImageSharp.Drawing.Shapes.PolygonClipper;
7+
using SixLabors.ImageSharp.Drawing.PolygonClipper;
88

99
namespace SixLabors.ImageSharp.Drawing
1010
{
@@ -22,6 +22,7 @@ public static class OutlinePathExtensions
2222
/// <param name="width">The final width outline</param>
2323
/// <param name="pattern">The pattern made of multiples of the width.</param>
2424
/// <returns>A new path representing the outline.</returns>
25+
/// <exception cref="ClipperException">Execute: Couldn't caculate Offset</exception>
2526
public static IPath GenerateOutline(this IPath path, float width, float[] pattern)
2627
=> path.GenerateOutline(width, new ReadOnlySpan<float>(pattern));
2728

@@ -32,6 +33,7 @@ public static IPath GenerateOutline(this IPath path, float width, float[] patter
3233
/// <param name="width">The final width outline</param>
3334
/// <param name="pattern">The pattern made of multiples of the width.</param>
3435
/// <returns>A new path representing the outline.</returns>
36+
/// <exception cref="ClipperException">Execute: Couldn't caculate Offset</exception>
3537
public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<float> pattern)
3638
=> path.GenerateOutline(width, pattern, false);
3739

@@ -43,6 +45,7 @@ public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<f
4345
/// <param name="pattern">The pattern made of multiples of the width.</param>
4446
/// <param name="startOff">Weather the first item in the pattern is on or off.</param>
4547
/// <returns>A new path representing the outline.</returns>
48+
/// <exception cref="ClipperException">Execute: Couldn't caculate Offset</exception>
4649
public static IPath GenerateOutline(this IPath path, float width, float[] pattern, bool startOff)
4750
=> path.GenerateOutline(width, new ReadOnlySpan<float>(pattern), startOff);
4851

@@ -54,6 +57,7 @@ public static IPath GenerateOutline(this IPath path, float width, float[] patter
5457
/// <param name="pattern">The pattern made of multiples of the width.</param>
5558
/// <param name="startOff">Weather the first item in the pattern is on or off.</param>
5659
/// <returns>A new path representing the outline.</returns>
60+
/// <exception cref="ClipperException">Execute: Couldn't caculate Offset</exception>
5761
public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<float> pattern, bool startOff)
5862
=> GenerateOutline(path, width, pattern, startOff, JointStyle.Square, EndCapStyle.Butt);
5963

@@ -67,6 +71,7 @@ public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<f
6771
/// <param name="jointStyle">The style to render the joints.</param>
6872
/// <param name="patternSectionCapStyle">The style to render between sections of the specified pattern.</param>
6973
/// <returns>A new path representing the outline.</returns>
74+
/// <exception cref="ClipperException">Execute: Couldn't caculate Offset</exception>
7075
public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<float> pattern, bool startOff, JointStyle jointStyle = JointStyle.Square, EndCapStyle patternSectionCapStyle = EndCapStyle.Butt)
7176
{
7277
if (pattern.Length < 2)
@@ -167,6 +172,7 @@ public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<f
167172
/// <param name="path">the path to outline</param>
168173
/// <param name="width">The final width outline</param>
169174
/// <returns>A new path representing the outline.</returns>
175+
/// <exception cref="ClipperException">Execute: Couldn't caculate Offset</exception>
170176
public static IPath GenerateOutline(this IPath path, float width) => GenerateOutline(path, width, JointStyle.Square, EndCapStyle.Butt);
171177

172178
/// <summary>
@@ -177,6 +183,7 @@ public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<f
177183
/// <param name="jointStyle">The style to render the joints.</param>
178184
/// <param name="endCapStyle">The style to render the end caps of open paths (ignored on closed paths).</param>
179185
/// <returns>A new path representing the outline.</returns>
186+
/// <exception cref="ClipperException">Execute: Couldn't caculate Offset</exception>
180187
public static IPath GenerateOutline(this IPath path, float width, JointStyle jointStyle = JointStyle.Square, EndCapStyle endCapStyle = EndCapStyle.Square)
181188
{
182189
var offset = new ClipperOffset(MiterOffsetDelta);

src/ImageSharp.Drawing/Shapes/PolygonClipper/Clipper.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
@@ -42,13 +42,21 @@ public Clipper(params ClippablePath[] shapes)
4242
/// <returns>
4343
/// Returns the <see cref="IPath" /> array containing the converted polygons.
4444
/// </returns>
45+
/// <exception cref="ClipperException">GenerateClippedShapes: Open paths have been disabled.</exception>
4546
public IPath[] GenerateClippedShapes()
4647
{
4748
var results = new List<PolyNode>();
4849

4950
lock (this.syncRoot)
5051
{
51-
this.innerClipper.Execute(ClipType.ctDifference, results);
52+
try
53+
{
54+
this.innerClipper.Execute(ClipType.ctDifference, results);
55+
}
56+
catch (ClipperLib.ClipperException exception)
57+
{
58+
throw new PolygonClipper.ClipperException(exception.Message);
59+
}
5260
}
5361

5462
var shapes = new IPath[results.Count];
@@ -79,6 +87,7 @@ public IPath[] GenerateClippedShapes()
7987
/// Adds the paths.
8088
/// </summary>
8189
/// <param name="paths">The paths.</param>
90+
/// <exception cref="ClipperException">AddPath: Open paths have been disabled</exception>
8291
public void AddPaths(ClippablePath[] paths)
8392
{
8493
if (paths == null)
@@ -99,6 +108,7 @@ public void AddPaths(ClippablePath[] paths)
99108
/// </summary>
100109
/// <param name="paths">The paths.</param>
101110
/// <param name="clippingType">The clipping type.</param>
111+
/// <exception cref="ClipperException">AddPath: Open paths have been disabled </exception>
102112
public void AddPaths(IEnumerable<IPath> paths, ClippingType clippingType)
103113
{
104114
if (paths is null)
@@ -117,6 +127,7 @@ public void AddPaths(IEnumerable<IPath> paths, ClippingType clippingType)
117127
/// </summary>
118128
/// <param name="path">The path.</param>
119129
/// <param name="clippingType">The clipping type.</param>
130+
/// <exception cref="ClipperException">AddPath: Open paths have been disabled </exception>
120131
public void AddPath(IPath path, ClippingType clippingType)
121132
{
122133
if (path is null)
@@ -149,8 +160,15 @@ internal void AddPath(ISimplePath path, ClippingType clippingType)
149160
PolyType type = clippingType == ClippingType.Clip ? PolyType.ptClip : PolyType.ptSubject;
150161
lock (this.syncRoot)
151162
{
152-
this.innerClipper.AddPath(points, type, path.IsClosed);
163+
try
164+
{
165+
this.innerClipper.AddPath(points, type, path.IsClosed);
166+
}
167+
catch (ClipperLib.ClipperException exception)
168+
{
169+
throw new PolygonClipper.ClipperException(exception.Message);
170+
}
153171
}
154172
}
155173
}
156-
}
174+
}

src/ImageSharp.Drawing/Shapes/PolygonClipper/ClipperException.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
@@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.Drawing.PolygonClipper
99
/// Clipper Exception
1010
/// </summary>
1111
/// <seealso cref="System.Exception" />
12-
internal class ClipperException : Exception
12+
public class ClipperException : Exception
1313
{
1414
/// <summary>
1515
/// Initializes a new instance of the <see cref="ClipperException"/> class.
@@ -20,4 +20,4 @@ public ClipperException(string description)
2020
{
2121
}
2222
}
23-
}
23+
}

src/ImageSharp.Drawing/Shapes/PolygonClipper/ClipperOffset.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Linq;
77
using ClipperLib;
88

9-
namespace SixLabors.ImageSharp.Drawing.Shapes.PolygonClipper
9+
namespace SixLabors.ImageSharp.Drawing.PolygonClipper
1010
{
1111
/// <summary>
1212
/// Wrapper for clipper offset
@@ -30,11 +30,22 @@ public class ClipperOffset
3030
/// </summary>
3131
/// <param name="width">Width</param>
3232
/// <returns>path offset</returns>
33-
/// <exception cref="ClipperException">Execute: </exception>
33+
/// <exception cref="ClipperException">Execute: Couldn't caculate Offset</exception>
3434
public ComplexPolygon Execute(float width)
3535
{
3636
var tree = new List<List<IntPoint>>();
37-
this.innerClipperOffest.Execute(ref tree, width * ScalingFactor / 2);
37+
lock (this.syncRoot)
38+
{
39+
try
40+
{
41+
this.innerClipperOffest.Execute(ref tree, width * ScalingFactor / 2);
42+
}
43+
catch (ClipperLib.ClipperException exception)
44+
{
45+
throw new PolygonClipper.ClipperException(exception.Message);
46+
}
47+
}
48+
3849
var polygons = new List<Polygon>();
3950
foreach (List<IntPoint> pt in tree)
4051
{
@@ -51,6 +62,7 @@ public ComplexPolygon Execute(float width)
5162
/// <param name="pathPoints">The path points</param>
5263
/// <param name="jointStyle">Joint Style</param>
5364
/// <param name="endCapStyle">Endcap Style</param>
65+
/// <exception cref="ClipperException">AddPath: Invalid Path</exception>
5466
public void AddPath(ReadOnlySpan<PointF> pathPoints, JointStyle jointStyle, EndCapStyle endCapStyle) =>
5567
this.AddPath(pathPoints, jointStyle, this.Convert(endCapStyle));
5668

@@ -60,6 +72,7 @@ public void AddPath(ReadOnlySpan<PointF> pathPoints, JointStyle jointStyle, EndC
6072
/// <param name="path">The path.</param>
6173
/// <param name="jointStyle">Joint Style</param>
6274
/// <param name="endCapStyle">Endcap Style</param>
75+
/// <exception cref="ClipperException">AddPath: Invalid Path</exception>
6376
public void AddPath(IPath path, JointStyle jointStyle, EndCapStyle endCapStyle)
6477
{
6578
if (path is null)
@@ -92,23 +105,25 @@ private void AddPath(ISimplePath path, JointStyle jointStyle, EndCapStyle endCap
92105
/// <param name="pathPoints">The path points</param>
93106
/// <param name="jointStyle">Joint Style</param>
94107
/// <param name="endCapStyle">Endcap Style</param>
95-
/// <exception cref="ClipperException">AddPath: Open paths have been disabled.</exception>
108+
/// <exception cref="ClipperException">AddPath: Invalid Path</exception>
96109
private void AddPath(ReadOnlySpan<PointF> pathPoints, JointStyle jointStyle, EndType endCapStyle)
97110
{
98111
var points = new List<IntPoint>();
99112
foreach (PointF v in pathPoints)
100113
{
101-
if (float.IsNaN(v.X) || float.IsNaN(v.Y))
102-
{
103-
throw new ClipperException("Invalid Point");
104-
}
105-
106114
points.Add(new IntPoint(v.X * ScalingFactor, v.Y * ScalingFactor));
107115
}
108116

109117
lock (this.syncRoot)
110118
{
111-
this.innerClipperOffest.AddPath(points, this.Convert(jointStyle), endCapStyle);
119+
try
120+
{
121+
this.innerClipperOffest.AddPath(points, this.Convert(jointStyle), endCapStyle);
122+
}
123+
catch (ClipperLib.ClipperException exception)
124+
{
125+
throw new PolygonClipper.ClipperException(exception.Message);
126+
}
112127
}
113128
}
114129

tests/ImageSharp.Drawing.Tests/Shapes/PolygonClipper/ClipperTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,22 @@ public void ClippingRectanglesCreateCorrectNumberOfPoints()
131131

132132
Assert.Equal(8, points.Count);
133133
}
134+
135+
[Fact]
136+
public void ClipperOffsetThrowsPublicException()
137+
{
138+
PointF naan = new PointF(float.NaN, float.NaN);
139+
Polygon path = new Polygon(new LinearLineSegment(new[] { naan, naan, naan, naan }));
140+
141+
// Throws internal exception:
142+
try
143+
{
144+
path.GenerateOutline(10);
145+
}
146+
catch (System.Exception ex)
147+
{
148+
Assert.True(ex is ClipperException);
149+
}
150+
}
134151
}
135152
}

0 commit comments

Comments
 (0)