/// <summary> /// 它将当前SVG文档保存到一个文件中。 /// </summary> public bool SaveToFile(string sFilename) { SVGErr err = new SVGErr("SvgDoc", "SaveToFile"); err.LogParameter("sFilename", sFilename); bool bResult = false; StreamWriter sw = null; try { sw = File.CreateText(sFilename); bResult = true; } catch (UnauthorizedAccessException uae) { err.LogException(uae); } catch (DirectoryNotFoundException dnfe) { err.LogException(dnfe); } catch (ArgumentException ae) { err.LogException(ae); } catch { err.LogUnhandledException(); } if (!bResult) { err.LogEnd(false); return(false); } try { sw.Write(GetXML()); sw.Close(); } catch { err.LogUnhandledException(); err.LogEnd(false); return(false); } err.LogEnd(true); return(true); }
private bool DeleteElement(SVGUnit ele, bool bDeleteFromParent) { SVGErr err = new SVGErr("SvgDoc", "DeleteElement"); if (ele == null) { err.LogEnd(false); return(false); } SVGUnit parent = ele.getParent(); if (parent == null) { err.Log("root node cannot be delete!", SVGErr._LogPriority.Info); err.LogEnd(false); return(false); } if (ele.getPrevious() != null) { ele.getPrevious().setNext(ele.getNext()); } if (ele.getNext() != null) { ele.getNext().setPrevious(ele.getPrevious()); } if (bDeleteFromParent) { if (IsFirstChild(ele)) { ele.getParent().setChild(ele.getNext()); } } SVGUnit child = ele.getChild(); while (child != null) { DeleteElement(child, false); child = child.getNext(); } m_elements.Remove(ele.getInternalId()); err.Log(ele.ElementInfo(), SVGErr._LogPriority.Info); err.LogEnd(true); return(true); }
/// <summary> /// 它将元素在树层次结构中向上移动一级。 /// </summary> public bool ElementLevelUp(SVGUnit ele) { SVGErr err = new SVGErr("SvgDoc", "ElementLevelUp"); err.Log("Element to move " + ele.ElementInfo(), SVGErr._LogPriority.Info); SVGUnit parent = ele.getParent(); if (parent == null) { err.Log("Root node cannot be moved", SVGErr._LogPriority.Info); err.LogEnd(false); return(false); } if (parent.getParent() == null) { err.Log("An element cannot be moved up to the root", SVGErr._LogPriority.Info); err.LogEnd(false); return(false); } SVGUnit nxt = ele.getNext(); // the first child of the parent became the next parent.setChild(nxt); if (nxt != null) { nxt.setPrevious(null); } // get the last sibling of the parent SVGUnit last = GetLastSibling(parent); if (last != null) { last.setNext(ele); } ele.setParent(parent.getParent()); ele.setPrevious(last); ele.setNext(null); return(true); }
/// <summary> /// 它将新元素eletoadd添加为给定父元素的最后一个子元素。 /// </summary> public void AddElement(SVGUnit parent, SVGUnit eleToAdd, ref SVGUnit last) { SVGErr err = new SVGErr("SvgDoc", "AddElement"); if (eleToAdd == null || m_root == null) { err.LogEnd(false); return; } SVGUnit parentToAdd = m_root; if (parent != null) { parentToAdd = parent; } eleToAdd.setInternalId(m_nNextId++); m_elements.Add(eleToAdd.getInternalId(), eleToAdd); eleToAdd.setParent(parentToAdd); if (parentToAdd.getChild() == null) { // 元素是第一个子元素 parentToAdd.setChild(eleToAdd); last = eleToAdd; } else { // 将元素添加为最后一个同级 //SvgElement last = GetLastSibling(parentToAdd.getChild()); if (last != null) { last.setNext(eleToAdd); eleToAdd.setPrevious(last); last = eleToAdd; } else { last = parentToAdd.getChild(); } } err.Log(eleToAdd.ElementInfo(), SVGErr._LogPriority.Info); err.LogEnd(true); }
public bool LoadFromFile(string sFilename) { SVGErr err = new SVGErr("SvgDoc", "LoadFromFile"); bool bResult = true; try { XmlTextReader reader; reader = new XmlTextReader(sFilename); if (!LoadFromFile(reader)) { MessageBox.Show("Error reading Svg document", "SvgPaint"); } } catch (Exception e) { err.LogException(e); bResult = false; } return(bResult); }
/// <summary> /// 它将元素移动到当前的上一个同级之前。 /// </summary> public bool ElementPositionUp(SVGUnit ele) { SVGErr err = new SVGErr("SvgDoc", "ElementPositionUp"); err.Log("Element to move " + ele.ElementInfo(), SVGErr._LogPriority.Info); SVGUnit parent = ele.getParent(); if (parent == null) { err.Log("Root node cannot be moved", SVGErr._LogPriority.Info); err.LogEnd(false); return(false); } if (IsFirstChild(ele)) { err.Log("Element is already at the first position", SVGErr._LogPriority.Info); err.LogEnd(false); return(false); } SVGUnit nxt = ele.getNext(); SVGUnit prv = ele.getPrevious(); SVGUnit prv2 = null; ele.setNext(null); ele.setPrevious(null); if (nxt != null) { nxt.setPrevious(prv); } if (prv != null) { prv.setNext(nxt); prv2 = prv.getPrevious(); prv.setPrevious(ele); if (IsFirstChild(prv)) { if (prv.getParent() != null) { prv.getParent().setChild(ele); } } } if (prv2 != null) { prv2.setNext(ele); } ele.setNext(prv); ele.setPrevious(prv2); err.Log("Element moved " + ele.ElementInfo(), SVGErr._LogPriority.Info); err.LogEnd(true); return(true); }
/// <summary> /// 它创建一个从文件读取的SVG文档。 /// 如果当前文档存在,则会将其销毁。 /// </summary> public bool LoadFromFile(XmlReader reader) { SVGErr err = new SVGErr("SvgDoc", "LoadFromFile"); err.LogParameter("sFilename", reader.BaseURI); if (m_root != null) { m_root = null; m_nNextId = 1; m_elements.Clear(); } bool bResult = true; try { string tmp; SVGUnit eleParent = null; SVGUnit eleLast = null; try { // 分析文件并显示每个节点。 while (reader.Read() && bResult) { switch (reader.NodeType) { case XmlNodeType.Attribute: tmp = reader.Name; tmp = reader.Value; break; case XmlNodeType.Element: //找到一个标签 { SVGUnit ele = AddElement(eleParent, reader.Name, ref eleLast); if (ele == null) { err.Log("Svg element cannot be added. Name: " + reader.Name, SVGErr._LogPriority.Warning); bResult = false; } else { eleParent = ele; if (reader.IsEmptyElement) { if (eleParent != null) { eleParent = eleParent.getParent(); } } bool bLoop = reader.MoveToFirstAttribute(); while (bLoop) //直到读完属性为止 { if (!ele.SetAttributeValue(reader.Name, reader.Value)) { err.Log("Read AttributeValue : " + reader.Value, SVGErr._LogPriority.Warning); } bLoop = reader.MoveToNextAttribute(); } } } break; case XmlNodeType.Text: if (eleParent != null) { eleParent.setElementValue(reader.Value); } break; case XmlNodeType.CDATA: err.Log("Unexpected item: " + reader.Value, SVGErr._LogPriority.Warning); break; case XmlNodeType.ProcessingInstruction: err.Log("Unexpected item: " + reader.Value, SVGErr._LogPriority.Warning); break; case XmlNodeType.Comment: err.Log("Unexpected item: " + reader.Value, SVGErr._LogPriority.Warning); break; case XmlNodeType.XmlDeclaration: m_sXmlDeclaration = "<?xml " + reader.Value + "?>"; break; case XmlNodeType.Document: err.Log("Unexpected item: " + reader.Value, SVGErr._LogPriority.Warning); break; case XmlNodeType.DocumentType: { string sDTD1; string sDTD2; sDTD1 = reader.GetAttribute("PUBLIC"); sDTD2 = reader.GetAttribute("SYSTEM"); m_sXmlDocType = "<!DOCTYPE svg PUBLIC \"" + sDTD1 + "\" \"" + sDTD2 + "\">"; } break; case XmlNodeType.EntityReference: err.Log("Unexpected item: " + reader.Value, SVGErr._LogPriority.Warning); break; case XmlNodeType.EndElement: if (eleParent != null) { eleParent = eleParent.getParent(); eleLast = null; if (eleParent != null) { var templast = eleParent.getChild(); while (templast != null) { eleLast = templast; templast = templast.getNext(); } } } break; } // switch } // while } // read try catch (XmlException xmle) { err.LogException(xmle); err.LogParameter("Line Number", xmle.LineNumber.ToString()); err.LogParameter("Line Position", xmle.LinePosition.ToString()); bResult = false; } catch (Exception e) { err.LogException(e); bResult = false; } finally { reader.Close(); } } catch { err.LogUnhandledException(); bResult = false; } err.LogEnd(bResult); return(bResult); }
/// <summary> /// It moves the element after its current next sibling. /// </summary> public bool ElementPositionDown(SVGUnit ele) { SVGErr err = new SVGErr("SvgDoc", "ElementPositionDown"); err.Log("Element to move " + ele.ElementInfo(), SVGErr._LogPriority.Info); SVGUnit parent = ele.getParent(); if (parent == null) { err.Log("Root node cannot be moved", SVGErr._LogPriority.Info); err.LogEnd(false); return(false); } if (IsLastSibling(ele)) { err.Log("Element is already at the last sibling position", SVGErr._LogPriority.Info); err.LogEnd(false); return(false); } SVGUnit nxt = ele.getNext(); SVGUnit nxt2 = null; SVGUnit prv = ele.getPrevious(); // fix Next if (nxt != null) { nxt.setPrevious(ele.getPrevious()); nxt2 = nxt.getNext(); nxt.setNext(ele); } // fix Previous if (prv != null) { prv.setNext(nxt); } // fix Element if (IsFirstChild(ele)) { parent.setChild(nxt); } ele.setPrevious(nxt); ele.setNext(nxt2); if (nxt2 != null) { nxt2.setPrevious(ele); } err.Log("Element moved " + ele.ElementInfo(), SVGErr._LogPriority.Info); err.LogEnd(true); return(true); }