SvgPolygon.cs 5.13 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
        
davescriven's avatar
davescriven committed
19
20
21
22
        /// <summary>
        /// The points that make up the SvgPolygon
        /// </summary>
        [SvgAttribute("points")]
Eric Domke's avatar
Eric Domke committed
23
        public SvgPointCollection Points
davescriven's avatar
davescriven committed
24
        {
Eric Domke's avatar
Eric Domke committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
            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")]
        public Uri MarkerEnd
        {
            get { return this.Attributes.GetAttribute<Uri>("marker-end"); }
            set { this.Attributes["marker-end"] = value; }
        }


        /// <summary>
        /// Gets or sets the marker (start cap) of the path.
        /// </summary>
        [SvgAttribute("marker-mid")]
        public Uri MarkerMid
        {
            get { return this.Attributes.GetAttribute<Uri>("marker-mid"); }
            set { this.Attributes["marker-mid"] = value; }
        }


        /// <summary>
        /// Gets or sets the marker (start cap) of the path.
        /// </summary>
        [SvgAttribute("marker-start")]
        public Uri MarkerStart
        {
            get { return this.Attributes.GetAttribute<Uri>("marker-start"); }
            set { this.Attributes["marker-start"] = value; }
davescriven's avatar
davescriven committed
59
60
61
62
63
64
65
        }

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

Eric Domke's avatar
Eric Domke committed
66
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
67
        {
68
            if (this._path == null || this.IsPathDirty)
davescriven's avatar
davescriven committed
69
            {
70
71
                this._path = new GraphicsPath();
                this._path.StartFigure();
davescriven's avatar
davescriven committed
72

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

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

                this._path.CloseFigure();
                this.IsPathDirty = false;
98
            }
99
            return this._path;
davescriven's avatar
davescriven committed
100
101
        }

Eric Domke's avatar
Eric Domke committed
102
103
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
        /// <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;
        }

Dan Backes's avatar
Dan Backes committed
133
        public override RectangleF CalculateBounds()
davescriven's avatar
davescriven committed
134
        {
Dan Backes's avatar
Dan Backes committed
135
            return this.Path(null).GetBounds();
davescriven's avatar
davescriven committed
136
        }
137
138
139
140
141
142
143
144
145
146


		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
147
			newObj.Points = new SvgPointCollection();
148
149
150
151
			foreach (var pt in this.Points)
				newObj.Points.Add(pt);
			return newObj;
		}
davescriven's avatar
davescriven committed
152
153
    }
}