SvgEllipse.cs 4.64 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml;
using System.ComponentModel;

namespace Svg
{
11
12
13
    /// <summary>
    /// Represents and SVG ellipse element.
    /// </summary>
14
    [SvgElement("ellipse")]
15
    public class SvgEllipse : SvgVisualElement
davescriven's avatar
davescriven committed
16
17
18
19
20
21
22
23
24
25
26
27
28
    {
        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
            {
29
30
31
32
33
34
            	if(_centerX != value)
            	{
            		this._centerX = value;
            		this.IsPathDirty = true;
            		OnAttributeChanged(new AttributeEventArgs{ Attribute = "cx", Value = value });
            	}
davescriven's avatar
davescriven committed
35
36
37
38
39
40
            }
        }

        [SvgAttribute("cy")]
        public virtual SvgUnit CenterY
        {
41
42
43
44
45
46
47
48
49
50
        	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
51
52
53
54
55
        }

        [SvgAttribute("rx")]
        public virtual SvgUnit RadiusX
        {
56
57
58
59
60
61
62
63
64
65
        	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
66
67
68
69
70
        }

        [SvgAttribute("ry")]
        public virtual SvgUnit RadiusY
        {
71
72
73
74
75
76
77
78
79
80
        	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
81
82
83
84
85
86
87
88
89
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
121
        }

        /// <summary>
        /// Gets or sets a value to determine if anti-aliasing should occur when the element is being rendered.
        /// </summary>
        /// <value></value>
        protected override bool RequiresSmoothRendering
        {
            get { return true; }
        }

        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
        public override RectangleF Bounds
        {
            get { return this.Path.GetBounds(); }
        }

        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        /// <value></value>
        public override GraphicsPath Path
        {
            get
            {
                if (this._path == null || this.IsPathDirty)
                {
                    PointF center = new PointF(this._centerX.ToDeviceValue(this), this._centerY.ToDeviceValue(this, true));
                    PointF radius = new PointF(this._radiusX.ToDeviceValue(this), this._radiusY.ToDeviceValue(this, true));

                    this._path = new GraphicsPath();
                    _path.StartFigure();
                    _path.AddEllipse(center.X - radius.X, center.Y - radius.Y, 2 * radius.X, 2 * radius.Y);
                    _path.CloseFigure();
                    this.IsPathDirty = false;
                }
                return _path;
            }
122
123
124
125
            protected set
            {
                _path = value;
            }
davescriven's avatar
davescriven committed
126
127
128
129
130
131
        }

        /// <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>
132
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
133
134
135
        {
            if (this._radiusX.Value > 0.0f && this._radiusY.Value > 0.0f)
            {
136
                base.Render(renderer);
davescriven's avatar
davescriven committed
137
138
139
140
141
142
143
144
145
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgEllipse"/> class.
        /// </summary>
        public SvgEllipse()
        {
        }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162



		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
163
164
    }
}