// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using System;
namespace TheArtOfDev.HtmlRenderer.Adapters.Entities
{
///
/// Stores an ordered pair of floating-point numbers, typically the width and height of a rectangle.
///
public struct RSize
{
#region Fields and Consts
///
/// Gets a structure that has a
///
/// and
///
/// value of 0.
///
///
/// A structure that has a
///
/// and
///
/// value of 0.
///
/// 1
public static readonly RSize Empty = new RSize();
private double _height;
private double _width;
#endregion
///
/// Initializes a new instance of the structure from the specified existing
///
/// structure.
///
///
/// The structure from which to create the new
///
/// structure.
///
public RSize(RSize size)
{
_width = size._width;
_height = size._height;
}
///
/// Initializes a new instance of the structure from the specified structure.
///
/// The structure from which to initialize this structure.
public RSize(RPoint pt)
{
_width = pt.X;
_height = pt.Y;
}
///
/// Initializes a new instance of the structure from the specified dimensions.
///
///
/// The width component of the new structure.
///
///
/// The height component of the new structure.
///
public RSize(double width, double height)
{
_width = width;
_height = height;
}
///
/// Gets a value that indicates whether this structure has zero width and height.
///
///
/// This property returns true when this structure has both a width and height of zero; otherwise, false.
///
/// 1
public bool IsEmpty
{
get
{
if (Math.Abs(_width) < 0.0001)
return Math.Abs(_height) < 0.0001;
else
return false;
}
}
///
/// Gets or sets the horizontal component of this structure.
///
///
/// The horizontal component of this structure, typically measured in pixels.
///
/// 1
public double Width
{
get { return _width; }
set { _width = value; }
}
internal void HeightExpand(double dShiftDown)
{
this._height += dShiftDown;
}
///
/// Gets or sets the vertical component of this structure.
///
///
/// The vertical component of this structure, typically measured in pixels.
///
/// 1
public double Height
{
get { return _height; }
set { _height = value; }
}
///
/// Converts the specified structure to a
/// structure.
///
/// The structure to which this operator converts.
/// The structure to be converted
///
public static explicit operator RPoint(RSize size)
{
return new RPoint(size.Width, size.Height);
}
///
/// Adds the width and height of one structure to the width and height of another
///
/// structure.
///
///
/// A structure that is the result of the addition operation.
///
///
/// The first structure to add.
///
///
/// The second structure to add.
///
/// 3
public static RSize operator +(RSize sz1, RSize sz2)
{
return Add(sz1, sz2);
}
///
/// Subtracts the width and height of one structure from the width and height of another
///
/// structure.
///
///
/// A that is the result of the subtraction operation.
///
///
/// The structure on the left side of the subtraction operator.
///
///
/// The structure on the right side of the subtraction operator.
///
/// 3
public static RSize operator -(RSize sz1, RSize sz2)
{
return Subtract(sz1, sz2);
}
///
/// Tests whether two structures are equal.
///
///
/// This operator returns true if and have equal width and height; otherwise, false.
///
///
/// The structure on the left side of the equality operator.
///
///
/// The structure on the right of the equality operator.
///
/// 3
public static bool operator ==(RSize sz1, RSize sz2)
{
if (Math.Abs(sz1.Width - sz2.Width) < 0.001)
return Math.Abs(sz1.Height - sz2.Height) < 0.001;
else
return false;
}
///
/// Tests whether two structures are different.
///
///
/// This operator returns true if and differ either in width or height; false if
///
/// and are equal.
///
///
/// The structure on the left of the inequality operator.
///
///
/// The structure on the right of the inequality operator.
///
/// 3
public static bool operator !=(RSize sz1, RSize sz2)
{
return !(sz1 == sz2);
}
///
/// Adds the width and height of one structure to the width and height of another
///
/// structure.
///
///
/// A structure that is the result of the addition operation.
///
///
/// The first structure to add.
///
///
/// The second structure to add.
///
public static RSize Add(RSize sz1, RSize sz2)
{
return new RSize(sz1.Width + sz2.Width, sz1.Height + sz2.Height);
}
///
/// Subtracts the width and height of one structure from the width and height of another
///
/// structure.
///
///
/// A structure that is a result of the subtraction operation.
///
///
/// The structure on the left side of the subtraction operator.
///
///
/// The structure on the right side of the subtraction operator.
///
public static RSize Subtract(RSize sz1, RSize sz2)
{
return new RSize(sz1.Width - sz2.Width, sz1.Height - sz2.Height);
}
///
/// Tests to see whether the specified object is a structure with the same dimensions as this
///
/// structure.
///
///
/// This method returns true if is a and has the same width and height as this
///
/// ; otherwise, false.
///
///
/// The to test.
///
/// 1
public override bool Equals(object obj)
{
if (!(obj is RSize))
return false;
var sizeF = (RSize)obj;
if (Math.Abs(sizeF.Width - Width) < 0.001 && Math.Abs(sizeF.Height - Height) < 0.001)
return sizeF.GetType() == GetType();
else
return false;
}
///
/// Returns a hash code for this structure.
///
///
/// An integer value that specifies a hash value for this structure.
///
/// 1
public override int GetHashCode()
{
return base.GetHashCode();
}
///
/// Converts a structure to a structure.
///
///
/// Returns a structure.
///
public RPoint ToPointF()
{
return (RPoint)this;
}
///
/// Creates a human-readable string that represents this structure.
///
///
/// A string that represents this structure.
///
/// 1
///
///
///
public override string ToString()
{
return "{Width=" + _width + ", Height=" + _height + "}";
}
}
}