SvgLine.cs 4.16 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Svg
{
6
7
8
    /// <summary>
    /// Represents and SVG line element.
    /// </summary>
9
    [SvgElement("line")]
10
    public class SvgLine : SvgMarkerElement
davescriven's avatar
davescriven committed
11
12
13
14
15
16
17
18
19
20
21
    {
        private SvgUnit _startX;
        private SvgUnit _startY;
        private SvgUnit _endX;
        private SvgUnit _endY;
        private GraphicsPath _path;

        [SvgAttribute("x1")]
        public SvgUnit StartX
        {
            get { return this._startX; }
22
23
24
25
26
27
28
29
30
            set 
            { 
            	if(_startX != value)
            	{
            		this._startX = value;
            		this.IsPathDirty = true;
            		OnAttributeChanged(new AttributeEventArgs{ Attribute = "x1", Value = value });
            	}
            }
davescriven's avatar
davescriven committed
31
32
33
34
35
36
        }

        [SvgAttribute("y1")]
        public SvgUnit StartY
        {
            get { return this._startY; }
37
38
39
40
41
42
43
44
45
            set 
            { 
            	if(_startY != value)
            	{
            		this._startY = value;
            		this.IsPathDirty = true;
            		OnAttributeChanged(new AttributeEventArgs{ Attribute = "y1", Value = value });
            	}
            }
davescriven's avatar
davescriven committed
46
47
48
49
50
51
        }

        [SvgAttribute("x2")]
        public SvgUnit EndX
        {
            get { return this._endX; }
52
53
54
55
56
57
58
59
60
            set 
            { 
            	if(_endX != value)
            	{
            		this._endX = value;
            		this.IsPathDirty = true;
            		OnAttributeChanged(new AttributeEventArgs{ Attribute = "x2", Value = value });
            	}
            }
davescriven's avatar
davescriven committed
61
62
63
64
65
66
        }

        [SvgAttribute("y2")]
        public SvgUnit EndY
        {
            get { return this._endY; }
67
68
69
70
71
72
73
74
75
            set 
            { 
            	if(_endY != value)
            	{
            		this._endY = value;
            		this.IsPathDirty = true;
            		OnAttributeChanged(new AttributeEventArgs{ Attribute = "y2", Value = value });
            	}
            }
davescriven's avatar
davescriven committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        }

        public override SvgPaintServer Fill
        {
            get { return null; /* Line can't have a fill */ }
            set
            {
                // Do nothing
            }
        }

        public SvgLine()
        {
        }

Eric Domke's avatar
Eric Domke committed
91
        public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
92
        {
93
            if ((this._path == null || this.IsPathDirty) && base.StrokeWidth > 0)
davescriven's avatar
davescriven committed
94
            {
95
96
97
98
                PointF start = new PointF(this.StartX.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this), 
                                          this.StartY.ToDeviceValue(renderer, UnitRenderingType.Vertical, this));
                PointF end = new PointF(this.EndX.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this), 
                                        this.EndY.ToDeviceValue(renderer, UnitRenderingType.Vertical, this));
davescriven's avatar
davescriven committed
99

100
                this._path = new GraphicsPath();
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

                // 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)
                {
                  this._path.AddLine(start, end);
                  this.IsPathDirty = false;
                }
                else
                {	 // only when calculating boundary 
                  _path.StartFigure();
                  var radius = base.StrokeWidth / 2;
                  _path.AddEllipse(start.X - radius, start.Y - radius, 2 * radius, 2 * radius);
                  _path.AddEllipse(end.X - radius, end.Y - radius, 2 * radius, 2 * radius);
                  _path.CloseFigure();
                }
117
            }
118
            return this._path;
davescriven's avatar
davescriven committed
119
120
        }

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgLine>();
		}

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgLine;
			newObj.StartX = this.StartX;
			newObj.EndX = this.EndX;
			newObj.StartY = this.StartY;
			newObj.EndY = this.EndY;
			if (this.Fill != null)
				newObj.Fill = this.Fill.DeepCopy() as SvgPaintServer;

			return newObj;
		}

davescriven's avatar
davescriven committed
139
140
    }
}