using System;
using System.Drawing;
#if TEST
using NUnit.Framework;
#endif
namespace SharpVectors.Dom.Css{
/// <summary>
/// The Rect interface is used to represent any rect value. This interface reflects the values in the underlying style property. Hence, modifications made to the CSSPrimitiveValue objects modify the style property.
/// </summary>
/// <developer>niklas@protocol7.com</developer>
/// <completed>100</completed>
public class Rect : IRect
{
#region Constructors
/// <summary>
/// Constructs a new Rect
/// </summary>
/// <param name="s">The string to parse that contains the Rect structure</param>
/// <param name="readOnly">Specifies if the Rect should be read-only</param>
public Rect(string s, bool readOnly)
{
this.readOnly = readOnly;
string[] parts = s.Split(new char[]{' '});
if(parts.Length == 4)
{
_top = new CssPrimitiveLengthValue(parts[0], readOnly);
_right = new CssPrimitiveLengthValue(parts[1], readOnly);
_bottom = new CssPrimitiveLengthValue(parts[2], readOnly);
_left = new CssPrimitiveLengthValue(parts[3], readOnly);
}
else
{
throw new DomException(DomExceptionType.SyntaxErr);
}
}
#endregion
#region Private fields
private bool readOnly;
#endregion
#region Implementation of IRect
private CssPrimitiveValue _left;
/// <summary>
/// This attribute is used for the left of the rect.
/// </summary>
public ICssPrimitiveValue Left
{
get
{
return _left;
}
}
private CssPrimitiveValue _bottom;
/// <summary>
/// This attribute is used for the bottom of the rect.
/// </summary>
public ICssPrimitiveValue Bottom
{
get
{
return _bottom;
}
}
private CssPrimitiveValue _right;
/// <summary>
/// This attribute is used for the right of the rect.
/// </summary>
public ICssPrimitiveValue Right
{
get
{
return _right;
}
}
private CssPrimitiveValue _top;
/// <summary>
/// This attribute is used for the top of the rect.
/// </summary>
public ICssPrimitiveValue Top
{
get
{
return _top;
}
}
#endregion
public RectangleF ToRectangleF()
{
float x = (float)Left.GetFloatValue(CssPrimitiveType.Px);
float y = (float)Top.GetFloatValue(CssPrimitiveType.Px);
float width = (float)Right.GetFloatValue(CssPrimitiveType.Px) - x;
float height = (float)Bottom.GetFloatValue(CssPrimitiveType.Px) - y;
return new RectangleF(x, y, width, height);
}
#region Unit tests
#if TEST
[TestFixture]
public class RectTests
{
Rect rect;
[Test]
public void TestRect()
{
rect = new Rect("rect(0 100cm 20% 23em)", false);
// test top
Assert.AreEqual(CssPrimitiveType.Number, rect.Top.PrimitiveType);
Assert.AreEqual(0, rect.Top.GetFloatValue(CssPrimitiveType.Cm));
// test right
Assert.AreEqual(CssPrimitiveType.Cm, rect.Right.PrimitiveType);
Assert.AreEqual(100, rect.Right.GetFloatValue(CssPrimitiveType.Cm));
// test bottom
Assert.AreEqual(CssPrimitiveType.Percentage, rect.Bottom.PrimitiveType);
Assert.AreEqual(20, rect.Bottom.GetFloatValue(CssPrimitiveType.Percentage));
// test left
Assert.AreEqual(CssPrimitiveType.Ems, rect.Left.PrimitiveType);
Assert.AreEqual(23, rect.Left.GetFloatValue(CssPrimitiveType.Ems));
}
}
#endif
#endregion
}
}
|