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

namespace Svg
{
    /// <summary>
    /// SvgPolygon defines a closed shape consisting of a set of connected straight line segments.
    /// </summary>
14
    [SvgElement("polygon")]
15
    public class SvgPolygon : SvgVisualElement
davescriven's avatar
davescriven committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    {
        protected GraphicsPath _path;
        protected SvgUnitCollection _points;

        /// <summary>
        /// The points that make up the SvgPolygon
        /// </summary>
        [SvgAttribute("points")]
        public SvgUnitCollection Points
        {
            get { return this._points; }
            set { this._points = value; this.IsPathDirty = true; }
        }

        protected override bool RequiresSmoothRendering
        {
            get { return true; }
        }

        public override GraphicsPath Path
        {
            get
            {
                if (this._path == null || this.IsPathDirty)
                {
                    this._path = new GraphicsPath();
                    this._path.StartFigure();

                    try
                    {
46
                        for (int i = 2; i < this._points.Count; i+=2)
davescriven's avatar
davescriven committed
47
48
49
                        {
                            PointF endPoint = new PointF(this._points[i].ToDeviceValue(this), this._points[i+1].ToDeviceValue(this));

50
                            //first line
davescriven's avatar
davescriven committed
51
52
                            if (_path.PointCount == 0)
                            {
53
                                _path.AddLine(new PointF(this._points[i-2].ToDeviceValue(this), this._points[i-1].ToDeviceValue(this)), endPoint);
davescriven's avatar
davescriven committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
                            }
                            else
                            {
                                _path.AddLine(_path.GetLastPoint(), endPoint);
                            }
                        }
                    }
                    catch
                    {
                        Trace.TraceError("Error parsing points");
                    }

                    this._path.CloseFigure();
                    this.IsPathDirty = false;
                }
                return this._path;
            }
        }

        public override RectangleF Bounds
        {
            get { return this.Path.GetBounds(); }
        }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91


		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgPolygon>();
		}

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgPolygon;
			newObj.Points = new SvgUnitCollection();
			foreach (var pt in this.Points)
				newObj.Points.Add(pt);
			return newObj;
		}
davescriven's avatar
davescriven committed
92
93
    }
}