Skip to content

Commit 26cc63e

Browse files
Fix method inheritance and update samples
1 parent ef1887c commit 26cc63e

36 files changed

Lines changed: 137 additions & 158 deletions

samples/DrawShapesWithImageSharp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private static void OutputStarOutline(int points, float inner = 10, float outer
189189
float offset = outer + 10;
190190

191191
var star = new Star(offset, offset, points, inner, outer);
192-
IPath outline = star.GenerateOutline(width, jointStyle);
192+
IPath outline = star.GenerateOutline(width, jointStyle, EndCapStyle.Butt);
193193
outline.SaveImage("Stars", $"StarOutline_{points}_{jointStyle}.png");
194194
}
195195

src/ImageSharp.Drawing/Processing/IPen.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public interface IPen
3131
public JointStyle JointStyle { get; set; }
3232

3333
/// <summary>
34-
/// Gets or sets the endcap style
34+
/// Gets or sets the stroke endcap style
3535
/// </summary>
36-
public EndCapStyle EndCap { get; set; }
36+
public EndCapStyle EndCapStyle { get; set; }
3737
}
3838
}

src/ImageSharp.Drawing/Processing/Pen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ public Pen(IBrush brush, float width)
7979
public JointStyle JointStyle { get; set; }
8080

8181
/// <inheritdoc/>
82-
public EndCapStyle EndCap { get; set; }
82+
public EndCapStyle EndCapStyle { get; set; }
8383
}
8484
}

src/ImageSharp.Drawing/Processing/Processors/Drawing/DrawPathProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuratio
4949
// The global transform is applied in the FillPathProcessor.
5050
IPath outline = this.Path
5151
.Transform(Matrix3x2.CreateTranslation(0.5F, 0.5F))
52-
.GenerateOutline(this.Pen.StrokeWidth, this.Pen.StrokePattern, this.Pen.JointStyle, this.Pen.EndCap);
52+
.GenerateOutline(this.Pen.StrokeWidth, this.Pen.StrokePattern, this.Pen.JointStyle, this.Pen.EndCapStyle);
5353

5454
return new FillPathProcessor(this.Options, this.Pen.StrokeFill, outline)
5555
.CreatePixelSpecificProcessor(configuration, source, sourceRectangle);

src/ImageSharp.Drawing/Processing/Processors/Text/DrawTextProcessor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public void EndGlyph()
306306
}
307307
else
308308
{
309-
path = path.GenerateOutline(this.Pen.StrokeWidth, this.Pen.StrokePattern, this.Pen.JointStyle, this.Pen.EndCap);
309+
path = path.GenerateOutline(this.Pen.StrokeWidth, this.Pen.StrokePattern, this.Pen.JointStyle, this.Pen.EndCapStyle);
310310
}
311311

312312
renderData.OutlineMap = this.Render(path);

src/ImageSharp.Drawing/Shapes/EndCapStyle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
namespace SixLabors.ImageSharp.Drawing

src/ImageSharp.Drawing/Shapes/JointStyle.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
namespace SixLabors.ImageSharp.Drawing
55
{
66
/// <summary>
7-
/// The style we use to generate the joints when outlining.
7+
/// The style to apply to the joints when generating an outline.
88
/// </summary>
99
public enum JointStyle
1010
{
1111
/// <summary>
12-
/// Joints will generate to a long point unless the end of the point will exceed 20 times the width then we generate the joint using <see cref="JointStyle.Square"/>.
12+
/// Joints will generate to a long point unless the end of the point will exceed 20 times the width then we generate the joint using <see cref="Square"/>.
1313
/// </summary>
1414
Miter = 2,
1515

src/ImageSharp.Drawing/Shapes/OutlinePathExtensions.cs

Lines changed: 60 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9,93 +9,98 @@
99
namespace SixLabors.ImageSharp.Drawing
1010
{
1111
/// <summary>
12-
/// Path extensions to generate outlines of paths.
12+
/// Extensions to <see cref="IPath"/> that allow the generation of outlines.
1313
/// </summary>
1414
public static class OutlinePathExtensions
1515
{
1616
private const double MiterOffsetDelta = 20;
17+
private const JointStyle DefaultJointStyle = JointStyle.Square;
18+
private const EndCapStyle DefaultEndCapStyle = EndCapStyle.Butt;
1719

1820
/// <summary>
19-
/// Generates a outline of the path with alternating on and off segments based on the pattern.
21+
/// Generates an outline of the path.
2022
/// </summary>
21-
/// <param name="path">the path to outline</param>
22-
/// <param name="width">The final width outline</param>
23-
/// <param name="pattern">The pattern made of multiples of the width.</param>
24-
/// <returns>A new path representing the outline.</returns>
25-
/// <exception cref="ClipperException">Couldn't calculate offset.</exception>
26-
public static IPath GenerateOutline(this IPath path, float width, float[] pattern)
27-
=> path.GenerateOutline(width, new ReadOnlySpan<float>(pattern));
23+
/// <param name="path">The path to outline</param>
24+
/// <param name="width">The outline width.</param>
25+
/// <returns>A new <see cref="IPath"/> representing the outline.</returns>
26+
/// <exception cref="ClipperException">Thrown when an offset cannot be calculated.</exception>
27+
public static IPath GenerateOutline(this IPath path, float width) => GenerateOutline(path, width, DefaultJointStyle, DefaultEndCapStyle);
2828

2929
/// <summary>
30-
/// Generates a outline of the path with alternating on and off segments based on the pattern.
30+
/// Generates an outline of the path.
3131
/// </summary>
32-
/// <param name="path">the path to outline</param>
33-
/// <param name="width">The final width outline</param>
34-
/// <param name="pattern">The pattern made of multiples of the width.</param>
35-
/// <returns>A new path representing the outline.</returns>
36-
/// <exception cref="ClipperException">Couldn't calculate offset.</exception>
37-
public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<float> pattern)
38-
=> path.GenerateOutline(width, pattern, false);
32+
/// <param name="path">The path to outline</param>
33+
/// <param name="width">The outline width.</param>
34+
/// <param name="jointStyle">The style to apply to the joints.</param>
35+
/// <param name="endCapStyle">The style to apply to the end caps.</param>
36+
/// <returns>A new <see cref="IPath"/> representing the outline.</returns>
37+
/// <exception cref="ClipperException">Thrown when an offset cannot be calculated.</exception>
38+
public static IPath GenerateOutline(this IPath path, float width, JointStyle jointStyle, EndCapStyle endCapStyle)
39+
{
40+
ClipperOffset offset = new(MiterOffsetDelta);
41+
offset.AddPath(path, jointStyle, endCapStyle);
42+
43+
return offset.Execute(width);
44+
}
3945

4046
/// <summary>
41-
/// Generates a outline of the path with alternating on and off segments based on the pattern.
47+
/// Generates an outline of the path with alternating on and off segments based on the pattern.
4248
/// </summary>
43-
/// <param name="path">the path to outline</param>
44-
/// <param name="width">The final width outline</param>
49+
/// <param name="path">The path to outline</param>
50+
/// <param name="width">The outline width.</param>
4551
/// <param name="pattern">The pattern made of multiples of the width.</param>
46-
/// <param name="startOff">Weather the first item in the pattern is on or off.</param>
47-
/// <returns>A new path representing the outline.</returns>
48-
/// <exception cref="ClipperException">Couldn't calculate offset.</exception>
49-
public static IPath GenerateOutline(this IPath path, float width, float[] pattern, bool startOff)
50-
=> path.GenerateOutline(width, new ReadOnlySpan<float>(pattern), startOff);
52+
/// <returns>A new <see cref="IPath"/> representing the outline.</returns>
53+
/// <exception cref="ClipperException">Thrown when an offset cannot be calculated.</exception>
54+
public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<float> pattern)
55+
=> path.GenerateOutline(width, pattern, false);
5156

5257
/// <summary>
53-
/// Generates a outline of the path with alternating on and off segments based on the pattern.
58+
/// Generates an outline of the path with alternating on and off segments based on the pattern.
5459
/// </summary>
55-
/// <param name="path">the path to outline</param>
56-
/// <param name="width">The final width outline</param>
60+
/// <param name="path">The path to outline</param>
61+
/// <param name="width">The outline width.</param>
5762
/// <param name="pattern">The pattern made of multiples of the width.</param>
58-
/// <param name="startOff">Weather the first item in the pattern is on or off.</param>
59-
/// <returns>A new path representing the outline.</returns>
60-
/// <exception cref="ClipperException">Couldn't calculate offset.</exception>
63+
/// <param name="startOff">Whether the first item in the pattern is on or off.</param>
64+
/// <returns>A new <see cref="IPath"/> representing the outline.</returns>
65+
/// <exception cref="ClipperException">Thrown when an offset cannot be calculated.</exception>
6166
public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<float> pattern, bool startOff)
62-
=> GenerateOutline(path, width, pattern, startOff, JointStyle.Square, EndCapStyle.Butt);
67+
=> GenerateOutline(path, width, pattern, startOff, DefaultJointStyle, DefaultEndCapStyle);
6368

6469
/// <summary>
65-
/// Generates a outline of the path with alternating on and off segments based on the pattern.
70+
/// Generates an outline of the path with alternating on and off segments based on the pattern.
6671
/// </summary>
67-
/// <param name="path">the path to outline</param>
68-
/// <param name="width">The final width outline</param>
72+
/// <param name="path">The path to outline</param>
73+
/// <param name="width">The outline width.</param>
6974
/// <param name="pattern">The pattern made of multiples of the width.</param>
70-
/// <param name="jointStyle">The style to render the joints.</param>
71-
/// <param name="patternSectionCapStyle">The style to render between sections of the specified pattern.</param>
72-
/// <returns>A new path representing the outline.</returns>
73-
/// <exception cref="ClipperException">Couldn't calculate offset.</exception>
74-
public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<float> pattern, JointStyle jointStyle, EndCapStyle patternSectionCapStyle)
75-
=> GenerateOutline(path, width, pattern, false, jointStyle, patternSectionCapStyle);
75+
/// <param name="jointStyle">The style to apply to the joints.</param>
76+
/// <param name="endCapStyle">The style to apply to the end caps.</param>
77+
/// <returns>A new <see cref="IPath"/> representing the outline.</returns>
78+
/// <exception cref="ClipperException">Thrown when an offset cannot be calculated.</exception>
79+
public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<float> pattern, JointStyle jointStyle, EndCapStyle endCapStyle)
80+
=> GenerateOutline(path, width, pattern, false, jointStyle, endCapStyle);
7681

7782
/// <summary>
78-
/// Generates a outline of the path with alternating on and off segments based on the pattern.
83+
/// Generates an outline of the path with alternating on and off segments based on the pattern.
7984
/// </summary>
80-
/// <param name="path">the path to outline</param>
81-
/// <param name="width">The final width outline</param>
85+
/// <param name="path">The path to outline</param>
86+
/// <param name="width">The outline width.</param>
8287
/// <param name="pattern">The pattern made of multiples of the width.</param>
83-
/// <param name="startOff">Weather the first item in the pattern is on or off.</param>
84-
/// <param name="jointStyle">The style to render the joints.</param>
85-
/// <param name="patternSectionCapStyle">The style to render between sections of the specified pattern.</param>
86-
/// <returns>A new path representing the outline.</returns>
87-
/// <exception cref="ClipperException">Couldn't calculate offset.</exception>
88-
public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<float> pattern, bool startOff, JointStyle jointStyle = JointStyle.Square, EndCapStyle patternSectionCapStyle = EndCapStyle.Butt)
88+
/// <param name="startOff">Whether the first item in the pattern is on or off.</param>
89+
/// <param name="jointStyle">The style to apply to the joints.</param>
90+
/// <param name="endCapStyle">The style to apply to the end caps.</param>
91+
/// <returns>A new <see cref="IPath"/> representing the outline.</returns>
92+
/// <exception cref="ClipperException">Thrown when an offset cannot be calculated.</exception>
93+
public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<float> pattern, bool startOff, JointStyle jointStyle, EndCapStyle endCapStyle)
8994
{
9095
if (pattern.Length < 2)
9196
{
92-
return path.GenerateOutline(width, jointStyle: jointStyle);
97+
return path.GenerateOutline(width, jointStyle, endCapStyle);
9398
}
9499

95100
IEnumerable<ISimplePath> paths = path.Flatten();
96101

97-
var offset = new ClipperOffset(MiterOffsetDelta);
98-
var buffer = new List<PointF>();
102+
ClipperOffset offset = new(MiterOffsetDelta);
103+
List<PointF> buffer = new();
99104
foreach (ISimplePath p in paths)
100105
{
101106
bool online = !startOff;
@@ -130,7 +135,7 @@ public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<f
130135
// we now inset a line joining
131136
if (online)
132137
{
133-
offset.AddPath(new ReadOnlySpan<PointF>(buffer.ToArray()), jointStyle, patternSectionCapStyle);
138+
offset.AddPath(new ReadOnlySpan<PointF>(buffer.ToArray()), jointStyle, endCapStyle);
134139
}
135140

136141
online = !online;
@@ -165,7 +170,7 @@ public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<f
165170

166171
if (online)
167172
{
168-
offset.AddPath(new ReadOnlySpan<PointF>(buffer.ToArray()), jointStyle, patternSectionCapStyle);
173+
offset.AddPath(new ReadOnlySpan<PointF>(buffer.ToArray()), jointStyle, endCapStyle);
169174
}
170175

171176
online = !online;
@@ -178,31 +183,5 @@ public static IPath GenerateOutline(this IPath path, float width, ReadOnlySpan<f
178183

179184
return offset.Execute(width);
180185
}
181-
182-
/// <summary>
183-
/// Generates a solid outline of the path.
184-
/// </summary>
185-
/// <param name="path">the path to outline</param>
186-
/// <param name="width">The final width outline</param>
187-
/// <returns>A new path representing the outline.</returns>
188-
/// <exception cref="ClipperException">Couldn't calculate offset.</exception>
189-
public static IPath GenerateOutline(this IPath path, float width) => GenerateOutline(path, width, JointStyle.Square, EndCapStyle.Butt);
190-
191-
/// <summary>
192-
/// Generates a solid outline of the path.
193-
/// </summary>
194-
/// <param name="path">the path to outline</param>
195-
/// <param name="width">The final width outline</param>
196-
/// <param name="jointStyle">The style to render the joints.</param>
197-
/// <param name="endCapStyle">The style to render the end caps of open paths (ignored on closed paths).</param>
198-
/// <returns>A new path representing the outline.</returns>
199-
/// <exception cref="ClipperException">Couldn't calculate offset.</exception>
200-
public static IPath GenerateOutline(this IPath path, float width, JointStyle jointStyle = JointStyle.Square, EndCapStyle endCapStyle = EndCapStyle.Square)
201-
{
202-
var offset = new ClipperOffset(MiterOffsetDelta);
203-
offset.AddPath(path, jointStyle, endCapStyle);
204-
205-
return offset.Execute(width);
206-
}
207186
}
208187
}

src/ImageSharp.Drawing/Shapes/PolygonClipper/ClipperOffset.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class ClipperOffset
1616
private const float ScalingFactor = 1000.0f;
1717

1818
private readonly ClipperLib.ClipperOffset innerClipperOffest;
19-
private readonly object syncRoot = new object();
19+
private readonly object syncRoot = new();
2020

2121
/// <summary>
2222
/// Initializes a new instance of the <see cref="ClipperOffset"/> class.
@@ -111,19 +111,19 @@ private void AddPath(ReadOnlySpan<PointF> pathPoints, JointStyle jointStyle, End
111111
}
112112

113113
private JoinType Convert(JointStyle style)
114-
=> style switch
115-
{
116-
JointStyle.Round => JoinType.jtRound,
117-
JointStyle.Miter => JoinType.jtMiter,
118-
_ => JoinType.jtSquare,
119-
};
114+
=> style switch
115+
{
116+
JointStyle.Round => JoinType.jtRound,
117+
JointStyle.Miter => JoinType.jtMiter,
118+
_ => JoinType.jtSquare,
119+
};
120120

121121
private EndType Convert(EndCapStyle style)
122-
=> style switch
123-
{
124-
EndCapStyle.Round => EndType.etOpenRound,
125-
EndCapStyle.Square => EndType.etOpenSquare,
126-
_ => EndType.etOpenButt,
127-
};
122+
=> style switch
123+
{
124+
EndCapStyle.Round => EndType.etOpenRound,
125+
EndCapStyle.Square => EndType.etOpenSquare,
126+
_ => EndType.etOpenButt,
127+
};
128128
}
129129
}

tests/ImageSharp.Drawing.Tests/Drawing/DrawLinesTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void DrawLines_EndCapRound<TPixel>(TestImageProvider<TPixel> provider, st
7777
{
7878
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
7979
Pen pen = new Pen(color, thickness, new float[] { 3f, 3f });
80-
pen.EndCap = EndCapStyle.Round;
80+
pen.EndCapStyle = EndCapStyle.Round;
8181

8282
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
8383
}
@@ -89,7 +89,7 @@ public void DrawLines_EndCapButt<TPixel>(TestImageProvider<TPixel> provider, str
8989
{
9090
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
9191
Pen pen = new Pen(color, thickness, new float[] { 3f, 3f });
92-
pen.EndCap = EndCapStyle.Butt;
92+
pen.EndCapStyle = EndCapStyle.Butt;
9393

9494
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
9595
}
@@ -101,7 +101,7 @@ public void DrawLines_EndCapSquare<TPixel>(TestImageProvider<TPixel> provider, s
101101
{
102102
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
103103
Pen pen = new Pen(color, thickness, new float[] { 3f, 3f });
104-
pen.EndCap = EndCapStyle.Square;
104+
pen.EndCapStyle = EndCapStyle.Square;
105105

106106
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
107107
}

0 commit comments

Comments
 (0)