Parser for SVG path geometry http://www.w3.org/TR/SVG11/paths.html.
Beispiel #1
0
        /// <summary>
        /// Parse a mini-language string representation of the <see cref="XPathGeometry"/>.
        /// </summary>
        /// <remarks>
        /// The path geometry syntax may start with a "wsp*Fwsp*(0|1)" which indicate the winding mode (F0 is EvenOdd while F1 is NonZero).
        /// </remarks>
        /// <param name="source">The string with geometry data.</param>
        /// <returns>The new instance of the <see cref="XPathGeometry"/> class.</returns>
        public static XPathGeometry Parse(string source)
        {
            var fillRule = XFillRule.EvenOdd;
            var geometry = XPathGeometry.Create(ImmutableArray.Create<XPathFigure>(), fillRule);

            if (source != null)
            {
                int curIndex = 0;
                while ((curIndex < source.Length) && Char.IsWhiteSpace(source, curIndex))
                {
                    curIndex++;
                }

                if (curIndex < source.Length)
                {
                    if (source[curIndex] == 'F')
                    {
                        curIndex++;

                        while ((curIndex < source.Length) && Char.IsWhiteSpace(source, curIndex))
                        {
                            curIndex++;
                        }

                        if ((curIndex == source.Length) || ((source[curIndex] != '0') && (source[curIndex] != '1')))
                        {
                            throw new FormatException("Illegal token.");
                        }

                        fillRule = source[curIndex] == '0' ? XFillRule.EvenOdd : XFillRule.Nonzero;
                        curIndex++;
                    }
                }

                var parser = new SvgToXPathGeometryParser();
                var context = new XPathGeometryContext(geometry);

                parser.Parse(context, source, curIndex);
            }

            geometry.FillRule = fillRule;

            return geometry;
        }
        /// <summary>
        /// Parse a mini-language string representation of the <see cref="XPathGeometry"/>.
        /// </summary>
        /// <remarks>
        /// The path geometry syntax may start with a "wsp*Fwsp*(0|1)" which indicate the winding mode (F0 is EvenOdd while F1 is NonZero).
        /// </remarks>
        /// <param name="source">The string with geometry data.</param>
        /// <returns>The new instance of the <see cref="XPathGeometry"/> class.</returns>
        public static XPathGeometry Parse(string source)
        {
            var fillRule = XFillRule.EvenOdd;
            var geometry = XPathGeometry.Create(ImmutableArray.Create <XPathFigure>(), fillRule);

            if (source != null)
            {
                int curIndex = 0;
                while ((curIndex < source.Length) && Char.IsWhiteSpace(source, curIndex))
                {
                    curIndex++;
                }

                if (curIndex < source.Length)
                {
                    if (source[curIndex] == 'F')
                    {
                        curIndex++;

                        while ((curIndex < source.Length) && Char.IsWhiteSpace(source, curIndex))
                        {
                            curIndex++;
                        }

                        if ((curIndex == source.Length) || ((source[curIndex] != '0') && (source[curIndex] != '1')))
                        {
                            throw new FormatException("Illegal token.");
                        }

                        fillRule = source[curIndex] == '0' ? XFillRule.EvenOdd : XFillRule.Nonzero;
                        curIndex++;
                    }
                }

                var parser  = new SvgToXPathGeometryParser();
                var context = new XPathGeometryContext(geometry);

                parser.Parse(context, source, curIndex);
            }

            geometry.FillRule = fillRule;

            return(geometry);
        }