SvgCircle.cs 4.74 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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    {
        private GraphicsPath _path;

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

        /// <summary>
        /// Gets or sets the center X co-ordinate.
        /// </summary>
        /// <value>The center X.</value>
        [SvgAttribute("cx")]
        public SvgUnit CenterX
        {
36
            get { return this.Attributes.GetAttribute<SvgUnit>("cx"); }
davescriven's avatar
davescriven committed
37
38
            set
            {
39
40
41
42
43
                if (this.Attributes.GetAttribute<SvgUnit>("cx") != value)
                {
                    this.Attributes["cx"] = value;
                    this.IsPathDirty = true;
                }
davescriven's avatar
davescriven committed
44
45
46
47
48
49
50
51
52
53
            }
        }

        /// <summary>
        /// Gets or sets the center Y co-ordinate.
        /// </summary>
        /// <value>The center Y.</value>
        [SvgAttribute("cy")]
        public SvgUnit CenterY
        {
54
            get { return this.Attributes.GetAttribute<SvgUnit>("cy"); }
davescriven's avatar
davescriven committed
55
56
            set
            {
57
58
59
60
61
                if (this.Attributes.GetAttribute<SvgUnit>("cy") != value)
                {
                    this.Attributes["cy"] = value;
                    this.IsPathDirty = true;
                }
davescriven's avatar
davescriven committed
62
63
64
65
66
67
68
69
70
71
            }
        }

        /// <summary>
        /// Gets or sets the radius of the circle.
        /// </summary>
        /// <value>The radius.</value>
        [SvgAttribute("r")]
        public SvgUnit Radius
        {
72
            get { return this.Attributes.GetAttribute<SvgUnit>("r"); }
davescriven's avatar
davescriven committed
73
74
            set
            {
75
76
77
78
79
                if (this.Attributes.GetAttribute<SvgUnit>("r") != value)
                {
                    this.Attributes["r"] = value;
                    this.IsPathDirty = true;
                }
davescriven's avatar
davescriven committed
80
81
82
83
84
85
            }
        }

        /// <summary>
        /// Gets the bounds of the circle.
        /// </summary>
86
        /// <value>The rectangular bounds of the circle.</value>
davescriven's avatar
davescriven committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
        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; }
        }

103
104
105
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> representing this element.
        /// </summary>
davescriven's avatar
davescriven committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        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;
            }
        }

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

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


		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
158
159
    }
}