Beispiel #1
0
        private static void TryParseElement(SvgStats stats, XmlElement e)
        {
            stats.ElementCount += 1;

            if (e.Name == "g")
            {
                if (e.HasAttribute("inkscape:groupmode") && e.GetAttribute("inkscape:groupmode").ToLowerInvariant() == "layer")
                {
                    stats.LayerCount += 1;
                }
                else
                {
                    stats.GroupCount += 1;
                }
            }
            else if (e.HasAttribute("clip-path"))
            {
                stats.ClipCount += 1;
            }
            else if (e.HasAttribute("mask") && e.GetAttribute("mask").ToLowerInvariant() != "none")
            {
                stats.MaskCount += 1;
            }

            TryParseElementColour(stats, e);
        }
Beispiel #2
0
        public static SvgStats TryParse(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return(null);
            }

            try
            {
                SvgStats stats = new SvgStats();

                XmlDocument document = new XmlDocument();
                document.Load(filePath);

                HashSet <string> ignore = new HashSet <string>()
                {
                    "svg", "metadata", "namedview"
                };

                foreach (XmlElement e in document.SelectNodes("//*"))
                {
                    if (ignore.Contains(e.LocalName))
                    {
                        continue;
                    }

                    TryParseElement(stats, e);
                }

                stats.SortColours();

                return(stats);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

                return(null);
            }
        }
Beispiel #3
0
        private static void TryParseElementColour(SvgStats stats, XmlElement e)
        {
            // Parse colours which are direct attributes of the XML element.
            if (e.HasAttribute("fill"))
            {
                Color c = Color.Parse(e.GetAttribute("fill"));

                stats.AddColour(c);
            }

            if (e.HasAttribute("stroke"))
            {
                Color c = Color.Parse(e.GetAttribute("stroke"));

                stats.AddColour(c);
            }

            if (e.HasAttribute("stop-color"))
            {
                Color c = Color.Parse(e.GetAttribute("stop-color"));

                stats.AddColour(c);
            }

            // Parse colours which are part of a style attribute.
            if (!e.HasAttribute("style"))
            {
                return;
            }

            string style = e.GetAttribute("style").TrimEnd(';');

            foreach (string attribute in style.Split(';'))
            {
                string[] x = attribute.Split(':');

                if (x.Length < 2)
                {
                    continue;
                }

                string key   = x[0];
                string value = x[1];

                switch (key)
                {
                case "fill":
                case "stroke":
                case "stop-color":
                {
                    Color c = new Color();

                    if (Color.TryParse(value, out c))
                    {
                        stats.AddColour(c);
                    }

                    break;
                }
                }
            }
        }