private static PathGeometry Convert(string data)
        {
            var result = new PathGeometry();

            var pathData = PathDataParser.ToAbsolute(PathDataParser.Parse(data));

            var currentFigure = new PathFigure();

            foreach (var cmd in pathData)
            {
                switch (cmd.Type)
                {
                case PathDataParser.CommandType.MoveTo:
                    if (currentFigure != null)
                    {
                        result.Figures.Add(currentFigure);
                    }

                    currentFigure = new PathFigure()
                    {
                        StartPoint = new Point(cmd.Arguments[0], cmd.Arguments[1])
                    };
                    break;

                case PathDataParser.CommandType.LineTo:
                    var lineSegment = new LineSegment {
                        Point = new Point(cmd.Arguments[0], cmd.Arguments[1])
                    };
                    currentFigure.Segments.Add(lineSegment);
                    break;

                case PathDataParser.CommandType.Bezier:
                {
                    var bizierSegment = new BezierSegment
                    {
                        Point1 = new Point(cmd.Arguments[0], cmd.Arguments[1]),
                        Point2 = new Point(cmd.Arguments[2], cmd.Arguments[3]),
                        Point3 = new Point(cmd.Arguments[4], cmd.Arguments[5])
                    };
                    currentFigure.Segments.Add(bizierSegment);
                }
                break;

                case PathDataParser.CommandType.Close:
                    currentFigure.IsClosed = true;
                    result.Figures.Add(currentFigure);
                    currentFigure = new PathFigure();
                    break;
                }
            }

            result.Figures.Add(currentFigure);
            return(result);
        }
        protected override void OnDraw(Canvas canvas)
        {
            var rect = new Rect();

            this.GetDrawingRect(rect);

            var path = new Android.Graphics.Path();


            var lastPoint = new PointF();

            foreach (var command in PathDataParser.Parse(Element.Data))
            {
                switch (command.Type)
                {
                case PathDataParser.CommandType.MoveTo:
                    lastPoint = new PointF(command.Arguments[0], command.Arguments[1]);
                    path.MoveTo(lastPoint.X, lastPoint.Y);
                    break;

                case PathDataParser.CommandType.LineTo:
                    lastPoint = new PointF(command.Arguments[0], command.Arguments[1]);
                    path.LineTo(lastPoint.X, lastPoint.Y);
                    break;

                case PathDataParser.CommandType.LineHor:
                    lastPoint = new PointF(command.Arguments[0], lastPoint.Y);
                    path.LineTo(lastPoint.X, lastPoint.Y);
                    break;

                case PathDataParser.CommandType.LineVer:
                    lastPoint = new PointF(lastPoint.X, command.Arguments[0]);
                    path.LineTo(lastPoint.X, lastPoint.Y);
                    break;

                case PathDataParser.CommandType.RelativeLineHor:
                    lastPoint = new PointF(command.Arguments[0] + lastPoint.X, lastPoint.Y);
                    path.LineTo(lastPoint.X, lastPoint.Y);
                    break;

                case PathDataParser.CommandType.RelativeLineVer:
                    lastPoint = new PointF(lastPoint.X, lastPoint.Y + command.Arguments[0]);
                    path.LineTo(lastPoint.X, lastPoint.Y);
                    break;

                case PathDataParser.CommandType.RelativeLineTo:
                    lastPoint = new PointF(lastPoint.X + command.Arguments[0], lastPoint.Y + command.Arguments[1]);
                    path.LineTo(lastPoint.X, lastPoint.Y);
                    break;

                case PathDataParser.CommandType.RelativeMoveTo:
                    lastPoint = new PointF(lastPoint.X + command.Arguments[0], lastPoint.Y + command.Arguments[1]);
                    path.MoveTo(lastPoint.X, lastPoint.Y);
                    break;

                case PathDataParser.CommandType.QBezier:
                    path.QuadTo(command.Arguments[0], command.Arguments[1],
                                command.Arguments[2], command.Arguments[3]);
                    lastPoint = new PointF(command.Arguments[2], command.Arguments[3]);
                    break;

                case PathDataParser.CommandType.Bezier:
                    path.CubicTo(
                        command.Arguments[0], command.Arguments[1],
                        command.Arguments[2], command.Arguments[3],
                        command.Arguments[4], command.Arguments[5]);
                    lastPoint = new PointF(command.Arguments[4], command.Arguments[5]);
                    break;

                case PathDataParser.CommandType.RelativeBezier:
                    var p1 = new PointF(lastPoint.X + command.Arguments[0], lastPoint.Y + command.Arguments[1]);
                    var p2 = new PointF(p1.X + command.Arguments[2], p1.Y + command.Arguments[3]);
                    lastPoint = new PointF(p2.X + command.Arguments[4], p2.Y + command.Arguments[5]);
                    path.CubicTo(p1.X, p1.Y, p2.X, p2.Y, lastPoint.X, lastPoint.Y);
                    break;

                case PathDataParser.CommandType.Close:
                    path.Close();
                    break;
                }
            }

            var paint = new Paint(PaintFlags.AntiAlias);

            paint.StrokeWidth = Element.StrokeThickness;
            paint.StrokeMiter = 10f;
            canvas.Save();
            paint.SetStyle(Paint.Style.Stroke);
            paint.Color = Element.Stroke.ToAndroid();
            canvas.DrawPath(path, paint);
            canvas.Restore();
        }
Esempio n. 3
0
 public string ParseToAbsolute(string data) =>
 PathDataParser.ToString(PathDataParser.ToAbsolute(PathDataParser.Parse(data)));
Esempio n. 4
0
 public string ParseAndToString(string data) =>
 PathDataParser.ToString(PathDataParser.Parse(data));
Esempio n. 5
0
        protected override void OnDraw(Canvas canvas)
        {
            var trans = Element.RenderTransform ?? new IdentityTransform();

            var path = new global::Android.Graphics.Path();

            foreach (var command in PathDataParser.ToAbsolute(PathDataParser.Parse(Element.Data)))
            {
                switch (command.Type)
                {
                case PathDataParser.CommandType.MoveTo:
                {
                    var pt = trans.TransformPoint(
                        new System.Drawing.PointF(command.Arguments[0], command.Arguments[1]));
                    path.MoveTo(pt.X, pt.Y);
                }
                break;

                case PathDataParser.CommandType.LineTo:
                {
                    var pt = trans.TransformPoint(
                        new System.Drawing.PointF(command.Arguments[0], command.Arguments[1]));
                    path.LineTo(pt.X, pt.Y);
                }
                break;

                case PathDataParser.CommandType.QBezier:
                {
                    var pt = trans.TransformPoint(
                        new System.Drawing.PointF(command.Arguments[0], command.Arguments[1]));
                    var pt2 = trans.TransformPoint(
                        new System.Drawing.PointF(command.Arguments[2], command.Arguments[3]));

                    path.QuadTo(pt.X, pt.Y, pt2.X, pt2.Y);
                }

                break;

                case PathDataParser.CommandType.Bezier:
                {
                    var pt = trans.TransformPoint(
                        new System.Drawing.PointF(command.Arguments[0], command.Arguments[1]));
                    var pt2 = trans.TransformPoint(
                        new System.Drawing.PointF(command.Arguments[2], command.Arguments[3]));
                    var pt3 = trans.TransformPoint(
                        new System.Drawing.PointF(command.Arguments[4], command.Arguments[5]));

                    path.CubicTo(pt.X, pt.Y, pt2.X, pt2.Y, pt3.X, pt3.Y);
                }
                break;

                case PathDataParser.CommandType.Close:
                    path.Close();
                    DrawPath(canvas, path);
                    path = new global::Android.Graphics.Path();
                    break;
                }
            }

            if (!path.IsEmpty)
            {
                DrawPath(canvas, path);
            }
        }