2
0
Эх сурвалжийг харах

replaced string builder with XDocument

flabbet 7 сар өмнө
parent
commit
67240b9b46

+ 37 - 12
src/PixiEditor.SVG/SvgDocument.cs

@@ -1,6 +1,8 @@
 using System.Text;
+using System.Xml.Linq;
 using Drawie.Numerics;
 using PixiEditor.SVG.Features;
+using PixiEditor.SVG.Units;
 
 namespace PixiEditor.SVG;
 
@@ -9,14 +11,22 @@ public class SvgDocument(RectD viewBox) : IElementContainer
     public string RootNamespace { get; set; } = "http://www.w3.org/2000/svg";
     public string Version { get; set; } = "1.1";
     public RectD ViewBox { get; set; } = viewBox;
+    
+    public SvgProperty<SvgColorUnit> Fill { get; } = new("fill");
+    public SvgProperty<SvgColorUnit> Stroke { get; } = new("stroke");
+    public SvgProperty<SvgNumericUnit> StrokeWidth { get; } = new("stroke-width");
     public List<SvgElement> Children { get; } = new();
 
     public string ToXml()
     {
-        StringBuilder builder = new();
-        builder.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
-        builder.AppendLine(
-            $"<svg xmlns=\"{RootNamespace}\" version=\"{Version}\" viewBox=\"{ViewBox.X} {ViewBox.Y} {ViewBox.Width} {ViewBox.Height}\"");
+        XDocument document = new XDocument();
+        document.Declaration = new XDeclaration("1.0", "UTF-8", "yes");
+        XNamespace ns = RootNamespace;
+        document.Add(new XElement(
+            ns + "svg",
+            new XAttribute("version", Version),
+            new XAttribute("viewBox", $"{ViewBox.X} {ViewBox.Y} {ViewBox.Width} {ViewBox.Height}"))
+        );
 
         Dictionary<string, string> usedNamespaces = new();
 
@@ -24,20 +34,17 @@ public class SvgDocument(RectD viewBox) : IElementContainer
 
         foreach (var usedNamespace in usedNamespaces)
         {
-            builder.AppendLine(
-                $"xmlns:{usedNamespace.Key}=\"{usedNamespace.Value}\"");
+            document.Root.Add(new XAttribute($"xmlns:{usedNamespace.Key}", usedNamespace.Value));
         }
-        
-        builder.AppendLine(">");
 
+        AppendProperties(document.Root);
+        
         foreach (SvgElement child in Children)
         {
-            builder.AppendLine(child.ToXml());
+            document.Root.Add(child.ToXml(ns));
         }
 
-        builder.AppendLine("</svg>");
-
-        return builder.ToString();
+        return document.ToString();
     }
 
     public static SvgDocument Parse(string xml)
@@ -60,4 +67,22 @@ public class SvgDocument(RectD viewBox) : IElementContainer
             }
         }
     }
+    
+    private void AppendProperties(XElement? root)
+    {
+        if (Fill.Unit != null)
+        {
+            root.Add(new XAttribute("fill", Fill.Unit.Value.ToXml()));
+        }
+        
+        if (Stroke.Unit != null)
+        {
+            root.Add(new XAttribute("stroke", Stroke.Unit.Value.ToXml()));
+        }
+        
+        if (StrokeWidth.Unit != null)
+        {
+            root.Add(new XAttribute("stroke-width", StrokeWidth.Unit.Value.ToXml()));
+        }
+    }
 }

+ 8 - 15
src/PixiEditor.SVG/SvgElement.cs

@@ -1,5 +1,6 @@
 using System.Text;
 using System.Xml;
+using System.Xml.Linq;
 using PixiEditor.SVG.Exceptions;
 using PixiEditor.SVG.Features;
 using PixiEditor.SVG.Units;
@@ -13,10 +14,9 @@ public class SvgElement(string tagName)
     public string TagName { get; } = tagName;
 
 
-    public string ToXml()
+    public XElement ToXml(XNamespace nameSpace)
     {
-        StringBuilder builder = new();
-        builder.Append($"<{TagName}");
+        XElement element = new XElement(nameSpace + TagName);
 
         foreach (var property in GetType().GetProperties())
         {
@@ -25,27 +25,20 @@ public class SvgElement(string tagName)
                 SvgProperty prop = (SvgProperty)property.GetValue(this);
                 if (prop?.Unit != null)
                 {
-                    builder.Append($" {prop.SvgName}=\"{prop.Unit.ToXml()}\"");
+                    element.Add(new XAttribute(prop.SvgName, prop.Unit.ToXml()));
                 }
             }
         }
 
-        if (this is not IElementContainer container)
+        if (this is IElementContainer container)
         {
-            builder.Append(" />");
-        }
-        else
-        {
-            builder.Append(">");
             foreach (SvgElement child in container.Children)
             {
-                builder.AppendLine(child.ToXml());
+                element.Add(child.ToXml(nameSpace));
             }
-
-            builder.Append($"</{TagName}>");
         }
-
-        return builder.ToString();
+        
+        return element;
     }
 
     public virtual void ParseData(XmlReader reader)