SvgPolyline.cs 4.71 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

namespace Svg
{
    /// <summary>
12
    /// SvgPolyline defines a set of connected straight line segments. Typically, <see cref="SvgPolyline"/> defines open shapes.
davescriven's avatar
davescriven committed
13
    /// </summary>
14
    [SvgElement("polyline")]
davescriven's avatar
davescriven committed
15
16
    public class SvgPolyline : SvgPolygon
    {
Eric Domke's avatar
Eric Domke committed
17
18
19
20
        /// <summary>
        /// Gets or sets the marker (end cap) of the path.
        /// </summary>
        [SvgAttribute("marker-end")]
21
        public override Uri MarkerEnd
Eric Domke's avatar
Eric Domke committed
22
        {
23
            get { return this.Attributes.GetAttribute<Uri>("marker-end").ReplaceWithNullIfNone(); }
Eric Domke's avatar
Eric Domke committed
24
25
26
27
28
29
30
31
            set { this.Attributes["marker-end"] = value; }
        }


        /// <summary>
        /// Gets or sets the marker (start cap) of the path.
        /// </summary>
        [SvgAttribute("marker-mid")]
32
        public override Uri MarkerMid
Eric Domke's avatar
Eric Domke committed
33
        {
34
            get { return this.Attributes.GetAttribute<Uri>("marker-mid").ReplaceWithNullIfNone(); }
Eric Domke's avatar
Eric Domke committed
35
36
37
38
39
40
41
42
            set { this.Attributes["marker-mid"] = value; }
        }


        /// <summary>
        /// Gets or sets the marker (start cap) of the path.
        /// </summary>
        [SvgAttribute("marker-start")]
43
        public override Uri MarkerStart
Eric Domke's avatar
Eric Domke committed
44
        {
45
            get { return this.Attributes.GetAttribute<Uri>("marker-start").ReplaceWithNullIfNone(); }
Eric Domke's avatar
Eric Domke committed
46
47
48
            set { this.Attributes["marker-start"] = value; }
        }

49
        private GraphicsPath _Path;
Eric Domke's avatar
Eric Domke committed
50
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
51
        {
52
            if ((_Path == null || this.IsPathDirty) && base.StrokeWidth > 0)
davescriven's avatar
davescriven committed
53
            {
54
                _Path = new GraphicsPath();
davescriven's avatar
davescriven committed
55

56
57
                try
                {
Eric Domke's avatar
Eric Domke committed
58
                    for (int i = 0; (i + 1) < Points.Count; i += 2)
davescriven's avatar
davescriven committed
59
                    {
60
61
                        PointF endPoint = new PointF(Points[i].ToDeviceValue(renderer, UnitRenderingType.Horizontal, this), 
                                                     Points[i + 1].ToDeviceValue(renderer, UnitRenderingType.Vertical, this));
davescriven's avatar
davescriven committed
62

63
64
65
66
67
68
69
                        if (renderer == null)
                        {
                          var radius = base.StrokeWidth / 2;
                          _Path.AddEllipse(endPoint.X - radius, endPoint.Y - radius, 2 * radius, 2 * radius);
                          continue;
                        }

70
71
72
73
74
75
76
77
                        // TODO: Remove unrequired first line
                        if (_Path.PointCount == 0)
                        {
                            _Path.AddLine(endPoint, endPoint);
                        }
                        else
                        {
                            _Path.AddLine(_Path.GetLastPoint(), endPoint);
davescriven's avatar
davescriven committed
78
79
80
                        }
                    }
                }
81
82
83
84
                catch (Exception exc)
                {
                    Trace.TraceError("Error rendering points: " + exc.Message);
                }
85
86
                if (renderer != null)
                  this.IsPathDirty = false;
davescriven's avatar
davescriven committed
87
            }
88
            return _Path;
davescriven's avatar
davescriven committed
89
        }
Eric Domke's avatar
Eric Domke committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

        /// <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;
        }
davescriven's avatar
davescriven committed
121
122
    }
}