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

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

Eric Domke's avatar
Eric Domke committed
24
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
25
        {
26
            if (this._path == null || this.IsPathDirty)
davescriven's avatar
davescriven committed
27
            {
28
29
                this._path = new GraphicsPath();
                this._path.StartFigure();
davescriven's avatar
davescriven committed
30

31
32
                try
                {
Eric Domke's avatar
Eric Domke committed
33
34
                    var points = this.Points;
                    for (int i = 2; (i + 1) < points.Count; i += 2)
davescriven's avatar
davescriven committed
35
                    {
Eric Domke's avatar
Eric Domke committed
36
                        var endPoint = SvgUnit.GetDevicePoint(points[i], points[i + 1], renderer, this);
davescriven's avatar
davescriven committed
37

38
39
40
41
42
43
44
45
46
                      // 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;
                        }

47
48
49
                        //first line
                        if (_path.PointCount == 0)
                        {
Eric Domke's avatar
Eric Domke committed
50
                            _path.AddLine(SvgUnit.GetDevicePoint(points[i - 2], points[i - 1], renderer, this), endPoint);
51
52
53
54
                        }
                        else
                        {
                            _path.AddLine(_path.GetLastPoint(), endPoint);
davescriven's avatar
davescriven committed
55
56
57
                        }
                    }
                }
58
59
60
61
62
63
                catch
                {
                    Trace.TraceError("Error parsing points");
                }

                this._path.CloseFigure();
64
65
                if (renderer != null)
                  this.IsPathDirty = false;
66
            }
67
            return this._path;
davescriven's avatar
davescriven committed
68
69
        }

70
71
72
73
74
75
76
77
		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
78
			newObj.Points = new SvgPointCollection();
79
80
81
82
			foreach (var pt in this.Points)
				newObj.Points.Add(pt);
			return newObj;
		}
davescriven's avatar
davescriven committed
83
84
    }
}