public static SvgElement LoadFromXML(XmlDocument doc, XmlElement el) { if (el == null) { foreach (XmlNode xmlNode in doc.ChildNodes) { if (xmlNode.GetType() == typeof(XmlElement)) { el = (XmlElement)xmlNode; break; } } } SvgElement result; if (el == null) { result = null; } else { if (SvgFactory._elementNameDictionary == null) { SvgFactory.BuildElementNameDictionary(); } Type type = (Type)SvgFactory._elementNameDictionary[el.Name]; SvgElement svgElement = (SvgElement)type.GetConstructor(new Type[0]).Invoke(new object[0]); SvgFactory.RecLoadFromXML(svgElement, doc, el); result = svgElement; } return(result); }
private static void RecLoadFromXML(SvgElement e, XmlDocument doc, XmlElement el) { e.ReadXmlElement(doc, el); foreach (XmlNode xmlNode in el.ChildNodes) { if (xmlNode.GetType() == typeof(XmlElement)) { XmlElement xmlElement = (XmlElement)xmlNode; Type type = (Type)SvgFactory._elementNameDictionary[xmlElement.Name]; SvgElement svgElement; if (type == null) { svgElement = new SvgGenericElement(xmlElement.Name); } else { svgElement = (SvgElement)type.GetConstructor(new Type[0]).Invoke(new object[0]); } e.AddChild(svgElement); SvgFactory.RecLoadFromXML(svgElement, doc, xmlElement); } else if (xmlNode.GetType() == typeof(XmlText)) { XmlText xmlText = (XmlText)xmlNode; TextNode ch = new TextNode(xmlText.InnerText); e.AddChild(ch); } } }
public string WriteSVGString(bool compressAttributes) { string internalSubset = ""; XmlDocument xmlDocument = new XmlDocument(); XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", null, "yes"); xmlDocument.AppendChild(xmlDeclaration); this.WriteXmlElements(xmlDocument, null); xmlDocument.DocumentElement.SetAttribute("xmlns", "http://www.w3.org/2000/svg"); if (compressAttributes) { internalSubset = SvgFactory.CompressXML(xmlDocument, xmlDocument.DocumentElement); } xmlDocument.XmlResolver = new SvgElement.DummyXmlResolver(); xmlDocument.InsertAfter(xmlDocument.CreateDocumentType("svg", "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd", internalSubset), xmlDeclaration); MemoryStream memoryStream = new MemoryStream(); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, new UTF8Encoding()); xmlTextWriter.Formatting = Formatting.None; xmlDocument.Save(xmlTextWriter); byte[] array = memoryStream.ToArray(); string @string = Encoding.UTF8.GetString(array, 0, array.Length); xmlTextWriter.Close(); return(@string); }
public static string CompressXML(XmlDocument doc, XmlElement el) { Hashtable hashtable = new Hashtable(); Hashtable hashtable2 = new Hashtable(); int num = 0; SvgFactory.RecCompXML(hashtable, hashtable2, doc, el, ref num); foreach (DictionaryEntry dictionaryEntry in hashtable2) { string text = (string)dictionaryEntry.Key; SvgFactory.EntitySingleton entitySingleton = (SvgFactory.EntitySingleton)dictionaryEntry.Value; entitySingleton.Element.RemoveAttribute(entitySingleton.AttributeName); XmlAttribute xmlAttribute = doc.CreateAttribute(entitySingleton.AttributeName); xmlAttribute.Value = text; entitySingleton.Element.SetAttributeNode(xmlAttribute); hashtable.Remove(text); } StringBuilder stringBuilder = new StringBuilder(); if (hashtable.Count > 0) { stringBuilder.Append("\n"); foreach (string text2 in hashtable.Keys) { stringBuilder.Append("<!ENTITY "); stringBuilder.Append(hashtable[text2]); stringBuilder.Append(" '"); stringBuilder.Append(text2.Replace("%", "%")); stringBuilder.Append("'>"); } stringBuilder.Append("\n"); } return(stringBuilder.ToString()); }
/// <summary> /// Get a string that contains a complete SVG document. XML version, DOCTYPE etc are included. /// </summary> /// <returns></returns> /// <param name="compressAttributes">Should usually be set true. Causes the XML output to be optimized so that /// long attributes like styles and transformations are represented with entities.</param> public string WriteSVGString(bool compressAttributes) { var doc = new XmlDocument(); var declaration = doc.CreateXmlDeclaration("1.0", null, "yes"); doc.AppendChild(declaration); //write out our SVG tree to the new XmlDocument WriteXmlElements(doc, null); doc.DocumentElement.SetAttribute("xmlns", "http://www.w3.org/2000/svg"); var ents = string.Empty; if (compressAttributes) { ents = SvgFactory.CompressXML(doc, doc.DocumentElement); } doc.XmlResolver = new DummyXmlResolver(); doc.InsertAfter( doc.CreateDocumentType("svg", "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd", ents), declaration ); return(ToXmlString(doc)); }
/// <summary> /// Get a string that contains a complete SVG document. XML version, DOCTYPE etc are included. /// </summary> /// <returns></returns> /// <param name="compressAttributes">Should usually be set true. Causes the XML output to be optimized so that /// long attributes like styles and transformations are represented with entities.</param> public string WriteSVGString(bool compressAttributes) { string s; string ents = ""; XmlDocument doc = new XmlDocument(); //write out our SVG tree to the new XmlDocument WriteXmlElements(doc, null); if (compressAttributes) { ents = SvgFactory.CompressXML(doc, doc.DocumentElement); } //This complicated business of writing to a memory stream and then reading back out to a string //is necessary in order to specify UTF8 -- for some reason the default is UTF16 (which makes most renderers //give up) MemoryStream ms = new MemoryStream(); XmlTextWriter wr = new XmlTextWriter(ms, new UTF8Encoding()); wr.Formatting = Formatting.Indented; wr.WriteStartDocument(true); wr.WriteDocType("svg", "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd", ents); doc.Save(wr); byte[] buf = ms.ToArray(); s = Encoding.UTF8.GetString(buf, 0, buf.Length); wr.Close(); return(s); }
public void TestSvgFactory_BuildElementNameDictionary() { var dict = SvgFactory.BuildElementNameDictionary(); Assert.NotNull(dict); Assert.That(dict, Has.Count.GreaterThanOrEqualTo(25)); Assert.IsTrue(dict.ContainsKey("svg")); }
private XmlDocument GetXmlDocument(bool compressAttributes, out string ents) { ents = ""; XmlDocument doc = new XmlDocument(); //write out our SVG tree to the new XmlDocument WriteXmlElements(doc, null); if (compressAttributes) { ents = SvgFactory.CompressXML(doc, doc.DocumentElement); } return(doc); }
/// <summary> /// Get a string that contains a complete SVG document. XML version, DOCTYPE etc are included. /// </summary> /// <returns></returns> /// <param name="compressAttributes">Should usually be set true. Causes the XML output to be optimized so that /// long attributes like styles and transformations are represented with entities.</param> public string WriteSVGString(bool compressAttributes) { string s; string ents = ""; XmlDocument doc = new XmlDocument(); var declaration = doc.CreateXmlDeclaration("1.0", null, "yes"); doc.AppendChild(declaration); //write out our SVG tree to the new XmlDocument WriteXmlElements(doc, null); doc.DocumentElement.SetAttribute("xmlns", "http://www.w3.org/2000/svg"); if (compressAttributes) { ents = SvgFactory.CompressXML(doc, doc.DocumentElement); } doc.XmlResolver = new DummyXmlResolver(); doc.InsertAfter( doc.CreateDocumentType("svg", "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd", ents), declaration ); //This complicated business of writing to a memory stream and then reading back out to a string //is necessary in order to specify UTF8 -- for some reason the default is UTF16 (which makes most renderers //give up) MemoryStream ms = new MemoryStream(); XmlTextWriter wr = new XmlTextWriter(ms, new UTF8Encoding()); wr.Formatting = Formatting.None; // Indented formatting would be nice for debugging but causes unwanted trailing white spaces between <text> and <tspan> elements in Internet Explorer doc.Save(wr); byte[] buf = ms.ToArray(); s = Encoding.UTF8.GetString(buf, 0, buf.Length); wr.Close(); return(s); }
private static void RecCompXML(Hashtable entities, Hashtable singletons, XmlDocument doc, XmlElement el, ref int idx) { ArrayList arrayList = new ArrayList(); foreach (XmlAttribute xmlAttribute in el.Attributes) { arrayList.Add(xmlAttribute.Name); } foreach (string text in arrayList) { string value = el.Attributes[text].Value; if (value.Length > 30) { string text2; if (entities[value] == null) { idx++; text2 = "E" + idx.ToString(); entities[value] = text2; singletons[value] = new SvgFactory.EntitySingleton { Element = el, AttributeName = text }; } else { text2 = (string)entities[value]; singletons.Remove(value); } XmlAttribute xmlAttribute2 = doc.CreateAttribute(text); xmlAttribute2.AppendChild(doc.CreateEntityReference(text2)); el.SetAttributeNode(xmlAttribute2); } } foreach (XmlNode xmlNode in el.ChildNodes) { if (xmlNode.GetType() == typeof(XmlElement)) { SvgFactory.RecCompXML(entities, singletons, doc, (XmlElement)xmlNode, ref idx); } } }
public static SvgElement CloneElement(SvgElement el) { SvgElement svgElement = (SvgElement)el.GetType().GetConstructor(new Type[0]).Invoke(new object[0]); foreach (string attname in el.Attributes.Keys) { object obj = el[attname]; if (typeof(ICloneable).IsInstanceOfType(obj)) { svgElement[attname] = ((ICloneable)obj).Clone(); } else { svgElement[attname] = obj; } } foreach (SvgElement el2 in el.Children) { svgElement.AddChild(SvgFactory.CloneElement(el2)); } return(svgElement); }