1 | using System;
|
---|
2 | using System.Xml;
|
---|
3 | using System.Globalization;
|
---|
4 |
|
---|
5 | using SharpVectors.Dom.Css;
|
---|
6 |
|
---|
7 | namespace SharpVectors.Dom.Svg
|
---|
8 | {
|
---|
9 | public sealed class SvgUseElement : SvgTransformableElement, ISvgUseElement
|
---|
10 | {
|
---|
11 | #region Private Fields
|
---|
12 |
|
---|
13 | private ISvgAnimatedLength x;
|
---|
14 | private ISvgAnimatedLength y;
|
---|
15 | private ISvgAnimatedLength width;
|
---|
16 | private ISvgAnimatedLength height;
|
---|
17 | private ISvgElementInstance instanceRoot;
|
---|
18 |
|
---|
19 | private SvgTests svgTests;
|
---|
20 | private SvgUriReference svgURIReference;
|
---|
21 | private SvgExternalResourcesRequired svgExternalResourcesRequired;
|
---|
22 |
|
---|
23 | // For rendering support...
|
---|
24 | private string saveTransform;
|
---|
25 | private string saveWidth;
|
---|
26 | private string saveHeight;
|
---|
27 |
|
---|
28 | #endregion
|
---|
29 |
|
---|
30 | #region Constructors and Destructor
|
---|
31 |
|
---|
32 | public SvgUseElement(string prefix, string localname, string ns, SvgDocument doc)
|
---|
33 | : base(prefix, localname, ns, doc)
|
---|
34 | {
|
---|
35 | svgURIReference = new SvgUriReference(this);
|
---|
36 | svgURIReference.NodeChanged += new NodeChangeHandler(ReferencedNodeChange);
|
---|
37 | svgExternalResourcesRequired = new SvgExternalResourcesRequired(this);
|
---|
38 | svgTests = new SvgTests(this);
|
---|
39 | }
|
---|
40 |
|
---|
41 | #endregion
|
---|
42 |
|
---|
43 | #region ISvgUseElement Members
|
---|
44 |
|
---|
45 | public ISvgAnimatedLength X
|
---|
46 | {
|
---|
47 | get
|
---|
48 | {
|
---|
49 | if (x == null)
|
---|
50 | {
|
---|
51 | x = new SvgAnimatedLength(this, "x", SvgLengthDirection.Horizontal, "0");
|
---|
52 | }
|
---|
53 | return x;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public ISvgAnimatedLength Y
|
---|
58 | {
|
---|
59 | get
|
---|
60 | {
|
---|
61 | if (y == null)
|
---|
62 | {
|
---|
63 | y = new SvgAnimatedLength(this, "y", SvgLengthDirection.Vertical, "0");
|
---|
64 | }
|
---|
65 | return y;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public ISvgAnimatedLength Width
|
---|
70 | {
|
---|
71 | get
|
---|
72 | {
|
---|
73 | if (width == null)
|
---|
74 | {
|
---|
75 | width = new SvgAnimatedLength(this, "width",
|
---|
76 | SvgLengthDirection.Horizontal, String.Empty);
|
---|
77 | }
|
---|
78 | return width;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | public ISvgAnimatedLength Height
|
---|
83 | {
|
---|
84 | get
|
---|
85 | {
|
---|
86 | if (height == null)
|
---|
87 | {
|
---|
88 | height = new SvgAnimatedLength(this, "height",
|
---|
89 | SvgLengthDirection.Vertical, String.Empty);
|
---|
90 | }
|
---|
91 | return height;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | public ISvgElementInstance InstanceRoot
|
---|
96 | {
|
---|
97 | get
|
---|
98 | {
|
---|
99 | if (instanceRoot == null)
|
---|
100 | {
|
---|
101 | instanceRoot = new SvgElementInstance(ReferencedElement, this, null);
|
---|
102 | }
|
---|
103 | return instanceRoot;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public ISvgElementInstance AnimatedInstanceRoot
|
---|
108 | {
|
---|
109 | get
|
---|
110 | {
|
---|
111 | return InstanceRoot;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | #endregion
|
---|
116 |
|
---|
117 | #region ISvgURIReference Members
|
---|
118 |
|
---|
119 | public ISvgAnimatedString Href
|
---|
120 | {
|
---|
121 | get
|
---|
122 | {
|
---|
123 | return svgURIReference.Href;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | public XmlElement ReferencedElement
|
---|
128 | {
|
---|
129 | get
|
---|
130 | {
|
---|
131 | return svgURIReference.ReferencedNode as XmlElement;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | #endregion
|
---|
136 |
|
---|
137 | #region ISvgExternalResourcesRequired Members
|
---|
138 |
|
---|
139 | public ISvgAnimatedBoolean ExternalResourcesRequired
|
---|
140 | {
|
---|
141 | get
|
---|
142 | {
|
---|
143 | return svgExternalResourcesRequired.ExternalResourcesRequired;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | #region ISvgTests Members
|
---|
150 |
|
---|
151 | public ISvgStringList RequiredFeatures
|
---|
152 | {
|
---|
153 | get { return svgTests.RequiredFeatures; }
|
---|
154 | }
|
---|
155 |
|
---|
156 | public ISvgStringList RequiredExtensions
|
---|
157 | {
|
---|
158 | get { return svgTests.RequiredExtensions; }
|
---|
159 | }
|
---|
160 |
|
---|
161 | public ISvgStringList SystemLanguage
|
---|
162 | {
|
---|
163 | get { return svgTests.SystemLanguage; }
|
---|
164 | }
|
---|
165 |
|
---|
166 | public bool HasExtension(string extension)
|
---|
167 | {
|
---|
168 | return svgTests.HasExtension(extension);
|
---|
169 | }
|
---|
170 |
|
---|
171 | #endregion
|
---|
172 |
|
---|
173 | #region Update Handling
|
---|
174 |
|
---|
175 | public override void HandleAttributeChange(XmlAttribute attribute)
|
---|
176 | {
|
---|
177 | if (attribute.NamespaceURI.Length == 0)
|
---|
178 | {
|
---|
179 | switch (attribute.LocalName)
|
---|
180 | {
|
---|
181 | case "x":
|
---|
182 | x = null;
|
---|
183 | return;
|
---|
184 | case "y":
|
---|
185 | y = null;
|
---|
186 | return;
|
---|
187 | case "width":
|
---|
188 | width = null;
|
---|
189 | return;
|
---|
190 | case "height":
|
---|
191 | height = null;
|
---|
192 | return;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | else if (attribute.NamespaceURI == SvgDocument.XLinkNamespace)
|
---|
196 | {
|
---|
197 | switch (attribute.LocalName)
|
---|
198 | {
|
---|
199 | case "href":
|
---|
200 | instanceRoot = null;
|
---|
201 | break;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | base.HandleAttributeChange(attribute);
|
---|
206 | }
|
---|
207 |
|
---|
208 | public void ReferencedNodeChange(Object src, XmlNodeChangedEventArgs args)
|
---|
209 | {
|
---|
210 | //TODO - This is getting called too often!
|
---|
211 | //instanceRoot = null;
|
---|
212 | }
|
---|
213 |
|
---|
214 | #endregion
|
---|
215 |
|
---|
216 | #region Rendering
|
---|
217 |
|
---|
218 | public void CopyToReferencedElement(XmlElement refEl)
|
---|
219 | {
|
---|
220 | // X and Y become a translate portion of any transform, width and height may get passed on
|
---|
221 | if (X.AnimVal.Value != 0 || Y.AnimVal.Value != 0)
|
---|
222 | {
|
---|
223 | saveTransform = this.GetAttribute("transform");
|
---|
224 | //this.SetAttribute("transform", saveTransform + " translate("
|
---|
225 | // + X.AnimVal.Value + "," + Y.AnimVal.Value + ")");
|
---|
226 | string transform = String.Format(CultureInfo.InvariantCulture,
|
---|
227 | "{0} translate({1},{2})", saveTransform, X.AnimVal.Value, Y.AnimVal.Value);
|
---|
228 | this.SetAttribute("transform", transform);
|
---|
229 | }
|
---|
230 |
|
---|
231 | // if (refEl is SvgSymbolElement)
|
---|
232 | if (String.Equals(refEl.Name, "symbol", StringComparison.OrdinalIgnoreCase))
|
---|
233 | {
|
---|
234 | refEl.SetAttribute("width", (HasAttribute("width")) ? GetAttribute("width") : "100%");
|
---|
235 | refEl.SetAttribute("height", (HasAttribute("height")) ? GetAttribute("height") : "100%");
|
---|
236 | }
|
---|
237 | // if (refEl is SvgSymbolElement)
|
---|
238 | if (String.Equals(refEl.Name, "symbol", StringComparison.OrdinalIgnoreCase))
|
---|
239 | {
|
---|
240 | saveWidth = refEl.GetAttribute("width");
|
---|
241 | saveHeight = refEl.GetAttribute("height");
|
---|
242 | if (HasAttribute("width"))
|
---|
243 | refEl.SetAttribute("width", GetAttribute("width"));
|
---|
244 | if (HasAttribute("height"))
|
---|
245 | refEl.SetAttribute("height", GetAttribute("height"));
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | public void RestoreReferencedElement(XmlElement refEl)
|
---|
250 | {
|
---|
251 | if (saveTransform != null)
|
---|
252 | this.SetAttribute("transform", saveTransform);
|
---|
253 | if (saveWidth != null)
|
---|
254 | {
|
---|
255 | refEl.SetAttribute("width", saveWidth);
|
---|
256 | refEl.SetAttribute("height", saveHeight);
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | #endregion
|
---|
261 | }
|
---|
262 | }
|
---|