SvgCircle.cs 4.02 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
            {
Tebjan Halm's avatar
Tebjan Halm committed
39
40
                this._centerX = value;
                this.IsPathDirty = true;
davescriven's avatar
davescriven committed
41
42
43
44
            }
        }

        [SvgAttribute("cy")]
Tebjan Halm's avatar
Tebjan Halm committed
45
        public virtual SvgUnit CenterY
davescriven's avatar
davescriven committed
46
        {
Tebjan Halm's avatar
Tebjan Halm committed
47
            get { return this._centerY; }
davescriven's avatar
davescriven committed
48
49
            set
            {
Tebjan Halm's avatar
Tebjan Halm committed
50
51
                this._centerY = value;
                this.IsPathDirty = true;
davescriven's avatar
davescriven committed
52
53
54
55
            }
        }

        [SvgAttribute("r")]
Tebjan Halm's avatar
Tebjan Halm committed
56
        public virtual SvgUnit Radius
davescriven's avatar
davescriven committed
57
        {
Tebjan Halm's avatar
Tebjan Halm committed
58
59
            get { return this._radius; }
            set { this._radius = value; this.IsPathDirty = true; }
davescriven's avatar
davescriven committed
60
61
62
63
64
        }

        /// <summary>
        /// Gets the bounds of the circle.
        /// </summary>
65
        /// <value>The rectangular bounds of the circle.</value>
davescriven's avatar
davescriven committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        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; }
        }

82
83
84
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> representing this element.
        /// </summary>
davescriven's avatar
davescriven committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        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;
            }
99
100
101
102
            protected set
            {
                _path = value;
            }
davescriven's avatar
davescriven committed
103
104
105
106
107
108
        }

        /// <summary>
        /// Renders the circle to the specified <see cref="Graphics"/> object.
        /// </summary>
        /// <param name="graphics">The graphics object.</param>
109
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
110
111
112
113
        {
            // Don't draw if there is no radius set
            if (this.Radius.Value > 0.0f)
            {
114
                base.Render(renderer);
davescriven's avatar
davescriven committed
115
116
117
118
119
120
121
122
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgCircle"/> class.
        /// </summary>
        public SvgCircle()
        {
Tebjan Halm's avatar
Tebjan Halm committed
123
124
            CenterX = new SvgUnit(0.0f);
            CenterY = new SvgUnit(0.0f);
davescriven's avatar
davescriven committed
125
        }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140


		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
141
142
    }
}