Skip to content

Commit 506b059

Browse files
Convert shape extensions to DrawingCanvas methods
1 parent a683e7b commit 506b059

3 files changed

Lines changed: 53 additions & 68 deletions

File tree

src/ImageSharp.Drawing/Processing/DRAWING_CANVAS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ If you want to move from the architecture into the code, this is the best order.
376376

377377
1. `DrawingCanvas.cs`
378378
2. `DrawingCanvas{TPixel}.cs`
379-
3. `DrawingCanvasFactoryExtensions.cs` and `DrawingCanvasShapeExtensions.cs`
379+
3. `DrawingCanvasFactoryExtensions.cs` and `DrawingCanvas.Shapes.cs`
380380
4. `DrawingCanvasBatcher{TPixel}.cs`
381381
5. `CompositionCommand.cs`
382382
6. `DefaultDrawingBackend.cs`

src/ImageSharp.Drawing/Processing/DrawingCanvasShapeExtensions.cs renamed to src/ImageSharp.Drawing/Processing/DrawingCanvas.Shapes.cs

Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,238 +3,223 @@
33

44
namespace SixLabors.ImageSharp.Drawing.Processing;
55

6-
/// <summary>
7-
/// Convenience canvas helpers that forward to the core <see cref="DrawingCanvas"/> primitives.
8-
/// </summary>
9-
public static class DrawingCanvasShapeExtensions
6+
/// <content>
7+
/// Convenience shape helpers that forward to the core <see cref="DrawingCanvas"/> primitives.
8+
/// </content>
9+
public abstract partial class DrawingCanvas
1010
{
1111
/// <summary>
1212
/// Saves the current drawing state and begins an isolated compositing layer over the whole canvas.
1313
/// </summary>
14-
/// <param name="canvas">The destination canvas.</param>
1514
/// <returns>The save count after the layer state has been pushed.</returns>
16-
public static int SaveLayer(this DrawingCanvas canvas)
17-
=> canvas.SaveLayer(new GraphicsOptions(), canvas.Bounds);
15+
public int SaveLayer()
16+
=> this.SaveLayer(new GraphicsOptions(), this.Bounds);
1817

1918
/// <summary>
2019
/// Saves the current drawing state and begins an isolated compositing layer over the whole canvas.
2120
/// </summary>
22-
/// <param name="canvas">The destination canvas.</param>
2321
/// <param name="layerOptions">Graphics options controlling how the layer is composited on restore.</param>
2422
/// <returns>The save count after the layer state has been pushed.</returns>
25-
public static int SaveLayer(this DrawingCanvas canvas, GraphicsOptions layerOptions)
26-
=> canvas.SaveLayer(layerOptions, canvas.Bounds);
23+
public int SaveLayer(GraphicsOptions layerOptions)
24+
=> this.SaveLayer(layerOptions, this.Bounds);
2725

2826
/// <summary>
2927
/// Fills the whole canvas using the given brush.
3028
/// </summary>
31-
/// <param name="canvas">The destination canvas.</param>
3229
/// <param name="brush">Brush used to shade destination pixels.</param>
33-
public static void Fill(this DrawingCanvas canvas, Brush brush)
30+
public void Fill(Brush brush)
3431
{
35-
Rectangle bounds = canvas.Bounds;
36-
canvas.Fill(brush, new RectangularPolygon(bounds));
32+
Rectangle bounds = this.Bounds;
33+
34+
this.Fill(brush, new RectangularPolygon(bounds));
3735
}
3836

3937
/// <summary>
4038
/// Fills a local region using the given brush.
4139
/// </summary>
42-
/// <param name="canvas">The destination canvas.</param>
4340
/// <param name="brush">Brush used to shade destination pixels.</param>
4441
/// <param name="region">Region to fill in local coordinates.</param>
45-
public static void Fill(this DrawingCanvas canvas, Brush brush, Rectangle region)
46-
=> canvas.Fill(brush, new RectangularPolygon(region));
42+
public void Fill(Brush brush, Rectangle region)
43+
=> this.Fill(brush, new RectangularPolygon(region));
4744

4845
/// <summary>
4946
/// Clears the whole canvas using the given brush and clear-style composition options.
5047
/// </summary>
51-
/// <param name="canvas">The destination canvas.</param>
5248
/// <param name="brush">Brush used to shade destination pixels during clear.</param>
53-
public static void Clear(this DrawingCanvas canvas, Brush brush)
49+
public void Clear(Brush brush)
5450
{
55-
Rectangle bounds = canvas.Bounds;
56-
canvas.Clear(brush, new RectangularPolygon(bounds));
51+
Rectangle bounds = this.Bounds;
52+
53+
this.Clear(brush, new RectangularPolygon(bounds));
5754
}
5855

5956
/// <summary>
6057
/// Clears a local region using the given brush and clear-style composition options.
6158
/// </summary>
62-
/// <param name="canvas">The destination canvas.</param>
6359
/// <param name="brush">Brush used to shade destination pixels during clear.</param>
6460
/// <param name="region">Region to clear in local coordinates.</param>
65-
public static void Clear(this DrawingCanvas canvas, Brush brush, Rectangle region)
66-
=> canvas.Clear(brush, new RectangularPolygon(region));
61+
public void Clear(Brush brush, Rectangle region)
62+
=> this.Clear(brush, new RectangularPolygon(region));
6763

6864
/// <summary>
6965
/// Fills all paths in a collection using the given brush.
7066
/// </summary>
71-
/// <param name="canvas">The destination canvas.</param>
7267
/// <param name="brush">Brush used to shade covered pixels.</param>
7368
/// <param name="paths">Path collection to fill.</param>
74-
public static void Fill(this DrawingCanvas canvas, Brush brush, IPathCollection paths)
69+
public void Fill(Brush brush, IPathCollection paths)
7570
{
7671
Guard.NotNull(paths, nameof(paths));
7772

7873
foreach (IPath path in paths)
7974
{
80-
canvas.Fill(brush, path);
75+
this.Fill(brush, path);
8176
}
8277
}
8378

8479
/// <summary>
8580
/// Fills a path built by the provided builder using the given brush.
8681
/// </summary>
87-
/// <param name="canvas">The destination canvas.</param>
8882
/// <param name="brush">Brush used to shade covered pixels.</param>
8983
/// <param name="pathBuilder">The path builder describing the fill region.</param>
90-
public static void Fill(this DrawingCanvas canvas, Brush brush, PathBuilder pathBuilder)
84+
public void Fill(Brush brush, PathBuilder pathBuilder)
9185
{
9286
Guard.NotNull(pathBuilder, nameof(pathBuilder));
93-
canvas.Fill(brush, pathBuilder.Build());
87+
88+
this.Fill(brush, pathBuilder.Build());
9489
}
9590

9691
/// <summary>
9792
/// Fills an ellipse using the provided brush.
9893
/// </summary>
99-
/// <param name="canvas">The destination canvas.</param>
10094
/// <param name="brush">Brush used to shade covered pixels.</param>
10195
/// <param name="center">Ellipse center point in local coordinates.</param>
10296
/// <param name="size">Ellipse width and height in local coordinates.</param>
103-
public static void FillEllipse(this DrawingCanvas canvas, Brush brush, PointF center, SizeF size)
104-
=> canvas.Fill(brush, new EllipsePolygon(center, size));
97+
public void FillEllipse(Brush brush, PointF center, SizeF size)
98+
=> this.Fill(brush, new EllipsePolygon(center, size));
10599

106100
/// <summary>
107101
/// Fills the closed arc shape produced by joining the arc endpoints with a straight line.
108102
/// </summary>
109-
/// <param name="canvas">The destination canvas.</param>
110103
/// <param name="brush">Brush used to shade covered pixels.</param>
111104
/// <param name="center">Arc center point in local coordinates.</param>
112105
/// <param name="radius">Arc radii in local coordinates.</param>
113106
/// <param name="rotation">Ellipse rotation in degrees.</param>
114107
/// <param name="startAngle">Arc start angle in degrees.</param>
115108
/// <param name="sweepAngle">Arc sweep angle in degrees.</param>
116-
public static void FillArc(this DrawingCanvas canvas, Brush brush, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle)
117-
=> canvas.Fill(brush, new Path(new ArcLineSegment(center, radius, rotation, startAngle, sweepAngle)));
109+
public void FillArc(Brush brush, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle)
110+
=> this.Fill(brush, new Path(new ArcLineSegment(center, radius, rotation, startAngle, sweepAngle)));
118111

119112
/// <summary>
120113
/// Fills a pie sector using the provided brush.
121114
/// </summary>
122-
/// <param name="canvas">The destination canvas.</param>
123115
/// <param name="brush">Brush used to shade covered pixels.</param>
124116
/// <param name="center">Pie center point in local coordinates.</param>
125117
/// <param name="radius">Pie radii in local coordinates.</param>
126118
/// <param name="rotation">Ellipse rotation in degrees.</param>
127119
/// <param name="startAngle">Pie start angle in degrees.</param>
128120
/// <param name="sweepAngle">Pie sweep angle in degrees.</param>
129-
public static void FillPie(this DrawingCanvas canvas, Brush brush, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle)
130-
=> canvas.Fill(brush, new Pie(center, radius, rotation, startAngle, sweepAngle));
121+
public void FillPie(Brush brush, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle)
122+
=> this.Fill(brush, new Pie(center, radius, rotation, startAngle, sweepAngle));
131123

132124
/// <summary>
133125
/// Fills a pie sector using the provided brush.
134126
/// </summary>
135-
/// <param name="canvas">The destination canvas.</param>
136127
/// <param name="brush">Brush used to shade covered pixels.</param>
137128
/// <param name="center">Pie center point in local coordinates.</param>
138129
/// <param name="radius">Pie radii in local coordinates.</param>
139130
/// <param name="startAngle">Pie start angle in degrees.</param>
140131
/// <param name="sweepAngle">Pie sweep angle in degrees.</param>
141-
public static void FillPie(this DrawingCanvas canvas, Brush brush, PointF center, SizeF radius, float startAngle, float sweepAngle)
142-
=> canvas.Fill(brush, new Pie(center, radius, startAngle, sweepAngle));
132+
public void FillPie(Brush brush, PointF center, SizeF radius, float startAngle, float sweepAngle)
133+
=> this.Fill(brush, new Pie(center, radius, startAngle, sweepAngle));
143134

144135
/// <summary>
145136
/// Draws an arc outline using the provided pen.
146137
/// </summary>
147-
/// <param name="canvas">The destination canvas.</param>
148138
/// <param name="pen">Pen used to generate the arc outline.</param>
149139
/// <param name="center">Arc center point in local coordinates.</param>
150140
/// <param name="radius">Arc radii in local coordinates.</param>
151141
/// <param name="rotation">Ellipse rotation in degrees.</param>
152142
/// <param name="startAngle">Arc start angle in degrees.</param>
153143
/// <param name="sweepAngle">Arc sweep angle in degrees.</param>
154-
public static void DrawArc(this DrawingCanvas canvas, Pen pen, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle)
155-
=> canvas.Draw(pen, new Path(new ArcLineSegment(center, radius, rotation, startAngle, sweepAngle)));
144+
public void DrawArc(Pen pen, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle)
145+
=> this.Draw(pen, new Path(new ArcLineSegment(center, radius, rotation, startAngle, sweepAngle)));
156146

157147
/// <summary>
158148
/// Draws a cubic bezier outline using the provided pen.
159149
/// </summary>
160-
/// <param name="canvas">The destination canvas.</param>
161150
/// <param name="pen">Pen used to generate the bezier outline.</param>
162151
/// <param name="points">Bezier control points.</param>
163-
public static void DrawBezier(this DrawingCanvas canvas, Pen pen, params PointF[] points)
152+
public void DrawBezier(Pen pen, params PointF[] points)
164153
{
165154
Guard.NotNull(points, nameof(points));
166-
canvas.Draw(pen, new Path(new CubicBezierLineSegment(points)));
155+
156+
this.Draw(pen, new Path(new CubicBezierLineSegment(points)));
167157
}
168158

169159
/// <summary>
170160
/// Draws an ellipse outline using the provided pen.
171161
/// </summary>
172-
/// <param name="canvas">The destination canvas.</param>
173162
/// <param name="pen">Pen used to generate the ellipse outline.</param>
174163
/// <param name="center">Ellipse center point in local coordinates.</param>
175164
/// <param name="size">Ellipse width and height in local coordinates.</param>
176-
public static void DrawEllipse(this DrawingCanvas canvas, Pen pen, PointF center, SizeF size)
177-
=> canvas.Draw(pen, new EllipsePolygon(center, size));
165+
public void DrawEllipse(Pen pen, PointF center, SizeF size)
166+
=> this.Draw(pen, new EllipsePolygon(center, size));
178167

179168
/// <summary>
180169
/// Draws a pie sector outline using the provided pen.
181170
/// </summary>
182-
/// <param name="canvas">The destination canvas.</param>
183171
/// <param name="pen">Pen used to generate the pie outline.</param>
184172
/// <param name="center">Pie center point in local coordinates.</param>
185173
/// <param name="radius">Pie radii in local coordinates.</param>
186174
/// <param name="rotation">Ellipse rotation in degrees.</param>
187175
/// <param name="startAngle">Pie start angle in degrees.</param>
188176
/// <param name="sweepAngle">Pie sweep angle in degrees.</param>
189-
public static void DrawPie(this DrawingCanvas canvas, Pen pen, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle)
190-
=> canvas.Draw(pen, new Pie(center, radius, rotation, startAngle, sweepAngle));
177+
public void DrawPie(Pen pen, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle)
178+
=> this.Draw(pen, new Pie(center, radius, rotation, startAngle, sweepAngle));
191179

192180
/// <summary>
193181
/// Draws a pie sector outline using the provided pen.
194182
/// </summary>
195-
/// <param name="canvas">The destination canvas.</param>
196183
/// <param name="pen">Pen used to generate the pie outline.</param>
197184
/// <param name="center">Pie center point in local coordinates.</param>
198185
/// <param name="radius">Pie radii in local coordinates.</param>
199186
/// <param name="startAngle">Pie start angle in degrees.</param>
200187
/// <param name="sweepAngle">Pie sweep angle in degrees.</param>
201-
public static void DrawPie(this DrawingCanvas canvas, Pen pen, PointF center, SizeF radius, float startAngle, float sweepAngle)
202-
=> canvas.Draw(pen, new Pie(center, radius, startAngle, sweepAngle));
188+
public void DrawPie(Pen pen, PointF center, SizeF radius, float startAngle, float sweepAngle)
189+
=> this.Draw(pen, new Pie(center, radius, startAngle, sweepAngle));
203190

204191
/// <summary>
205192
/// Draws a rectangular outline using the provided pen.
206193
/// </summary>
207-
/// <param name="canvas">The destination canvas.</param>
208194
/// <param name="pen">Pen used to generate the rectangle outline.</param>
209195
/// <param name="region">Rectangle region to stroke.</param>
210-
public static void Draw(this DrawingCanvas canvas, Pen pen, Rectangle region)
211-
=> canvas.Draw(pen, new RectangularPolygon(region));
196+
public void Draw(Pen pen, Rectangle region)
197+
=> this.Draw(pen, new RectangularPolygon(region));
212198

213199
/// <summary>
214200
/// Draws all paths in a collection using the provided pen.
215201
/// </summary>
216-
/// <param name="canvas">The destination canvas.</param>
217202
/// <param name="pen">Pen used to generate outlines.</param>
218203
/// <param name="paths">Path collection to stroke.</param>
219-
public static void Draw(this DrawingCanvas canvas, Pen pen, IPathCollection paths)
204+
public void Draw(Pen pen, IPathCollection paths)
220205
{
221206
Guard.NotNull(paths, nameof(paths));
222207

223208
foreach (IPath path in paths)
224209
{
225-
canvas.Draw(pen, path);
210+
this.Draw(pen, path);
226211
}
227212
}
228213

229214
/// <summary>
230215
/// Draws a path outline built by the provided builder using the given pen.
231216
/// </summary>
232-
/// <param name="canvas">The destination canvas.</param>
233217
/// <param name="pen">Pen used to generate the outline fill path.</param>
234218
/// <param name="pathBuilder">The path builder describing the path to stroke.</param>
235-
public static void Draw(this DrawingCanvas canvas, Pen pen, PathBuilder pathBuilder)
219+
public void Draw(Pen pen, PathBuilder pathBuilder)
236220
{
237221
Guard.NotNull(pathBuilder, nameof(pathBuilder));
238-
canvas.Draw(pen, pathBuilder.Build());
222+
223+
this.Draw(pen, pathBuilder.Build());
239224
}
240225
}

src/ImageSharp.Drawing/Processing/DrawingCanvas.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Drawing.Processing;
1111
/// <summary>
1212
/// Represents a drawing canvas over a frame target.
1313
/// </summary>
14-
public abstract class DrawingCanvas : IDisposable
14+
public abstract partial class DrawingCanvas : IDisposable
1515
{
1616
/// <summary>
1717
/// Gets the local bounds of this canvas.

0 commit comments

Comments
 (0)