Rect.cs :  » GUI » SharpVectorGraphics » SharpVectors » Dom » Css » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » SharpVectorGraphics 
SharpVectorGraphics » SharpVectors » Dom » Css » Rect.cs
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
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.