Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorModel/DocumentStructure/SvgImageElement.cs @ 12762

Last change on this file since 12762 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 10.3 KB
Line 
1using System;
2using System.IO;
3using System.Net;
4using System.Xml;
5using System.Globalization;
6
7namespace SharpVectors.Dom.Svg
8{
9    public sealed class SvgImageElement : SvgTransformableElement, ISvgImageElement
10    {
11        #region Private Fields
12
13        private ISvgAnimatedLength x;
14        private ISvgAnimatedLength y;
15        private ISvgAnimatedLength width;
16        private ISvgAnimatedLength height;
17
18        private SvgTests svgTests;
19        private SvgUriReference svgURIReference;
20        private SvgFitToViewBox svgFitToViewBox;
21        private SvgExternalResourcesRequired svgExternalResourcesRequired;
22
23        #endregion
24
25        #region Constructors and Destructor
26
27        public SvgImageElement(string prefix, string localname, string ns, SvgDocument doc)
28            : base(prefix, localname, ns, doc)
29        {
30            svgExternalResourcesRequired = new SvgExternalResourcesRequired(this);
31            svgTests = new SvgTests(this);
32            svgURIReference = new SvgUriReference(this);
33            svgFitToViewBox = new SvgFitToViewBox(this);
34        }
35
36        #endregion
37
38        #region Public properties
39
40        //public SvgRect CalculatedViewbox
41        //{
42        //    get
43        //    {
44        //        SvgRect viewBox = null;
45
46        //        if (IsSvgImage)
47        //        {
48        //            SvgDocument doc = GetImageDocument();
49        //            SvgSvgElement outerSvg = (SvgSvgElement)doc.DocumentElement;
50
51        //            if (outerSvg.HasAttribute("viewBox"))
52        //            {
53        //                viewBox = (SvgRect)outerSvg.ViewBox.AnimVal;
54        //            }
55        //            else
56        //            {
57        //                viewBox = SvgRect.Empty;
58        //            }
59        //        }
60        //        else
61        //        {
62        //            viewBox = new SvgRect(0, 0, Bitmap.Size.Width, Bitmap.Size.Height);
63        //        }
64
65        //        return viewBox;
66        //    }
67        //}
68
69        public bool IsSvgImage
70        {
71            get
72            {
73                if (!Href.AnimVal.StartsWith("data:"))
74                {
75                    try
76                    {
77                        WebResponse resource = svgURIReference.ReferencedResource;
78                        if (resource == null)
79                        {
80                            return false;
81                        }
82
83                        // local files are returning as binary/octet-stream
84                        // this "fix" tests the file extension for .svg and .svgz
85                        string name = resource.ResponseUri.ToString().ToLower(CultureInfo.InvariantCulture);
86                        return (resource.ContentType.StartsWith("image/svg+xml") ||
87                            name.EndsWith(".svg") || name.EndsWith(".svgz"));
88                    }
89                    catch (WebException)
90                    {
91                        return false;
92                    }
93                    catch (IOException)
94                    {
95                        return false;
96                    }
97                }
98
99                return false;
100            }
101        }
102
103        public SvgWindow SvgWindow
104        {
105            get
106            {
107                if (IsSvgImage)
108                {
109                    SvgWindow parentWindow = (SvgWindow)OwnerDocument.Window;
110
111                    if (parentWindow != null)
112                    {
113                        SvgWindow wnd = parentWindow.CreateOwnedWindow(
114                            (long)Width.AnimVal.Value, (long)Height.AnimVal.Value);
115
116                        SvgDocument doc = new SvgDocument(wnd);
117                        wnd.Document = doc;
118
119                        string absoluteUri = svgURIReference.AbsoluteUri;
120
121                        Stream resStream = svgURIReference.ReferencedResource.GetResponseStream();
122                        doc.Load(absoluteUri, resStream);
123
124                        return wnd;
125                    }
126                }
127
128                return null;
129            }
130        }
131
132        #endregion
133
134        #region ISvgElement Members
135
136        /// <summary>
137        /// Gets a value providing a hint on the rendering defined by this element.
138        /// </summary>
139        /// <value>
140        /// An enumeration of the <see cref="SvgRenderingHint"/> specifying the rendering hint.
141        /// This will always return <see cref="SvgRenderingHint.Image"/>
142        /// </value>
143        public override SvgRenderingHint RenderingHint
144        {
145            get
146            {
147                return SvgRenderingHint.Image;
148            }
149        }
150
151        #endregion
152
153        #region ISvgImageElement Members
154
155        public ISvgAnimatedLength Width
156        {
157            get
158            {
159                if (width == null)
160                {
161                    width = new SvgAnimatedLength(this, "width", SvgLengthDirection.Horizontal, "0");
162                }
163                return width;
164            }
165        }
166
167        public ISvgAnimatedLength Height
168        {
169            get
170            {
171                if (height == null)
172                {
173                    height = new SvgAnimatedLength(this, "height", SvgLengthDirection.Vertical, "0");
174                }
175                return height;
176            }
177
178        }
179
180        public ISvgAnimatedLength X
181        {
182            get
183            {
184                if (x == null)
185                {
186                    x = new SvgAnimatedLength(this, "x", SvgLengthDirection.Horizontal, "0");
187                }
188                return x;
189            }
190
191        }
192
193        public ISvgAnimatedLength Y
194        {
195            get
196            {
197                if (y == null)
198                {
199                    y = new SvgAnimatedLength(this, "y", SvgLengthDirection.Vertical, "0");
200                }
201                return y;
202            }
203
204        }
205
206        public ISvgColorProfileElement ColorProfile
207        {
208            get
209            {
210                string colorProfile = this.GetAttribute("color-profile");
211
212                if (String.IsNullOrEmpty(colorProfile))
213                {
214                    return null;
215                }
216
217                XmlElement profileElement = this.OwnerDocument.GetElementById(colorProfile);
218                if (profileElement == null)
219                {
220                    XmlElement root = this.OwnerDocument.DocumentElement;
221                    XmlNodeList elemList = root.GetElementsByTagName("color-profile");
222                    if (elemList != null && elemList.Count != 0)
223                    { 
224                        for (int i = 0; i < elemList.Count; i++)
225                        {
226                            XmlElement elementNode = elemList[i] as XmlElement;
227                            if (elementNode != null && String.Equals(colorProfile,
228                                elementNode.GetAttribute("id")))
229                            {
230                                profileElement = elementNode;
231                                break;
232                            }                                     
233                        }
234                    }
235                }
236
237                return profileElement as SvgColorProfileElement;
238            }
239        }
240
241        #endregion
242
243        #region ISvgURIReference Members
244
245        public ISvgAnimatedString Href
246        {
247            get
248            {
249                return svgURIReference.Href;
250            }
251        }
252
253        public SvgUriReference UriReference
254        {
255            get
256            {
257                return svgURIReference;
258            }
259        }
260
261        public XmlElement ReferencedElement
262        {
263            get
264            {
265                return svgURIReference.ReferencedNode as XmlElement;
266            }
267        }
268
269        #endregion
270
271        #region ISvgFitToViewBox Members
272
273        public ISvgAnimatedPreserveAspectRatio PreserveAspectRatio
274        {
275            get
276            {
277                return svgFitToViewBox.PreserveAspectRatio;
278            }
279        }
280
281        #endregion
282
283        #region ISvgImageElement Members from SVG 1.2
284
285        public SvgDocument GetImageDocument()
286        {
287            SvgWindow window = this.SvgWindow;
288            if (window == null)
289            {
290                return null;
291            }
292            else
293            {
294                return (SvgDocument)window.Document;
295            }
296        }
297
298        #endregion
299
300        #region Update handling
301        public override void HandleAttributeChange(XmlAttribute attribute)
302        {
303            if (attribute.NamespaceURI.Length == 0)
304            {
305                // This list may be too long to be useful...
306                switch (attribute.LocalName)
307                {
308                    // Additional attributes
309                    case "x":
310                        x = null;
311                        return;
312                    case "y":
313                        y = null;
314                        return;
315                    case "width":
316                        width = null;
317                        return;
318                    case "height":
319                        height = null;
320                        return;
321                }
322
323                base.HandleAttributeChange(attribute);
324            }
325        }
326        #endregion
327
328        #region ISvgExternalResourcesRequired Members
329
330        public ISvgAnimatedBoolean ExternalResourcesRequired
331        {
332            get
333            {
334                return svgExternalResourcesRequired.ExternalResourcesRequired;
335            }
336        }
337
338        #endregion
339
340        #region ISvgTests Members
341
342        public ISvgStringList RequiredFeatures
343        {
344            get { return svgTests.RequiredFeatures; }
345        }
346
347        public ISvgStringList RequiredExtensions
348        {
349            get { return svgTests.RequiredExtensions; }
350        }
351
352        public ISvgStringList SystemLanguage
353        {
354            get { return svgTests.SystemLanguage; }
355        }
356
357        public bool HasExtension(string extension)
358        {
359            return svgTests.HasExtension(extension);
360        }
361
362        #endregion
363    }
364}
Note: See TracBrowser for help on using the repository browser.