SvgGroup.cs 2.12 KB
Newer Older
1
using System.Drawing;
Tebjan Halm's avatar
Tebjan Halm committed
2
using System.Drawing.Drawing2D;
davescriven's avatar
davescriven committed
3
4
5

namespace Svg
{
6
7
8
    /// <summary>
    /// An element used to group SVG shapes.
    /// </summary>
9
    [SvgElement("g")]
10
    public class SvgGroup : SvgVisualElement
davescriven's avatar
davescriven committed
11
    {
12
13
14
15
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        /// <value></value>
Eric Domke's avatar
Eric Domke committed
16
        public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
17
        {
18
            return GetPaths(this, renderer);
davescriven's avatar
davescriven committed
19
20
        }

21
22
23
        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
Dan Backes's avatar
Dan Backes committed
24
25
        /// <returns>The bounds.</returns>
        public override RectangleF CalculateBounds()
davescriven's avatar
davescriven committed
26
        {
Dan Backes's avatar
Dan Backes committed
27
28
29
30
            var r = new RectangleF();
            foreach (var c in this.Children)
            {
                if (c is SvgVisualElement)
James Welle's avatar
James Welle committed
31
                {
Dan Backes's avatar
Dan Backes committed
32
33
34
                    // First it should check if rectangle is empty or it will return the wrong Bounds.
                    // This is because when the Rectangle is Empty, the Union method adds as if the first values where X=0, Y=0
                    if (r.IsEmpty)
HarkDev's avatar
HarkDev committed
35
                    {
Dan Backes's avatar
Dan Backes committed
36
37
38
39
40
41
                        r = ((SvgVisualElement) c).CalculateBounds();
                    }
                    else
                    {
                        var childBounds = ((SvgVisualElement) c).CalculateBounds();
                        if (!childBounds.IsEmpty)
James Welle's avatar
James Welle committed
42
                        {
Dan Backes's avatar
Dan Backes committed
43
                            r = RectangleF.Union(r, childBounds);
James Welle's avatar
James Welle committed
44
                        }
HarkDev's avatar
HarkDev committed
45
                    }
James Welle's avatar
James Welle committed
46
                }
Tebjan Halm's avatar
Tebjan Halm committed
47
            }
Dan Backes's avatar
Dan Backes committed
48
49

            return r;
davescriven's avatar
davescriven committed
50
51
        }

Eric Domke's avatar
Eric Domke committed
52
        protected override bool Renderable { get { return false; } }
Eric Domke's avatar
Eric Domke committed
53
                
James Welle's avatar
James Welle committed
54
55
56
57
        public override SvgElement DeepCopy()
        {
            return DeepCopy<SvgGroup>();
        }
58

James Welle's avatar
James Welle committed
59
60
61
62
63
64
65
        public override SvgElement DeepCopy<T>()
        {
            var newObj = base.DeepCopy<T>() as SvgGroup;
            if (this.Fill != null)
                newObj.Fill = this.Fill.DeepCopy() as SvgPaintServer;
            return newObj;
        }
davescriven's avatar
davescriven committed
66
    }
HarkDev's avatar
HarkDev committed
67
}