SvgGroup.cs 2.97 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>
davescriven's avatar
davescriven committed
20
21
        public override System.Drawing.Drawing2D.GraphicsPath Path
        {
Tebjan Halm's avatar
Tebjan Halm committed
22
23
            get 
            { 
James Welle's avatar
James Welle committed
24
25
                //var path = new GraphicsPath();
                //AddPaths(this, path);
Tebjan Halm's avatar
Tebjan Halm committed
26
  
James Welle's avatar
James Welle committed
27
                return GetPaths(this);
Tebjan Halm's avatar
Tebjan Halm committed
28
            }
29
30
            protected set
            { }
davescriven's avatar
davescriven committed
31
32
        }

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

67
68
69
70
        /// <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>
71
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
72
        {
C Moore's avatar
C Moore committed
73
            if (!Visible || !Displayable)
C Moore's avatar
C Moore committed
74
75
                return;

76
            this.PushTransforms(renderer);
77
            this.SetClip(renderer);
78
            base.RenderChildren(renderer);
79
            this.ResetClip(renderer);
80
            this.PopTransforms(renderer);
davescriven's avatar
davescriven committed
81
        }
82

James Welle's avatar
James Welle committed
83
84
85
86
87
        
        public override SvgElement DeepCopy()
        {
            return DeepCopy<SvgGroup>();
        }
88

James Welle's avatar
James Welle committed
89
90
91
92
93
94
95
        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
96
    }
HarkDev's avatar
HarkDev committed
97
}