SvgPath.cs 2.83 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
using System.Drawing.Drawing2D;
using Svg.Pathing;

namespace Svg
{
6
7
8
    /// <summary>
    /// Represents an SVG path element.
    /// </summary>
9
    [SvgElement("path")]
10
    public class SvgPath : SvgMarkerElement
davescriven's avatar
davescriven committed
11
12
13
    {
        private GraphicsPath _path;

14
15
16
        /// <summary>
        /// Gets or sets a <see cref="SvgPathSegmentList"/> of path data.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
17
        [SvgAttribute("d", true)]
davescriven's avatar
davescriven committed
18
19
        public SvgPathSegmentList PathData
        {
20
        	get { return this.Attributes.GetAttribute<SvgPathSegmentList>("d"); }
21
22
23
24
25
26
            set
            {
            	this.Attributes["d"] = value;
            	value._owner = this;
                this.IsPathDirty = true;
            }
davescriven's avatar
davescriven committed
27
28
        }

29
30
31
        /// <summary>
        /// Gets or sets the length of the path.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
32
        [SvgAttribute("pathLength", true)]
Eric Domke's avatar
Eric Domke committed
33
        public float PathLength
davescriven's avatar
davescriven committed
34
        {
Eric Domke's avatar
Eric Domke committed
35
            get { return this.Attributes.GetAttribute<float>("pathLength"); }
36
            set { this.Attributes["pathLength"] = value; }
davescriven's avatar
davescriven committed
37
38
        }

39
	
40

davescriven's avatar
davescriven committed
41
42
43
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
44
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
45
        {
46
            if (this._path == null || this.IsPathDirty)
davescriven's avatar
davescriven committed
47
            {
48
                _path = new GraphicsPath();
davescriven's avatar
davescriven committed
49

50
51
52
                foreach (SvgPathSegment segment in this.PathData)
                {
                    segment.AddToPath(_path);
davescriven's avatar
davescriven committed
53
                }
54
55

                this.IsPathDirty = false;
56
            }
57
            return _path;
davescriven's avatar
davescriven committed
58
59
60
61
62
        }

        internal void OnPathUpdated()
        {
            this.IsPathDirty = true;
63
            OnAttributeChanged(new AttributeEventArgs{ Attribute = "d", Value = this.Attributes.GetAttribute<SvgPathSegmentList>("d") });
davescriven's avatar
davescriven committed
64
65
        }

66
67
68
        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
Tebjan Halm's avatar
Tebjan Halm committed
69
70
        /// <value>The bounds.</value>
        public override System.Drawing.RectangleF Bounds
davescriven's avatar
davescriven committed
71
        {
Tebjan Halm's avatar
Tebjan Halm committed
72
            get { return this.Path(null).GetBounds(); }
davescriven's avatar
davescriven committed
73
74
        }

75
76
77
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgPath"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
78
79
        public SvgPath()
        {
80
81
82
            var pathData = new SvgPathSegmentList();
            this.Attributes["d"] = pathData;
            pathData._owner = this;
davescriven's avatar
davescriven committed
83
        }
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgPath>();
		}

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgPath;
			foreach (var pathData in this.PathData)
				newObj.PathData.Add(pathData.Clone());
			newObj.PathLength = this.PathLength;
			newObj.MarkerStart = this.MarkerStart;
			newObj.MarkerEnd = this.MarkerEnd;
			return newObj;

		}
davescriven's avatar
davescriven committed
101
102
    }
}