SvgTransform.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 » SvgTransform.cs
using System;
using System.Text.RegularExpressions;

namespace SharpVectors.Dom.Svg{
  /// <summary>
  /// Summary description for SvgTransform.
  /// <developer>niklas@protocol7.com</developer>
  /// <developer>kevin@kevlindev.com</developer>
  /// </summary>
  public class SvgTransform : ISvgTransform
  {
        #region Enum
        enum SvgTransformType : short
        {
            Unknown,

            Matrix,

            Translate,

            Scale,

            Rotate,

            SkewX,

            SkewY
        }
        #endregion

        #region Fields
        private short type;
        private ISvgMatrix matrix;
        private double angle;
        #endregion

        #region Constructors
    public SvgTransform()
    {
    }

        public SvgTransform(ISvgMatrix matrix)
        {
            this.type = (short) SvgTransformType.Matrix;
            this.matrix = matrix;
        }

    public SvgTransform(string str)
    {
      int start = str.IndexOf("(");
      string type = str.Substring(0, start);
      string valuesList = (str.Substring(start+1, str.Length - start - 2)).Trim(); //JR added trim
      Regex re = new Regex("[\\s\\,]+"); 
      valuesList = re.Replace(valuesList, ",");
      string[] valuesStr = valuesList.Split(new char[]{','});
      int len = valuesStr.GetLength(0);
      float[] values = new float[len];

      for(int i = 0; i<len; i++)
      {
        values.SetValue(SvgNumber.ParseToFloat(valuesStr[i]), i);
      }

            switch (type)
            {
                case "translate":
                    switch (len)
                    {
                        case 1:
                            SetTranslate(values[0], 0);
                            break;
                        case 2:
                            SetTranslate(values[0], values[1]);
                            break;
                        default:
                            throw new ApplicationException("Wrong number of arguments in translate transform");
                    }
                    break;
                case "rotate":
                    switch (len)
                    {
                        case 1:
                            SetRotate(values[0]);
                            break;
                        case 3:
                            SetRotate(values[0], values[1], values[2]);
                            break;
                        default:
                            throw new ApplicationException("Wrong number of arguments in rotate transform");
                    }
                    break;
                case "scale":
                    switch (len)
                    {
                        case 1:
                            SetScale(values[0], values[0]);
                            break;
                        case 2:
                            SetScale(values[0], values[1]);
                            break;
                        default:
                            throw new ApplicationException("Wrong number of arguments in scale transform");
                    }
                    break;
                case "skewX":
                    if (len != 1)
                        throw new ApplicationException("Wrong number of arguments in skewX transform");
                    SetSkewX(values[0]);
                    break;
                case "skewY":
                    if (len != 1)
                        throw new ApplicationException("Wrong number of arguments in skewY transform");
                    SetSkewY(values[0]);
                    break;
                case "matrix":
                    if(len != 6)
                        throw new ApplicationException("Wrong number of arguments in matrix transform");
                    SetMatrix(
                        new SvgMatrix(
                        values[0],
                        values[1],
                        values[2],
                        values[3],
                        values[4],
                        values[5]
                        ));
                    break;
                default:
                    this.type = (short) SvgTransformType.Unknown;
                    break;
            }
    }
        #endregion

        #region ISvgTransform Interface
    public short Type
    {
      get { return type; }
    }

    public ISvgMatrix Matrix
    {
      get { return matrix; }
    }

    public double Angle
    {
      get { return angle; }
    }

    public void SetMatrix(ISvgMatrix matrix)
    {
      type = (short) SvgTransform.SvgTransformType.Matrix;
      this.matrix = matrix;
    }

    public void SetTranslate(float tx, float ty)
    {
      type = (short) SvgTransform.SvgTransformType.Translate;
      matrix = new SvgMatrix().Translate(tx, ty);
    }

    public void SetScale(float sx, float sy)
    {
      type = (short) SvgTransform.SvgTransformType.Scale;
      matrix = new SvgMatrix().ScaleNonUniform(sx, sy);
    }

    public void SetRotate(float angle)
    {
      type = (short) SvgTransform.SvgTransformType.Rotate;
      this.angle = angle;
      matrix = new SvgMatrix().Rotate(angle);
    }

    public void SetRotate(float angle, float cx, float cy)
    {
      type = (short) SvgTransform.SvgTransformType.Rotate;
      this.angle = angle;
      matrix = new SvgMatrix().Translate(cx, cy).Rotate(angle).Translate(-cx,-cy);
    }

    public void SetSkewX(float angle)
    {
      type = (short) SvgTransform.SvgTransformType.SkewX;
      this.angle = angle;
      matrix = new SvgMatrix().SkewX(angle);
    }

    public void SetSkewY(float angle)
    {
      type = (short) SvgTransform.SvgTransformType.SkewY;
      this.angle = angle;
      matrix = new SvgMatrix().SkewY(angle);
    }
        #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.