public List <SVGCommand> Parse(string filename)
        {
            List <SVGCommand> result     = new List <SVGCommand>();
            TextReader        textReader = new StreamReader(filename);
            string            line;
            Regex             regex     = new Regex(linePattern);
            Point             lastPoint = new Point();

            while ((line = textReader.ReadLine()) != null)
            {
                Match match = regex.Match(line);
                if (match.Success)
                {
                    Point point = new Point(Convert.ToDouble(match.Groups["x"].Value, CultureInfo.InvariantCulture), Convert.ToDouble(match.Groups["y"].Value, CultureInfo.InvariantCulture));
                    if (point != lastPoint)
                    {
                        SVGCommand svgCommand = new SVGCommand(Convert.ToDouble(match.Groups["z"].Value) > 0 ? "M" : "L");
                        svgCommand.Points.Add(point);
                        result.Add(svgCommand);
                    }
                }
            }
            textReader.Close();
            return(result);
        }
Пример #2
0
        private List <SVGCommand> ParseNode(XmlNode elementNode)
        {
            List <SVGCommand> result = new List <SVGCommand>();

            if (elementNode.Name == "path" && elementNode.Attributes.GetNamedItem("d") != null)
            {
                Point           firstPoint      = new Point(0, 0);
                bool            firstPointFound = false;
                Point           lastPoint       = new Point(0, 0);
                Regex           commandRegex    = new Regex(commandPattern);
                MatchCollection commandMatches  = commandRegex.Matches(elementNode.Attributes["d"].Value);
                foreach (Match commandMatch in commandMatches)
                {
                    SVGCommand      command       = new SVGCommand(commandMatch.Groups["command"].Value);
                    Regex           pointsRegex   = new Regex(pointsPattern);
                    MatchCollection pointsMatches = pointsRegex.Matches(commandMatch.Groups["parameters"].Value);
                    foreach (Match pointMatch in pointsMatches)
                    {
                        Point point = new Point(Convert.ToDouble(pointMatch.Groups["x"].Value, CultureInfo.InvariantCulture), Convert.ToDouble(pointMatch.Groups["y"].Value, CultureInfo.InvariantCulture));
                        command.Points.Add(point);
                        if (!firstPointFound)
                        {
                            firstPointFound = true;
                            firstPoint      = point;
                            if (command.IsRelative)
                            {
                                firstPoint += lastPoint;
                            }
                        }
                    }
                    if (command.CommandType == SVGCommand.CommandTypes.ClosePath)
                    {
                        command.Points.Add(firstPoint);
                        firstPointFound = false;
                    }
                    lastPoint = command.Points.Last();
                    result.Add(command);
                }
            }
            return(result);
        }