Skip to content

Commit c02cc1c

Browse files
committed
merge PathDrawer and PathBuilder
1 parent 25648d4 commit c02cc1c

3 files changed

Lines changed: 60 additions & 150 deletions

File tree

src/ImageSharp.Drawing/Shapes/PathBuilder.cs

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class PathBuilder
1919
private Matrix3x2 currentTransform;
2020
private Matrix3x2 setTransform;
2121

22+
private Vector2 currentPoint = default;
23+
2224
/// <summary>
2325
/// Initializes a new instance of the <see cref="PathBuilder" /> class.
2426
/// </summary>
@@ -88,20 +90,32 @@ public PathBuilder ResetOrigin()
8890
return this;
8991
}
9092

93+
/// <summary>
94+
/// Moves to current point to the supplied vector.
95+
/// </summary>
96+
/// <param name="point">The point.</param>
97+
public void MoveTo(Vector2 point)
98+
{
99+
this.StartFigure();
100+
this.currentPoint = PointF.Transform(point, this.currentTransform);
101+
}
102+
103+
/// <summary>
104+
/// Draws the line connecting the current the current point to the new point.
105+
/// </summary>
106+
/// <param name="point">The point.</param>
107+
/// <returns>The <see cref="PathBuilder"/></returns>
108+
public PathBuilder LineTo(PointF point)
109+
=> this.AddLine(this.currentPoint, point);
110+
91111
/// <summary>
92112
/// Adds the line connecting the current point to the new point.
93113
/// </summary>
94114
/// <param name="start">The start.</param>
95115
/// <param name="end">The end.</param>
96116
/// <returns>The <see cref="PathBuilder"/></returns>
97117
public PathBuilder AddLine(PointF start, PointF end)
98-
{
99-
end = PointF.Transform(end, this.currentTransform);
100-
start = PointF.Transform(start, this.currentTransform);
101-
this.currentFigure.AddSegment(new LinearLineSegment(start, end));
102-
103-
return this;
104-
}
118+
=> this.AddSegment(new LinearLineSegment(start, end));
105119

106120
/// <summary>
107121
/// Adds the line connecting the current point to the new point.
@@ -112,11 +126,7 @@ public PathBuilder AddLine(PointF start, PointF end)
112126
/// <param name="y2">The y2.</param>
113127
/// <returns>The <see cref="PathBuilder"/></returns>
114128
public PathBuilder AddLine(float x1, float y1, float x2, float y2)
115-
{
116-
this.AddLine(new PointF(x1, y1), new PointF(x2, y2));
117-
118-
return this;
119-
}
129+
=> this.AddLine(new PointF(x1, y1), new PointF(x2, y2));
120130

121131
/// <summary>
122132
/// Adds a series of line segments connecting the current point to the new points.
@@ -141,11 +151,7 @@ public PathBuilder AddLines(IEnumerable<PointF> points)
141151
/// <param name="points">The points.</param>
142152
/// <returns>The <see cref="PathBuilder"/></returns>
143153
public PathBuilder AddLines(params PointF[] points)
144-
{
145-
this.AddSegment(new LinearLineSegment(points));
146-
147-
return this;
148-
}
154+
=> this.AddSegment(new LinearLineSegment(points));
149155

150156
/// <summary>
151157
/// Adds the segment.
@@ -154,11 +160,31 @@ public PathBuilder AddLines(params PointF[] points)
154160
/// <returns>The <see cref="PathBuilder"/></returns>
155161
public PathBuilder AddSegment(ILineSegment segment)
156162
{
157-
this.currentFigure.AddSegment(segment.Transform(this.currentTransform));
158-
163+
segment = segment.Transform(this.currentTransform);
164+
this.currentFigure.AddSegment(segment);
165+
this.currentPoint = segment.EndPoint;
159166
return this;
160167
}
161168

169+
/// <summary>
170+
/// Draws a quadratics bezier from the current point to the <paramref name="point"/>
171+
/// </summary>
172+
/// <param name="secondControlPoint">The second control point.</param>
173+
/// <param name="point">The point.</param>
174+
/// <returns>The <see cref="PathBuilder"/></returns>
175+
public PathBuilder QuadraticBezierTo(Vector2 secondControlPoint, Vector2 point)
176+
=> this.AddBezier(this.currentPoint, secondControlPoint, point);
177+
178+
/// <summary>
179+
/// Draws a quadratics bezier from the current point to the <paramref name="point"/>
180+
/// </summary>
181+
/// <param name="secondControlPoint">The second control point.</param>
182+
/// <param name="thirdControlPoint">The third control point.</param>
183+
/// <param name="point">The point.</param>
184+
/// <returns>The <see cref="PathBuilder"/></returns>
185+
public PathBuilder CubicBezierTo(Vector2 secondControlPoint, Vector2 thirdControlPoint, Vector2 point)
186+
=> this.AddBezier(this.currentPoint, secondControlPoint, thirdControlPoint, point);
187+
162188
/// <summary>
163189
/// Adds a quadratic bezier curve to the current figure joining the last point to the endPoint.
164190
/// </summary>
@@ -189,15 +215,7 @@ public PathBuilder AddBezier(PointF startPoint, PointF controlPoint, PointF endP
189215
/// <param name="endPoint">The end point.</param>
190216
/// <returns>The <see cref="PathBuilder"/></returns>
191217
public PathBuilder AddBezier(PointF startPoint, PointF controlPoint1, PointF controlPoint2, PointF endPoint)
192-
{
193-
this.currentFigure.AddSegment(new CubicBezierLineSegment(
194-
PointF.Transform(startPoint, this.currentTransform),
195-
PointF.Transform(controlPoint1, this.currentTransform),
196-
PointF.Transform(controlPoint2, this.currentTransform),
197-
PointF.Transform(endPoint, this.currentTransform)));
198-
199-
return this;
200-
}
218+
=> this.AddSegment(new CubicBezierLineSegment(startPoint, controlPoint1, controlPoint2, endPoint));
201219

202220
/// <summary>
203221
/// Adds an elliptical arc to the current figure
@@ -207,7 +225,8 @@ public PathBuilder AddBezier(PointF startPoint, PointF controlPoint1, PointF con
207225
/// <param name="startAngle">The Start angle of the ellipsis, measured in degrees anticlockwise from the Y-axis.</param>
208226
/// <param name="sweepAngle"> The angle between (<paramref name="startAngle"/> and the end of the arc. </param>
209227
/// <returns>The <see cref="PathBuilder"/></returns>
210-
public PathBuilder AddEllipticalArc(RectangleF rect, float rotation, float startAngle, float sweepAngle) => this.AddEllipticalArc((rect.Right + rect.Left) / 2, (rect.Bottom + rect.Top) / 2, rect.Width / 2, rect.Height / 2, rotation, startAngle, sweepAngle);
228+
public PathBuilder AddEllipticalArc(RectangleF rect, float rotation, float startAngle, float sweepAngle)
229+
=> this.AddEllipticalArc((rect.Right + rect.Left) / 2, (rect.Bottom + rect.Top) / 2, rect.Width / 2, rect.Height / 2, rotation, startAngle, sweepAngle);
211230

212231
/// <summary>
213232
/// Adds an elliptical arc to the current figure
@@ -217,7 +236,8 @@ public PathBuilder AddBezier(PointF startPoint, PointF controlPoint1, PointF con
217236
/// <param name="startAngle">The Start angle of the ellipsis, measured in degrees anticlockwise from the Y-axis.</param>
218237
/// <param name="sweepAngle"> The angle between (<paramref name="startAngle"/> and the end of the arc. </param>
219238
/// <returns>The <see cref="PathBuilder"/></returns>
220-
public PathBuilder AddEllipticalArc(Rectangle rect, int rotation, int startAngle, int sweepAngle) => this.AddEllipticalArc((float)(rect.Right + rect.Left) / 2, (float)(rect.Bottom + rect.Top) / 2, (float)rect.Width / 2, (float)rect.Height / 2, rotation, startAngle, sweepAngle);
239+
public PathBuilder AddEllipticalArc(Rectangle rect, int rotation, int startAngle, int sweepAngle)
240+
=> this.AddEllipticalArc((float)(rect.Right + rect.Left) / 2, (float)(rect.Bottom + rect.Top) / 2, (float)rect.Width / 2, (float)rect.Height / 2, rotation, startAngle, sweepAngle);
221241

222242
/// <summary>
223243
/// Adds an elliptical arc to the current figure
@@ -229,7 +249,8 @@ public PathBuilder AddBezier(PointF startPoint, PointF controlPoint1, PointF con
229249
/// <param name="startAngle">The Start angle of the ellipsis, measured in degrees anticlockwise from the Y-axis.</param>
230250
/// <param name="sweepAngle"> The angle between (<paramref name="startAngle"/> and the end of the arc. </param>
231251
/// <returns>The <see cref="PathBuilder"/></returns>
232-
public PathBuilder AddEllipticalArc(PointF center, float radiusX, float radiusY, float rotation, float startAngle, float sweepAngle) => this.AddEllipticalArc(center.X, center.Y, radiusX, radiusY, rotation, startAngle, sweepAngle);
252+
public PathBuilder AddEllipticalArc(PointF center, float radiusX, float radiusY, float rotation, float startAngle, float sweepAngle)
253+
=> this.AddEllipticalArc(center.X, center.Y, radiusX, radiusY, rotation, startAngle, sweepAngle);
233254

234255
/// <summary>
235256
/// Adds an elliptical arc to the current figure
@@ -241,7 +262,8 @@ public PathBuilder AddBezier(PointF startPoint, PointF controlPoint1, PointF con
241262
/// <param name="startAngle">The Start angle of the ellipsis, measured in degrees anticlockwise from the Y-axis.</param>
242263
/// <param name="sweepAngle"> The angle between (<paramref name="startAngle"/> and the end of the arc. </param>
243264
/// <returns>The <see cref="PathBuilder"/></returns>
244-
public PathBuilder AddEllipticalArc(Point center, int radiusX, int radiusY, int rotation, int startAngle, int sweepAngle) => this.AddEllipticalArc(center.X, center.Y, radiusX, radiusY, rotation, startAngle, sweepAngle);
265+
public PathBuilder AddEllipticalArc(Point center, int radiusX, int radiusY, int rotation, int startAngle, int sweepAngle)
266+
=> this.AddEllipticalArc(center.X, center.Y, radiusX, radiusY, rotation, startAngle, sweepAngle);
245267

246268
/// <summary>
247269
/// Adds an elliptical arc to the current figure
@@ -255,11 +277,7 @@ public PathBuilder AddBezier(PointF startPoint, PointF controlPoint1, PointF con
255277
/// <param name="sweepAngle"> The angle between (<paramref name="startAngle"/> and the end of the arc. </param>
256278
/// <returns>The <see cref="PathBuilder"/></returns>
257279
public PathBuilder AddEllipticalArc(int x, int y, int radiusX, int radiusY, int rotation, int startAngle, int sweepAngle)
258-
{
259-
this.currentFigure.AddSegment(new EllipticalArcLineSegment(x, y, radiusX, radiusY, rotation, startAngle, sweepAngle, this.currentTransform));
260-
261-
return this;
262-
}
280+
=> this.AddSegment(new EllipticalArcLineSegment(x, y, radiusX, radiusY, rotation, startAngle, sweepAngle, Matrix3x2.Identity));
263281

264282
/// <summary>
265283
/// Adds an elliptical arc to the current figure
@@ -273,11 +291,7 @@ public PathBuilder AddEllipticalArc(int x, int y, int radiusX, int radiusY, int
273291
/// <param name="sweepAngle"> The angle between (<paramref name="startAngle"/> and the end of the arc. </param>
274292
/// <returns>The <see cref="PathBuilder"/></returns>
275293
public PathBuilder AddEllipticalArc(float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float sweepAngle)
276-
{
277-
this.currentFigure.AddSegment(new EllipticalArcLineSegment(x, y, radiusX, radiusY, rotation, startAngle, sweepAngle, this.currentTransform));
278-
279-
return this;
280-
}
294+
=> this.AddSegment(new EllipticalArcLineSegment(x, y, radiusX, radiusY, rotation, startAngle, sweepAngle, Matrix3x2.Identity));
281295

282296
/// <summary>
283297
/// Starts a new figure but leaves the previous one open.
@@ -349,6 +363,7 @@ public PathBuilder Reset()
349363
{
350364
this.Clear();
351365
this.ResetTransform();
366+
this.currentPoint = default;
352367

353368
return this;
354369
}

src/ImageSharp.Drawing/Shapes/PathDrawer.cs

Lines changed: 0 additions & 106 deletions
This file was deleted.

src/ImageSharp.Drawing/Shapes/Text/BaseGlyphBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ internal class BaseGlyphBuilder : IGlyphRenderer
1717
/// The builder. TODO: Should this be a property?
1818
/// </summary>
1919
// ReSharper disable once InconsistentNaming
20-
protected readonly PathDrawer drawer;
20+
protected readonly PathBuilder drawer;
2121
#pragma warning restore SA1401 // Fields should be private
2222
private readonly List<IPath> paths = new();
2323

2424
/// <summary>
2525
/// Initializes a new instance of the <see cref="BaseGlyphBuilder"/> class.
2626
/// </summary>
27-
public BaseGlyphBuilder() => this.drawer = new PathDrawer();
27+
public BaseGlyphBuilder()
28+
=> this.drawer = new PathBuilder();
2829

2930
/// <summary>
3031
/// Gets the paths that have been rendered by this.

0 commit comments

Comments
 (0)