Ejemplo n.º 1
0
        /// <summary>
        /// 它创建了一个新的空SVG文档,其中只包含根元素。
        /// 如果当前文档存在,则会将其销毁。
        /// </summary>
        public SVGRoot CreateNewDocument()
        {
            if (m_root != null)
            {
                m_root    = null;
                m_nNextId = 1;
                m_elements.Clear();
            }

            m_root = new SVGRoot(this);
            m_root.setInternalId(m_nNextId++);

            m_elements.Add(m_root.getInternalId(), m_root);

            m_sXmlDeclaration = "<?xml version=\"1.0\" standalone=\"no\"?>";
            m_sXmlDocType     =
                "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">";

            m_root.SetAttributeValue(SVGAttribute._SvgAttribute.attrSvg_XmlNs, "http://www.w3.org/2000/svg");
            m_root.SetAttributeValue(SVGAttribute._SvgAttribute.attrSvg_Version, "1.1");

            return(m_root);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 它根据提供的元素名称创建一个新元素
        /// 并将新元素添加为给定父元素的最后一个子元素。
        /// </summary>
        public SVGUnit AddElement(SVGUnit parent, string sName, ref SVGUnit last)
        {
            SVGUnit eleToReturn = null;

            if (sName == "svg")
            {
                m_root = new SVGRoot(this);
                m_root.setInternalId(m_nNextId++);

                m_elements.Add(m_root.getInternalId(), m_root);
                eleToReturn = m_root;
            }
            else if (sName == "desc")
            {
                eleToReturn = AddDesc(parent, ref last);
            }
            else if (sName == "text")
            {
                eleToReturn = AddText(parent, ref last);
            }
            else if (sName == "g")//检查元素添加一个组
            {
                eleToReturn = AddGroup(parent, ref last);
            }
            else if (sName == "rect")
            {
                eleToReturn = AddRect(parent, ref last);
            }
            else if (sName == "circle")
            {
                eleToReturn = AddCircle(parent, ref last);
            }
            else if (sName == "ellipse")
            {
                eleToReturn = AddEllipse(parent, ref last);
            }
            else if (sName == "line")
            {
                eleToReturn = AddLine(parent, ref last);
            }
            else if (sName == "path")
            {
                eleToReturn = AddPath(parent, ref last);
            }
            else if (sName == "polygon")
            {
                eleToReturn = AddPolygon(parent, ref last);
            }
            else if (sName == "image")
            {
                eleToReturn = AddImage(parent, ref last);
            }
            else if (sName == "polyline")
            {
                eleToReturn = AddPolyline(parent, ref last);
            }
            else if (sName == "animate")
            {
                eleToReturn = AddAnimate(parent, ref last);
            }
            else if (sName == "devicePort")
            {
                eleToReturn = AddDevicePort(parent, ref last);
            }
            else if (sName == "defs")
            {
                eleToReturn = AddDefs(parent, ref last);
            }
            else if (sName == "symbol")
            {
                eleToReturn = AddSymbols(parent, ref last);
            }
            else if (sName == "use")
            {
                eleToReturn = AddUse(parent, ref last);
            }
            else if (sName == "metaData")
            {
                eleToReturn = AddMetaData(parent, ref last);
            }
            else if (sName == "cge:CN_Ref")
            {
                eleToReturn = AddCN_Ref(parent, ref last);
            }
            else
            {
                if (parent != null)
                {
                    eleToReturn = AddUnsupported(parent, sName, ref last);
                }
            }

            return(eleToReturn);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 构造器
 /// </summary>
 public SVGWord()
 {
     m_root     = null;
     m_nNextId  = 1;
     m_elements = new Hashtable();
 }