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

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

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

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
        static private bool ElementReferencesUri(SvgElement element, List<Uri> elementUris)
        {
            var useElement = element as SvgUse;
            if (useElement != null)
            {
                if (elementUris.Contains(useElement.ReferencedElement))
                {
                    return true;
                }
                // also detect cycles in referenced elements
                elementUris.Add(useElement.ReferencedElement);
                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);
        }

64
65
66
67
68
69
70
71
72
73
74
75
76
77
        [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; }
        }

78
        /// <summary>
Eric Domke's avatar
Eric Domke committed
79
        /// Applies the required transforms to <see cref="ISvgRenderer"/>.
80
        /// </summary>
Eric Domke's avatar
Eric Domke committed
81
82
        /// <param name="renderer">The <see cref="ISvgRenderer"/> to be transformed.</param>
        protected internal override bool PushTransforms(ISvgRenderer renderer)
83
        {
84
            if (!base.PushTransforms(renderer)) return false;
Eric Domke's avatar
Eric Domke committed
85
            renderer.TranslateTransform(this.X.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
mrbean-bremen's avatar
mrbean-bremen committed
86
87
                                        this.Y.ToDeviceValue(renderer, UnitRenderingType.Vertical, this),
                                        MatrixOrder.Prepend);
88
            return true;
89
90
        }

91
92
93
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgUse"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
94
95
        public SvgUse()
        {
96
97
            this.X = 0;
            this.Y = 0;
davescriven's avatar
davescriven committed
98
99
        }

Eric Domke's avatar
Eric Domke committed
100
        public override System.Drawing.Drawing2D.GraphicsPath Path(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
101
        {
102
            SvgVisualElement element = (SvgVisualElement)this.OwnerDocument.IdManager.GetElementById(this.ReferencedElement);
103
            return (element != null && !this.HasRecursiveReference()) ? element.Path(renderer) : null;
davescriven's avatar
davescriven committed
104
105
        }

Tebjan Halm's avatar
Tebjan Halm committed
106
        public override System.Drawing.RectangleF Bounds
davescriven's avatar
davescriven committed
107
        {
108
109
110
111
112
113
114
115
116
            get
            {
                var element = this.OwnerDocument.IdManager.GetElementById(this.ReferencedElement) as SvgVisualElement;
                if (element != null)
                {
                    return element.Bounds;
                }
                return new System.Drawing.RectangleF();
            }
davescriven's avatar
davescriven committed
117
118
        }

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

Eric Domke's avatar
Eric Domke committed
121
        protected override void Render(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
122
        {
123
            if (this.Visible && this.Displayable && !this.HasRecursiveReference() && this.PushTransforms(renderer))
Eric Domke's avatar
Eric Domke committed
124
125
126
            {
                this.SetClip(renderer);

Eric Domke's avatar
Eric Domke committed
127
                var element = this.OwnerDocument.IdManager.GetElementById(this.ReferencedElement) as SvgVisualElement;
Eric Domke's avatar
Eric Domke committed
128
129
130
131
                if (element != null)
                {
                    var origParent = element.Parent;
                    element._parent = this;
132
133
134
                    // 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
135
136
137
138
139
140
141
                    element.RenderElement(renderer);
                    element._parent = origParent;
                }

                this.ResetClip(renderer);
                this.PopTransforms(renderer);
            }
davescriven's avatar
davescriven committed
142
        }
143
144


145
146
147
148
        public override SvgElement DeepCopy()
        {
            return DeepCopy<SvgUse>();
        }
149

150
151
152
153
154
155
        public override SvgElement DeepCopy<T>()
        {
            var newObj = base.DeepCopy<T>() as SvgUse;
            newObj.ReferencedElement = this.ReferencedElement;
            newObj.X = this.X;
            newObj.Y = this.Y;
156

157
158
            return newObj;
        }
159

davescriven's avatar
davescriven committed
160
161
    }
}