SvgPath.cs 6.76 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
using System;
using System.Collections.Generic;
using System.Text;
4
using System.Linq;
davescriven's avatar
davescriven committed
5
6
7
8
9
10
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
11
using Svg.Transforms;
davescriven's avatar
davescriven committed
12
13
14

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

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

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

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


Eric Domke's avatar
Eric Domke committed
60
61
62
63
64
65
66
67
68
        /// <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; }
        }
69
70


Eric Domke's avatar
Eric Domke committed
71
72
73
74
75
76
77
78
79
        /// <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; }
        }
80
81


davescriven's avatar
davescriven committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
        /// <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;
            }
102
103
104
105
            protected set
            {
                _path = value;
            }
davescriven's avatar
davescriven committed
106
107
108
109
110
        }

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

114
115
116
        /// <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
117
118
119
120
121
        protected override bool RequiresSmoothRendering
        {
            get { return true; }
        }

122
123
124
125
        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
davescriven's avatar
davescriven committed
126
127
128
129
130
        public override System.Drawing.RectangleF Bounds
        {
            get { return this.Path.GetBounds(); }
        }

131
132
133
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgPath"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
134
135
        public SvgPath()
        {
136
137
138
            var pathData = new SvgPathSegmentList();
            this.Attributes["d"] = pathData;
            pathData._owner = this;
davescriven's avatar
davescriven committed
139
        }
140

Eric Domke's avatar
Eric Domke committed
141
142
143
144
145
        /// <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)
146
        {
Eric Domke's avatar
Eric Domke committed
147
            if (this.Stroke != null)
148
            {
Eric Domke's avatar
Eric Domke committed
149
150
151
152
153
154
155
156
                float strokeWidth = this.StrokeWidth.ToDeviceValue(this);
                using (Pen 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();
                    }
157

Eric Domke's avatar
Eric Domke committed
158
                    renderer.DrawPath(pen, this.Path);
159

Eric Domke's avatar
Eric Domke committed
160
161
162
163
164
                    if (this.MarkerStart != null)
                    {
                        SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerStart.ToString());
                        marker.RenderMarker(renderer, this, Path.PathPoints[0], Path.PathPoints[0], Path.PathPoints[1]);
                    }
165

Eric Domke's avatar
Eric Domke committed
166
167
168
169
170
171
                    if (this.MarkerMid != null)
                    {
                        SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerMid.ToString());
                        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]);
                    }
172

Eric Domke's avatar
Eric Domke committed
173
174
175
176
177
178
179
180
                    if (this.MarkerEnd != null)
                    {
                        SvgMarker marker = this.OwnerDocument.GetElementById<SvgMarker>(this.MarkerEnd.ToString());
                        marker.RenderMarker(renderer, this, Path.PathPoints[Path.PathPoints.Length - 1], Path.PathPoints[Path.PathPoints.Length - 2], Path.PathPoints[Path.PathPoints.Length - 1]);
                    }
                }
            }
        }
181

Eric Domke's avatar
Eric Domke committed
182
183
184
185
        public override SvgElement DeepCopy()
        {
            return DeepCopy<SvgPath>();
        }
186

Eric Domke's avatar
Eric Domke committed
187
188
189
190
191
192
193
194
195
196
        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
197
198
    }
}