SvgCircle.cs 4.33 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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>
    public class SvgCircle : SvgGraphicsElement
    {
        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
        {
35
            get { return this.Attributes.GetAttribute<SvgUnit>("cx"); }
davescriven's avatar
davescriven committed
36
37
            set
            {
38
39
40
41
42
                if (this.Attributes.GetAttribute<SvgUnit>("cx") != value)
                {
                    this.Attributes["cx"] = value;
                    this.IsPathDirty = true;
                }
davescriven's avatar
davescriven committed
43
44
45
46
47
48
49
50
51
52
            }
        }

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

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

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

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

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgCircle"/> class.
        /// </summary>
        public SvgCircle()
        {
            
        }
    }
}