Skip to content

Commit a42176b

Browse files
Ensure options properties are carried and document
1 parent 40e02d3 commit a42176b

4 files changed

Lines changed: 93 additions & 44 deletions

File tree

src/ImageSharp.Drawing/Processing/Pen.cs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,68 @@ public sealed class Pen
2020
{
2121
private readonly float[] pattern;
2222

23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="Pen"/> class.
25+
/// </summary>
26+
/// <param name="strokeFill">The brush used to fill the stroke outline.</param>
2327
public Pen(Brush strokeFill)
24-
: this(strokeFill, 0)
28+
: this(strokeFill, 1)
2529
{
2630
}
2731

32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="Pen"/> class.
34+
/// </summary>
35+
/// <param name="strokeFill">The brush used to fill the stroke outline.</param>
36+
/// <param name="strokeWidth">The stroke width in px units.</param>
2837
public Pen(Brush strokeFill, float strokeWidth)
2938
: this(strokeFill, strokeWidth, Pens.EmptyPattern)
3039
{
3140
}
3241

33-
public Pen(PenOptions options)
34-
: this(options.StrokeFill, options.StrokeWidth)
42+
/// <summary>
43+
/// Initializes a new instance of the <see cref="Pen"/> class.
44+
/// </summary>
45+
/// <param name="strokeFill">The brush used to fill the stroke outline.</param>
46+
/// <param name="strokeWidth">The stroke width in px units.</param>
47+
/// <param name="strokePattern">The stroke pattern.</param>
48+
private Pen(Brush strokeFill, float strokeWidth, float[] strokePattern)
3549
{
50+
Guard.MustBeGreaterThan(strokeWidth, 0, nameof(strokeWidth));
51+
52+
this.StrokeFill = strokeFill;
53+
this.StrokeWidth = strokeWidth;
54+
this.pattern = strokePattern;
3655
}
3756

3857
/// <summary>
3958
/// Initializes a new instance of the <see cref="Pen"/> class.
4059
/// </summary>
41-
/// <param name="brush">The brush.</param>
42-
/// <param name="width">The width.</param>
43-
/// <param name="pattern">The pattern.</param>
44-
private Pen(Brush brush, float width, float[] pattern)
60+
/// <param name="options">The pen options.</param>
61+
public Pen(PenOptions options)
4562
{
46-
Guard.MustBeGreaterThan(width, 0, nameof(width));
63+
Guard.NotNull(options, nameof(options));
4764

48-
this.StrokeFill = brush;
49-
this.StrokeWidth = width;
50-
this.pattern = pattern;
65+
this.StrokeFill = options.StrokeFill;
66+
this.StrokeWidth = options.StrokeWidth;
67+
this.pattern = options.StrokePattern;
68+
this.JointStyle = options.JointStyle;
69+
this.EndCapStyle = options.EndCapStyle;
5170
}
5271

53-
/// <inheritdoc/>
72+
/// <inheritdoc cref="PenOptions.StrokeFill"/>
5473
public Brush StrokeFill { get; }
5574

56-
/// <inheritdoc/>
75+
/// <inheritdoc cref="PenOptions.StrokeWidth"/>
5776
public float StrokeWidth { get; }
5877

59-
/// <inheritdoc/>
78+
/// <inheritdoc cref="PenOptions.StrokePattern"/>
6079
public ReadOnlySpan<float> StrokePattern => this.pattern;
6180

62-
/// <inheritdoc/>
63-
public JointStyle JointStyle { get; set; }
81+
/// <inheritdoc cref="PenOptions.JointStyle"/>
82+
public JointStyle JointStyle { get; }
6483

65-
/// <inheritdoc/>
66-
public EndCapStyle EndCapStyle { get; set; }
84+
/// <inheritdoc cref="PenOptions.EndCapStyle"/>
85+
public EndCapStyle EndCapStyle { get; }
6786
}
6887
}
Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,81 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4-
using System.Collections.Generic;
5-
64
namespace SixLabors.ImageSharp.Drawing.Processing
75
{
86
/// <summary>
9-
/// Options for the Pen
7+
/// Provides a set of configurations options for pens.
108
/// </summary>
119
public class PenOptions
1210
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="PenOptions"/> class.
13+
/// </summary>
14+
/// <param name="strokeWidth">The stroke width in px units.</param>
1315
public PenOptions(float strokeWidth)
1416
: this(Color.Black, strokeWidth)
1517
{
1618
}
1719

20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="PenOptions"/> class.
22+
/// </summary>
23+
/// <param name="color">The color.</param>
24+
/// <param name="strokeWidth">The stroke width in px units.</param>
1825
public PenOptions(Color color, float strokeWidth)
1926
: this(color, strokeWidth, null)
2027
{
2128
}
2229

23-
public PenOptions(Color color, float strokeWidth, IEnumerable<float>? strokePattern)
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="PenOptions"/> class.
32+
/// </summary>
33+
/// <param name="color">The color.</param>
34+
/// <param name="strokeWidth">The stroke width in px units.</param>
35+
/// <param name="strokePattern">The stroke pattern.</param>
36+
public PenOptions(Color color, float strokeWidth, float[] strokePattern)
2437
: this(new SolidBrush(color), strokeWidth, strokePattern)
2538
{
2639
}
2740

28-
public PenOptions(Brush strokeFill, float strokeWidth, IEnumerable<float>? strokePattern)
41+
/// <summary>
42+
/// Initializes a new instance of the <see cref="PenOptions"/> class.
43+
/// </summary>
44+
/// <param name="strokeFill">The brush used to fill the stroke outline.</param>
45+
/// <param name="strokeWidth">The stroke width in px units.</param>
46+
/// <param name="strokePattern">The stroke pattern.</param>
47+
public PenOptions(Brush strokeFill, float strokeWidth, float[] strokePattern)
2948
{
49+
Guard.MustBeGreaterThan(strokeWidth, 0, nameof(strokeWidth));
50+
3051
this.StrokeFill = strokeFill;
3152
this.StrokeWidth = strokeWidth;
3253
this.StrokePattern = strokePattern;
3354
}
3455

35-
public Brush StrokeFill { get; set; } // defaults to black solid brush when undefined
56+
/// <summary>
57+
/// Gets the brush used to fill the stroke outline. Defaults to <see cref="SolidBrush"/>.
58+
/// </summary>
59+
public Brush StrokeFill { get; }
3660

37-
public float StrokeWidth { get; set; }
61+
/// <summary>
62+
/// Gets the stroke width in px units. Defaults to 1px.
63+
/// </summary>
64+
public float StrokeWidth { get; }
3865

39-
IEnumerable<float>? StrokePattern { get; set; }
66+
/// <summary>
67+
/// Gets the stroke pattern.
68+
/// </summary>
69+
public float[] StrokePattern { get; }
4070

41-
public JointStyle? JointStyle { get; set; }
71+
/// <summary>
72+
/// Gets or sets the joint style.
73+
/// </summary>
74+
public JointStyle JointStyle { get; set; }
4275

43-
public EndCapStyle? EndCap { get; set; }
76+
/// <summary>
77+
/// Gets or sets the end cap style.
78+
/// </summary>
79+
public EndCapStyle EndCapStyle { get; set; }
4480
}
4581
}

src/ImageSharp.Drawing/Shapes/JointStyle.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ namespace SixLabors.ImageSharp.Drawing
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="Square"/>.
12+
/// Joints are squared off 1 width distance from the corner.
1313
/// </summary>
14-
Miter = 2,
14+
Square = 0,
1515

1616
/// <summary>
1717
/// Rounded joints. Joints generate with a rounded profile.
1818
/// </summary>
1919
Round = 1,
2020

2121
/// <summary>
22-
/// Joints are squared off 1 width distance from the corner.
22+
/// 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"/>.
2323
/// </summary>
24-
Square = 0
24+
Miter = 2
2525
}
2626
}

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public void DrawLines_EndCapRound<TPixel>(TestImageProvider<TPixel> provider, st
7676
where TPixel : unmanaged, IPixel<TPixel>
7777
{
7878
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
79-
Pen pen = new Pen(new PenOptions(color, thickness, new float[] { 3f, 3f }));
80-
pen.EndCapStyle = EndCapStyle.Round;
79+
Pen pen = new Pen(new PenOptions(color, thickness, new float[] { 3f, 3f }) { EndCapStyle = EndCapStyle.Round });
8180

8281
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
8382
}
@@ -88,8 +87,7 @@ public void DrawLines_EndCapButt<TPixel>(TestImageProvider<TPixel> provider, str
8887
where TPixel : unmanaged, IPixel<TPixel>
8988
{
9089
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
91-
Pen pen = new Pen(new PenOptions(color, thickness, new float[] { 3f, 3f }));
92-
pen.EndCapStyle = EndCapStyle.Butt;
90+
Pen pen = new Pen(new PenOptions(color, thickness, new float[] { 3f, 3f }) { EndCapStyle = EndCapStyle.Butt });
9391

9492
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
9593
}
@@ -100,8 +98,7 @@ public void DrawLines_EndCapSquare<TPixel>(TestImageProvider<TPixel> provider, s
10098
where TPixel : unmanaged, IPixel<TPixel>
10199
{
102100
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
103-
Pen pen = new Pen(new PenOptions(color, thickness, new float[] { 3f, 3f }));
104-
pen.EndCapStyle = EndCapStyle.Square;
101+
Pen pen = new Pen(new PenOptions(color, thickness, new float[] { 3f, 3f }) { EndCapStyle = EndCapStyle.Square });
105102

106103
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
107104
}
@@ -112,8 +109,7 @@ public void DrawLines_JointStyleRound<TPixel>(TestImageProvider<TPixel> provider
112109
where TPixel : unmanaged, IPixel<TPixel>
113110
{
114111
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
115-
var pen = new Pen(new PenOptions(color, thickness));
116-
pen.JointStyle = JointStyle.Round;
112+
var pen = new Pen(new PenOptions(color, thickness) { JointStyle = JointStyle.Round });
117113

118114
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
119115
}
@@ -124,8 +120,7 @@ public void DrawLines_JointStyleSquare<TPixel>(TestImageProvider<TPixel> provide
124120
where TPixel : unmanaged, IPixel<TPixel>
125121
{
126122
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
127-
var pen = new Pen(new PenOptions(color, thickness));
128-
pen.JointStyle = JointStyle.Square;
123+
var pen = new Pen(new PenOptions(color, thickness) { JointStyle = JointStyle.Square });
129124

130125
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
131126
}
@@ -136,8 +131,7 @@ public void DrawLines_JointStyleMiter<TPixel>(TestImageProvider<TPixel> provider
136131
where TPixel : unmanaged, IPixel<TPixel>
137132
{
138133
Color color = TestUtils.GetColorByName(colorName).WithAlpha(alpha);
139-
var pen = new Pen(new PenOptions(color, thickness));
140-
pen.JointStyle = JointStyle.Miter;
134+
var pen = new Pen(new PenOptions(color, thickness) { JointStyle = JointStyle.Miter });
141135

142136
DrawLinesImpl(provider, colorName, alpha, thickness, antialias, pen);
143137
}

0 commit comments

Comments
 (0)