SvgLength.cs :  » GUI » SharpVectorGraphics » SharpVectors » Dom » Svg » 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 » Svg » SvgLength.cs
using System;
using System.Collections;
using System.Xml;
using System.Text.RegularExpressions;
using SharpVectors.Dom.Css;


namespace SharpVectors.Dom.Svg{
  /// <summary>
  /// Defines the direction of a SvgLength
  /// </summary>
  public enum SvgLengthDirection{Vertical, Horizontal, Viewport}

  /// <summary>
  /// Defines the origin type of the SvgLength value
  /// </summary>
  public enum SvgLengthSource{Css, Xml, String}

  public class SvgLength : ISvgLength
  {
    private static Regex reUnit = new Regex(CssValue.LengthUnitPattern + "$");

    #region Constructors
    public SvgLength(SvgElement ownerElement, string propertyName, SvgLengthSource source, SvgLengthDirection direction, string defaultValue)
    {
      this.ownerElement = ownerElement;
      this.propertyName = propertyName;
      this.direction = direction;
      this.defaultValue = defaultValue;
      this.source = source;

      if(this.source == SvgLengthSource.Xml || this.source == SvgLengthSource.Css)
      {
        getCssXmlValue();
      }
    }

    public SvgLength(SvgElement ownerElement, string propertyName, SvgLengthDirection direction, string baseVal) : 
      this(ownerElement, propertyName, direction, baseVal, String.Empty)
    {
    }

    public SvgLength(SvgElement ownerElement, string propertyName, SvgLengthDirection direction, string baseVal, string defaultValue) :
      this(ownerElement, propertyName, SvgLengthSource.String, direction, defaultValue)
    {
      if(baseVal == null || baseVal.Length == 0)
      {
        baseVal = defaultValue;
      }
      baseVal = SvgNumber.ScientificToDec(baseVal);
      cssLength = new CssAbsPrimitiveLengthValue(new CssPrimitiveLengthValue(baseVal, false), propertyName, ownerElement);
    }
    #endregion

    #region Private methods
    private void getCssXmlValue()
    {
      if(source == SvgLengthSource.Css)
      {
        ICssStyleDeclaration csd = this.ownerElement.GetComputedStyle(String.Empty);
        CssPrimitiveLengthValue cssValue = csd.GetPropertyCssValue(propertyName) as CssPrimitiveLengthValue;

        if(cssValue != null)
        {
          cssLength = new CssAbsPrimitiveLengthValue(cssValue, propertyName, ownerElement);
        }
        else
        {
          throw new DomException(DomExceptionType.SyntaxErr, "Not a length value");
        }
      }
      else
      {
        string baseVal = ownerElement.GetAttribute(propertyName);

        if(baseVal == null || baseVal.Length == 0)
        {
          baseVal = defaultValue;
        }
        baseVal = SvgNumber.ScientificToDec(baseVal);
        cssLength = new CssAbsPrimitiveLengthValue(new CssPrimitiveLengthValue(baseVal, false), propertyName, ownerElement);
      }
    }

    #endregion

    #region Private Fields
    private SvgLengthSource source;
    private SvgElement ownerElement;
    private SvgLengthDirection direction;
    private CssAbsPrimitiveLengthValue cssLength;
    private string defaultValue;
    #endregion

    #region Public Properties
    private string propertyName;
    public string PropertyName
    {
      get
      {
        return propertyName;
      }
    }
    
    /// <summary>
    /// The type of the value as specified by one of the constants specified above. 
    /// </summary>
    public SvgLengthType UnitType
    {
      get
      {
        return (SvgLengthType)cssLength.PrimitiveType;
      }
    }

    /// <summary>
    /// The value as an floating point value, in user units. Setting this attribute will cause valueInSpecifiedUnits and valueAsString to be updated automatically to reflect this setting. 
    /// </summary>
    /// <exception cref="DomException"> NO_MODIFICATION_ALLOWED_ERR: Raised on an attempt to change the value of a readonly attribute.</exception>
    public double Value
    {
      get
      {
        double ret = 0;
        switch(UnitType)
        {
          case SvgLengthType.Number:
          case SvgLengthType.Px:
          case SvgLengthType.Cm:
          case SvgLengthType.Mm:
          case SvgLengthType.In:
          case SvgLengthType.Pt:
          case SvgLengthType.Pc:
          case SvgLengthType.Ems:
          case SvgLengthType.Exs:
            ret = cssLength.GetFloatValue(CssPrimitiveType.Px);
            break;
          case SvgLengthType.Percentage:
            double valueInSpecifiedUnits = cssLength.GetFloatValue(CssPrimitiveType.Percentage);
            if(ownerElement is SvgGradientElement) 
            {
              ret = valueInSpecifiedUnits / 100F;
            }
            else
            {
              //ISvgSvgElement svp = (ISvgSvgElement)((ownerElement.ViewportElement != null) ? ownerElement.ViewportElement: ownerElement);
              //ISvgFitToViewBox ftv = (ISvgFitToViewBox)svp;

              ISvgSvgElement svp = null;
              double w = 0;
              double h = 0;
              if (ownerElement.ViewportElement != null) 
              {
                svp = (ISvgSvgElement)ownerElement.ViewportElement;
                ISvgFitToViewBox ftv = (ISvgFitToViewBox)svp;
                w = ftv.ViewBox.AnimVal.Width;
                h = ftv.ViewBox.AnimVal.Height;
              } 
              else 
              {
                w = ownerElement.OwnerDocument.Window.InnerWidth;
                h = ownerElement.OwnerDocument.Window.InnerHeight;
              }
              
              if(direction == SvgLengthDirection.Horizontal)
              {
                ret = valueInSpecifiedUnits * w / 100;
              }
              else if(direction == SvgLengthDirection.Vertical)
              {
                ret = valueInSpecifiedUnits * h / 100;
              }
              else
              {
                ret = Math.Sqrt(w*w + h*h)/Math.Sqrt(2) * valueInSpecifiedUnits / 100;
              }
            }
            break;
          case SvgLengthType.Unknown:
            throw new SvgException(SvgExceptionType.SvgInvalidValueErr, "Bad length unit");
        }
        if(double.IsNaN(ret))
        {
          ret = 10;
        }
        return ret;
      }
      set
      {
        CssPrimitiveType oldType = cssLength.PrimitiveType;
        cssLength.SetFloatValue(CssPrimitiveType.Px, value);
        ConvertToSpecifiedUnits((SvgLengthType)oldType);
      }
    }  

    /// <summary>
    /// The value as an floating point value, in the units expressed by unitType. Setting this attribute will cause value and valueAsString to be updated automatically to reflect this setting.
    /// </summary>
    /// <exception cref="DomException"> NO_MODIFICATION_ALLOWED_ERR: Raised on an attempt to change the value of a readonly attribute.</exception>
    public double ValueInSpecifiedUnits
    {
      get
      {
        return cssLength.GetFloatValue(cssLength.PrimitiveType);
      }
      set
      {
        cssLength.SetFloatValue(cssLength.PrimitiveType, value);
      }
    }

    /// <summary>
    /// The value as a string value, in the units expressed by unitType. Setting this attribute will cause value and valueInSpecifiedUnits to be updated automatically to reflect this setting.
    /// </summary>
    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised on an attempt to change the value of a readonly attribute.</exception>
    public string ValueAsString
    {
      get
      {
        return cssLength.CssText;
      }
      set
      {
        cssLength = new CssAbsPrimitiveLengthValue(new CssPrimitiveLengthValue(value, false), "", ownerElement);
      }
    }
     
    #endregion

    #region Public Methods
    /// <summary>
    /// Reset the value as a number with an associated unitType, thereby replacing the values for all of the attributes on the object.
    /// </summary>
    /// <param name="unitType">The unitType for the value (e.g., MM). </param>
    /// <param name="valueInSpecifiedUnits">The new value</param>
    public void NewValueSpecifiedUnits(SvgLengthType unitType, double valueInSpecifiedUnits)
    {
      cssLength.SetFloatValue((CssPrimitiveType)unitType, valueInSpecifiedUnits);
    }

    /// <summary>
    /// Preserve the same underlying stored value, but reset the stored unit identifier to the given unitType. Object attributes unitType, valueAsSpecified and valueAsString might be modified as a result of this method. For example, if the original value were "0.5cm" and the method was invoked to convert to millimeters, then the unitType would be changed to MM, valueAsSpecified would be changed to the numeric value 5 and valueAsString would be changed to "5mm". 
    /// </summary>
    /// <param name="unitType">The unitType to switch to (e.g., MM).</param>
    public void ConvertToSpecifiedUnits(SvgLengthType unitType)
    {
      double newValue = cssLength.GetFloatValue((CssPrimitiveType)unitType);
      cssLength.SetFloatValue((CssPrimitiveType)unitType, newValue);
    }

    #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.