public static SvgScriptable Parse(string svgData) { SvgScriptable rtnval = new SvgScriptable(); LoadScriptableFromString(rtnval, svgData); return(rtnval); }
static void LoadSvgDataIntoScriptable(SvgScriptable svgObj, XmlNode node) { foreach (XmlNode childNode in node.ChildNodes) { if (!(childNode is XmlElement)) { continue; } var xmlElement = childNode as XmlElement; // if group, flatten it if (xmlElement.Name == "g") { LoadSvgDataIntoScriptable(svgObj, childNode); continue; } SvgElement svgElement = new SvgElement(); LoadFillAndStrode(svgElement, xmlElement); switch (xmlElement.Name) { case "line": { svgElement.Points = ExtractLinePoints(xmlElement).ToList(); break; } case "polyline": { svgElement.Points = ExtractPolyPoints(xmlElement).ToList(); break; } case "polygon": { svgElement.Points = ExtractPolyPoints(xmlElement).ToList(); svgElement.IsClosed = true; break; } case "path": { svgElement.Points = ExtractPathPoints(xmlElement).ToList(); if (xmlElement.GetAttribute("d").ToLower().Trim().Last() == 'z') { svgElement.IsClosed = true; } break; } case "rect": { svgElement.Points = ExtractRectPoints(xmlElement).ToList(); svgElement.IsClosed = true; break; } } RemoveRepeatedPoints(svgElement); if (svgElement.Points.Count <= 1) { continue; } CheckForClosedElement(svgElement); //matrix mult if (xmlElement.HasAttribute("transform")) { var mtxStr = xmlElement.GetAttribute("transform"); if (mtxStr.StartsWith("matrix")) { mtxStr = mtxStr.Substring(7, mtxStr.Length - 8); var coords = mtxStr.Split().Select(p => float.Parse(p)).ToArray(); for (int ptInx = 0; ptInx < svgElement.Points.Count; ptInx++) { var oldPt = svgElement.Points[ptInx]; var newX = coords[0] * oldPt.x + coords[2] * oldPt.y + coords[4]; var newY = coords[1] * oldPt.x + coords[3] * oldPt.y + coords[5]; svgElement.Points[ptInx] = new Vector2(newX, newY); } } } svgObj.SvgData.Add(svgElement); } }
public static void LoadScriptableFromString(SvgScriptable svgObj, string svgFileContents) { var root = SvgStringToXmlRoot(svgFileContents); LoadSvgDataIntoScriptable(svgObj, root); }