Exemplo n.º 1
0
        /// <summary>
        /// Creates a new/empty SVG document that contains just the root element.
        /// </summary>
        /// <returns>
        /// The root element of the SVG document.
        /// </returns>
        public SvgRoot CreateNewDocument()
        {
            if (svgRoot != null)
            {
                svgRoot = null;
                svgDocumentNextInternalId = 1;
                svgDocumentElements.Clear();
            }

            svgRoot = new SvgRoot(this);
            svgRoot.SetInternalId(svgDocumentNextInternalId++);

            svgDocumentElements.Add(svgRoot.GetInternalId(), svgRoot);

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

            svgRoot.SetAttributeValue(SvgAttribute.SvgAttributes.AttrSvgXmlNs, "http://www.w3.org/2000/svg");
            svgRoot.SetAttributeValue(SvgAttribute.SvgAttributes.AttrSvgVersion, "1.1");

            return(svgRoot);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load SVG document from a file.
        /// </summary>
        /// <param name="filename">The complete path of a valid SVG file.</param>
        /// <returns>
        ///     True - the file is loaded successfully and it is a valid SVG document
        ///     False - the file cannot be opened or it is not a valid SVG document.
        /// </returns>
        public bool LoadFromFile(string filename)
        {
            if ( svgRoot != null )
            {
                svgRoot = null;
                svgDocumentNextInternalId = 1;
                svgDocumentElements.Clear();
            }

            var result = true;

            try
            {
                var reader = new XmlTextReader(filename)
                {
                    WhitespaceHandling = WhitespaceHandling.None,
                    Normalization = false,
                    XmlResolver = null,
                    Namespaces = false
                };

                SvgElement parentElement = null;

                try
                {
                    // parse the file and display each of the nodes.
                    while ( reader.Read() && result )
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Attribute:
                                break;

                            case XmlNodeType.Element:
                                var element = AddElement(parentElement, reader.Name);

                                if ( element != null )
                                {
                                    parentElement = element;

                                    if (reader.IsEmptyElement)
                                    {
                                        if ( parentElement != null )
                                        {
                                            parentElement = parentElement.GetParent();
                                        }
                                    }

                                    var attribute = reader.MoveToFirstAttribute();
                                    while (attribute)
                                    {
                                        element.SetAttributeValue(reader.Name, reader.Value);

                                        attribute = reader.MoveToNextAttribute();
                                    }
                                }

                                break;

                            case XmlNodeType.Text:
                                if ( parentElement != null )
                                {
                                    parentElement.SetElementValue(reader.Value);
                                }
                                break;

                            case XmlNodeType.CDATA:
                                break;

                            case XmlNodeType.ProcessingInstruction:
                                break;

                            case XmlNodeType.Comment:
                                break;

                            case XmlNodeType.XmlDeclaration:
                                svgDocumentXmlDeclaration = "<?xml " + reader.Value + "?>";
                                break;

                            case XmlNodeType.Document:
                                break;

                            case XmlNodeType.DocumentType:
                            {
                                var sDtd1 = reader.GetAttribute("PUBLIC");
                                var sDtd2 = reader.GetAttribute("SYSTEM");

                                svgDocumentXmlDocumentType = "<!DOCTYPE svg PUBLIC \"" + sDtd1 + "\" \"" + sDtd2 + "\">";
                            }
                                break;

                            case XmlNodeType.EntityReference:
                                break;

                            case XmlNodeType.EndElement:
                                if ( parentElement != null )
                                {
                                    parentElement = parentElement.GetParent();
                                }
                                break;
                        } // switch
                    } // while
                } // read try
                catch(XmlException xmle)
                {
                    ErrorMessage =
                        $"{xmle.Message}\r\nLine Number: {xmle.LineNumber.ToString()}\r\nLine Position: {xmle.LinePosition.ToString()}";

                    result = false;
                }
                catch(Exception e)
                {
                    ErrorMessage = e.Message;
                    result = false;
                }
                finally
                {
                    reader.Close();
                }
            }
            catch
            {
                ErrorMessage = "Unhandled Exception";
                result = false;
            }

            return result;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a new/empty SVG document that contains just the root element.
        /// </summary>
        /// <returns>
        /// The root element of the SVG document.
        /// </returns>
        public SvgRoot CreateNewDocument()
        {
            if ( svgRoot != null )
            {
                svgRoot = null;
                svgDocumentNextInternalId = 1;
                svgDocumentElements.Clear();
            }

            svgRoot = new SvgRoot(this);
            svgRoot.SetInternalId(svgDocumentNextInternalId++);

            svgDocumentElements.Add(svgRoot.GetInternalId(), svgRoot);

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

            svgRoot.SetAttributeValue(SvgAttribute.SvgAttributes.AttrSvgXmlNs, "http://www.w3.org/2000/svg");
            svgRoot.SetAttributeValue(SvgAttribute.SvgAttributes.AttrSvgVersion, "1.1");

            return svgRoot;
        }
Exemplo n.º 4
0
 public SvgDocument()
 {
     svgRoot = null;
     svgDocumentNextInternalId = 1;
     svgDocumentElements = new Hashtable();
 }
Exemplo n.º 5
0
        /// <summary>
        /// It creates a new element according to the element name provided
        /// and it adds the new element as the last children of the given parent element.
        /// </summary>
        /// <param name="parent">Parent element. If null the element is added under the root.</param>
        /// <param name="name">SVG element name.</param>
        /// <returns>The new created element.</returns>
        public SvgElement AddElement(SvgElement parent, string name)
        {
            SvgElement eleToReturn = null;

            if ( name == "svg" )
            {
                svgRoot = new SvgRoot(this);
                svgRoot.SetInternalId(svgDocumentNextInternalId++);

                svgDocumentElements.Add(svgRoot.GetInternalId(), svgRoot);
                eleToReturn = svgRoot;
            }
            else if ( name == "desc" )
            {
                eleToReturn = AddDesc(parent);
            }
            else if ( name == "text" )
            {
                eleToReturn = AddText(parent);
            }
            else if ( name == "g" )
            {
                eleToReturn = AddGroup(parent);
            }
            else if ( name == "rect" )
            {
                eleToReturn = AddRect(parent);
            }
            else if ( name == "circle" )
            {
                eleToReturn = AddCircle(parent);
            }
            else if ( name == "ellipse" )
            {
                eleToReturn = AddEllipse(parent);
            }
            else if ( name == "line" )
            {
                eleToReturn = AddLine(parent);
            }
            else if ( name == "path" )
            {
                eleToReturn = AddPath(parent);
            }
            else if ( name == "polygon" )
            {
                eleToReturn = AddPolygon(parent);
            }
            else if ( name == "image" )
            {
                eleToReturn = AddImage(parent);
            }
            else
            {
                if ( parent != null )
                {
                    eleToReturn = AddUnsupported(parent, name);
                }
            }

            return eleToReturn;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Load SVG document from a file.
        /// </summary>
        /// <param name="filename">The complete path of a valid SVG file.</param>
        /// <returns>
        ///     True - the file is loaded successfully and it is a valid SVG document
        ///     False - the file cannot be opened or it is not a valid SVG document.
        /// </returns>
        public bool LoadFromFile(string filename)
        {
            if (svgRoot != null)
            {
                svgRoot = null;
                svgDocumentNextInternalId = 1;
                svgDocumentElements.Clear();
            }

            var result = true;

            try
            {
                var reader = new XmlTextReader(filename)
                {
                    WhitespaceHandling = WhitespaceHandling.None,
                    Normalization      = false,
                    XmlResolver        = null,
                    Namespaces         = false
                };

                SvgElement parentElement = null;

                try
                {
                    // parse the file and display each of the nodes.
                    while (reader.Read() && result)
                    {
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Attribute:
                            break;

                        case XmlNodeType.Element:
                            var element = AddElement(parentElement, reader.Name);

                            if (element != null)
                            {
                                parentElement = element;

                                if (reader.IsEmptyElement)
                                {
                                    if (parentElement != null)
                                    {
                                        parentElement = parentElement.GetParent();
                                    }
                                }

                                var attribute = reader.MoveToFirstAttribute();
                                while (attribute)
                                {
                                    element.SetAttributeValue(reader.Name, reader.Value);

                                    attribute = reader.MoveToNextAttribute();
                                }
                            }

                            break;

                        case XmlNodeType.Text:
                            if (parentElement != null)
                            {
                                parentElement.SetElementValue(reader.Value);
                            }
                            break;

                        case XmlNodeType.CDATA:
                            break;

                        case XmlNodeType.ProcessingInstruction:
                            break;

                        case XmlNodeType.Comment:
                            break;

                        case XmlNodeType.XmlDeclaration:
                            svgDocumentXmlDeclaration = "<?xml " + reader.Value + "?>";
                            break;

                        case XmlNodeType.Document:
                            break;

                        case XmlNodeType.DocumentType:
                        {
                            var sDtd1 = reader.GetAttribute("PUBLIC");
                            var sDtd2 = reader.GetAttribute("SYSTEM");

                            svgDocumentXmlDocumentType = "<!DOCTYPE svg PUBLIC \"" + sDtd1 + "\" \"" + sDtd2 + "\">";
                        }
                        break;

                        case XmlNodeType.EntityReference:
                            break;

                        case XmlNodeType.EndElement:
                            if (parentElement != null)
                            {
                                parentElement = parentElement.GetParent();
                            }
                            break;
                        }         // switch
                    }             // while
                }                 // read try
                catch (XmlException xmle)
                {
                    ErrorMessage =
                        $"{xmle.Message}\r\nLine Number: {xmle.LineNumber.ToString()}\r\nLine Position: {xmle.LinePosition.ToString()}";

                    result = false;
                }
                catch (Exception e)
                {
                    ErrorMessage = e.Message;
                    result       = false;
                }
                finally
                {
                    reader.Close();
                }
            }
            catch
            {
                ErrorMessage = "Unhandled Exception";
                result       = false;
            }

            return(result);
        }
Exemplo n.º 7
0
        /// <summary>
        /// It creates a new element according to the element name provided
        /// and it adds the new element as the last children of the given parent element.
        /// </summary>
        /// <param name="parent">Parent element. If null the element is added under the root.</param>
        /// <param name="name">SVG element name.</param>
        /// <returns>The new created element.</returns>
        public SvgElement AddElement(SvgElement parent, string name)
        {
            SvgElement eleToReturn = null;

            if (name == "svg")
            {
                svgRoot = new SvgRoot(this);
                svgRoot.SetInternalId(svgDocumentNextInternalId++);

                svgDocumentElements.Add(svgRoot.GetInternalId(), svgRoot);
                eleToReturn = svgRoot;
            }
            else if (name == "desc")
            {
                eleToReturn = AddDesc(parent);
            }
            else if (name == "text")
            {
                eleToReturn = AddText(parent);
            }
            else if (name == "g")
            {
                eleToReturn = AddGroup(parent);
            }
            else if (name == "rect")
            {
                eleToReturn = AddRect(parent);
            }
            else if (name == "circle")
            {
                eleToReturn = AddCircle(parent);
            }
            else if (name == "ellipse")
            {
                eleToReturn = AddEllipse(parent);
            }
            else if (name == "line")
            {
                eleToReturn = AddLine(parent);
            }
            else if (name == "path")
            {
                eleToReturn = AddPath(parent);
            }
            else if (name == "polygon")
            {
                eleToReturn = AddPolygon(parent);
            }
            else if (name == "image")
            {
                eleToReturn = AddImage(parent);
            }
            else
            {
                if (parent != null)
                {
                    eleToReturn = AddUnsupported(parent, name);
                }
            }

            return(eleToReturn);
        }
Exemplo n.º 8
0
 public SvgDocument()
 {
     svgRoot = null;
     svgDocumentNextInternalId = 1;
     svgDocumentElements       = new Hashtable();
 }