|
@@ -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()));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|