SvgLine.cs 7.01 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.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
7
using Svg.ExtensionMethods;
davescriven's avatar
davescriven committed
8
9
10

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

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

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

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

        [SvgAttribute("y2")]
        public SvgUnit EndY
        {
            get { return this._endY; }
72
73
74
75
76
77
78
79
80
            set 
            { 
            	if(_endY != value)
            	{
            		this._endY = value;
            		this.IsPathDirty = true;
            		OnAttributeChanged(new AttributeEventArgs{ Attribute = "y2", Value = value });
            	}
            }
davescriven's avatar
davescriven committed
81
82
        }

Eric Domke's avatar
Eric Domke committed
83
84
85
86
87
88
        /// <summary>
        /// Gets or sets the marker (end cap) of the path.
        /// </summary>
        [SvgAttribute("marker-end")]
        public Uri MarkerEnd
        {
89
            get { return this.Attributes.GetAttribute<Uri>("marker-end").ReplaceWithNullIfNone(); }
Eric Domke's avatar
Eric Domke committed
90
91
92
93
94
95
96
97
98
99
            set { this.Attributes["marker-end"] = value; }
        }


        /// <summary>
        /// Gets or sets the marker (start cap) of the path.
        /// </summary>
        [SvgAttribute("marker-mid")]
        public Uri MarkerMid
        {
100
            get { return this.Attributes.GetAttribute<Uri>("marker-mid").ReplaceWithNullIfNone(); }
Eric Domke's avatar
Eric Domke committed
101
102
103
104
105
106
107
108
109
110
            set { this.Attributes["marker-mid"] = value; }
        }


        /// <summary>
        /// Gets or sets the marker (start cap) of the path.
        /// </summary>
        [SvgAttribute("marker-start")]
        public Uri MarkerStart
        {
111
	        get { return this.Attributes.GetAttribute<Uri>("marker-start").ReplaceWithNullIfNone(); }
Eric Domke's avatar
Eric Domke committed
112
113
114
            set { this.Attributes["marker-start"] = value; }
        }

davescriven's avatar
davescriven committed
115
116
117
118
119
120
121
122
123
124
125
126
127
        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
128
        public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
129
        {
130
            if ((this._path == null || this.IsPathDirty) && base.StrokeWidth > 0)
davescriven's avatar
davescriven committed
131
            {
132
133
134
135
                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
136

137
                this._path = new GraphicsPath();
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

                // 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();
                }
154
            }
155
            return this._path;
davescriven's avatar
davescriven committed
156
157
        }

Eric Domke's avatar
Eric Domke committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
        /// <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
189
        public override System.Drawing.RectangleF Bounds
davescriven's avatar
davescriven committed
190
        {
Tebjan Halm's avatar
Tebjan Halm committed
191
            get { return this.Path(null).GetBounds(); }
davescriven's avatar
davescriven committed
192
        }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

		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
212
213
    }
}