SvgSwitch.cs 2.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Svg
{
    /// <summary>
    /// The switch element evaluates the requiredFeatures, requiredExtensions and systemLanguage attributes on its direct child elements in order, and then processes and renders the first child for which these attributes evaluate to true
    /// </summary>
    [SvgElement("switch")]
    public class SvgSwitch : SvgVisualElement
    {
        public SvgSwitch()
        {
        }

        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        /// <value></value>
Eric Domke's avatar
Eric Domke committed
20
        public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer)
21
        {
22
            return GetPaths(this, renderer);
23
24
25
26
27
        }

        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
Dan Backes's avatar
Dan Backes committed
28
29
        /// <returns>The bounds.</returns>
        public override RectangleF CalculateBounds()
30
        {
Dan Backes's avatar
Dan Backes committed
31
32
            var r = new RectangleF();
            foreach (var c in this.Children)
33
            {
Dan Backes's avatar
Dan Backes committed
34
                if (c is SvgVisualElement)
35
                {
Dan Backes's avatar
Dan Backes committed
36
37
38
                    // 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)
39
                    {
Dan Backes's avatar
Dan Backes committed
40
41
42
43
44
45
                        r = ((SvgVisualElement) c).CalculateBounds();
                    }
                    else
                    {
                        var childBounds = ((SvgVisualElement) c).CalculateBounds();
                        if (!childBounds.IsEmpty)
46
                        {
Dan Backes's avatar
Dan Backes committed
47
                            r = RectangleF.Union(r, childBounds);
48
49
50
51
                        }
                    }
                }
            }
Dan Backes's avatar
Dan Backes committed
52
53

            return r;
54
55
56
57
58
59
        }

        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
        /// </summary>
        /// <param name="renderer">The <see cref="Graphics"/> object to render to.</param>
Eric Domke's avatar
Eric Domke committed
60
        protected override void Render(ISvgRenderer renderer)
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        {
            if (!Visible || !Displayable)
                return;

            this.PushTransforms(renderer);
            this.SetClip(renderer);
            base.RenderChildren(renderer);
            this.ResetClip(renderer);
            this.PopTransforms(renderer);
        }


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

        public override SvgElement DeepCopy<T>()
        {
            var newObj = base.DeepCopy<T>() as SvgSwitch;
            if (this.Fill != null)
                newObj.Fill = this.Fill.DeepCopy() as SvgPaintServer;
            return newObj;
        }
    }
}