SvgPath.cs 6.17 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"); }
davescriven's avatar
davescriven committed
29
30
            set
            {
31
32
            	this.Attributes["d"] = value;
            	value._owner = this;
davescriven's avatar
davescriven committed
33
34
35
36
                this.IsPathDirty = true;
            }
        }

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

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


		/// <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; }
		}


davescriven's avatar
davescriven committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        public override GraphicsPath Path
        {
            get
            {
                if (this._path == null || this.IsPathDirty)
                {
                    _path = new GraphicsPath();

                    foreach (SvgPathSegment segment in this.PathData)
                    {
                        segment.AddToPath(_path);
                    }

                    this.IsPathDirty = false;
                }
                return _path;
            }
90
91
92
93
            protected set
            {
                _path = value;
            }
davescriven's avatar
davescriven committed
94
95
96
97
98
        }

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

102
103
104
        /// <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
105
106
107
108
109
        protected override bool RequiresSmoothRendering
        {
            get { return true; }
        }

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

119
120
121
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgPath"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
122
123
        public SvgPath()
        {
124
125
126
            var pathData = new SvgPathSegmentList();
            this.Attributes["d"] = pathData;
            pathData._owner = this;
davescriven's avatar
davescriven committed
127
        }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

		/// <summary>
		/// Renders the stroke of the <see cref="SvgVisualElement"/> to the specified <see cref="SvgRenderer"/>
		/// </summary>
		/// <param name="renderer">The <see cref="SvgRenderer"/> object to render to.</param>
		protected internal override void  RenderStroke(SvgRenderer renderer)
		{
 			if (this.Stroke != null)
			{
				float strokeWidth = this.StrokeWidth.ToDeviceValue(this);
				using (var pen = new Pen(this.Stroke.GetBrush(this, this.StrokeOpacity), strokeWidth))
				{
					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();
					}

					if (this.MarkerStart != null)
					{
Eric Domke's avatar
Eric Domke committed
148
149
150
151
152
153
154
155
                        var marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerStart.ToString()); 
                        var markerPath = marker.Path.Clone() as GraphicsPath;

                        var transMatrix = new Matrix();
                        transMatrix.Scale(.5f, .5f);
                        transMatrix.RotateAt(90f, new PointF(marker.RefX.ToDeviceValue(), marker.RefY.ToDeviceValue()));
                        transMatrix.Translate(-1 * marker.RefX.ToDeviceValue(), -1 * marker.RefY.ToDeviceValue());

156
						markerPath.Transform(transMatrix);
Eric Domke's avatar
Eric Domke committed
157
                        pen.CustomStartCap = new CustomLineCap(markerPath, null);
158
159
160
161
					}

					if (this.MarkerEnd != null)
					{
Eric Domke's avatar
Eric Domke committed
162
                        var marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerEnd.ToString()); 
163
						var markerPath = marker.Path.Clone() as GraphicsPath;
Eric Domke's avatar
Eric Domke committed
164
165
166
167
168
169

                        var transMatrix = new Matrix();
                        transMatrix.Scale(.5f, .5f);
                        transMatrix.Rotate(90f);
                        transMatrix.Translate(-1 * marker.RefX.ToDeviceValue(), -1 * marker.RefY.ToDeviceValue());

170
						markerPath.Transform(transMatrix);
Eric Domke's avatar
Eric Domke committed
171
                        pen.CustomEndCap = new CustomLineCap(markerPath, null);
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
					}

					renderer.DrawPath(pen, this.Path);
				}
			}
		}


		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
200
201
    }
}