Browse Source

Parsing number and defs skipping

Krzysztof Krysiński 2 months ago
parent
commit
dc3e370778
2 changed files with 20 additions and 5 deletions
  1. 9 4
      src/PixiEditor.SVG/Elements/SvgPolyline.cs
  2. 11 1
      src/PixiEditor.SVG/SvgParser.cs

+ 9 - 4
src/PixiEditor.SVG/Elements/SvgPolyline.cs

@@ -40,7 +40,7 @@ public class SvgPolyline() : SvgPrimitive("polyline")
                 {
                     if (x.HasValue)
                     {
-                        points.Add(new VecD(x.Value, double.Parse(currentNumberString)));
+                        points.Add(new VecD(x.Value, ParseNumber(currentNumberString)));
                         x = null;
                         currentNumberString = string.Empty;
                     }
@@ -55,7 +55,7 @@ public class SvgPolyline() : SvgPrimitive("polyline")
             }
             else if (character == ',')
             {
-                x = double.Parse(currentNumberString);
+                x = ParseNumber(currentNumberString);
                 currentNumberString = string.Empty;
                 nextSpaceIsSeparator = false;
             }
@@ -65,14 +65,19 @@ public class SvgPolyline() : SvgPrimitive("polyline")
         {
             if (x.HasValue)
             {
-                points.Add(new VecD(x.Value, double.Parse(currentNumberString)));
+                points.Add(new VecD(x.Value, ParseNumber(currentNumberString)));
             }
             else
             {
-                points.Add(new VecD(double.Parse(currentNumberString), 0));
+                points.Add(new VecD(ParseNumber(currentNumberString), 0));
             }
         }
 
         return points.ToArray();
     }
+
+    private static double ParseNumber(string currentNumberString)
+    {
+        return double.Parse(currentNumberString, System.Globalization.CultureInfo.InvariantCulture);
+    }
 }

+ 11 - 1
src/PixiEditor.SVG/SvgParser.cs

@@ -47,12 +47,22 @@ public class SvgParser
         {
             if (reader.NodeType == XmlNodeType.Element)
             {
+                if (reader.LocalName == "defs")
+                {
+                    // already parsed defs, skip
+                    reader.Skip();
+                    if(reader.NodeType != XmlNodeType.Element)
+                    {
+                        continue;
+                    }
+                }
+
                 SvgElement? element = ParseElement(reader, root.Defs);
                 if (element != null)
                 {
                     root.Children.Add(element);
 
-                    if (element is IElementContainer container && element.TagName != "defs")
+                    if (element is IElementContainer container)
                     {
                         ParseChildren(reader, container, root.Defs, element.TagName);
                     }