|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +namespace SixLabors.ImageSharp.Drawing.Processing; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Convenience shape helpers that build paths and forward to the core <see cref="IDrawingCanvas"/> fill and draw primitives. |
| 8 | +/// </summary> |
| 9 | +public static class DrawingCanvasShapeExtensions |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Fills a local region using the given brush. |
| 13 | + /// </summary> |
| 14 | + /// <param name="canvas">The destination canvas.</param> |
| 15 | + /// <param name="brush">Brush used to shade destination pixels.</param> |
| 16 | + /// <param name="region">Region to fill in local coordinates.</param> |
| 17 | + public static void Fill(this IDrawingCanvas canvas, Brush brush, Rectangle region) |
| 18 | + => canvas.Fill(brush, new RectangularPolygon(region.X, region.Y, region.Width, region.Height)); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Fills all paths in a collection using the given brush. |
| 22 | + /// </summary> |
| 23 | + /// <param name="canvas">The destination canvas.</param> |
| 24 | + /// <param name="brush">Brush used to shade covered pixels.</param> |
| 25 | + /// <param name="paths">Path collection to fill.</param> |
| 26 | + public static void Fill(this IDrawingCanvas canvas, Brush brush, IPathCollection paths) |
| 27 | + { |
| 28 | + Guard.NotNull(paths, nameof(paths)); |
| 29 | + |
| 30 | + foreach (IPath path in paths) |
| 31 | + { |
| 32 | + canvas.Fill(brush, path); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Fills a path built by the provided builder using the given brush. |
| 38 | + /// </summary> |
| 39 | + /// <param name="canvas">The destination canvas.</param> |
| 40 | + /// <param name="brush">Brush used to shade covered pixels.</param> |
| 41 | + /// <param name="pathBuilder">The path builder describing the fill region.</param> |
| 42 | + public static void Fill(this IDrawingCanvas canvas, Brush brush, PathBuilder pathBuilder) |
| 43 | + { |
| 44 | + Guard.NotNull(pathBuilder, nameof(pathBuilder)); |
| 45 | + canvas.Fill(brush, pathBuilder.Build()); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Fills an ellipse using the provided brush. |
| 50 | + /// </summary> |
| 51 | + /// <param name="canvas">The destination canvas.</param> |
| 52 | + /// <param name="brush">Brush used to shade covered pixels.</param> |
| 53 | + /// <param name="center">Ellipse center point in local coordinates.</param> |
| 54 | + /// <param name="size">Ellipse width and height in local coordinates.</param> |
| 55 | + public static void FillEllipse(this IDrawingCanvas canvas, Brush brush, PointF center, SizeF size) |
| 56 | + => canvas.Fill(brush, new EllipsePolygon(center, size)); |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Fills the closed arc shape produced by joining the arc endpoints with a straight line. |
| 60 | + /// </summary> |
| 61 | + /// <param name="canvas">The destination canvas.</param> |
| 62 | + /// <param name="brush">Brush used to shade covered pixels.</param> |
| 63 | + /// <param name="center">Arc center point in local coordinates.</param> |
| 64 | + /// <param name="radius">Arc radii in local coordinates.</param> |
| 65 | + /// <param name="rotation">Ellipse rotation in degrees.</param> |
| 66 | + /// <param name="startAngle">Arc start angle in degrees.</param> |
| 67 | + /// <param name="sweepAngle">Arc sweep angle in degrees.</param> |
| 68 | + public static void FillArc(this IDrawingCanvas canvas, Brush brush, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle) |
| 69 | + => canvas.Fill(brush, new Path(new ArcLineSegment(center, radius, rotation, startAngle, sweepAngle))); |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Fills a pie sector using the provided brush. |
| 73 | + /// </summary> |
| 74 | + /// <param name="canvas">The destination canvas.</param> |
| 75 | + /// <param name="brush">Brush used to shade covered pixels.</param> |
| 76 | + /// <param name="center">Pie center point in local coordinates.</param> |
| 77 | + /// <param name="radius">Pie radii in local coordinates.</param> |
| 78 | + /// <param name="rotation">Ellipse rotation in degrees.</param> |
| 79 | + /// <param name="startAngle">Pie start angle in degrees.</param> |
| 80 | + /// <param name="sweepAngle">Pie sweep angle in degrees.</param> |
| 81 | + public static void FillPie(this IDrawingCanvas canvas, Brush brush, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle) |
| 82 | + => canvas.Fill(brush, new Pie(center, radius, rotation, startAngle, sweepAngle)); |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Fills a pie sector using the provided brush. |
| 86 | + /// </summary> |
| 87 | + /// <param name="canvas">The destination canvas.</param> |
| 88 | + /// <param name="brush">Brush used to shade covered pixels.</param> |
| 89 | + /// <param name="center">Pie center point in local coordinates.</param> |
| 90 | + /// <param name="radius">Pie radii in local coordinates.</param> |
| 91 | + /// <param name="startAngle">Pie start angle in degrees.</param> |
| 92 | + /// <param name="sweepAngle">Pie sweep angle in degrees.</param> |
| 93 | + public static void FillPie(this IDrawingCanvas canvas, Brush brush, PointF center, SizeF radius, float startAngle, float sweepAngle) |
| 94 | + => canvas.Fill(brush, new Pie(center, radius, startAngle, sweepAngle)); |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Draws an arc outline using the provided pen. |
| 98 | + /// </summary> |
| 99 | + /// <param name="canvas">The destination canvas.</param> |
| 100 | + /// <param name="pen">Pen used to generate the arc outline.</param> |
| 101 | + /// <param name="center">Arc center point in local coordinates.</param> |
| 102 | + /// <param name="radius">Arc radii in local coordinates.</param> |
| 103 | + /// <param name="rotation">Ellipse rotation in degrees.</param> |
| 104 | + /// <param name="startAngle">Arc start angle in degrees.</param> |
| 105 | + /// <param name="sweepAngle">Arc sweep angle in degrees.</param> |
| 106 | + public static void DrawArc(this IDrawingCanvas canvas, Pen pen, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle) |
| 107 | + => canvas.Draw(pen, new Path(new ArcLineSegment(center, radius, rotation, startAngle, sweepAngle))); |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Draws a cubic bezier outline using the provided pen. |
| 111 | + /// </summary> |
| 112 | + /// <param name="canvas">The destination canvas.</param> |
| 113 | + /// <param name="pen">Pen used to generate the bezier outline.</param> |
| 114 | + /// <param name="points">Bezier control points.</param> |
| 115 | + public static void DrawBezier(this IDrawingCanvas canvas, Pen pen, params PointF[] points) |
| 116 | + { |
| 117 | + Guard.NotNull(points, nameof(points)); |
| 118 | + canvas.Draw(pen, new Path(new CubicBezierLineSegment(points))); |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Draws an ellipse outline using the provided pen. |
| 123 | + /// </summary> |
| 124 | + /// <param name="canvas">The destination canvas.</param> |
| 125 | + /// <param name="pen">Pen used to generate the ellipse outline.</param> |
| 126 | + /// <param name="center">Ellipse center point in local coordinates.</param> |
| 127 | + /// <param name="size">Ellipse width and height in local coordinates.</param> |
| 128 | + public static void DrawEllipse(this IDrawingCanvas canvas, Pen pen, PointF center, SizeF size) |
| 129 | + => canvas.Draw(pen, new EllipsePolygon(center, size)); |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Draws a pie sector outline using the provided pen. |
| 133 | + /// </summary> |
| 134 | + /// <param name="canvas">The destination canvas.</param> |
| 135 | + /// <param name="pen">Pen used to generate the pie outline.</param> |
| 136 | + /// <param name="center">Pie center point in local coordinates.</param> |
| 137 | + /// <param name="radius">Pie radii in local coordinates.</param> |
| 138 | + /// <param name="rotation">Ellipse rotation in degrees.</param> |
| 139 | + /// <param name="startAngle">Pie start angle in degrees.</param> |
| 140 | + /// <param name="sweepAngle">Pie sweep angle in degrees.</param> |
| 141 | + public static void DrawPie(this IDrawingCanvas canvas, Pen pen, PointF center, SizeF radius, float rotation, float startAngle, float sweepAngle) |
| 142 | + => canvas.Draw(pen, new Pie(center, radius, rotation, startAngle, sweepAngle)); |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Draws a pie sector outline using the provided pen. |
| 146 | + /// </summary> |
| 147 | + /// <param name="canvas">The destination canvas.</param> |
| 148 | + /// <param name="pen">Pen used to generate the pie outline.</param> |
| 149 | + /// <param name="center">Pie center point in local coordinates.</param> |
| 150 | + /// <param name="radius">Pie radii in local coordinates.</param> |
| 151 | + /// <param name="startAngle">Pie start angle in degrees.</param> |
| 152 | + /// <param name="sweepAngle">Pie sweep angle in degrees.</param> |
| 153 | + public static void DrawPie(this IDrawingCanvas canvas, Pen pen, PointF center, SizeF radius, float startAngle, float sweepAngle) |
| 154 | + => canvas.Draw(pen, new Pie(center, radius, startAngle, sweepAngle)); |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Draws a rectangular outline using the provided pen. |
| 158 | + /// </summary> |
| 159 | + /// <param name="canvas">The destination canvas.</param> |
| 160 | + /// <param name="pen">Pen used to generate the rectangle outline.</param> |
| 161 | + /// <param name="region">Rectangle region to stroke.</param> |
| 162 | + public static void Draw(this IDrawingCanvas canvas, Pen pen, Rectangle region) |
| 163 | + => canvas.Draw(pen, new RectangularPolygon(region.X, region.Y, region.Width, region.Height)); |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// Draws all paths in a collection using the provided pen. |
| 167 | + /// </summary> |
| 168 | + /// <param name="canvas">The destination canvas.</param> |
| 169 | + /// <param name="pen">Pen used to generate outlines.</param> |
| 170 | + /// <param name="paths">Path collection to stroke.</param> |
| 171 | + public static void Draw(this IDrawingCanvas canvas, Pen pen, IPathCollection paths) |
| 172 | + { |
| 173 | + Guard.NotNull(paths, nameof(paths)); |
| 174 | + |
| 175 | + foreach (IPath path in paths) |
| 176 | + { |
| 177 | + canvas.Draw(pen, path); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + /// <summary> |
| 182 | + /// Draws a path outline built by the provided builder using the given pen. |
| 183 | + /// </summary> |
| 184 | + /// <param name="canvas">The destination canvas.</param> |
| 185 | + /// <param name="pen">Pen used to generate the outline fill path.</param> |
| 186 | + /// <param name="pathBuilder">The path builder describing the path to stroke.</param> |
| 187 | + public static void Draw(this IDrawingCanvas canvas, Pen pen, PathBuilder pathBuilder) |
| 188 | + { |
| 189 | + Guard.NotNull(pathBuilder, nameof(pathBuilder)); |
| 190 | + canvas.Draw(pen, pathBuilder.Build()); |
| 191 | + } |
| 192 | +} |
0 commit comments