SvgEllipse.cs 4.06 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
using System.Drawing.Drawing2D;

namespace Svg
{
5
6
7
    /// <summary>
    /// Represents and SVG ellipse element.
    /// </summary>
8
    [SvgElement("ellipse")]
9
    public class SvgEllipse : SvgPathBasedElement
davescriven's avatar
davescriven committed
10
11
12
13
14
15
16
17
18
19
20
21
22
    {
        private SvgUnit _radiusX;
        private SvgUnit _radiusY;
        private SvgUnit _centerX;
        private SvgUnit _centerY;
        private GraphicsPath _path;

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

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

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

        [SvgAttribute("ry")]
        public virtual SvgUnit RadiusY
        {
65
66
67
68
69
70
71
72
73
74
        	get { return this._radiusY; }
        	set
        	{
        		if(_radiusY != value)
        		{
        			this._radiusY = value;
        			this.IsPathDirty = true;
        			OnAttributeChanged(new AttributeEventArgs{ Attribute = "ry", Value = value });
        		}
        	}
davescriven's avatar
davescriven committed
75
76
77
78
79
80
        }

        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        /// <value></value>
Eric Domke's avatar
Eric Domke committed
81
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
82
        {
83
            if (this._path == null || this.IsPathDirty)
davescriven's avatar
davescriven committed
84
            {
85
86
87
88
89
90
91
92
93
94
							float halfStrokeWidth = base.StrokeWidth / 2;

							// 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)
							{
								halfStrokeWidth = 0;
								this.IsPathDirty = false;
							}

95
                var center = SvgUnit.GetDevicePoint(this._centerX, this._centerY, renderer, this);
96
								var radius = SvgUnit.GetDevicePoint(this._radiusX + halfStrokeWidth, this._radiusY + halfStrokeWidth, renderer, this);
davescriven's avatar
davescriven committed
97

98
99
100
101
                this._path = new GraphicsPath();
                _path.StartFigure();
                _path.AddEllipse(center.X - radius.X, center.Y - radius.Y, 2 * radius.X, 2 * radius.Y);
                _path.CloseFigure();
102
            }
103
            return _path;
davescriven's avatar
davescriven committed
104
105
106
107
108
109
        }

        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
        /// </summary>
        /// <param name="graphics">The <see cref="Graphics"/> object to render to.</param>
Eric Domke's avatar
Eric Domke committed
110
        protected override void Render(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
111
112
113
        {
            if (this._radiusX.Value > 0.0f && this._radiusY.Value > 0.0f)
            {
114
                base.Render(renderer);
davescriven's avatar
davescriven committed
115
116
117
118
119
120
121
122
123
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgEllipse"/> class.
        /// </summary>
        public SvgEllipse()
        {
        }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140



		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgEllipse>();
		}

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgEllipse;
			newObj.CenterX = this.CenterX;
			newObj.CenterY = this.CenterY;
			newObj.RadiusX = this.RadiusX;
			newObj.RadiusY = this.RadiusY;
			return newObj;
		}
davescriven's avatar
davescriven committed
141
142
    }
}