SvgPolygon.cs 5.44 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
using System;
using System.Drawing.Drawing2D;
using System.Diagnostics;
4
using Svg.ExtensionMethods;
davescriven's avatar
davescriven committed
5
6
7
8
9
10

namespace Svg
{
    /// <summary>
    /// SvgPolygon defines a closed shape consisting of a set of connected straight line segments.
    /// </summary>
11
    [SvgElement("polygon")]
12
    public class SvgPolygon : SvgPathBasedElement
davescriven's avatar
davescriven committed
13
    {
14
        private GraphicsPath _path;
Eric Domke's avatar
Eric Domke committed
15
        
davescriven's avatar
davescriven committed
16
17
18
19
        /// <summary>
        /// The points that make up the SvgPolygon
        /// </summary>
        [SvgAttribute("points")]
Eric Domke's avatar
Eric Domke committed
20
        public SvgPointCollection Points
davescriven's avatar
davescriven committed
21
        {
Eric Domke's avatar
Eric Domke committed
22
23
24
25
26
27
28
29
            get { return this.Attributes["points"] as SvgPointCollection; }
            set { this.Attributes["points"] = value; this.IsPathDirty = true; }
        }

        /// <summary>
        /// Gets or sets the marker (end cap) of the path.
        /// </summary>
        [SvgAttribute("marker-end")]
30
        public virtual Uri MarkerEnd
Eric Domke's avatar
Eric Domke committed
31
        {
32
            get { return this.Attributes.GetAttribute<Uri>("marker-end").ReplaceWithNullIfNone(); }
Eric Domke's avatar
Eric Domke committed
33
34
35
36
37
38
39
40
            set { this.Attributes["marker-end"] = value; }
        }


        /// <summary>
        /// Gets or sets the marker (start cap) of the path.
        /// </summary>
        [SvgAttribute("marker-mid")]
41
        public virtual Uri MarkerMid
Eric Domke's avatar
Eric Domke committed
42
        {
43
            get { return this.Attributes.GetAttribute<Uri>("marker-mid").ReplaceWithNullIfNone(); }
Eric Domke's avatar
Eric Domke committed
44
45
46
47
48
49
50
51
            set { this.Attributes["marker-mid"] = value; }
        }


        /// <summary>
        /// Gets or sets the marker (start cap) of the path.
        /// </summary>
        [SvgAttribute("marker-start")]
52
        public virtual Uri MarkerStart
Eric Domke's avatar
Eric Domke committed
53
        {
54
            get { return this.Attributes.GetAttribute<Uri>("marker-start").ReplaceWithNullIfNone(); }
Eric Domke's avatar
Eric Domke committed
55
            set { this.Attributes["marker-start"] = value; }
davescriven's avatar
davescriven committed
56
57
        }

Eric Domke's avatar
Eric Domke committed
58
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
59
        {
60
            if (this._path == null || this.IsPathDirty)
davescriven's avatar
davescriven committed
61
            {
62
63
                this._path = new GraphicsPath();
                this._path.StartFigure();
davescriven's avatar
davescriven committed
64

65
66
                try
                {
Eric Domke's avatar
Eric Domke committed
67
68
                    var points = this.Points;
                    for (int i = 2; (i + 1) < points.Count; i += 2)
davescriven's avatar
davescriven committed
69
                    {
Eric Domke's avatar
Eric Domke committed
70
                        var endPoint = SvgUnit.GetDevicePoint(points[i], points[i + 1], renderer, this);
davescriven's avatar
davescriven committed
71

72
73
74
75
76
77
78
79
80
                      // If it is to render, don't need to consider stroke width.
                        // i.e stroke width only to be considered when calculating boundary
                        if (renderer == null)
                        {
                          var radius = base.StrokeWidth / 2;
                          _path.AddEllipse(endPoint.X - radius, endPoint.Y - radius, 2 * radius, 2 * radius);
                          continue;
                        }

81
82
83
                        //first line
                        if (_path.PointCount == 0)
                        {
Eric Domke's avatar
Eric Domke committed
84
                            _path.AddLine(SvgUnit.GetDevicePoint(points[i - 2], points[i - 1], renderer, this), endPoint);
85
86
87
88
                        }
                        else
                        {
                            _path.AddLine(_path.GetLastPoint(), endPoint);
davescriven's avatar
davescriven committed
89
90
91
                        }
                    }
                }
92
93
94
95
96
97
                catch
                {
                    Trace.TraceError("Error parsing points");
                }

                this._path.CloseFigure();
98
99
                if (renderer != null)
                  this.IsPathDirty = false;
100
            }
101
            return this._path;
davescriven's avatar
davescriven committed
102
103
        }

Eric Domke's avatar
Eric Domke committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        /// <summary>
        /// Renders the stroke of the <see cref="SvgVisualElement"/> to the specified <see cref="ISvgRenderer"/>
        /// </summary>
        /// <param name="renderer">The <see cref="ISvgRenderer"/> object to render to.</param>
        protected internal override bool RenderStroke(ISvgRenderer renderer)
        {
            var result = base.RenderStroke(renderer);
            var path = this.Path(renderer);

            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]);
            }

            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]);
            }

            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]);
            }

            return result;
        }

135
136
137
138
139
140
141
142
		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgPolygon>();
		}

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgPolygon;
Eric Domke's avatar
Eric Domke committed
143
			newObj.Points = new SvgPointCollection();
144
145
146
147
			foreach (var pt in this.Points)
				newObj.Points.Add(pt);
			return newObj;
		}
davescriven's avatar
davescriven committed
148
149
    }
}