SvgPolygon.cs 5.77 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
34
35
            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
        {
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
45
46
            set { this.Attributes["marker-end"] = value; }
        }


        /// <summary>
        /// Gets or sets the marker (start cap) of the path.
        /// </summary>
        [SvgAttribute("marker-mid")]
        public Uri MarkerMid
        {
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
56
57
            set { this.Attributes["marker-mid"] = value; }
        }


        /// <summary>
        /// Gets or sets the marker (start cap) of the path.
        /// </summary>
        [SvgAttribute("marker-start")]
        public Uri MarkerStart
        {
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
62
63
64
65
66
        }

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

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

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

81
82
83
84
85
86
87
88
89
                      // 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;
                        }

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

                this._path.CloseFigure();
107
108
                if (renderer != null)
                  this.IsPathDirty = false;
109
            }
110
            return this._path;
davescriven's avatar
davescriven committed
111
112
        }

Eric Domke's avatar
Eric Domke committed
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
139
140
141
142
143
        /// <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
144
        public override RectangleF Bounds
davescriven's avatar
davescriven committed
145
        {
Tebjan Halm's avatar
Tebjan Halm committed
146
            get { return this.Path(null).GetBounds(); }
davescriven's avatar
davescriven committed
147
        }
148
149
150
151
152
153
154
155
156
157


		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
158
			newObj.Points = new SvgPointCollection();
159
160
161
162
			foreach (var pt in this.Points)
				newObj.Points.Add(pt);
			return newObj;
		}
davescriven's avatar
davescriven committed
163
164
    }
}