SVGParser.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System.IO;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public enum SVGNodeName {
  6. Rect,
  7. Line,
  8. Circle,
  9. Ellipse,
  10. PolyLine,
  11. Polygon,
  12. Path,
  13. SVG,
  14. G,
  15. LinearGradient,
  16. RadialGradient,
  17. Defs,
  18. Title,
  19. Desc,
  20. Stop
  21. }
  22. public class Node {
  23. public SVGNodeName Name;
  24. public Dictionary<string, string> Attributes;
  25. public Node(SVGNodeName n, Dictionary<string, string> a) {
  26. Name = n;
  27. Attributes = a;
  28. }
  29. }
  30. public class InlineNode : Node {
  31. public InlineNode(SVGNodeName n, Dictionary<string, string> a) : base(n, a) {
  32. }
  33. }
  34. public class BlockOpenNode : Node {
  35. public BlockOpenNode(SVGNodeName n, Dictionary<string, string> a) : base(n, a) {
  36. }
  37. }
  38. public class BlockCloseNode : Node {
  39. public BlockCloseNode(SVGNodeName n, Dictionary<string, string> a) : base(n, a) {
  40. }
  41. }
  42. public class SVGParser : SmallXmlParser.IContentHandler {
  43. private SmallXmlParser _parser = new SmallXmlParser();
  44. /***********************************************************************************/
  45. public SVGParser() {
  46. }
  47. public SVGParser(string text) {
  48. _parser.Parse(new StringReader(text), this);
  49. }
  50. /***********************************************************************************/
  51. private List<Node> Stream = new List<Node>();
  52. private int idx = 0;
  53. public Node Node { get { return Stream[idx]; } }
  54. public bool Next() {
  55. idx++;
  56. return !IsEOF;
  57. }
  58. public bool IsEOF { get { return idx >= Stream.Count; } }
  59. public void OnStartParsing(SmallXmlParser parser) {
  60. idx = 0;
  61. }
  62. public void OnInlineElement(string name, Dictionary<string, string> attrs) {
  63. Stream.Add(new InlineNode(Lookup(name), new Dictionary<string, string>(attrs)));
  64. }
  65. public void OnStartElement(string name, Dictionary<string, string> attrs) {
  66. Stream.Add(new BlockOpenNode(Lookup(name), new Dictionary<string, string>(attrs)));
  67. }
  68. public void OnEndElement(string name) {
  69. Stream.Add(new BlockCloseNode(Lookup(name), new Dictionary<string, string>()));
  70. }
  71. public void GetElementList(List<ISVGDrawable> elementList, SVGPaintable paintable,
  72. SVGGraphics render, SVGTransformList summaryTransformList) {
  73. bool exitFlag = false;
  74. while(!exitFlag && Next()) {
  75. if(Node is BlockCloseNode) {
  76. exitFlag = true;
  77. continue;
  78. }
  79. switch(Node.Name) {
  80. case SVGNodeName.Rect: elementList.Add(new SVGRectElement(Node.Attributes, summaryTransformList, paintable, render)); break;
  81. case SVGNodeName.Line: elementList.Add(new SVGLineElement(Node.Attributes, summaryTransformList, paintable, render)); break;
  82. case SVGNodeName.Circle: elementList.Add(new SVGCircleElement(Node.Attributes, summaryTransformList, paintable, render)); break;
  83. case SVGNodeName.Ellipse: elementList.Add(new SVGEllipseElement(Node.Attributes, summaryTransformList, paintable, render)); break;
  84. case SVGNodeName.PolyLine: elementList.Add(new SVGPolylineElement(Node.Attributes, summaryTransformList, paintable, render)); break;
  85. case SVGNodeName.Polygon: elementList.Add(new SVGPolygonElement(Node.Attributes, summaryTransformList, paintable, render)); break;
  86. case SVGNodeName.Path: elementList.Add(new SVGPathElement(Node.Attributes, summaryTransformList, paintable, render)); break;
  87. case SVGNodeName.SVG: elementList.Add(new SVGSVGElement(this, summaryTransformList, paintable, render)); break;
  88. case SVGNodeName.G: elementList.Add(new SVGGElement(this, summaryTransformList, paintable, render)); break;
  89. case SVGNodeName.LinearGradient: paintable.AppendLinearGradient(new SVGLinearGradientElement(this, Node.Attributes)); break;
  90. case SVGNodeName.RadialGradient: paintable.AppendRadialGradient(new SVGRadialGradientElement(this, Node.Attributes)); break;
  91. case SVGNodeName.Defs: GetElementList(elementList, paintable, render, summaryTransformList); break;
  92. case SVGNodeName.Title: GetElementList(elementList, paintable, render, summaryTransformList); break;
  93. case SVGNodeName.Desc: GetElementList(elementList, paintable, render, summaryTransformList); break;
  94. }
  95. }
  96. }
  97. private static SVGNodeName Lookup(string name) {
  98. SVGNodeName retVal;
  99. // TODO: Experiment with a dictionary lookup here.
  100. switch(name) {
  101. case "rect": retVal = SVGNodeName.Rect; break;
  102. case "line": retVal = SVGNodeName.Line; break;
  103. case "circle": retVal = SVGNodeName.Circle; break;
  104. case "ellipse": retVal = SVGNodeName.Ellipse; break;
  105. case "polyline": retVal = SVGNodeName.PolyLine; break;
  106. case "polygon": retVal = SVGNodeName.Polygon; break;
  107. case "path": retVal = SVGNodeName.Path; break;
  108. case "svg": retVal = SVGNodeName.SVG; break;
  109. case "g": retVal = SVGNodeName.G; break;
  110. case "linearGradient": retVal = SVGNodeName.LinearGradient; break;
  111. case "radialGradient": retVal = SVGNodeName.RadialGradient; break;
  112. case "defs": retVal = SVGNodeName.Defs; break;
  113. case "title": retVal = SVGNodeName.Title; break;
  114. case "desc": retVal = SVGNodeName.Desc; break;
  115. case "stop": retVal = SVGNodeName.Stop; break;
  116. default: throw new System.Exception("Unknown element type '" + name + "'!");
  117. }
  118. return retVal;
  119. }
  120. }