Example #1
0
        public static SVGDocument LoadFromFile(string path)
        {
            DateTime start = DateTime.UtcNow;

            // All this nonsense is to prevent a 10 second delay when reading
            // the first SVG file. It gets hung up trying to resolve the DTD?
            MemoryStream ms = new MemoryStream();
            {
                StreamReader sr = new StreamReader(path);
                StreamWriter sw = new StreamWriter(ms);

                while (sr.EndOfStream == false)
                {
                    string line = sr.ReadLine();
                    if (line.StartsWith("<!DOCTYPE") == false)
                    {
                        sw.WriteLine(line);
                    }
                }
                sw.Flush();
                ms.Seek(0, SeekOrigin.Begin);
            }

            // Here begins the reading of the SVG file
            XmlTextReader reader = new XmlTextReader(ms);
            SVGDocument doc = new SVGDocument();
            Dictionary<string, SVGStyle> styleDictionary = new Dictionary<string, SVGStyle>();

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "style")
                    {
                        // Inline style
                        string styleData = reader.ReadElementContentAsString();
                        StringReader styleReader = new StringReader(styleData);
                        string line;

                        while ((line = styleReader.ReadLine()) != null)
                        {
                            string[] splitLine;

                            line = line.Trim();
                            splitLine = line.Split(new char[] { ' ', '\t' });

                            string name = splitLine[0];
                            if (name.StartsWith("."))
                            {
                                name = name.Substring(1);
                            }
                            if (splitLine.Count() == 2)
                            {
                                styleDictionary.Add(name, new SVGStyle(name, splitLine[1]));
                            }

                        };
                    }
                    else if (reader.Name == "rect")
                    {
                        if (reader.GetAttribute("class") != null)
                        {
                            doc.AddShape(new SVGRect(reader, styleDictionary));
                        }
                    }
                    else if (reader.Name == "circle")
                    {
                        doc.AddShape(new SVGCircle(reader, styleDictionary));
                    }
                    else if (reader.Name == "polygon")
                    {
                        doc.AddShape(new SVGPolygon(reader, styleDictionary));
                    }
                    else if (reader.Name == "polyline")
                    {
                        doc.AddShape(new SVGPolygon(reader, styleDictionary));
                    }
                    else if (reader.Name == "path")
                    {
                        doc.AddShape(new SVGPath(reader, styleDictionary));
                    }
                    else if (reader.Name == "image")
                    {
                        doc.AddShape(new SVGImage(reader, styleDictionary, path));
                    }
                }
            }

            TimeSpan duration = DateTime.UtcNow - start;
            System.Console.WriteLine("### Load took {0}s", ((double)duration.TotalMilliseconds / 1000.0));

            return doc;
        }
Example #2
0
 private void LoadSVG(string path)
 {
     document = SVGDocument.LoadFromFile(path);
     UpdatePreview();
 }
Example #3
0
        public static SVGDocument LoadFromFile(string path)
        {
            DateTime start = DateTime.UtcNow;

            // All this nonsense is to prevent a 10 second delay when reading
            // the first SVG file. It gets hung up trying to resolve the DTD?
            MemoryStream ms = new MemoryStream();
            {
                StreamReader sr = new StreamReader(path);
                StreamWriter sw = new StreamWriter(ms);

                while (sr.EndOfStream == false)
                {
                    string line = sr.ReadLine();
                    if (line.StartsWith("<!DOCTYPE") == false)
                    {
                        sw.WriteLine(line);
                    }
                }
                sw.Flush();
                ms.Seek(0, SeekOrigin.Begin);
            }

            // Here begins the reading of the SVG file
            XmlTextReader reader = new XmlTextReader(ms);
            SVGDocument   doc    = new SVGDocument();
            Dictionary <string, SVGStyle> styleDictionary = new Dictionary <string, SVGStyle>();

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "style")
                    {
                        // Inline style
                        string       styleData   = reader.ReadElementContentAsString();
                        StringReader styleReader = new StringReader(styleData);
                        string       line;

                        while ((line = styleReader.ReadLine()) != null)
                        {
                            string[] splitLine;

                            line      = line.Trim();
                            splitLine = line.Split(new char[] { ' ', '\t' });

                            string name = splitLine[0];
                            if (name.StartsWith("."))
                            {
                                name = name.Substring(1);
                            }
                            if (splitLine.Count() == 2)
                            {
                                styleDictionary.Add(name, new SVGStyle(name, splitLine[1]));
                            }
                        }
                        ;
                    }
                    else if (reader.Name == "rect")
                    {
                        if (reader.GetAttribute("class") != null)
                        {
                            doc.AddShape(new SVGRect(reader, styleDictionary));
                        }
                    }
                    else if (reader.Name == "circle")
                    {
                        doc.AddShape(new SVGCircle(reader, styleDictionary));
                    }
                    else if (reader.Name == "polygon")
                    {
                        doc.AddShape(new SVGPolygon(reader, styleDictionary));
                    }
                    else if (reader.Name == "polyline")
                    {
                        doc.AddShape(new SVGPolygon(reader, styleDictionary));
                    }
                    else if (reader.Name == "path")
                    {
                        doc.AddShape(new SVGPath(reader, styleDictionary));
                    }
                    else if (reader.Name == "image")
                    {
                        doc.AddShape(new SVGImage(reader, styleDictionary, path));
                    }
                }
            }

            TimeSpan duration = DateTime.UtcNow - start;

            System.Console.WriteLine("### Load took {0}s", ((double)duration.TotalMilliseconds / 1000.0));

            return(doc);
        }