SvgUse.cs 7.92 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
using System;
using System.Collections.Generic;
3
using System.Drawing;
4
using System.Drawing.Drawing2D;
davescriven's avatar
davescriven committed
5
6
7

namespace Svg
{
8
    [SvgElement("use")]
9
    public class SvgUse : SvgVisualElement
davescriven's avatar
davescriven committed
10
11
12
    {
        private Uri _referencedElement;

Eric Domke's avatar
Eric Domke committed
13
        [SvgAttribute("href", SvgAttributeAttribute.XLinkNamespace)]
davescriven's avatar
davescriven committed
14
15
16
17
18
19
        public virtual Uri ReferencedElement
        {
            get { return this._referencedElement; }
            set { this._referencedElement = value; }
        }

20
        private bool ElementReferencesUri(SvgElement element, List<Uri> elementUris)
21
22
23
24
25
26
27
28
29
        {
            var useElement = element as SvgUse;
            if (useElement != null)
            {
                if (elementUris.Contains(useElement.ReferencedElement))
                {
                    return true;
                }
                // also detect cycles in referenced elements
30
31
32
33
34
                var refElement = this.OwnerDocument.IdManager.GetElementById(useElement.ReferencedElement);
                if (refElement is SvgUse)
                {
                    elementUris.Add(useElement.ReferencedElement);
                }
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
                return useElement.ReferencedElementReferencesUri(elementUris);
            }
            var groupElement = element as SvgGroup;
            if (groupElement != null)
            {
                foreach (var child in groupElement.Children)
                {
                    if (ElementReferencesUri(child, elementUris))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        private bool ReferencedElementReferencesUri( List<Uri> elementUris )
        {
            var refElement = this.OwnerDocument.IdManager.GetElementById(ReferencedElement);
            return ElementReferencesUri(refElement, elementUris);
        }

        /// <summary>
        /// Checks for any direct or indirect recursions in referenced elements, 
        /// including recursions via groups.
        /// </summary>
        /// <returns>True if any recursions are found.</returns>
        private bool HasRecursiveReference()
        {
            var refElement = this.OwnerDocument.IdManager.GetElementById(ReferencedElement);
            var uris = new List<Uri>() { ReferencedElement };
            return ElementReferencesUri(refElement, uris);
        }

69
70
71
72
73
74
75
76
77
78
79
80
81
82
        [SvgAttribute("x")]
        public virtual SvgUnit X
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("x"); }
            set { this.Attributes["x"] = value; }
        }

        [SvgAttribute("y")]
        public virtual SvgUnit Y
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("y"); }
            set { this.Attributes["y"] = value; }
        }

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

        [SvgAttribute("width")]
        public virtual SvgUnit Width
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("width"); }
            set { this.Attributes["width"] = value; }
        }

        [SvgAttribute("height")]
        public virtual SvgUnit Height
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("height"); }
            set { this.Attributes["height"] = value; }
        }

98
        /// <summary>
Eric Domke's avatar
Eric Domke committed
99
        /// Applies the required transforms to <see cref="ISvgRenderer"/>.
100
        /// </summary>
Eric Domke's avatar
Eric Domke committed
101
102
        /// <param name="renderer">The <see cref="ISvgRenderer"/> to be transformed.</param>
        protected internal override bool PushTransforms(ISvgRenderer renderer)
103
        {
104
            if (!base.PushTransforms(renderer)) return false;
Eric Domke's avatar
Eric Domke committed
105
            renderer.TranslateTransform(this.X.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
mrbean-bremen's avatar
mrbean-bremen committed
106
107
                                        this.Y.ToDeviceValue(renderer, UnitRenderingType.Vertical, this),
                                        MatrixOrder.Prepend);
108
            return true;
109
110
        }

111
112
113
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgUse"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
114
115
        public SvgUse()
        {
116
117
            this.X = 0;
            this.Y = 0;
118
119
            this.Width = 0;
            this.Height = 0;
davescriven's avatar
davescriven committed
120
121
        }

Eric Domke's avatar
Eric Domke committed
122
        public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
123
        {
124
            SvgVisualElement element = (SvgVisualElement)this.OwnerDocument.IdManager.GetElementById(this.ReferencedElement);
125
            return (element != null && !this.HasRecursiveReference()) ? element.Path(renderer) : null;
davescriven's avatar
davescriven committed
126
127
        }

128
129
130
131
132
133
134
135
136
137
138
139
        /// <summary>
        /// Gets an <see cref="SvgPoint"/> representing the top left point of the rectangle.
        /// </summary>
        public SvgPoint Location
        {
            get { return new SvgPoint(X, Y); }
        }

        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
Tebjan Halm's avatar
Tebjan Halm committed
140
        public override System.Drawing.RectangleF Bounds
davescriven's avatar
davescriven committed
141
        {
142
143
            get
            {
144
145
146
147
148
                var ew = this.Width.ToDeviceValue(null, UnitRenderingType.Horizontal, this);
                var eh = this.Height.ToDeviceValue(null, UnitRenderingType.Vertical, this);
                if (ew > 0 && eh > 0)
                    return TransformedBounds(new RectangleF(this.Location.ToDeviceValue(null, this),
                        new SizeF(ew, eh)));
149
150
151
152
153
                var element = this.OwnerDocument.IdManager.GetElementById(this.ReferencedElement) as SvgVisualElement;
                if (element != null)
                {
                    return element.Bounds;
                }
154

155
156
                return new System.Drawing.RectangleF();
            }
davescriven's avatar
davescriven committed
157
158
        }

Eric Domke's avatar
Eric Domke committed
159
160
        protected override bool Renderable { get { return false; } }

Eric Domke's avatar
Eric Domke committed
161
        protected override void Render(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
162
        {
163
            if (this.Visible && this.Displayable && this.ReferencedElement != null && !this.HasRecursiveReference() && this.PushTransforms(renderer))
Eric Domke's avatar
Eric Domke committed
164
165
166
            {
                this.SetClip(renderer);

Eric Domke's avatar
Eric Domke committed
167
                var element = this.OwnerDocument.IdManager.GetElementById(this.ReferencedElement) as SvgVisualElement;
Eric Domke's avatar
Eric Domke committed
168
169
                if (element != null)
                {
170
171
172
173
174
175
176
177
178
179
180
181
182
                    var ew = Width.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this);
                    var eh = Height.ToDeviceValue(renderer, UnitRenderingType.Vertical, this);
                    if (ew > 0 && eh > 0)
                    {
                        var viewBox = element.Attributes.GetAttribute<SvgViewBox>("viewBox");
                        if (viewBox!=SvgViewBox.Empty && Math.Abs(ew - viewBox.Width) > float.Epsilon && Math.Abs(eh - viewBox.Height) > float.Epsilon)
                        {
                            var sw = ew / viewBox.Width;
                            var sh = eh / viewBox.Height;
                            renderer.ScaleTransform(sw, sh, MatrixOrder.Prepend);
                        }
                    }

Eric Domke's avatar
Eric Domke committed
183
184
                    var origParent = element.Parent;
                    element._parent = this;
185
186
187
                    // as the new parent may have other styles that are inherited,
                    // we have to redraw the paths for the children
                    element.InvalidateChildPaths();
Eric Domke's avatar
Eric Domke committed
188
189
190
191
192
193
194
                    element.RenderElement(renderer);
                    element._parent = origParent;
                }

                this.ResetClip(renderer);
                this.PopTransforms(renderer);
            }
davescriven's avatar
davescriven committed
195
        }
196
197


198
199
200
201
        public override SvgElement DeepCopy()
        {
            return DeepCopy<SvgUse>();
        }
202

203
204
205
206
207
208
        public override SvgElement DeepCopy<T>()
        {
            var newObj = base.DeepCopy<T>() as SvgUse;
            newObj.ReferencedElement = this.ReferencedElement;
            newObj.X = this.X;
            newObj.Y = this.Y;
209

210
211
            return newObj;
        }
212

davescriven's avatar
davescriven committed
213
214
    }
}