1
#region PDFsharp - A .NET library for processing PDF
4
// Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
8
// http://www.pdfsharp.com
9
// http://sourceforge.net/projects/pdfsharp
11
// Permission is hereby granted, free of charge, to any person obtaining a
12
// copy of this software and associated documentation files (the "Software"),
13
// to deal in the Software without restriction, including without limitation
14
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
// and/or sell copies of the Software, and to permit persons to whom the
16
// Software is furnished to do so, subject to the following conditions:
18
// The above copyright notice and this permission notice shall be included
19
// in all copies or substantial portions of the Software.
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
// DEALINGS IN THE SOFTWARE.
31
using System.Diagnostics;
32
using System.Globalization;
33
using System.ComponentModel;
34
using System.Runtime.InteropServices;
41
using PdfSharp.Internal;
43
namespace PdfSharp.Drawing
47
/// Represents a pair of floating-point numbers, typically the width and height of a
50
//[DebuggerDisplay("({Width}, {Height})")]
51
[DebuggerDisplay("Width={Width.ToString(\"0.####\",System.Globalization.CultureInfo.InvariantCulture)}, Height={Height.ToString(\"0.####\",System.Globalization.CultureInfo.InvariantCulture)}")]
52
[Serializable, StructLayout(LayoutKind.Sequential)] //, ValueSerializer(typeof(SizeValueSerializer)), TypeConverter(typeof(SizeConverter))]
53
public struct XSize : IFormattable
56
/// Initializes a new instance of the XPoint class with the specified values.
58
public XSize(double width, double height)
60
if (width < 0 || height < 0)
61
throw new ArgumentException("WidthAndHeightCannotBeNegative"); //SR.Get(SRID.Size_WidthAndHeightCannotBeNegative, new object[0]));
68
/// Initializes a new instance of the <see cref="XSize"/> class.
70
[Obsolete("Use explicit conversion to make your code more readable.")]
71
public XSize(XPoint pt) // DELETE: 08-12-31
78
/// Determines whether two size objects are equal.
80
public static bool operator ==(XSize size1, XSize size2)
82
return size1.Width == size2.Width && size1.Height == size2.Height;
86
/// Determines whether two size objects are not equal.
88
public static bool operator !=(XSize size1, XSize size2)
90
return !(size1 == size2);
94
/// Indicates whether this tow instance are equal.
96
public static bool Equals(XSize size1, XSize size2)
100
return size1.Width.Equals(size2.Width) && size1.Height.Equals(size2.Height);
104
/// Indicates whether this instance and a specified object are equal.
106
public override bool Equals(object o)
108
if (o == null || !(o is XSize))
110
XSize size = (XSize)o;
111
return Equals(this, size);
115
/// Indicates whether this instance and a specified size are equal.
117
public bool Equals(XSize value)
119
return Equals(this, value);
123
/// Returns the hash code for this instance.
125
public override int GetHashCode()
129
return this.Width.GetHashCode() ^ this.Height.GetHashCode();
133
/// Parses the size from a string.
135
public static XSize Parse(string source)
138
CultureInfo cultureInfo = CultureInfo.InvariantCulture;
139
TokenizerHelper helper = new TokenizerHelper(source, cultureInfo);
140
string str = helper.NextTokenRequired();
144
empty = new XSize(Convert.ToDouble(str, cultureInfo), Convert.ToDouble(helper.NextTokenRequired(), cultureInfo));
145
helper.LastTokenRequired();
151
/// Converts this XSize to a PointF.
153
public PointF ToPointF()
155
return new PointF((float)this.width, (float)this.height);
160
/// Converts this XSize to an XPoint.
162
public XPoint ToXPoint()
164
return new XPoint(this.width, this.height);
168
/// Converts this XSize to an XVector.
170
public XVector ToXVector()
172
return new XVector(this.width, this.height);
177
/// Converts this XSize to a SizeF.
179
public SizeF ToSizeF()
181
return new SizeF((float)this.width, (float)this.height);
187
/// Converts this XSize to a System.Windows.Size.
189
public System.Windows.Size ToSize()
191
return new System.Windows.Size(this.width, this.height);
197
/// Creates an XSize from a System.Drawing.Size.
199
public static XSize FromSize(System.Drawing.Size size)
201
return new XSize(size.Width, size.Height);
205
/// Implicit conversion from XSize to System.Drawing.Size. The conversion must be implicit because the
206
/// WinForms designer uses it.
208
public static implicit operator XSize(System.Drawing.Size size)
210
return new XSize(size.Width, size.Height);
216
/// Creates an XSize from a System.Drawing.Size.
218
public static XSize FromSize(System.Windows.Size size)
220
return new XSize(size.Width, size.Height);
226
/// Creates an XSize from a System.Drawing.Size.
228
public static XSize FromSizeF(SizeF size)
230
return new XSize(size.Width, size.Height);
235
/// Converts this XSize to a human readable string.
237
public override string ToString()
239
return this.ConvertToString(null, null);
243
/// Converts this XSize to a human readable string.
245
public string ToString(IFormatProvider provider)
247
return this.ConvertToString(null, provider);
251
/// Converts this XSize to a human readable string.
253
string IFormattable.ToString(string format, IFormatProvider provider)
255
return this.ConvertToString(format, provider);
258
internal string ConvertToString(string format, IFormatProvider provider)
263
char numericListSeparator = TokenizerHelper.GetNumericListSeparator(provider);
264
return string.Format(provider, "{1:" + format + "}{0}{2:" + format + "}", new object[] { numericListSeparator, this.width, this.height });
268
/// Returns an empty size, i.e. a size with a width or height less than 0.
270
public static XSize Empty
272
get { return XSize.s_empty; }
276
/// Gets a value indicating whether this instance is empty.
280
get { return this.width < 0; }
284
/// Gets or sets the width.
288
get { return this.width; }
292
throw new InvalidOperationException("CannotModifyEmptySize"); //SR.Get(SRID.Size_CannotModifyEmptySize, new object[0]));
294
throw new ArgumentException("WidthCannotBeNegative"); //SR.Get(SRID.Size_WidthCannotBeNegative, new object[0]));
300
/// Gets or sets the height.
304
get { return this.height; }
308
throw new InvalidOperationException("CannotModifyEmptySize"); // SR.Get(SRID.Size_CannotModifyEmptySize, new object[0]));
310
throw new ArgumentException("HeightCannotBeNegative"); //SR.Get(SRID.Size_HeightCannotBeNegative, new object[0]));
316
/// Performs an explicit conversion from XSize to XVector.
318
public static explicit operator XVector(XSize size)
320
return new XVector(size.width, size.height);
324
/// Performs an explicit conversion from XSize to XPoint.
326
public static explicit operator XPoint(XSize size)
328
return new XPoint(size.width, size.height);
333
/// Performs an explicit conversion from Size to XSize.
335
public static explicit operator XSize(System.Windows.Size size)
337
return new XSize(size.Width, size.Height);
341
private static XSize CreateEmptySize()
343
XSize size = new XSize();
344
size.width = double.NegativeInfinity;
345
size.height = double.NegativeInfinity;
351
s_empty = CreateEmptySize();
354
internal double width;
355
internal double height;
356
private static readonly XSize s_empty;
360
// Old code, delete end of 2008
363
/// Represents a pair of floating-point numbers, typically the width and height of a
364
/// graphical object.
366
//[DebuggerDisplay("({Width}, {Height})")]
367
[DebuggerDisplay("Width={Width.ToString(\"0.####\",System.Globalization.CultureInfo.InvariantCulture)}, Width={Width.ToString(\"0.####\",System.Globalization.CultureInfo.InvariantCulture)}")]
371
/// Initializes a new instance of the <see cref="XSize"/> class.
373
public XSize(XSize size)
375
this.width = size.width;
376
this.height = size.height;
380
/// Initializes a new instance of the <see cref="XSize"/> class.
382
/// <param name="pt">The pt.</param>
383
public XSize(XPoint pt)
390
/// Initializes a new instance of the <see cref="XSize"/> class.
392
/// <param name="width">The width.</param>
393
/// <param name="height">The height.</param>
394
public XSize(double width, double height)
397
this.height = height;
401
/// Returns the hash code for this instance.
403
public override int GetHashCode()
405
return this.width.GetHashCode() ^ this.height.GetHashCode();
409
/// Indicates whether this instance and a specified object are equal.
411
public override bool Equals(object obj)
415
XSize size = (XSize)obj;
416
return size.width == this.width && size.height == this.height;
423
/// Creates an XSize from a System.Drawing.Size.
425
public static XSize FromSize(System.Drawing.Size size)
427
return new XSize(size.Width, size.Height);
433
/// Creates an XSize from a System.Drawing.Size.
435
public static XSize FromSize(System.Windows.Size size)
437
return new XSize(size.Width, size.Height);
443
/// Creates an XSize from a System.Drawing.Size.
445
public static XSize FromSizeF(SizeF size)
447
return new XSize(size.Width, size.Height);
452
/// Returns a string with the values of this instance.
454
public override string ToString()
456
return string.Format("{{Width={0}, Height={1}}}", this.width, this.height);
461
/// Converts this XSize to a PointF.
463
public PointF ToPointF()
465
return new PointF((float)this.width, (float)this.height);
470
/// Converts this XSize to an XPoint.
472
public XPoint ToXPoint()
474
return new XPoint(this.width, this.height);
479
/// Converts this XSize to a SizeF.
481
public SizeF ToSizeF()
483
return new SizeF((float)this.width, (float)this.height);
488
/// Gets a value indicating whether this instance is empty.
493
get { return this.width == 0 && this.height == 0; }
497
/// Gets or sets the width.
501
get { return this.width; }
502
set { this.width = value; }
506
/// Gets or sets the height.
510
get { return this.height; }
511
set { this.height = value; }
515
/// Adds two size objects.
517
public static XSize operator +(XSize size1, XSize size2)
519
return new XSize(size1.width + size2.width, size1.height + size2.height);
523
/// Subtracts two size objects.
525
public static XSize operator -(XSize size1, XSize size2)
527
return new XSize(size1.width - size2.width, size1.height - size2.height);
531
/// Multiplies a size with a scalar.
533
public static XSize operator *(XSize size, double f)
535
return new XSize(size.width * f, size.height * f);
539
/// Multiplies a scalar with a size.
541
public static XSize operator *(double f, XSize size)
543
return new XSize(f * size.width, f * size.height);
547
/// Divides a size by a scalar.
549
public static XSize operator /(XSize size, double f)
552
throw new DivideByZeroException("Divisor is zero.");
554
return new XSize(size.width / f, size.height / f);
558
/// Determines whether two size objects are equal.
560
public static bool operator ==(XSize left, XSize right)
562
return left.width == right.width && left.height == right.height;
566
/// Determines whether two size objects are not equal.
568
public static bool operator !=(XSize left, XSize right)
570
return !(left == right);
574
/// Explicit conversion from XSize to XPoint.
576
public static explicit operator XPoint(XSize size)
578
return new XPoint(size.width, size.height);
582
/// Explicit conversion from XSize to System.Drawing.Size.
584
public static implicit operator XSize(System.Drawing.Size size)
586
return new XSize(size.Width, size.Height);
590
/// Represents the empty size.
592
public static readonly XSize Empty = new XSize();
594
internal double width;
595
internal double height;
b'\\ No newline at end of file'