SvgCircle.cs 4.29 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>
Tebjan Halm's avatar
Tebjan Halm committed
81
82
        /// <value>The rectangular bounds of the circle.</value>
        public override RectangleF Bounds
davescriven's avatar
davescriven committed
83
        {
Tebjan Halm's avatar
Tebjan Halm committed
84
            get { return this.Path(null).GetBounds(); }
davescriven's avatar
davescriven committed
85
86
        }

87
88
89
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> representing this element.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
90
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
91
        {
92
            if ((this._path == null || this.IsPathDirty)	&& base.StrokeWidth > 0)
davescriven's avatar
davescriven committed
93
            {
94
95
96
97
98
99
100
101
102
103
							float halfStrokeWidth = base.StrokeWidth / 2;

							// If it is to render, don't need to consider stroke width.
							// i.e stroke width only to be considered when calculating boundary
							if (renderer != null)
							{
								halfStrokeWidth = 0;
								this.IsPathDirty = false;
							}

104
105
                _path = new GraphicsPath();
                _path.StartFigure();
106
107
108
								var center = this.Center.ToDeviceValue(renderer, this);
								var radius = this.Radius.ToDeviceValue(renderer, UnitRenderingType.Other, this) + halfStrokeWidth;
								_path.AddEllipse(center.X - radius, center.Y - radius, 2 * radius, 2 * radius);
109
                _path.CloseFigure();
110
            }
111
            return _path;
davescriven's avatar
davescriven committed
112
113
114
115
116
117
        }

        /// <summary>
        /// Renders the circle to the specified <see cref="Graphics"/> object.
        /// </summary>
        /// <param name="graphics">The graphics object.</param>
Eric Domke's avatar
Eric Domke committed
118
        protected override void Render(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
119
120
121
122
        {
            // Don't draw if there is no radius set
            if (this.Radius.Value > 0.0f)
            {
123
                base.Render(renderer);
davescriven's avatar
davescriven committed
124
125
126
127
128
129
130
131
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgCircle"/> class.
        /// </summary>
        public SvgCircle()
        {
Tebjan Halm's avatar
Tebjan Halm committed
132
133
            CenterX = new SvgUnit(0.0f);
            CenterY = new SvgUnit(0.0f);
davescriven's avatar
davescriven committed
134
        }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149


		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
150
151
    }
}