1 | using System;
|
---|
2 | using System.Globalization;
|
---|
3 |
|
---|
4 | namespace SharpVectors.Dom.Svg
|
---|
5 | {
|
---|
6 | /// <summary>
|
---|
7 | /// Stores an ordered pair of floating-point numbers, typically the width
|
---|
8 | /// and height of a rectangle.
|
---|
9 | /// </summary>
|
---|
10 | [Serializable]
|
---|
11 | public struct SvgSizeF : IEquatable<SvgSizeF>
|
---|
12 | {
|
---|
13 | #region Private Fields
|
---|
14 |
|
---|
15 | /// <summary>
|
---|
16 | /// Initializes a new instance of the <see cref="SvgSizeF"/> class.
|
---|
17 | /// </summary>
|
---|
18 | public static readonly SvgSizeF Empty = new SvgSizeF();
|
---|
19 |
|
---|
20 | private float _width;
|
---|
21 | private float _height;
|
---|
22 |
|
---|
23 | #endregion
|
---|
24 |
|
---|
25 | #region Constructors and Destructor
|
---|
26 |
|
---|
27 | /// <summary>Initializes a new instance of the <see cref="SvgSizeF"/> class from the specified dimensions.</summary>
|
---|
28 | /// <param name="width">The width component of the new <see cref="SvgSizeF"/>. </param>
|
---|
29 | /// <param name="height">The height component of the new <see cref="SvgSizeF"/>. </param>
|
---|
30 | public SvgSizeF(float width, float height)
|
---|
31 | {
|
---|
32 | _width = width;
|
---|
33 | _height = height;
|
---|
34 | }
|
---|
35 |
|
---|
36 | /// <summary>Initializes a new instance of the <see cref="SvgSizeF"/> class from the specified existing <see cref="SvgSizeF"/>.</summary>
|
---|
37 | /// <param name="size">The <see cref="SvgSizeF"/> from which to create the new <see cref="SvgSizeF"/>. </param>
|
---|
38 | public SvgSizeF(SvgSizeF size)
|
---|
39 | {
|
---|
40 | _width = size._width;
|
---|
41 | _height = size._height;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /// <summary>Initializes a new instance of the <see cref="SvgSizeF"/> class from the specified <see cref="SvgPointF"/>.</summary>
|
---|
45 | /// <param name="pt">The <see cref="SvgPointF"/> from which to initialize this <see cref="SvgSizeF"/>. </param>
|
---|
46 | public SvgSizeF(SvgPointF pt)
|
---|
47 | {
|
---|
48 | _width = pt.X;
|
---|
49 | _height = pt.Y;
|
---|
50 | }
|
---|
51 |
|
---|
52 | #endregion
|
---|
53 |
|
---|
54 | #region Public Operators
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// Adds the width and height of one <see cref="SvgSizeF"/> structure
|
---|
58 | /// to the width and height of another <see cref="SvgSizeF"/> structure.
|
---|
59 | /// </summary>
|
---|
60 | /// <param name="sz1">The first <see cref="SvgSizeF"/> to add.</param>
|
---|
61 | /// <param name="sz2">The second <see cref="SvgSizeF"/> to add.</param>
|
---|
62 | /// <returns>
|
---|
63 | /// A <see cref="SvgSizeF"/> structure that is the result of the
|
---|
64 | /// addition operation.
|
---|
65 | /// </returns>
|
---|
66 | public static SvgSizeF operator +(SvgSizeF sz1, SvgSizeF sz2)
|
---|
67 | {
|
---|
68 | return SvgSizeF.Add(sz1, sz2);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Subtracts the width and height of one <see cref="SvgSizeF"/>
|
---|
73 | /// structure from the width and height of another
|
---|
74 | /// <see cref="SvgSizeF"/> structure.
|
---|
75 | /// </summary>
|
---|
76 | /// <param name="sz1">
|
---|
77 | /// The <see cref="SvgSizeF"/> on the left side of the subtraction
|
---|
78 | /// operator.
|
---|
79 | /// </param>
|
---|
80 | /// <param name="sz2">
|
---|
81 | /// The <see cref="SvgSizeF"/> on the right side of the subtraction
|
---|
82 | /// operator.
|
---|
83 | /// </param>
|
---|
84 | /// <returns>
|
---|
85 | /// A <see cref="SvgSizeF"/> that is the result of the subtraction
|
---|
86 | /// operation.
|
---|
87 | /// </returns>
|
---|
88 | public static SvgSizeF operator -(SvgSizeF sz1, SvgSizeF sz2)
|
---|
89 | {
|
---|
90 | return SvgSizeF.Subtract(sz1, sz2);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /// <summary>
|
---|
94 | /// Tests whether two <see cref="SvgSizeF"/> structures are equal.
|
---|
95 | /// </summary>
|
---|
96 | /// <param name="sz1">
|
---|
97 | /// The <see cref="SvgSizeF"/> structure on the left side of the
|
---|
98 | /// equality operator.
|
---|
99 | /// </param>
|
---|
100 | /// <param name="sz2">
|
---|
101 | /// The <see cref="SvgSizeF"/> structure on the right of the equality
|
---|
102 | /// operator.
|
---|
103 | /// </param>
|
---|
104 | /// <returns>
|
---|
105 | /// This operator returns <see langword="true"/> if <paramref name="sz1"/>
|
---|
106 | /// and <paramref name="sz2"/> have equal width and height; otherwise,
|
---|
107 | /// <see langword="false"/>.
|
---|
108 | /// </returns>
|
---|
109 | public static bool operator ==(SvgSizeF sz1, SvgSizeF sz2)
|
---|
110 | {
|
---|
111 | if (sz1.Width == sz2.Width)
|
---|
112 | {
|
---|
113 | return (sz1.Height == sz2.Height);
|
---|
114 | }
|
---|
115 |
|
---|
116 | return false;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /// <summary>
|
---|
120 | /// Tests whether two <see cref="SvgSizeF"/> structures are different.
|
---|
121 | /// </summary>
|
---|
122 | /// <param name="sz1">
|
---|
123 | /// The <see cref="SvgSizeF"/> structure on the left of the inequality
|
---|
124 | /// operator.
|
---|
125 | /// </param>
|
---|
126 | /// <param name="sz2">
|
---|
127 | /// The <see cref="SvgSizeF"/> structure on the right of the inequality
|
---|
128 | /// operator.
|
---|
129 | /// </param>
|
---|
130 | /// <returns>
|
---|
131 | /// This operator returns <see langword="true"/> if
|
---|
132 | /// <paramref name="sz1"/> and <paramref name="sz2"/> differ either
|
---|
133 | /// in width or height; <see langword="false"/> if <paramref name="sz1"/>
|
---|
134 | /// and <paramref name="sz2"/> are equal.
|
---|
135 | /// </returns>
|
---|
136 | public static bool operator !=(SvgSizeF sz1, SvgSizeF sz2)
|
---|
137 | {
|
---|
138 | return !(sz1 == sz2);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /// <summary>
|
---|
142 | /// This converts the specified <see cref="SvgSizeF"/> to a
|
---|
143 | /// <see cref="SvgPointF"/>.
|
---|
144 | /// </summary>
|
---|
145 | /// <param name="size">
|
---|
146 | /// The <see cref="SvgSizeF"/> structure to be converted.
|
---|
147 | /// </param>
|
---|
148 | /// <returns>
|
---|
149 | /// The <see cref="SvgPointF"/> structure specifying the result of the
|
---|
150 | /// conversion.
|
---|
151 | /// </returns>
|
---|
152 | public static explicit operator SvgPointF(SvgSizeF size)
|
---|
153 | {
|
---|
154 | return new SvgPointF(size.Width, size.Height);
|
---|
155 | }
|
---|
156 |
|
---|
157 | #endregion
|
---|
158 |
|
---|
159 | #region Public Properties
|
---|
160 |
|
---|
161 | /// <summary>
|
---|
162 | /// Gets a value indicating whether this <see cref="SvgSizeF"/> has zero
|
---|
163 | /// width and height.
|
---|
164 | /// </summary>
|
---|
165 | /// <value>
|
---|
166 | /// This property returns <see langword="true"/> when this
|
---|
167 | /// <see cref="SvgSizeF"/> has both a width and height of zero;
|
---|
168 | /// otherwise, <see langword="false"/>.
|
---|
169 | /// </value>
|
---|
170 | public bool IsEmpty
|
---|
171 | {
|
---|
172 | get
|
---|
173 | {
|
---|
174 | if (_width == 0.0)
|
---|
175 | {
|
---|
176 | return (_height == 0.0);
|
---|
177 | }
|
---|
178 |
|
---|
179 | return false;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /// <summary>
|
---|
184 | /// Gets or sets the horizontal component of this <see cref="SvgSizeF"/>.
|
---|
185 | /// </summary>
|
---|
186 | /// <value>
|
---|
187 | /// The horizontal component of this <see cref="SvgSizeF"/>.
|
---|
188 | /// </value>
|
---|
189 | public float Width
|
---|
190 | {
|
---|
191 | get
|
---|
192 | {
|
---|
193 | return _width;
|
---|
194 | }
|
---|
195 |
|
---|
196 | set
|
---|
197 | {
|
---|
198 | _width = value;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | /// <summary>
|
---|
203 | /// Gets or sets the vertical component of this <see cref="SvgSizeF"/>.
|
---|
204 | /// </summary>
|
---|
205 | /// <value>
|
---|
206 | /// The vertical component of this <see cref="SvgSizeF"/>.
|
---|
207 | /// </value>
|
---|
208 | public float Height
|
---|
209 | {
|
---|
210 | get
|
---|
211 | {
|
---|
212 | return _height;
|
---|
213 | }
|
---|
214 |
|
---|
215 | set
|
---|
216 | {
|
---|
217 | _height = value;
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | #endregion
|
---|
222 |
|
---|
223 | #region Public Methods
|
---|
224 |
|
---|
225 | /// <summary>
|
---|
226 | /// This tests whether the specified object is a <see cref="SvgSizeF"/>
|
---|
227 | /// with the same dimensions as this <see cref="SvgSizeF"/>.
|
---|
228 | /// </summary>
|
---|
229 | /// <param name="obj">The <see cref="System.Object"/> to test. </param>
|
---|
230 | /// <returns>
|
---|
231 | /// This returns <see langword="true"/> if specified object is a
|
---|
232 | /// <see cref="SvgSizeF"/> and has the same width and height as this
|
---|
233 | /// <see cref="SvgSizeF"/>; otherwise, <see langword="false"/>.
|
---|
234 | /// </returns>
|
---|
235 | public override bool Equals(object obj)
|
---|
236 | {
|
---|
237 | if (obj is SvgSizeF)
|
---|
238 | {
|
---|
239 | return Equals((SvgSizeF)obj);
|
---|
240 | }
|
---|
241 |
|
---|
242 | return false;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /// <summary>
|
---|
246 | /// This to see whether the specified <see cref="SvgSizeF"/> is with the
|
---|
247 | /// same dimensions as this <see cref="SvgSizeF"/>.
|
---|
248 | /// </summary>
|
---|
249 | /// <param name="other">The <see cref="SvgSizeF"/> to test. </param>
|
---|
250 | /// <returns>
|
---|
251 | /// This returns <see langword="true"/> if the specified <see cref="SvgSizeF"/> and has the same width and height as this <see cref="SvgSizeF"/>;
|
---|
252 | /// otherwise, <see langword="false"/>.
|
---|
253 | /// </returns>
|
---|
254 | public bool Equals(SvgSizeF other)
|
---|
255 | {
|
---|
256 | return ((other.Width.Equals(_width)) &&
|
---|
257 | (other.Height.Equals(_height)));
|
---|
258 | }
|
---|
259 |
|
---|
260 | /// <summary>
|
---|
261 | /// This returns a hash code for this <see cref="SvgSizeF"/> structure.
|
---|
262 | /// </summary>
|
---|
263 | /// <returns>
|
---|
264 | /// An integer value that specifies a hash value for this
|
---|
265 | /// <see cref="SvgSizeF"/> structure.
|
---|
266 | /// </returns>
|
---|
267 | public override int GetHashCode()
|
---|
268 | {
|
---|
269 | return (_width.GetHashCode() ^ _height.GetHashCode());
|
---|
270 | }
|
---|
271 |
|
---|
272 | /// <summary>
|
---|
273 | /// This creates a human-readable string that represents this
|
---|
274 | /// <see cref="SvgSizeF"/>.
|
---|
275 | /// </summary>
|
---|
276 | /// <returns>A string that represents this <see cref="SvgSizeF"/>.</returns>
|
---|
277 | public override string ToString()
|
---|
278 | {
|
---|
279 | CultureInfo culture = CultureInfo.CurrentCulture;
|
---|
280 |
|
---|
281 | return ("{Width=" + _width.ToString(culture)
|
---|
282 | + ", Height=" + _height.ToString(culture) + "}");
|
---|
283 | }
|
---|
284 |
|
---|
285 | #endregion
|
---|
286 |
|
---|
287 | #region Public Static Methods
|
---|
288 |
|
---|
289 | /// <summary>
|
---|
290 | /// Adds the width and height of one <see cref="SvgSizeF"/> structure to
|
---|
291 | /// the width and height of another <see cref="SvgSizeF"/> structure.
|
---|
292 | /// </summary>
|
---|
293 | /// <param name="sz1">The first <see cref="SvgSizeF"/> to add.</param>
|
---|
294 | /// <param name="sz2">The second <see cref="SvgSizeF"/> to add.</param>
|
---|
295 | /// <returns>
|
---|
296 | /// A <see cref="SvgSizeF"/> structure that is the result of the addition
|
---|
297 | /// operation.
|
---|
298 | /// </returns>
|
---|
299 | public static SvgSizeF Add(SvgSizeF sz1, SvgSizeF sz2)
|
---|
300 | {
|
---|
301 | return new SvgSizeF(sz1.Width + sz2.Width, sz1.Height + sz2.Height);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /// <summary>
|
---|
305 | /// Subtracts the width and height of one <see cref="SvgSizeF"/>
|
---|
306 | /// structure from the width and height of another
|
---|
307 | /// <see cref="SvgSizeF"/> structure.
|
---|
308 | /// </summary>
|
---|
309 | /// <param name="sz1">
|
---|
310 | /// The <see cref="SvgSizeF"/> structure on the left side of the
|
---|
311 | /// subtraction operator.
|
---|
312 | /// </param>
|
---|
313 | /// <param name="sz2">
|
---|
314 | /// The <see cref="SvgSizeF"/> structure on the right side of the
|
---|
315 | /// subtraction operator.
|
---|
316 | /// </param>
|
---|
317 | /// <returns>
|
---|
318 | /// The <see cref="SvgSizeF"/> that is a result of the subtraction
|
---|
319 | /// operation.
|
---|
320 | /// </returns>
|
---|
321 | public static SvgSizeF Subtract(SvgSizeF sz1, SvgSizeF sz2)
|
---|
322 | {
|
---|
323 | return new SvgSizeF(sz1.Width - sz2.Width, sz1.Height - sz2.Height);
|
---|
324 | }
|
---|
325 |
|
---|
326 | #endregion
|
---|
327 | }
|
---|
328 | }
|
---|