SvgPolyline.cs 2.01 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;

namespace Svg
{
    /// <summary>
9
    /// SvgPolyline defines a set of connected straight line segments. Typically, <see cref="SvgPolyline"/> defines open shapes.
davescriven's avatar
davescriven committed
10
    /// </summary>
11
    [SvgElement("polyline")]
davescriven's avatar
davescriven committed
12
13
    public class SvgPolyline : SvgPolygon
    {
14
        private GraphicsPath _Path;
Eric Domke's avatar
Eric Domke committed
15
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
16
        {
17
            if ((_Path == null || this.IsPathDirty) && base.StrokeWidth > 0)
davescriven's avatar
davescriven committed
18
            {
19
                _Path = new GraphicsPath();
davescriven's avatar
davescriven committed
20

21
22
                try
                {
Eric Domke's avatar
Eric Domke committed
23
                    for (int i = 0; (i + 1) < Points.Count; i += 2)
davescriven's avatar
davescriven committed
24
                    {
25
26
                        PointF endPoint = new PointF(Points[i].ToDeviceValue(renderer, UnitRenderingType.Horizontal, this), 
                                                     Points[i + 1].ToDeviceValue(renderer, UnitRenderingType.Vertical, this));
davescriven's avatar
davescriven committed
27

28
29
30
31
32
33
34
                        if (renderer == null)
                        {
                          var radius = base.StrokeWidth / 2;
                          _Path.AddEllipse(endPoint.X - radius, endPoint.Y - radius, 2 * radius, 2 * radius);
                          continue;
                        }

35
36
37
38
39
40
41
42
                        // TODO: Remove unrequired first line
                        if (_Path.PointCount == 0)
                        {
                            _Path.AddLine(endPoint, endPoint);
                        }
                        else
                        {
                            _Path.AddLine(_Path.GetLastPoint(), endPoint);
davescriven's avatar
davescriven committed
43
44
45
                        }
                    }
                }
46
47
48
49
                catch (Exception exc)
                {
                    Trace.TraceError("Error rendering points: " + exc.Message);
                }
50
51
                if (renderer != null)
                  this.IsPathDirty = false;
davescriven's avatar
davescriven committed
52
            }
53
            return _Path;
davescriven's avatar
davescriven committed
54
55
56
        }
    }
}