SvgCircle.cs 4.48 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Svg
{
    /// <summary>
    /// An SVG element to render circles to the document.
    /// </summary>
15
    [SvgElement("circle")]
16
    public class SvgCircle : SvgVisualElement
davescriven's avatar
davescriven committed
17
18
    {
        private GraphicsPath _path;
Tebjan Halm's avatar
Tebjan Halm committed
19
20
21
22
        
        private SvgUnit _radius;
        private SvgUnit _centerX;
        private SvgUnit _centerY;
davescriven's avatar
davescriven committed
23
24
25
26
27
28
29
30
31
32
33

        /// <summary>
        /// Gets the center point of the circle.
        /// </summary>
        /// <value>The center.</value>
        public SvgPoint Center
        {
            get { return new SvgPoint(this.CenterX, this.CenterY); }
        }

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

        [SvgAttribute("cy")]
Tebjan Halm's avatar
Tebjan Halm committed
49
        public virtual SvgUnit CenterY
davescriven's avatar
davescriven committed
50
        {
51
52
53
54
55
56
57
58
59
60
        	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
61
62
63
        }

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

        /// <summary>
        /// Gets the bounds of the circle.
        /// </summary>
81
        /// <value>The rectangular bounds of the circle.</value>
davescriven's avatar
davescriven committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        public override RectangleF Bounds
        {
            get { return this.Path.GetBounds(); }
        }

        /// <summary>
        /// Gets a value indicating whether the circle requires anti-aliasing when being rendered.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the circle requires anti-aliasing; otherwise, <c>false</c>.
        /// </value>
        protected override bool RequiresSmoothRendering
        {
            get { return true; }
        }

98
99
100
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> representing this element.
        /// </summary>
davescriven's avatar
davescriven committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        public override GraphicsPath Path
        {
            get
            {
                if (this._path == null || this.IsPathDirty)
                {
                    _path = new GraphicsPath();
                    _path.StartFigure();
                    _path.AddEllipse(this.Center.ToDeviceValue().X - this.Radius.ToDeviceValue(), this.Center.ToDeviceValue().Y - this.Radius.ToDeviceValue(), 2 * this.Radius.ToDeviceValue(), 2 * this.Radius.ToDeviceValue());
                    _path.CloseFigure();
                    this.IsPathDirty = false;
                }
                return _path;
            }
115
116
117
118
            protected set
            {
                _path = value;
            }
davescriven's avatar
davescriven committed
119
120
121
122
123
124
        }

        /// <summary>
        /// Renders the circle to the specified <see cref="Graphics"/> object.
        /// </summary>
        /// <param name="graphics">The graphics object.</param>
125
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
126
127
128
129
        {
            // Don't draw if there is no radius set
            if (this.Radius.Value > 0.0f)
            {
130
                base.Render(renderer);
davescriven's avatar
davescriven committed
131
132
133
134
135
136
137
138
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgCircle"/> class.
        /// </summary>
        public SvgCircle()
        {
Tebjan Halm's avatar
Tebjan Halm committed
139
140
            CenterX = new SvgUnit(0.0f);
            CenterY = new SvgUnit(0.0f);
davescriven's avatar
davescriven committed
141
        }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156


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

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgCircle;
			newObj.CenterX = this.CenterX;
			newObj.CenterY = this.CenterY;
			newObj.Radius = this.Radius;
			return newObj;
		}
davescriven's avatar
davescriven committed
157
158
    }
}