SvgPolygon.cs 5.67 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)	&& base.StrokeWidth > 0)
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
83
84
85
86
87
88
                      // 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;
                        }

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

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

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


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