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); }
public string ParseToAbsolute(string data) => PathDataParser.ToString(PathDataParser.ToAbsolute(PathDataParser.Parse(data)));
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); } }