Пример #1
0
        public static Geometry CreateGeometryEx(SvgPathElement element)
        {
            PathGeometry geometry = new PathGeometry();

            string pathScript = element.PathScript;
            if (String.IsNullOrEmpty(pathScript))
            {
                return geometry;
            }

            string fillRule = element.GetPropertyValue("fill-rule");
            string clipRule = element.GetAttribute("clip-rule");
            if (!String.IsNullOrEmpty(clipRule) &&
                String.Equals(clipRule, "evenodd") || String.Equals(clipRule, "nonzero"))
            {
                fillRule = clipRule;
            }
            if (fillRule == "evenodd")
                geometry.FillRule = FillRule.EvenOdd;
            else if (fillRule == "nonzero")
                geometry.FillRule = FillRule.Nonzero;

            try
            {
                geometry.Figures = PathFigureCollection.Parse(pathScript);
            }
            catch
            {
            }

            return geometry;
        }
Пример #2
0
        public static Geometry CreateGeometry(SvgPathElement element)
        {
            PathGeometry geometry = new PathGeometry();

            string fillRule = element.GetPropertyValue("fill-rule");
            string clipRule = element.GetAttribute("clip-rule");
            if (!String.IsNullOrEmpty(clipRule) &&
                String.Equals(clipRule, "evenodd") || String.Equals(clipRule, "nonzero"))
            {
                fillRule = clipRule;
            }
            if (fillRule == "evenodd")
                geometry.FillRule = FillRule.EvenOdd;
            else if (fillRule == "nonzero")
                geometry.FillRule = FillRule.Nonzero;

            SvgPointF initPoint = new SvgPointF(0, 0);
            SvgPointF lastPoint = new SvgPointF(0, 0);

            ISvgPathSeg segment           = null;
            SvgPathSegMoveto pathMoveTo   = null;
            SvgPathSegLineto pathLineTo   = null;
            SvgPathSegCurveto pathCurveTo = null;
            SvgPathSegArc pathArc         = null;

            ISvgPathSegList segments = element.PathSegList;
            int nElems = segments.NumberOfItems;

            PathFigure pathFigure = null;

            for (int i = 0; i < nElems; i++)
            {
                segment = segments.GetItem(i);

                if (DynamicCast.Cast(segment, out pathMoveTo))
                {
                    if (pathFigure != null)
                    {
                        pathFigure.IsClosed = false;
                        pathFigure.IsFilled = true;
                        geometry.Figures.Add(pathFigure);
                        pathFigure = null;
                    }

                    lastPoint = initPoint = pathMoveTo.AbsXY;

                    pathFigure = new PathFigure();
                    pathFigure.StartPoint = new Point(initPoint.ValueX, initPoint.ValueY);
                }
                else if (DynamicCast.Cast(segment, out pathLineTo))
                {
                    SvgPointF p = pathLineTo.AbsXY;
                    pathFigure.Segments.Add(new LineSegment(new Point(p.ValueX, p.ValueY), true));

                    lastPoint = p;
                }
                else if (DynamicCast.Cast(segment, out pathCurveTo))
                {
                    SvgPointF xy   = pathCurveTo.AbsXY;
                    SvgPointF x1y1 = pathCurveTo.CubicX1Y1;
                    SvgPointF x2y2 = pathCurveTo.CubicX2Y2;
                    pathFigure.Segments.Add(new BezierSegment(new Point(x1y1.ValueX, x1y1.ValueY),
                        new Point(x2y2.ValueX, x2y2.ValueY), new Point(xy.ValueX, xy.ValueY), true));

                    lastPoint = xy;
                }
                else if (DynamicCast.Cast(segment, out pathArc))
                {
                    SvgPointF p = pathArc.AbsXY;
                    if (lastPoint.Equals(p))
                    {
                        // If the endpoints (x, y) and (x0, y0) are identical, then this
                        // is equivalent to omitting the elliptical arc segment entirely.
                    }
                    else if (pathArc.R1 == 0 || pathArc.R2 == 0)
                    {
                        // Ensure radii are valid
                        pathFigure.Segments.Add(new LineSegment(new Point(p.ValueX, p.ValueY), true));
                    }
                    else
                    {
                        CalculatedArcValues calcValues = pathArc.GetCalculatedArcValues();

                        pathFigure.Segments.Add(new ArcSegment(new Point(p.ValueX, p.ValueY),
                            new Size(pathArc.R1, pathArc.R2), pathArc.Angle, pathArc.LargeArcFlag,
                            pathArc.SweepFlag ? SweepDirection.Clockwise : SweepDirection.Counterclockwise,
                            true));
                    }

                    lastPoint = p;
                }
                else if (segment is SvgPathSegClosePath)
                {
                    if (pathFigure != null)
                    {
                        pathFigure.IsClosed = true;
                        pathFigure.IsFilled = true;
                        geometry.Figures.Add(pathFigure);
                        pathFigure = null;
                    }

                    lastPoint = initPoint;
                }
            }

            if (pathFigure != null)
            {
                pathFigure.IsClosed = false;
                pathFigure.IsFilled = true;
                geometry.Figures.Add(pathFigure);
            }

            return geometry;
        }