SvgPolygon.cs 5.65 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;
7
using Svg.ExtensionMethods;
davescriven's avatar
davescriven committed
8
9
10
11
12
13
14
using Svg.Pathing;

namespace Svg
{
    /// <summary>
    /// SvgPolygon defines a closed shape consisting of a set of connected straight line segments.
    /// </summary>
15
    [SvgElement("polygon")]
16
    public class SvgPolygon : SvgVisualElement
davescriven's avatar
davescriven committed
17
    {
18
        private GraphicsPath _path;
Eric Domke's avatar
Eric Domke committed
19
        
davescriven's avatar
davescriven committed
20
21
22
23
        /// <summary>
        /// The points that make up the SvgPolygon
        /// </summary>
        [SvgAttribute("points")]
Eric Domke's avatar
Eric Domke committed
24
        public SvgPointCollection Points
davescriven's avatar
davescriven committed
25
        {
Eric Domke's avatar
Eric Domke committed
26
27
28
29
30
31
32
33
            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")]
34
        public virtual Uri MarkerEnd
Eric Domke's avatar
Eric Domke committed
35
        {
36
            get { return this.Attributes.GetAttribute<Uri>("marker-end").ReplaceWithNullIfNone(); }
Eric Domke's avatar
Eric Domke committed
37
38
39
40
41
42
43
44
            set { this.Attributes["marker-end"] = value; }
        }


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


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

Eric Domke's avatar
Eric Domke committed
62
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
63
        {
64
            if (this._path == null || this.IsPathDirty)
davescriven's avatar
davescriven committed
65
            {
66
67
                this._path = new GraphicsPath();
                this._path.StartFigure();
davescriven's avatar
davescriven committed
68

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

76
77
78
79
80
81
82
83
84
                      // 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;
                        }

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

                this._path.CloseFigure();
102
103
                if (renderer != null)
                  this.IsPathDirty = false;
104
            }
105
            return this._path;
davescriven's avatar
davescriven committed
106
107
        }

Eric Domke's avatar
Eric Domke committed
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
135
136
137
138
        /// <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;
        }

Tebjan Halm's avatar
Tebjan Halm committed
139
        public override RectangleF Bounds
davescriven's avatar
davescriven committed
140
        {
Tebjan Halm's avatar
Tebjan Halm committed
141
            get { return this.Path(null).GetBounds(); }
davescriven's avatar
davescriven committed
142
        }
143
144
145
146
147
148
149
150
151
152


		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
153
			newObj.Points = new SvgPointCollection();
154
155
156
157
			foreach (var pt in this.Points)
				newObj.Points.Add(pt);
			return newObj;
		}
davescriven's avatar
davescriven committed
158
159
    }
}