SvgPath.cs 6.14 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using System.Xml;
using System.Diagnostics;
using Svg.Pathing;
owaits's avatar
owaits committed
10
using Svg.Transforms;
davescriven's avatar
davescriven committed
11
12
13

namespace Svg
{
14
15
16
    /// <summary>
    /// Represents an SVG path element.
    /// </summary>
17
    [SvgElement("path")]
18
    public class SvgPath : SvgVisualElement
davescriven's avatar
davescriven committed
19
20
21
    {
        private GraphicsPath _path;

22
23
24
        /// <summary>
        /// Gets or sets a <see cref="SvgPathSegmentList"/> of path data.
        /// </summary>
davescriven's avatar
davescriven committed
25
26
27
        [SvgAttribute("d")]
        public SvgPathSegmentList PathData
        {
28
        	get { return this.Attributes.GetAttribute<SvgPathSegmentList>("d"); }
29
30
31
32
33
34
            set
            {
            	this.Attributes["d"] = value;
            	value._owner = this;
                this.IsPathDirty = true;
            }
davescriven's avatar
davescriven committed
35
36
        }

37
38
39
        /// <summary>
        /// Gets or sets the length of the path.
        /// </summary>
davescriven's avatar
davescriven committed
40
        [SvgAttribute("pathLength")]
Eric Domke's avatar
Eric Domke committed
41
        public float PathLength
davescriven's avatar
davescriven committed
42
        {
Eric Domke's avatar
Eric Domke committed
43
            get { return this.Attributes.GetAttribute<float>("pathLength"); }
44
            set { this.Attributes["pathLength"] = value; }
davescriven's avatar
davescriven committed
45
46
        }

47
48
49
50
		
        /// <summary>
        /// Gets or sets the marker (end cap) of the path.
        /// </summary>
51
52
		[SvgAttribute("marker-end")]
		public Uri MarkerEnd
53
        {
54
55
56
			get { return this.Attributes.GetAttribute<Uri>("marker-end"); }
			set { this.Attributes["marker-end"] = value; }
		}
57
58


59
60
61
62
63
64
65
66
67
		/// <summary>
		/// Gets or sets the marker (start cap) of the path.
		/// </summary>
		[SvgAttribute("marker-mid")]
		public Uri MarkerMid
		{
			get { return this.Attributes.GetAttribute<Uri>("marker-mid"); }
			set { this.Attributes["marker-mid"] = value; }
		}
68
69


70
71
72
73
74
75
76
77
78
		/// <summary>
		/// Gets or sets the marker (start cap) of the path.
		/// </summary>
		[SvgAttribute("marker-start")]
		public Uri MarkerStart
		{
			get { return this.Attributes.GetAttribute<Uri>("marker-start"); }
			set { this.Attributes["marker-start"] = value; }
		}
79
80


davescriven's avatar
davescriven committed
81
82
83
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
84
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
85
        {
86
            if (this._path == null || this.IsPathDirty)
davescriven's avatar
davescriven committed
87
            {
88
                _path = new GraphicsPath();
davescriven's avatar
davescriven committed
89

90
91
92
                foreach (SvgPathSegment segment in this.PathData)
                {
                    segment.AddToPath(_path);
davescriven's avatar
davescriven committed
93
                }
94
95

                this.IsPathDirty = false;
96
            }
97
            return _path;
davescriven's avatar
davescriven committed
98
99
100
101
102
        }

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

106
107
108
        /// <summary>
        /// Gets or sets a value to determine if anti-aliasing should occur when the element is being rendered.
        /// </summary>
davescriven's avatar
davescriven committed
109
110
111
112
113
        protected override bool RequiresSmoothRendering
        {
            get { return true; }
        }

114
115
116
117
        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
davescriven's avatar
davescriven committed
118
119
        public override System.Drawing.RectangleF Bounds
        {
120
            get { return this.Path(null).GetBounds(); }
davescriven's avatar
davescriven committed
121
122
        }

123
124
125
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgPath"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
126
127
        public SvgPath()
        {
128
129
130
            var pathData = new SvgPathSegmentList();
            this.Attributes["d"] = pathData;
            pathData._owner = this;
davescriven's avatar
davescriven committed
131
        }
132

133
		/// <summary>
Eric Domke's avatar
Eric Domke committed
134
		/// Renders the stroke of the <see cref="SvgVisualElement"/> to the specified <see cref="ISvgRenderer"/>
135
		/// </summary>
Eric Domke's avatar
Eric Domke committed
136
137
		/// <param name="renderer">The <see cref="ISvgRenderer"/> object to render to.</param>
		protected internal override void  RenderStroke(ISvgRenderer renderer)
138
		{
Eric Domke's avatar
Eric Domke committed
139
            if (this.Stroke != null && this.Stroke != SvgColourServer.None)
140
			{
141
				float strokeWidth = this.StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, this);
Eric Domke's avatar
Eric Domke committed
142
                using (Pen pen = new Pen(this.Stroke.GetBrush(this, renderer, this.StrokeOpacity * this.Opacity), strokeWidth))
143
144
145
146
147
148
149
				{
					if (this.StrokeDashArray != null && this.StrokeDashArray.Count > 0)
					{
						/* divide by stroke width - GDI behaviour that I don't quite understand yet.*/
						pen.DashPattern = this.StrokeDashArray.ConvertAll(u => u.Value / ((strokeWidth <= 0) ? 1 : strokeWidth)).ToArray();
					}

150
151
                    var path = this.Path(renderer);
                    renderer.DrawPath(pen, path);
152
153
154
155

					if (this.MarkerStart != null)
					{
						SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerStart.ToString());
156
                        marker.RenderMarker(renderer, this, path.PathPoints[0], path.PathPoints[0], path.PathPoints[1]);
157
158
159
160
161
					}

					if (this.MarkerMid != null)
					{
						SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerMid.ToString());
162
163
                        for (int i = 1; i <= path.PathPoints.Length - 2; i++)
                            marker.RenderMarker(renderer, this, path.PathPoints[i], path.PathPoints[i - 1], path.PathPoints[i], path.PathPoints[i + 1]);
164
165
166
167
168
					}

					if (this.MarkerEnd != null)
					{
						SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerEnd.ToString());
169
                        marker.RenderMarker(renderer, this, path.PathPoints[path.PathPoints.Length - 1], path.PathPoints[path.PathPoints.Length - 2], path.PathPoints[path.PathPoints.Length - 1]);
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
					}
				}
			}
		}

		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
191
192
    }
}