|
1 | 1 | // Copyright (c) Six Labors. |
2 | 2 | // Licensed under the Apache License, Version 2.0. |
3 | 3 |
|
4 | | -using System; |
5 | | -using System.Diagnostics.CodeAnalysis; |
6 | | -using System.Linq; |
7 | | - |
8 | 4 | namespace SixLabors.ImageSharp.Drawing.Processing |
9 | 5 | { |
10 | 6 | /// <summary> |
11 | | - /// Provides a pen that can apply a pattern to a line with a set brush and thickness |
| 7 | + /// Defines a pen that can apply a pattern to a line with a set brush and thickness |
12 | 8 | /// </summary> |
13 | 9 | /// <remarks> |
14 | | - /// The pattern will be in to the form of new float[]{ 1f, 2f, 0.5f} this will be |
15 | | - /// converted into a pattern that is 3.5 times longer that the width with 3 sections |
16 | | - /// section 1 will be width long (making a square) and will be filled by the brush |
17 | | - /// section 2 will be width * 2 long and will be empty |
18 | | - /// section 3 will be width/2 long and will be filled |
19 | | - /// the pattern will immediately repeat without gap. |
| 10 | + /// The pattern will be in to the form of <code>new float[]{ 1f, 2f, 0.5f}</code> this will be |
| 11 | + /// converted into a pattern that is 3.5 times longer that the width with 3 sections. |
| 12 | + /// <list type="bullet"> |
| 13 | + /// <item>Section 1 will be width long (making a square) and will be filled by the brush.</item> |
| 14 | + /// <item>Section 2 will be width * 2 long and will be empty.</item> |
| 15 | + /// <item>ection 3 will be width/2 long and will be filled.</item> |
| 16 | + /// </list> |
| 17 | + /// The pattern will immediately repeat without gap. |
20 | 18 | /// </remarks> |
21 | | - public class PatternPen : IPen |
| 19 | + public class PatternPen : Pen |
22 | 20 | { |
23 | | - private readonly float[] pattern; |
24 | | - |
25 | 21 | /// <summary> |
26 | 22 | /// Initializes a new instance of the <see cref="PatternPen"/> class. |
27 | 23 | /// </summary> |
28 | 24 | /// <param name="color">The color.</param> |
29 | | - /// <param name="width">The width.</param> |
30 | | - /// <param name="pattern">The pattern.</param> |
31 | | - public PatternPen(Color color, float width, float[] pattern) |
32 | | - : this(new SolidBrush(color), width, pattern) |
33 | | - { |
34 | | - } |
35 | | - |
36 | | - /// <summary> |
37 | | - /// Initializes a new instance of the <see cref="PatternPen"/> class. |
38 | | - /// </summary> |
39 | | - /// <param name="brush">The brush.</param> |
40 | | - /// <param name="width">The width.</param> |
41 | | - /// <param name="pattern">The pattern.</param> |
42 | | - public PatternPen(IBrush brush, float width, float[] pattern) |
43 | | - { |
44 | | - Guard.MustBeGreaterThan(width, 0, nameof(width)); |
45 | | - |
46 | | - this.StrokeFill = brush; |
47 | | - this.StrokeWidth = width; |
48 | | - this.pattern = pattern; |
49 | | - } |
50 | | - |
51 | | - /// <inheritdoc/> |
52 | | - public IBrush StrokeFill { get; } |
53 | | - |
54 | | - /// <summary> |
55 | | - /// Gets the width to apply to the stroke |
56 | | - /// </summary> |
57 | | - public float? StrokeWidth { get; } |
58 | | - |
59 | | - /// <summary> |
60 | | - /// Gets the stoke pattern. |
61 | | - /// </summary> |
62 | | - public ReadOnlySpan<float> StrokePattern => this.pattern; |
63 | | - |
64 | | - /// <summary> |
65 | | - /// Gets or sets the stroke joint style |
66 | | - /// </summary> |
67 | | - public JointStyle JointStyle { get; set; } |
68 | | - |
69 | | - /// <summary> |
70 | | - /// Gets or sets the stroke endcap style |
71 | | - /// </summary> |
72 | | - public EndCapStyle EndCapStyle { get; set; } |
73 | | - |
74 | | - /// <inheritdoc/> |
75 | | - public bool Equals(IPen other) |
76 | | - { |
77 | | - if (other is PatternPen p) |
78 | | - { |
79 | | - return p.StrokeWidth == this.StrokeWidth && |
80 | | - p.JointStyle == this.JointStyle && |
81 | | - p.EndCapStyle == this.EndCapStyle && |
82 | | - p.StrokeFill.Equals(this.StrokeFill) && |
83 | | - Enumerable.SequenceEqual(p.pattern, this.pattern); |
84 | | - } |
85 | | - |
86 | | - return false; |
87 | | - } |
88 | | - |
89 | | - /// <inheritdoc /> |
90 | | - public IPath GeneratePath(IPath path, float? defaultWidth = null) |
91 | | - => path.GenerateOutline(this.StrokeWidth ?? defaultWidth ?? 1, this.StrokePattern, this.JointStyle, this.EndCapStyle); |
92 | | - } |
93 | | - |
94 | | - /// <summary> |
95 | | - /// Provides a pen that can apply a pattern to a line with a set brush and thickness |
96 | | - /// </summary> |
97 | | - /// <remarks> |
98 | | - /// The pattern will be in to the form of new float[]{ 1f, 2f, 0.5f} this will be |
99 | | - /// converted into a pattern that is 3.5 times longer that the width with 3 sections |
100 | | - /// section 1 will be width long (making a square) and will be filled by the brush |
101 | | - /// section 2 will be width * 2 long and will be empty |
102 | | - /// section 3 will be width/2 long and will be filled |
103 | | - /// the pattern will immediately repeat without gap. |
104 | | - /// </remarks> |
105 | | - public sealed class Pen |
106 | | - { |
107 | | - private readonly float[] pattern; |
108 | | - |
109 | | - /// <summary> |
110 | | - /// Initializes a new instance of the <see cref="PatternPen"/> class. |
111 | | - /// </summary> |
112 | | - /// <param name="strokeFill">The brush used to fill the stroke outline.</param> |
113 | | - public Pen(Brush strokeFill) |
114 | | - : this(strokeFill, 1) |
| 25 | + /// <param name="strokePattern">The stroke pattern.</param> |
| 26 | + public PatternPen(Color color, float[] strokePattern) |
| 27 | + : base(new SolidBrush(color), 1, strokePattern) |
115 | 28 | { |
116 | 29 | } |
117 | 30 |
|
118 | 31 | /// <summary> |
119 | 32 | /// Initializes a new instance of the <see cref="PatternPen"/> class. |
120 | 33 | /// </summary> |
121 | | - /// <param name="strokeFill">The brush used to fill the stroke outline.</param> |
| 34 | + /// <param name="color">The color.</param> |
122 | 35 | /// <param name="strokeWidth">The stroke width in px units.</param> |
123 | | - public Pen(Brush strokeFill, float strokeWidth) |
124 | | - : this(strokeFill, strokeWidth, Pens.EmptyPattern) |
| 36 | + /// <param name="strokePattern">The stroke pattern.</param> |
| 37 | + public PatternPen(Color color, float strokeWidth, float[] strokePattern) |
| 38 | + : base(new SolidBrush(color), strokeWidth, strokePattern) |
125 | 39 | { |
126 | 40 | } |
127 | 41 |
|
128 | 42 | /// <summary> |
129 | | - /// Initializes a new instance of the <see cref="Pen"/> class. |
| 43 | + /// Initializes a new instance of the <see cref="PatternPen"/> class. |
130 | 44 | /// </summary> |
131 | 45 | /// <param name="strokeFill">The brush used to fill the stroke outline.</param> |
132 | 46 | /// <param name="strokeWidth">The stroke width in px units.</param> |
133 | 47 | /// <param name="strokePattern">The stroke pattern.</param> |
134 | | - private Pen(Brush strokeFill, float strokeWidth, float[] strokePattern) |
| 48 | + public PatternPen(Brush strokeFill, float strokeWidth, float[] strokePattern) |
| 49 | + : base(strokeFill, strokeWidth, strokePattern) |
135 | 50 | { |
136 | | - Guard.MustBeGreaterThan(strokeWidth, 0, nameof(strokeWidth)); |
137 | | - |
138 | | - this.StrokeFill = strokeFill; |
139 | | - this.StrokeWidth = strokeWidth; |
140 | | - this.pattern = strokePattern; |
141 | 51 | } |
142 | 52 |
|
143 | 53 | /// <summary> |
144 | | - /// Initializes a new instance of the <see cref="Pen"/> class. |
| 54 | + /// Initializes a new instance of the <see cref="PatternPen"/> class. |
145 | 55 | /// </summary> |
146 | 56 | /// <param name="options">The pen options.</param> |
147 | | - public Pen(PenOptions options) |
| 57 | + public PatternPen(PenOptions options) |
| 58 | + : base(options) |
148 | 59 | { |
149 | | - Guard.NotNull(options, nameof(options)); |
150 | | - |
151 | | - this.StrokeFill = options.StrokeFill; |
152 | | - this.StrokeWidth = options.StrokeWidth; |
153 | | - this.pattern = options.StrokePattern; |
154 | | - this.JointStyle = options.JointStyle; |
155 | | - this.EndCapStyle = options.EndCapStyle; |
156 | 60 | } |
157 | 61 |
|
158 | | - /// <inheritdoc cref="PenOptions.StrokeFill"/> |
159 | | - public Brush StrokeFill { get; } |
160 | | - |
161 | | - /// <inheritdoc cref="PenOptions.StrokeWidth"/> |
162 | | - public float StrokeWidth { get; } |
163 | | - |
164 | | - /// <inheritdoc cref="PenOptions.StrokePattern"/> |
165 | | - public ReadOnlySpan<float> StrokePattern => this.pattern; |
| 62 | + /// <inheritdoc/> |
| 63 | + public override bool Equals(Pen other) |
| 64 | + { |
| 65 | + if (other is PatternPen) |
| 66 | + { |
| 67 | + return base.Equals(other); |
| 68 | + } |
166 | 69 |
|
167 | | - /// <inheritdoc cref="PenOptions.JointStyle"/> |
168 | | - public JointStyle JointStyle { get; } |
| 70 | + return false; |
| 71 | + } |
169 | 72 |
|
170 | | - /// <inheritdoc cref="PenOptions.EndCapStyle"/> |
171 | | - public EndCapStyle EndCapStyle { get; } |
| 73 | + /// <inheritdoc /> |
| 74 | + public override IPath GeneratePath(IPath path, float strokeWidth) |
| 75 | + => path.GenerateOutline(strokeWidth, this.StrokePattern, this.JointStyle, this.EndCapStyle); |
172 | 76 | } |
173 | 77 | } |
0 commit comments