SvgSymbol.cs 3.67 KB
Newer Older
Eric Domke's avatar
Eric Domke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Svg.Document_Structure
{
    /// <summary>
    /// An element used to group SVG shapes.
    /// </summary>
    [SvgElement("symbol")]
    public class SvgSymbol : SvgVisualElement
    {

        /// <summary>
        /// Gets or sets the viewport of the element.
        /// </summary>
        /// <value></value>
        [SvgAttribute("viewBox")]
        public SvgViewBox ViewBox
        {
            get { return this.Attributes.GetAttribute<SvgViewBox>("viewBox"); }
            set { this.Attributes["viewBox"] = value; }
        }

        /// <summary>
        /// Gets or sets the aspect of the viewport.
        /// </summary>
        /// <value></value>
        [SvgAttribute("preserveAspectRatio")]
        public SvgAspectRatio AspectRatio
        {
            get { return this.Attributes.GetAttribute<SvgAspectRatio>("preserveAspectRatio"); }
            set { this.Attributes["preserveAspectRatio"] = value; }
        }

        /// <summary>
39
        /// Gets the <see cref="System.Drawing.Drawing2D.GraphicsPath"/> for this element.
Eric Domke's avatar
Eric Domke committed
40
41
42
43
44
45
46
47
48
49
50
        /// </summary>
        /// <value></value>
        public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer)
        {
            return GetPaths(this, renderer);
        }

        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
Tebjan Halm's avatar
Tebjan Halm committed
51
        public override System.Drawing.RectangleF Bounds
Eric Domke's avatar
Eric Domke committed
52
        {
Tebjan Halm's avatar
Tebjan Halm committed
53
            get
Eric Domke's avatar
Eric Domke committed
54
            {
Tebjan Halm's avatar
Tebjan Halm committed
55
56
                var r = new RectangleF();
                foreach (var c in this.Children)
Eric Domke's avatar
Eric Domke committed
57
                {
Tebjan Halm's avatar
Tebjan Halm committed
58
                    if (c is SvgVisualElement)
Eric Domke's avatar
Eric Domke committed
59
                    {
Tebjan Halm's avatar
Tebjan Halm committed
60
61
62
63
64
65
66
                        // 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)
                        {
                            r = ((SvgVisualElement)c).Bounds;
                        }
                        else
Eric Domke's avatar
Eric Domke committed
67
                        {
Tebjan Halm's avatar
Tebjan Halm committed
68
69
70
71
72
                            var childBounds = ((SvgVisualElement)c).Bounds;
                            if (!childBounds.IsEmpty)
                            {
                                r = RectangleF.Union(r, childBounds);
                            }
Eric Domke's avatar
Eric Domke committed
73
74
75
                        }
                    }
                }
Dan Backes's avatar
Dan Backes committed
76

77
                return TransformedBounds(r);
Tebjan Halm's avatar
Tebjan Halm committed
78
            }
Eric Domke's avatar
Eric Domke committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        }

        protected override bool Renderable { get { return false; } }

        /// <summary>
        /// Applies the required transforms to <see cref="ISvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="ISvgRenderer"/> to be transformed.</param>
        protected internal override bool PushTransforms(ISvgRenderer renderer)
        {
            if (!base.PushTransforms(renderer)) return false;
            this.ViewBox.AddViewBoxTransform(this.AspectRatio, renderer, null);
            return true;
        }

Eric Domke's avatar
Eric Domke committed
94
95
96
97
98
99
        // Only render if the parent is set to a Use element
        protected override void Render(ISvgRenderer renderer)
        {
            if (_parent is SvgUse) base.Render(renderer);
        }

Eric Domke's avatar
Eric Domke committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        public override SvgElement DeepCopy()
        {
            return DeepCopy<SvgSymbol>();
        }

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