SvgCircle.cs 3.83 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
using System.Drawing.Drawing2D;

namespace Svg
{
    /// <summary>
    /// An SVG element to render circles to the document.
    /// </summary>
8
    [SvgElement("circle")]
9
    public class SvgCircle : SvgPathBasedElement
davescriven's avatar
davescriven committed
10
11
    {
        private GraphicsPath _path;
Tebjan Halm's avatar
Tebjan Halm committed
12
13
14
15
        
        private SvgUnit _radius;
        private SvgUnit _centerX;
        private SvgUnit _centerY;
davescriven's avatar
davescriven committed
16
17
18
19
20
21
22
23
24
25
26

        /// <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
27
        public virtual SvgUnit CenterX
davescriven's avatar
davescriven committed
28
        {
Tebjan Halm's avatar
Tebjan Halm committed
29
            get { return this._centerX; }
davescriven's avatar
davescriven committed
30
31
            set
            {
32
33
34
35
36
37
            	if(_centerX != value)
            	{
            		this._centerX = value;
            		this.IsPathDirty = true;
            		OnAttributeChanged(new AttributeEventArgs{ Attribute = "cx", Value = value });
            	}
davescriven's avatar
davescriven committed
38
39
40
41
            }
        }

        [SvgAttribute("cy")]
Tebjan Halm's avatar
Tebjan Halm committed
42
        public virtual SvgUnit CenterY
davescriven's avatar
davescriven committed
43
        {
44
45
46
47
48
49
50
51
52
53
        	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
54
55
56
        }

        [SvgAttribute("r")]
Tebjan Halm's avatar
Tebjan Halm committed
57
        public virtual SvgUnit Radius
davescriven's avatar
davescriven committed
58
        {
59
60
61
62
63
64
65
66
67
68
        	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
69
70
71
        }

        /// <summary>
72
73
        /// Gets the <see cref="GraphicsPath"/> representing this element.
        /// </summary>
Eric Domke's avatar
Eric Domke committed
74
        public override GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
75
        {
76
            if (this._path == null || this.IsPathDirty)
davescriven's avatar
davescriven committed
77
            {
78
79
80
81
82
83
84
85
86
87
							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;
							}

88
89
                _path = new GraphicsPath();
                _path.StartFigure();
90
91
92
								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);
93
                _path.CloseFigure();
94
            }
95
            return _path;
davescriven's avatar
davescriven committed
96
97
98
        }

        /// <summary>
99
        /// Renders the circle using the specified <see cref="ISvgRenderer"/> object.
davescriven's avatar
davescriven committed
100
        /// </summary>
101
        /// <param name="renderer">The renderer object.</param>
Eric Domke's avatar
Eric Domke committed
102
        protected override void Render(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
103
104
105
106
        {
            // Don't draw if there is no radius set
            if (this.Radius.Value > 0.0f)
            {
107
                base.Render(renderer);
davescriven's avatar
davescriven committed
108
109
110
111
112
113
114
115
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgCircle"/> class.
        /// </summary>
        public SvgCircle()
        {
Tebjan Halm's avatar
Tebjan Halm committed
116
117
            CenterX = new SvgUnit(0.0f);
            CenterY = new SvgUnit(0.0f);
davescriven's avatar
davescriven committed
118
        }
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133


		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
134
135
    }
}