SvgRectangle.cs 9.22 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
4
using Svg.Transforms;
davescriven's avatar
davescriven committed
5
6
7

namespace Svg
{
8
9
10
    /// <summary>
    /// Represents and SVG rectangle that could also have reounded edges.
    /// </summary>
11
    [SvgElement("rect")]
12
    public class SvgRectangle : SvgVisualElement
davescriven's avatar
davescriven committed
13
14
15
    {
        private SvgUnit _cornerRadiusX;
        private SvgUnit _cornerRadiusY;
16
        private SvgUnit _height;
davescriven's avatar
davescriven committed
17
        private GraphicsPath _path;
18
19
20
21
        private SvgUnit _width;
        private SvgUnit _x;
        private SvgUnit _y;

22
23
24
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgRectangle"/> class.
        /// </summary>
25
26
27
28
        public SvgRectangle()
        {
            _width = new SvgUnit(0.0f);
            _height = new SvgUnit(0.0f);
29
30
            _cornerRadiusX = new SvgUnit(0.0f);
            _cornerRadiusY = new SvgUnit(0.0f);
31
        }
davescriven's avatar
davescriven committed
32

33
34
35
        /// <summary>
        /// Gets an <see cref="SvgPoint"/> representing the top left point of the rectangle.
        /// </summary>
davescriven's avatar
davescriven committed
36
37
        public SvgPoint Location
        {
38
39
40
            get { return new SvgPoint(X, Y); }
        }

41
42
43
        /// <summary>
        /// Gets or sets the position where the left point of the rectangle should start.
        /// </summary>
davescriven's avatar
davescriven committed
44
45
46
        [SvgAttribute("x")]
        public SvgUnit X
        {
47
48
49
50
51
52
            get { return _x; }
            set
            {
                _x = value;
                IsPathDirty = true;
            }
davescriven's avatar
davescriven committed
53
54
        }

55
56
57
        /// <summary>
        /// Gets or sets the position where the top point of the rectangle should start.
        /// </summary>
davescriven's avatar
davescriven committed
58
59
60
        [SvgAttribute("y")]
        public SvgUnit Y
        {
61
62
63
64
65
66
            get { return _y; }
            set
            {
                _y = value;
                IsPathDirty = true;
            }
davescriven's avatar
davescriven committed
67
68
        }

69
70
71
        /// <summary>
        /// Gets or sets the width of the rectangle.
        /// </summary>
davescriven's avatar
davescriven committed
72
73
74
        [SvgAttribute("width")]
        public SvgUnit Width
        {
75
76
77
78
79
80
            get { return _width; }
            set
            {
                _width = value;
                IsPathDirty = true;
            }
davescriven's avatar
davescriven committed
81
82
        }

83
84
85
        /// <summary>
        /// Gets or sets the height of the rectangle.
        /// </summary>
davescriven's avatar
davescriven committed
86
87
88
        [SvgAttribute("height")]
        public SvgUnit Height
        {
89
90
91
92
93
94
            get { return _height; }
            set
            {
                _height = value;
                IsPathDirty = true;
            }
davescriven's avatar
davescriven committed
95
96
        }

97
98
99
        /// <summary>
        /// Gets or sets the X-radius of the rounded edges of this rectangle.
        /// </summary>
davescriven's avatar
davescriven committed
100
101
102
103
104
105
        [SvgAttribute("rx")]
        public SvgUnit CornerRadiusX
        {
            get
            {
                // If ry has been set and rx hasn't, use it's value
106
107
                if (_cornerRadiusX.Value == 0.0f && _cornerRadiusY.Value > 0.0f)
                    return _cornerRadiusY;
davescriven's avatar
davescriven committed
108

109
110
111
112
113
114
                return _cornerRadiusX;
            }
            set
            {
                _cornerRadiusX = value;
                IsPathDirty = true;
davescriven's avatar
davescriven committed
115
116
117
            }
        }

118
119
120
        /// <summary>
        /// Gets or sets the Y-radius of the rounded edges of this rectangle.
        /// </summary>
davescriven's avatar
davescriven committed
121
122
123
124
125
126
        [SvgAttribute("ry")]
        public SvgUnit CornerRadiusY
        {
            get
            {
                // If rx has been set and ry hasn't, use it's value
127
128
                if (_cornerRadiusY.Value == 0.0f && _cornerRadiusX.Value > 0.0f)
                    return _cornerRadiusX;
davescriven's avatar
davescriven committed
129

130
                return _cornerRadiusY;
davescriven's avatar
davescriven committed
131
132
133
            }
            set
            {
134
135
                _cornerRadiusY = value;
                IsPathDirty = true;
davescriven's avatar
davescriven committed
136
137
138
            }
        }

139
140
141
        /// <summary>
        /// Gets or sets a value to determine if anti-aliasing should occur when the element is being rendered.
        /// </summary>
davescriven's avatar
davescriven committed
142
143
        protected override bool RequiresSmoothRendering
        {
144
            get { return (CornerRadiusX.Value > 0 || CornerRadiusY.Value > 0); }
davescriven's avatar
davescriven committed
145
146
        }

147
148
149
150
        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
davescriven's avatar
davescriven committed
151
152
        public override RectangleF Bounds
        {
153
            get { return Path.GetBounds(); }
davescriven's avatar
davescriven committed
154
155
        }

156
157
158
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
davescriven's avatar
davescriven committed
159
160
161
162
        public override GraphicsPath Path
        {
            get
            {
163
                if (_path == null || IsPathDirty)
davescriven's avatar
davescriven committed
164
165
                {
                    // If the corners aren't to be rounded just create a rectangle
166
                    if (CornerRadiusX.Value == 0.0f && CornerRadiusY.Value == 0.0f)
davescriven's avatar
davescriven committed
167
                    {
168
169
                        var rectangle = new RectangleF(Location.ToDeviceValue(),
                            new SizeF(Width.ToDeviceValue(), Height.ToDeviceValue()));
davescriven's avatar
davescriven committed
170
171
172
173
174
175
176
177
178

                        _path = new GraphicsPath();
                        _path.StartFigure();
                        _path.AddRectangle(rectangle);
                        _path.CloseFigure();
                    }
                    else
                    {
                        _path = new GraphicsPath();
179
180
181
182
183
                        var arcBounds = new RectangleF();
                        var lineStart = new PointF();
                        var lineEnd = new PointF();
                        var width = Width.ToDeviceValue();
                        var height = Height.ToDeviceValue();
184
185
                        var rx = CornerRadiusX.ToDeviceValue() * 2;
                        var ry = CornerRadiusY.ToDeviceValue() * 2;
186
                        var location = Location.ToDeviceValue();
davescriven's avatar
davescriven committed
187
188
189
190
191
192
193
194
195
196
197

                        // Start
                        _path.StartFigure();

                        // Add first arc
                        arcBounds.Location = location;
                        arcBounds.Width = rx;
                        arcBounds.Height = ry;
                        _path.AddArc(arcBounds, 180, 90);

                        // Add first line
198
                        lineStart.X = Math.Min(location.X + rx, location.X + width * 0.5f);
davescriven's avatar
davescriven committed
199
                        lineStart.Y = location.Y;
200
                        lineEnd.X = Math.Max(location.X + width - rx, location.X + width * 0.5f);
davescriven's avatar
davescriven committed
201
202
203
204
205
206
207
208
209
                        lineEnd.Y = lineStart.Y;
                        _path.AddLine(lineStart, lineEnd);

                        // Add second arc
                        arcBounds.Location = new PointF(location.X + width - rx, location.Y);
                        _path.AddArc(arcBounds, 270, 90);

                        // Add second line
                        lineStart.X = location.X + width;
210
                        lineStart.Y = Math.Min(location.Y + ry, location.Y + height * 0.5f);
davescriven's avatar
davescriven committed
211
                        lineEnd.X = lineStart.X;
212
                        lineEnd.Y = Math.Max(location.Y + height - ry, location.Y + height * 0.5f);
davescriven's avatar
davescriven committed
213
214
215
216
217
218
219
                        _path.AddLine(lineStart, lineEnd);

                        // Add third arc
                        arcBounds.Location = new PointF(location.X + width - rx, location.Y + height - ry);
                        _path.AddArc(arcBounds, 0, 90);

                        // Add third line
220
                        lineStart.X = Math.Max(location.X + width - rx, location.X + width * 0.5f);
davescriven's avatar
davescriven committed
221
                        lineStart.Y = location.Y + height;
222
                        lineEnd.X = Math.Min(location.X + rx, location.X + width * 0.5f);
davescriven's avatar
davescriven committed
223
224
225
226
227
228
229
230
231
                        lineEnd.Y = lineStart.Y;
                        _path.AddLine(lineStart, lineEnd);

                        // Add third arc
                        arcBounds.Location = new PointF(location.X, location.Y + height - ry);
                        _path.AddArc(arcBounds, 90, 90);

                        // Add fourth line
                        lineStart.X = location.X;
232
                        lineStart.Y = Math.Max(location.Y + height - ry, location.Y + height * 0.5f);
davescriven's avatar
davescriven committed
233
                        lineEnd.X = lineStart.X;
234
                        lineEnd.Y = Math.Min(location.Y + ry, location.Y + height * 0.5f);
davescriven's avatar
davescriven committed
235
236
237
238
239
                        _path.AddLine(lineStart, lineEnd);

                        // Close
                        _path.CloseFigure();
                    }
240
                    IsPathDirty = false;
davescriven's avatar
davescriven committed
241
242
243
244
245
                }
                return _path;
            }
        }

246
247
248
249
        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
        /// </summary>
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
250
        {
251
            if (Width.Value > 0.0f && Height.Value > 0.0f)
252
253
254
            {
                base.Render(renderer);
            }
davescriven's avatar
davescriven committed
255
        }
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273


		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgRectangle>();
		}

		public override SvgElement DeepCopy<T>()
		{
 			var newObj = base.DeepCopy<T>() as SvgRectangle;
			newObj.CornerRadiusX = this.CornerRadiusX;
			newObj.CornerRadiusY = this.CornerRadiusY;
			newObj.Height = this.Height;
			newObj.Width = this.Width;
			newObj.X = this.X;
			newObj.Y = this.Y;
			return newObj;
		}
davescriven's avatar
davescriven committed
274
275
    }
}