SvgParser.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System.Globalization;
  2. using System.Xml;
  3. using System.Xml.Linq;
  4. using Drawie.Numerics;
  5. using PixiEditor.SVG.Elements;
  6. using PixiEditor.SVG.Features;
  7. using PixiEditor.SVG.Units;
  8. namespace PixiEditor.SVG;
  9. public class SvgParser
  10. {
  11. private static Dictionary<string, Type> wellKnownElements = new()
  12. {
  13. { "ellipse", typeof(SvgEllipse) },
  14. { "rect", typeof(SvgRectangle) },
  15. { "circle", typeof(SvgCircle) },
  16. { "line", typeof(SvgLine) },
  17. { "path", typeof(SvgPath) },
  18. { "g", typeof(SvgGroup) },
  19. { "mask", typeof(SvgMask) },
  20. { "image", typeof(SvgImage) },
  21. { "svg", typeof(SvgDocument) },
  22. { "text", typeof(SvgText) }
  23. };
  24. public string Source { get; set; }
  25. public SvgParser(string xml)
  26. {
  27. Source = xml;
  28. }
  29. public SvgDocument? Parse()
  30. {
  31. XDocument document = XDocument.Parse(Source);
  32. using var reader = document.CreateReader();
  33. XmlNodeType node = reader.MoveToContent();
  34. if (node != XmlNodeType.Element || reader.LocalName != "svg")
  35. {
  36. return null;
  37. }
  38. SvgDocument root = (SvgDocument)ParseElement(reader)!;
  39. RectD bounds = ParseBounds(reader); // this takes into account viewBox, width, height, x, y
  40. root.ViewBox.Unit = new SvgRectUnit(bounds);
  41. while (reader.Read())
  42. {
  43. if (reader.NodeType == XmlNodeType.Element)
  44. {
  45. SvgElement? element = ParseElement(reader);
  46. if (element != null)
  47. {
  48. root.Children.Add(element);
  49. if (element is IElementContainer container)
  50. {
  51. ParseChildren(reader, container, element.TagName);
  52. }
  53. }
  54. }
  55. }
  56. return root;
  57. }
  58. private void ParseChildren(XmlReader reader, IElementContainer container, string tagName)
  59. {
  60. while (reader.Read())
  61. {
  62. if (reader.NodeType == XmlNodeType.Element)
  63. {
  64. SvgElement? element = ParseElement(reader);
  65. if (element != null)
  66. {
  67. container.Children.Add(element);
  68. if (element is IElementContainer childContainer)
  69. {
  70. ParseChildren(reader, childContainer, element.TagName);
  71. }
  72. }
  73. }
  74. else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == tagName)
  75. {
  76. break;
  77. }
  78. }
  79. }
  80. private SvgElement? ParseElement(XmlReader reader)
  81. {
  82. if (wellKnownElements.TryGetValue(reader.LocalName, out Type elementType))
  83. {
  84. SvgElement element = (SvgElement)Activator.CreateInstance(elementType);
  85. if (reader.MoveToFirstAttribute())
  86. {
  87. element.ParseData(reader);
  88. }
  89. return element;
  90. }
  91. return null;
  92. }
  93. private RectD ParseBounds(XmlReader reader)
  94. {
  95. string viewBox = reader.GetAttribute("viewBox");
  96. string width = reader.GetAttribute("width");
  97. string height = reader.GetAttribute("height");
  98. string x = reader.GetAttribute("x");
  99. string y = reader.GetAttribute("y");
  100. if (viewBox == null && width == null && height == null && x == null && y == null)
  101. {
  102. return new RectD(0, 0, 0, 0);
  103. }
  104. double finalX = 0;
  105. double finalY = 0;
  106. double finalWidth = 0;
  107. double finalHeight = 0;
  108. if (viewBox != null)
  109. {
  110. string[] parts = viewBox.Split(' ').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
  111. if (parts.Length == 4)
  112. {
  113. finalX = double.Parse(parts[0], CultureInfo.InvariantCulture);
  114. finalY = double.Parse(parts[1], CultureInfo.InvariantCulture);
  115. finalWidth = double.Parse(parts[2], CultureInfo.InvariantCulture);
  116. finalHeight = double.Parse(parts[3], CultureInfo.InvariantCulture);
  117. }
  118. }
  119. if (x != null)
  120. {
  121. if (double.TryParse(x, CultureInfo.InvariantCulture, out double xValue))
  122. {
  123. finalX = xValue;
  124. }
  125. }
  126. if (y != null)
  127. {
  128. if (double.TryParse(y, CultureInfo.InvariantCulture, out double yValue))
  129. {
  130. finalY = yValue;
  131. }
  132. }
  133. if (width != null)
  134. {
  135. if (double.TryParse(width, CultureInfo.InvariantCulture, out double widthValue))
  136. {
  137. finalWidth = widthValue;
  138. }
  139. }
  140. if (height != null)
  141. {
  142. if (double.TryParse(height, CultureInfo.InvariantCulture, out double heightValue))
  143. {
  144. finalHeight = heightValue;
  145. }
  146. }
  147. return new RectD(finalX, finalY, finalWidth, finalHeight);
  148. }
  149. }