Exemplo n.º 1
0
        public SvgPathSegList(ISvgPathSegList pathList)
        {
            _pathScript = string.Empty;
            _readOnly   = false;

            _isClosed      = false;
            _mayHaveCurves = false;

            if (pathList == null)
            {
                _segments = new List <SvgPathSeg>();
            }
            else
            {
                _segments = new List <SvgPathSeg>(pathList.NumberOfItems);
                for (int i = 0; i < pathList.NumberOfItems; i++)
                {
                    _segments.Add((SvgPathSeg)pathList.GetItem(i));
                }

                var segList = pathList as SvgPathSegList;

                if (segList != null)
                {
                    _pathScript    = segList.PathScript;
                    _readOnly      = segList.IsReadOnly;
                    _isClosed      = segList.IsClosed;
                    _mayHaveCurves = segList.MayHaveCurves;
                }
            }
        }
        public Geometry CreateGeometry(SvgPathElement element)
        {
            PathGeometry geometry = new PathGeometry();

            string fillRule = element.GetPropertyValue("fill-rule");
            string clipRule = element.GetAttribute("clip-rule");

            if (!string.IsNullOrWhiteSpace(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.Equals(0) || pathArc.R2.Equals(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);
        }
Exemplo n.º 3
0
        public static GraphicsPath CreatePath(SvgPathElement element)
        {
            GraphicsPath gp = new GraphicsPath();

            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;

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

                if (DynamicCast.Cast(segment, out pathMoveTo))
                {
                    //SvgPathSegMoveto seg = (SvgPathSegMoveto)segment;
                    gp.StartFigure();
                    lastPoint = initPoint = pathMoveTo.AbsXY;
                }
                else if (DynamicCast.Cast(segment, out pathLineTo))
                {
                    //SvgPathSegLineto seg = (SvgPathSegLineto)segment;
                    SvgPointF p = pathLineTo.AbsXY;
                    gp.AddLine(lastPoint.X, lastPoint.Y, p.X, p.Y);

                    lastPoint = p;
                }
                else if (DynamicCast.Cast(segment, out pathCurveTo))
                {
                    // SvgPathSegCurveto seg = (SvgPathSegCurveto)segment;
                    SvgPointF xy   = pathCurveTo.AbsXY;
                    SvgPointF x1y1 = pathCurveTo.CubicX1Y1;
                    SvgPointF x2y2 = pathCurveTo.CubicX2Y2;
                    gp.AddBezier(lastPoint.X, lastPoint.Y, x1y1.X, x1y1.Y, x2y2.X, x2y2.Y, xy.X, xy.Y);

                    lastPoint = xy;
                }
                else if (DynamicCast.Cast(segment, out pathArc))
                {
                    //SvgPathSegArc seg = (SvgPathSegArc)segment;
                    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
                        gp.AddLine(lastPoint.X, lastPoint.Y, p.X, p.Y);
                    }
                    else
                    {
                        CalculatedArcValues calcValues = pathArc.GetCalculatedArcValues();

                        GraphicsPath gp2 = new GraphicsPath();
                        gp2.StartFigure();
                        gp2.AddArc((float)(calcValues.Cx - calcValues.CorrRx),
                                   (float)(calcValues.Cy - calcValues.CorrRy),
                                   (float)calcValues.CorrRx * 2, (float)calcValues.CorrRy * 2,
                                   (float)calcValues.AngleStart, (float)calcValues.AngleExtent);

                        Matrix matrix = new Matrix();
                        matrix.Translate(-(float)calcValues.Cx, -(float)calcValues.Cy);
                        gp2.Transform(matrix);

                        matrix = new Matrix();
                        matrix.Rotate((float)pathArc.Angle);
                        gp2.Transform(matrix);

                        matrix = new Matrix();
                        matrix.Translate((float)calcValues.Cx, (float)calcValues.Cy);
                        gp2.Transform(matrix);

                        gp.AddPath(gp2, true);
                    }

                    lastPoint = p;
                }
                else if (segment is SvgPathSegClosePath)
                {
                    gp.CloseFigure();
                    lastPoint = initPoint;
                }
            }

            string fillRule = element.GetPropertyValue("fill-rule");

            if (fillRule == "evenodd")
            {
                gp.FillMode = FillMode.Alternate;
            }
            else
            {
                gp.FillMode = FillMode.Winding;
            }

            return(gp);
        }