SvgUse.cs 3.32 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
7
using System.Drawing.Drawing2D;
davescriven's avatar
davescriven committed
8
9
10

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

16
        [SvgAttribute("xlink:href")]
davescriven's avatar
davescriven committed
17
18
19
20
21
22
        public virtual Uri ReferencedElement
        {
            get { return this._referencedElement; }
            set { this._referencedElement = value; }
        }

23
24
25
26
27
28
29
30
31
32
33
34
35
36
        [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; }
        }

37
38
39
40
        /// <summary>
        /// Applies the required transforms to <see cref="SvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> to be transformed.</param>
41
        protected internal override void PushTransforms(SvgRenderer renderer)
42
        {
43
44
            base.PushTransforms(renderer);
            renderer.TranslateTransform(this.X.ToDeviceValue(this), this.Y.ToDeviceValue(this, true));
45
46
        }

47
48
49
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgUse"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
50
51
52
53
54
55
        public SvgUse()
        {
        }

        public override System.Drawing.Drawing2D.GraphicsPath Path
        {
56
57
            get
            {
58
                SvgVisualElement element = (SvgVisualElement)this.OwnerDocument.IdManager.GetElementById(this.ReferencedElement);
59
60
                return (element != null) ? element.Path : null;
            }
davescriven's avatar
davescriven committed
61
62
63
64
65
66
67
        }

        public override System.Drawing.RectangleF Bounds
        {
            get { return new System.Drawing.RectangleF(); }
        }

68
69
70
71
72
73
74
75
76
77
//        public override SvgElementCollection Children
//        {
//            get
//            {
//                SvgElement element = this.OwnerDocument.IdManager.GetElementById(this.ReferencedElement);
//                SvgElementCollection elements = new SvgElementCollection(this, true);
//                elements.Add(element);
//                return elements;
//            }
//        }
78

79
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
80
        {
81
            this.PushTransforms(renderer);
davescriven's avatar
davescriven committed
82

83
            SvgVisualElement element = (SvgVisualElement)this.OwnerDocument.IdManager.GetElementById(this.ReferencedElement);
davescriven's avatar
davescriven committed
84
85
86
87
            // For the time of rendering we want the referenced element to inherit
            // this elements transforms
            SvgElement parent = element._parent;
            element._parent = this;
88
            element.RenderElement(renderer);
davescriven's avatar
davescriven committed
89
90
            element._parent = parent;

91
            this.PopTransforms(renderer);
davescriven's avatar
davescriven committed
92
        }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109


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

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgUse;
			newObj.ReferencedElement = this.ReferencedElement;
			newObj.X = this.X;
			newObj.Y = this.Y;

			return newObj;
		}

davescriven's avatar
davescriven committed
110
111
    }
}