@@ -15,6 +15,31 @@ public static class OutlinePathExtensions
15
15
private const JointStyle DefaultJointStyle = JointStyle . Square ;
16
16
private const EndCapStyle DefaultEndCapStyle = EndCapStyle . Butt ;
17
17
18
+ /// <summary>
19
+ /// Calculates the scaling matrixes tha tmust be applied to the inout and output paths of for successful clipping.
20
+ /// </summary>
21
+ /// <param name="width">the requested width</param>
22
+ /// <param name="scaleUpMartrix">The matrix to apply to the input path</param>
23
+ /// <param name="scaleDownMartrix">The matrix to apply to the output path</param>
24
+ /// <returns>The final width to use internally to outlining</returns>
25
+ private static float CalculateScalingMatrix ( float width , out Matrix3x2 scaleUpMartrix , out Matrix3x2 scaleDownMartrix )
26
+ {
27
+ // when the thickness is below a 0.5 threshold we need to scale
28
+ // the source path (up) and result path (down) by a factor to ensure
29
+ // the offest is greater than 0.5 to ensure offsetting isn't skipped.
30
+ scaleUpMartrix = Matrix3x2 . Identity ;
31
+ scaleDownMartrix = Matrix3x2 . Identity ;
32
+ if ( width < 0.5 )
33
+ {
34
+ float scale = 1 / width ;
35
+ scaleUpMartrix = Matrix3x2 . CreateScale ( scale ) ;
36
+ scaleDownMartrix = Matrix3x2 . CreateScale ( width ) ;
37
+ width = 1 ;
38
+ }
39
+
40
+ return width ;
41
+ }
42
+
18
43
/// <summary>
19
44
/// Generates an outline of the path.
20
45
/// </summary>
@@ -41,10 +66,14 @@ public static IPath GenerateOutline(this IPath path, float width, JointStyle joi
41
66
return Path . Empty ;
42
67
}
43
68
69
+ width = CalculateScalingMatrix ( width , out Matrix3x2 scaleUpMartrix , out Matrix3x2 scaleDownMartrix ) ;
70
+
44
71
ClipperOffset offset = new ( MiterOffsetDelta ) ;
45
- offset . AddPath ( path , jointStyle , endCapStyle ) ;
46
72
47
- return offset . Execute ( width ) ;
73
+ // transform is noop for Matrix3x2.Identity
74
+ offset . AddPath ( path . Transform ( scaleUpMartrix ) , jointStyle , endCapStyle ) ;
75
+
76
+ return offset . Execute ( width ) . Transform ( scaleDownMartrix ) ;
48
77
}
49
78
50
79
/// <summary>
@@ -106,7 +135,9 @@ public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<f
106
135
return path . GenerateOutline ( width , jointStyle , endCapStyle ) ;
107
136
}
108
137
109
- IEnumerable < ISimplePath > paths = path . Flatten ( ) ;
138
+ width = CalculateScalingMatrix ( width , out Matrix3x2 scaleUpMartrix , out Matrix3x2 scaleDownMartrix ) ;
139
+
140
+ IEnumerable < ISimplePath > paths = path . Transform ( scaleUpMartrix ) . Flatten ( ) ;
110
141
111
142
ClipperOffset offset = new ( MiterOffsetDelta ) ;
112
143
List < PointF > buffer = new ( ) ;
@@ -186,6 +217,6 @@ public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<f
186
217
}
187
218
}
188
219
189
- return offset . Execute ( width ) ;
220
+ return offset . Execute ( width ) . Transform ( scaleDownMartrix ) ;
190
221
}
191
222
}
0 commit comments