SvgPolygon.cs 2.57 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
        private GraphicsPath _path;
Eric Domke's avatar
Eric Domke committed
18
        private SvgPointCollection _points;
davescriven's avatar
davescriven committed
19
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
26
27
28
29
30
31
32
33
34
        {
            get { return this._points; }
            set { this._points = value; this.IsPathDirty = true; }
        }

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

Eric Domke's avatar
Eric Domke committed
35
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
36
        {
37
            if (this._path == null || this.IsPathDirty)
davescriven's avatar
davescriven committed
38
            {
39
40
                this._path = new GraphicsPath();
                this._path.StartFigure();
davescriven's avatar
davescriven committed
41

42
43
                try
                {
Eric Domke's avatar
Eric Domke committed
44
                    for (int i = 2; (i + 1) < this._points.Count; i += 2)
davescriven's avatar
davescriven committed
45
                    {
46
                        var endPoint = SvgUnit.GetDevicePoint(this._points[i], this._points[i+1], renderer, this);
davescriven's avatar
davescriven committed
47

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

                this._path.CloseFigure();
                this.IsPathDirty = false;
66
            }
67
            return this._path;
davescriven's avatar
davescriven committed
68
69
70
71
        }

        public override RectangleF Bounds
        {
72
            get { return this.Path(null).GetBounds(); }
davescriven's avatar
davescriven committed
73
        }
74
75
76
77
78
79
80
81
82
83


		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
84
			newObj.Points = new SvgPointCollection();
85
86
87
88
			foreach (var pt in this.Points)
				newObj.Points.Add(pt);
			return newObj;
		}
davescriven's avatar
davescriven committed
89
90
    }
}