-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Path.cs
425 lines (366 loc) · 13.3 KB
/
Path.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Numerics;
namespace SixLabors.ImageSharp.Drawing;
/// <summary>
/// A aggregate of <see cref="ILineSegment"/>s making a single logical path.
/// </summary>
/// <seealso cref="IPath" />
public class Path : IPath, ISimplePath, IPathInternals, IInternalPathOwner
{
private readonly ILineSegment[] lineSegments;
private InternalPath? innerPath;
/// <summary>
/// Initializes a new instance of the <see cref="Path"/> class.
/// </summary>
/// <param name="points">The collection of points; processed as a series of linear line segments.</param>
public Path(PointF[] points)
: this(new LinearLineSegment(points))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Path"/> class.
/// </summary>
/// <param name="segments">The segments.</param>
public Path(IEnumerable<ILineSegment> segments)
: this(segments.ToArray())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Path" /> class.
/// </summary>
/// <param name="path">The path.</param>
public Path(Path path)
: this(path.LineSegments)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Path"/> class.
/// </summary>
/// <param name="segments">The segments.</param>
public Path(params ILineSegment[] segments)
{
ArgumentNullException.ThrowIfNull(segments);
this.lineSegments = segments;
}
/// <summary>
/// Gets the default empty path.
/// </summary>
public static IPath Empty { get; } = EmptyPath.OpenPath;
/// <inheritdoc/>
bool ISimplePath.IsClosed => this.IsClosed;
/// <inheritdoc cref="ISimplePath.IsClosed"/>
public virtual bool IsClosed => false;
/// <inheritdoc/>
public ReadOnlyMemory<PointF> Points => this.InnerPath.Points();
/// <inheritdoc />
public RectangleF Bounds => this.InnerPath.Bounds;
/// <inheritdoc />
public PathTypes PathType => this.IsClosed ? PathTypes.Closed : PathTypes.Open;
/// <summary>
/// Gets the maximum number intersections that a shape can have when testing a line.
/// </summary>
internal int MaxIntersections => this.InnerPath.PointCount;
/// <summary>
/// Gets readonly collection of line segments.
/// </summary>
public IReadOnlyList<ILineSegment> LineSegments => this.lineSegments;
/// <summary>
/// Gets or sets a value indicating whether close or collinear vertices should be removed. TEST ONLY!
/// </summary>
internal bool RemoveCloseAndCollinearPoints { get; set; } = true;
private InternalPath InnerPath =>
this.innerPath ??= new InternalPath(this.lineSegments, this.IsClosed, this.RemoveCloseAndCollinearPoints);
/// <inheritdoc />
public virtual IPath Transform(Matrix3x2 matrix)
{
if (matrix.IsIdentity)
{
return this;
}
ILineSegment[] segments = new ILineSegment[this.lineSegments.Length];
for (int i = 0; i < this.LineSegments.Count; i++)
{
segments[i] = this.lineSegments[i].Transform(matrix);
}
return new Path(segments);
}
/// <inheritdoc />
public IPath AsClosedPath()
{
if (this.IsClosed)
{
return this;
}
return new Polygon(this.LineSegments);
}
/// <inheritdoc />
public IEnumerable<ISimplePath> Flatten()
{
yield return this;
}
/// <inheritdoc/>
SegmentInfo IPathInternals.PointAlongPath(float distance)
=> this.InnerPath.PointAlongPath(distance);
/// <inheritdoc/>
IReadOnlyList<InternalPath> IInternalPathOwner.GetRingsAsInternalPath() => new[] { this.InnerPath };
/// <summary>
/// Converts an SVG path string into an <see cref="IPath"/>.
/// </summary>
/// <param name="svgPath">The string containing the SVG path data.</param>
/// <param name="value">
/// When this method returns, contains the logic path converted from the given SVG path string; otherwise, <see langword="null"/>.
/// This parameter is passed uninitialized.
/// </param>
/// <returns><see langword="true"/> if the input value can be parsed and converted; otherwise, <see langword="false"/>.</returns>
public static bool TryParseSvgPath(string svgPath, [NotNullWhen(true)] out IPath? value)
=> TryParseSvgPath(svgPath.AsSpan(), out value);
/// <summary>
/// Converts an SVG path string into an <see cref="IPath"/>.
/// </summary>
/// <param name="svgPath">The string containing the SVG path data.</param>
/// <param name="value">
/// When this method returns, contains the logic path converted from the given SVG path string; otherwise, <see langword="null"/>.
/// This parameter is passed uninitialized.
/// </param>
/// <returns><see langword="true"/> if the input value can be parsed and converted; otherwise, <see langword="false"/>.</returns>
public static bool TryParseSvgPath(ReadOnlySpan<char> svgPath, [NotNullWhen(true)] out IPath? value)
{
value = null;
PathBuilder builder = new();
PointF first = PointF.Empty;
PointF c = PointF.Empty;
PointF lastc = PointF.Empty;
PointF point1;
PointF point2;
PointF point3;
char op = '\0';
char previousOp = '\0';
bool relative = false;
while (true)
{
svgPath = svgPath.TrimStart();
if (svgPath.Length == 0)
{
break;
}
char ch = svgPath[0];
if (char.IsDigit(ch) || ch == '-' || ch == '+' || ch == '.')
{
// Are we are the end of the string or we are at the end of the path?
if (svgPath.Length == 0 || op == 'Z')
{
return false;
}
}
else if (IsSeparator(ch))
{
svgPath = TrimSeparator(svgPath);
}
else
{
op = ch;
relative = false;
if (char.IsLower(op))
{
op = char.ToUpper(op, CultureInfo.InvariantCulture);
relative = true;
}
svgPath = TrimSeparator(svgPath[1..]);
}
switch (op)
{
case 'M':
svgPath = FindPoint(svgPath, relative, c, out point1);
builder.MoveTo(point1);
previousOp = '\0';
op = 'L';
c = point1;
break;
case 'L':
svgPath = FindPoint(svgPath, relative, c, out point1);
builder.LineTo(point1);
c = point1;
break;
case 'H':
svgPath = FindScaler(svgPath, out float x);
if (relative)
{
x += c.X;
}
builder.LineTo(x, c.Y);
c.X = x;
break;
case 'V':
svgPath = FindScaler(svgPath, out float y);
if (relative)
{
y += c.Y;
}
builder.LineTo(c.X, y);
c.Y = y;
break;
case 'C':
svgPath = FindPoint(svgPath, relative, c, out point1);
svgPath = FindPoint(svgPath, relative, c, out point2);
svgPath = FindPoint(svgPath, relative, c, out point3);
builder.CubicBezierTo(point1, point2, point3);
lastc = point2;
c = point3;
break;
case 'S':
svgPath = FindPoint(svgPath, relative, c, out point2);
svgPath = FindPoint(svgPath, relative, c, out point3);
point1 = c;
if (previousOp is 'C' or 'S')
{
point1.X -= lastc.X - c.X;
point1.Y -= lastc.Y - c.Y;
}
builder.CubicBezierTo(point1, point2, point3);
lastc = point2;
c = point3;
break;
case 'Q': // Quadratic Bezier Curve
svgPath = FindPoint(svgPath, relative, c, out point1);
svgPath = FindPoint(svgPath, relative, c, out point2);
builder.QuadraticBezierTo(point1, point2);
lastc = point1;
c = point2;
break;
case 'T':
svgPath = FindPoint(svgPath, relative, c, out point2);
point1 = c;
if (previousOp is 'Q' or 'T')
{
point1.X -= lastc.X - c.X;
point1.Y -= lastc.Y - c.Y;
}
builder.QuadraticBezierTo(point1, point2);
lastc = point1;
c = point2;
break;
case 'A':
if (TryFindScaler(ref svgPath, out float radiiX)
&& TryTrimSeparator(ref svgPath)
&& TryFindScaler(ref svgPath, out float radiiY)
&& TryTrimSeparator(ref svgPath)
&& TryFindScaler(ref svgPath, out float angle)
&& TryTrimSeparator(ref svgPath)
&& TryFindScaler(ref svgPath, out float largeArc)
&& TryTrimSeparator(ref svgPath)
&& TryFindScaler(ref svgPath, out float sweep)
&& TryFindPoint(ref svgPath, relative, c, out PointF point))
{
builder.ArcTo(radiiX, radiiY, angle, largeArc == 1, sweep == 1, point);
c = point;
}
break;
case 'Z':
builder.CloseFigure();
c = first;
break;
case '~':
svgPath = FindPoint(svgPath, relative, c, out point1);
svgPath = FindPoint(svgPath, relative, c, out point2);
builder.MoveTo(point1).LineTo(point2);
break;
default:
return false;
}
if (previousOp == 0)
{
first = c;
}
previousOp = op;
}
value = builder.Build();
return true;
}
private static bool TryTrimSeparator(ref ReadOnlySpan<char> str)
{
ReadOnlySpan<char> result = TrimSeparator(str);
if (str[^result.Length..].StartsWith(result))
{
str = result;
return true;
}
return false;
}
private static bool TryFindScaler(ref ReadOnlySpan<char> str, out float value)
{
ReadOnlySpan<char> result = FindScaler(str, out float valueInner);
if (str[^result.Length..].StartsWith(result))
{
value = valueInner;
str = result;
return true;
}
value = default;
return false;
}
private static bool TryFindPoint(ref ReadOnlySpan<char> str, bool relative, PointF current, out PointF value)
{
ReadOnlySpan<char> result = FindPoint(str, relative, current, out PointF valueInner);
if (str[^result.Length..].StartsWith(result))
{
value = valueInner;
str = result;
return true;
}
value = default;
return false;
}
private static ReadOnlySpan<char> FindPoint(ReadOnlySpan<char> str, bool isRelative, PointF relative, out PointF value)
{
str = FindScaler(str, out float x);
str = FindScaler(str, out float y);
if (isRelative)
{
x += relative.X;
y += relative.Y;
}
value = new PointF(x, y);
return str;
}
private static ReadOnlySpan<char> FindScaler(ReadOnlySpan<char> str, out float scaler)
{
str = TrimSeparator(str);
scaler = 0;
for (int i = 0; i < str.Length; i++)
{
if (IsSeparator(str[i]) || i == str.Length)
{
scaler = ParseFloat(str[..i]);
return str[i..];
}
}
if (str.Length > 0)
{
scaler = ParseFloat(str);
}
return ReadOnlySpan<char>.Empty;
}
private static bool IsSeparator(char ch)
=> char.IsWhiteSpace(ch) || ch == ',';
private static ReadOnlySpan<char> TrimSeparator(ReadOnlySpan<char> data)
{
if (data.Length == 0)
{
return data;
}
int idx = 0;
for (; idx < data.Length; idx++)
{
if (!IsSeparator(data[idx]))
{
break;
}
}
return data[idx..];
}
private static float ParseFloat(ReadOnlySpan<char> str)
=> float.Parse(str, provider: CultureInfo.InvariantCulture);
}