SvgGroup.cs 2.86 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
    {
        public SvgGroup()
        {
        }

16
17
18
19
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        /// <value></value>
20
        public override System.Drawing.Drawing2D.GraphicsPath Path(SvgRenderer renderer)
davescriven's avatar
davescriven committed
21
        {
22
            return GetPaths(this, renderer);
davescriven's avatar
davescriven committed
23
24
        }

25
26
27
28
        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
davescriven's avatar
davescriven committed
29
30
        public override System.Drawing.RectangleF Bounds
        {
Tebjan Halm's avatar
Tebjan Halm committed
31
32
            get 
            { 
James Welle's avatar
James Welle committed
33
34
35
                var r = new RectangleF();
                foreach(var c in this.Children)
                {
HarkDev's avatar
HarkDev committed
36
37
                    if (c is SvgVisualElement)
                    {
James Welle's avatar
James Welle committed
38
39
                        // 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
HarkDev's avatar
HarkDev committed
40
                        if (r.IsEmpty)
James Welle's avatar
James Welle committed
41
                        {
HarkDev's avatar
HarkDev committed
42
                            r = ((SvgVisualElement)c).Bounds;
James Welle's avatar
James Welle committed
43
                        }
HarkDev's avatar
HarkDev committed
44
                        else
James Welle's avatar
James Welle committed
45
46
47
48
49
50
51
                        {
                            var childBounds = ((SvgVisualElement)c).Bounds;
                            if (!childBounds.IsEmpty)
                            {
                                r = RectangleF.Union(r, childBounds);
                            }
                        }
HarkDev's avatar
HarkDev committed
52
                    }
James Welle's avatar
James Welle committed
53
54
55
                }
                
                return r;
Tebjan Halm's avatar
Tebjan Halm committed
56
            }
davescriven's avatar
davescriven committed
57
58
        }

59
60
61
62
        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
        /// </summary>
        /// <param name="graphics">The <see cref="Graphics"/> object to render to.</param>
63
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
64
        {
C Moore's avatar
C Moore committed
65
            if (!Visible || !Displayable)
C Moore's avatar
C Moore committed
66
67
                return;

68
69
70
71
72
73
74
            if (this.PushTransforms(renderer))
            {
                this.SetClip(renderer);
                base.RenderChildren(renderer);
                this.ResetClip(renderer);
                this.PopTransforms(renderer);
            }
davescriven's avatar
davescriven committed
75
        }
76

James Welle's avatar
James Welle committed
77
78
79
80
81
        
        public override SvgElement DeepCopy()
        {
            return DeepCopy<SvgGroup>();
        }
82

James Welle's avatar
James Welle committed
83
84
85
86
87
88
89
        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
90
    }
HarkDev's avatar
HarkDev committed
91
}