Example #1
0
    public PointShapeViewModel?TryToGetPoint(BaseShapeViewModel shape, Point2 target, double radius, double scale, IDictionary <Type, IBounds> registered)
    {
        if (shape is not PointShapeViewModel point)
        {
            throw new ArgumentNullException(nameof(shape));
        }

        // ReSharper disable once CompareOfFloatsByEqualityOperator
        if (point.State.HasFlag(ShapeStateFlags.Size) && scale != 1.0)
        {
            if (Point2.FromXY(point.X, point.Y).ExpandToRect(radius / scale).Contains(target.X, target.Y))
            {
                return(point);
            }
        }
        else
        {
            if (Point2.FromXY(point.X, point.Y).ExpandToRect(radius).Contains(target.X, target.Y))
            {
                return(point);
            }
        }

        return(null);
    }
Example #2
0
        public IBaseShape Overlaps(IBaseShape shape, Rect2 target, double radius, IHitTest hitTest, Modifier modifier)
        {
            if (!(shape is IPointShape point))
            {
                throw new ArgumentNullException("shape");
            }

            return(Point2.FromXY(point.X, point.Y).ExpandToRect(radius).IntersectsWith(target) ? shape : null);
        }
Example #3
0
        public IBaseShape Contains(IBaseShape shape, Point2 target, double radius, IHitTest hitTest, Modifier modifier)
        {
            if (!(shape is IPointShape point))
            {
                throw new ArgumentNullException("shape");
            }

            return(Point2.FromXY(point.X, point.Y).ExpandToRect(radius).Contains(target.X, target.Y) ? shape : null);
        }
Example #4
0
        public override bool Contains(IBaseShape shape, Point2 target, double radius, IDictionary <Type, HitTestBase> registered)
        {
            if (!(shape is IPointShape point))
            {
                throw new ArgumentNullException(nameof(shape));
            }

            return(Point2.FromXY(point.X, point.Y).ExpandToRect(radius).Contains(target.X, target.Y));
        }
Example #5
0
        public bool Overlaps(IBaseShape shape, Rect2 target, double radius, IDictionary <Type, IBounds> registered)
        {
            if (!(shape is IPointShape point))
            {
                throw new ArgumentNullException(nameof(shape));
            }

            return(Point2.FromXY(point.X, point.Y).ExpandToRect(radius).IntersectsWith(target));
        }
Example #6
0
        public override BaseShape Overlaps(BaseShape shape, Rect2 target, double radius, IHitTest hitTest)
        {
            var point = shape as PointShape;

            if (point == null)
            {
                throw new ArgumentNullException("shape");
            }

            return(Point2.FromXY(point.X, point.Y).ExpandToRect(radius).IntersectsWith(target) ? shape : null);
        }
Example #7
0
        public override BaseShape Contains(BaseShape shape, Point2 target, double radius, IHitTest hitTest)
        {
            var point = shape as PointShape;

            if (point == null)
            {
                throw new ArgumentNullException("shape");
            }

            return(Point2.FromXY(point.X, point.Y).ExpandToRect(radius).Contains(target.X, target.Y) ? shape : null);
        }
Example #8
0
        public override bool Overlaps(BaseShape shape, Rect2 target, double radius, IDictionary <Type, HitTestBase> registered)
        {
            var point = shape as PointShape;

            if (point == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }

            return(Point2.FromXY(point.X, point.Y).ExpandToRect(radius).IntersectsWith(target));
        }
Example #9
0
        private Point2 ReadPoint(char cmd, bool allowcomma)
        {
            double x = ReadNumber(allowcomma);
            double y = ReadNumber(_allowComma);

            if (cmd >= 'a')
            {
                x += _lastPoint.X;
                y += _lastPoint.Y;
            }

            return(Point2.FromXY(x, y));
        }
Example #10
0
        public IPointShape TryToGetPoint(IBaseShape shape, Point2 target, double radius, IDictionary <Type, IBounds> registered)
        {
            if (!(shape is IPointShape point))
            {
                throw new ArgumentNullException(nameof(shape));
            }

            if (Point2.FromXY(point.X, point.Y).ExpandToRect(radius).Contains(target.X, target.Y))
            {
                return(point);
            }

            return(null);
        }
Example #11
0
 public bool Overlaps(BaseShapeViewModel shape, Rect2 target, double radius, double scale, IDictionary <Type, IBounds> registered)
 {
     if (!(shape is PointShapeViewModel point))
     {
         throw new ArgumentNullException(nameof(shape));
     }
     if (point.State.HasFlag(ShapeStateFlags.Size) && scale != 1.0)
     {
         return(Point2.FromXY(point.X, point.Y).ExpandToRect(radius / scale).IntersectsWith(target));
     }
     else
     {
         return(Point2.FromXY(point.X, point.Y).ExpandToRect(radius).IntersectsWith(target));
     }
 }
Example #12
0
        public bool Contains(IBaseShape shape, Point2 target, double radius, double scale, IDictionary <Type, IBounds> registered)
        {
            if (!(shape is IPointShape point))
            {
                throw new ArgumentNullException(nameof(shape));
            }

            if (point.State.Flags.HasFlag(ShapeStateFlags.Size) && scale != 1.0)
            {
                return(Point2.FromXY(point.X, point.Y).ExpandToRect(radius / scale).Contains(target.X, target.Y));
            }
            else
            {
                return(Point2.FromXY(point.X, point.Y).ExpandToRect(radius).Contains(target.X, target.Y));
            }
        }
Example #13
0
        public override PointShape TryToGetPoint(BaseShape shape, Point2 target, double radius, IDictionary <Type, HitTestBase> registered)
        {
            var point = shape as PointShape;

            if (point == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }

            if (Point2.FromXY(point.X, point.Y).ExpandToRect(radius).Contains(target.X, target.Y))
            {
                return(point);
            }

            return(null);
        }
Example #14
0
        /// <inheritdoc/>
        public override void Draw(object dc, XArc arc, double dx, double dy, ImmutableArray <XProperty> db, XRecord r)
        {
            var a = new GdiArc(
                Point2.FromXY(arc.Point1.X, arc.Point1.Y),
                Point2.FromXY(arc.Point2.X, arc.Point2.Y),
                Point2.FromXY(arc.Point3.X, arc.Point3.Y),
                Point2.FromXY(arc.Point4.X, arc.Point4.Y));

            if (a.Width <= 0.0 || a.Height <= 0.0)
            {
                return;
            }

            var _gfx = dc as Graphics;

            Brush brush = ToSolidBrush(arc.Style.Fill);
            Pen   pen   = ToPen(arc.Style, _scaleToPage);

            if (arc.IsFilled)
            {
                var path = new GraphicsPath();
                path.AddArc(
                    _scaleToPage(a.X + dx),
                    _scaleToPage(a.Y + dy),
                    _scaleToPage(a.Width),
                    _scaleToPage(a.Height),
                    (float)a.StartAngle,
                    (float)a.SweepAngle);
                _gfx.FillPath(brush, path);
            }

            if (arc.IsStroked)
            {
                _gfx.DrawArc(
                    pen,
                    _scaleToPage(a.X + dx),
                    _scaleToPage(a.Y + dy),
                    _scaleToPage(a.Width),
                    _scaleToPage(a.Height),
                    (float)a.StartAngle,
                    (float)a.SweepAngle);
            }

            brush.Dispose();
            pen.Dispose();
        }
Example #15
0
        /// <inheritdoc/>
        public override void Draw(object dc, ArcShape arc, double dx, double dy, object db, object r)
        {
            if (!arc.IsFilled && !arc.IsStroked)
            {
                return;
            }

            var _dc = dc as AM.DrawingContext;

            AM.IBrush brush = ToBrush(arc.Style.Fill);
            AM.Pen    pen   = ToPen(arc.Style, _scaleToPage);

            var sg = new AM.StreamGeometry();

            using (var sgc = sg.Open())
            {
                var a = new WpfArc(
                    Point2.FromXY(arc.Point1.X, arc.Point1.Y),
                    Point2.FromXY(arc.Point2.X, arc.Point2.Y),
                    Point2.FromXY(arc.Point3.X, arc.Point3.Y),
                    Point2.FromXY(arc.Point4.X, arc.Point4.Y));

                sgc.BeginFigure(
                    new A.Point(a.Start.X + dx, a.Start.Y),
                    arc.IsFilled);

                sgc.ArcTo(
                    new A.Point(a.End.X + dx, a.End.Y + dy),
                    new A.Size(a.Radius.Width, a.Radius.Height),
                    0.0,
                    a.IsLargeArc,
                    AM.SweepDirection.Clockwise);

                sgc.EndFigure(false);
            }

            _dc.DrawGeometry(
                arc.IsFilled ? brush : null,
                arc.IsStroked ? pen : null,
                sg);
        }
Example #16
0
        /// <inheritdoc/>
        public override void Draw(object dc, XArc arc, double dx, double dy, ImmutableArray <XProperty> db, XRecord r)
        {
            var canvas = dc as SKCanvas;

            using (SKPaint brush = ToSKPaintBrush(arc.Style.Fill))
                using (SKPaint pen = ToSKPaintPen(arc.Style, _scaleToPage, _sourceDpi, _targetDpi))
                    using (var path = new SKPath())
                    {
                        var a = new GdiArc(
                            Point2.FromXY(arc.Point1.X, arc.Point1.Y),
                            Point2.FromXY(arc.Point2.X, arc.Point2.Y),
                            Point2.FromXY(arc.Point3.X, arc.Point3.Y),
                            Point2.FromXY(arc.Point4.X, arc.Point4.Y));
                        var rect = new SKRect(
                            _scaleToPage(a.X + dx),
                            _scaleToPage(a.Y + dy),
                            _scaleToPage(a.X + dx + a.Width),
                            _scaleToPage(a.Y + dy + a.Height));
                        path.AddArc(rect, (float)a.StartAngle, (float)a.SweepAngle);
                        DrawPathInternal(canvas, brush, pen, arc.IsStroked, arc.IsFilled, path);
                    }
        }
Example #17
0
        public static AM.Geometry ToGeometry(ArcShapeViewModel arc)
        {
            var sg = new AM.StreamGeometry();

            using var sgc = sg.Open();
            var a = new WpfArc(
                Point2.FromXY(arc.Point1.X, arc.Point1.Y),
                Point2.FromXY(arc.Point2.X, arc.Point2.Y),
                Point2.FromXY(arc.Point3.X, arc.Point3.Y),
                Point2.FromXY(arc.Point4.X, arc.Point4.Y));

            sgc.BeginFigure(
                new A.Point(a.Start.X, a.Start.Y),
                arc.IsFilled);
            sgc.ArcTo(
                new A.Point(a.End.X, a.End.Y),
                new A.Size(a.Radius.Width, a.Radius.Height),
                0.0,
                a.IsLargeArc,
                AM.SweepDirection.Clockwise);
            sgc.EndFigure(false);
            return(sg);
        }
Example #18
0
        public PointShapeViewModel TryToGetPoint(BaseShapeViewModel shape, Point2 target, double radius, double scale, IDictionary <Type, IBounds> registered)
        {
            if (!(shape is PointShapeViewModel point))
            {
                throw new ArgumentNullException(nameof(shape));
            }

            if (point.State.HasFlag(ShapeStateFlags.Size) && scale != 1.0)
            {
                if (Point2.FromXY(point.X, point.Y).ExpandToRect(radius / scale).Contains(target.X, target.Y))
                {
                    return(point);
                }
            }
            else
            {
                if (Point2.FromXY(point.X, point.Y).ExpandToRect(radius).Contains(target.X, target.Y))
                {
                    return(point);
                }
            }

            return(null);
        }
Example #19
0
 private Point2 Reflect()
 {
     return(Point2.FromXY(2 * _lastPoint.X - _secondLastPoint.X, 2 * _lastPoint.Y - _secondLastPoint.Y));
 }
Example #20
0
        /// <summary>
        /// Move selection.
        /// </summary>
        public void Move()
        {
            var a = new WpfArc(
                Point2.FromXY(_arc.Point1.X, _arc.Point1.Y),
                Point2.FromXY(_arc.Point2.X, _arc.Point2.Y),
                Point2.FromXY(_arc.Point3.X, _arc.Point3.Y),
                Point2.FromXY(_arc.Point4.X, _arc.Point4.Y));

            if (_ellipse != null)
            {
                _ellipse.TopLeft.X     = a.P1.X;
                _ellipse.TopLeft.Y     = a.P1.Y;
                _ellipse.BottomRight.X = a.P2.X;
                _ellipse.BottomRight.Y = a.P2.Y;
            }

            if (_startLine != null)
            {
                _startLine.Start.X = a.Center.X;
                _startLine.Start.Y = a.Center.Y;
                _startLine.End.X   = a.Start.X;
                _startLine.End.Y   = a.Start.Y;
            }

            if (_endLine != null)
            {
                _endLine.Start.X = a.Center.X;
                _endLine.Start.Y = a.Center.Y;
                _endLine.End.X   = a.End.X;
                _endLine.End.Y   = a.End.Y;
            }

            if (_p1HelperPoint != null)
            {
                _p1HelperPoint.X = a.P1.X;
                _p1HelperPoint.Y = a.P1.Y;
            }

            if (_p2HelperPoint != null)
            {
                _p2HelperPoint.X = a.P2.X;
                _p2HelperPoint.Y = a.P2.Y;
            }

            if (_centerHelperPoint != null)
            {
                _centerHelperPoint.X = a.Center.X;
                _centerHelperPoint.Y = a.Center.Y;
            }

            if (_startHelperPoint != null)
            {
                _startHelperPoint.X = a.Start.X;
                _startHelperPoint.Y = a.Start.Y;
            }

            if (_endHelperPoint != null)
            {
                _endHelperPoint.X = a.End.X;
                _endHelperPoint.Y = a.End.Y;
            }

            _layer.Invalidate();
        }
Example #21
0
        /// <inheritdoc/>
        public override void Draw(object dc, ArcShape arc, double dx, double dy, object db, object r)
        {
            var _dc = dc as DrawingContext;

            var style = arc.Style;

            if (style == null)
            {
                return;
            }

            double thickness = style.Thickness / _state.ZoomX;
            double half      = thickness / 2.0;

            Tuple <Brush, Pen> styleCached = _styleCache.Get(style);
            Brush fill;
            Pen   stroke;

            if (styleCached != null)
            {
                fill   = styleCached.Item1;
                stroke = styleCached.Item2;
            }
            else
            {
                fill   = CreateBrush(style.Fill);
                stroke = CreatePen(style, thickness);
                _styleCache.Set(style, Tuple.Create(fill, stroke));
            }

            var a = new WpfArc(
                Point2.FromXY(arc.Point1.X, arc.Point1.Y),
                Point2.FromXY(arc.Point2.X, arc.Point2.Y),
                Point2.FromXY(arc.Point3.X, arc.Point3.Y),
                Point2.FromXY(arc.Point4.X, arc.Point4.Y));

            System.Windows.Media.PathGeometry pg = _arcCache.Get(arc);
            if (pg != null)
            {
                var pf = pg.Figures[0];
                pf.StartPoint = new Point(a.Start.X + dx, a.Start.Y + dy);
                pf.IsFilled   = arc.IsFilled;
                var segment = pf.Segments[0] as ArcSegment;
                segment.Point      = new Point(a.End.X + dx, a.End.Y + dy);
                segment.Size       = new Size(a.Radius.Width, a.Radius.Height);
                segment.IsLargeArc = a.IsLargeArc;
                segment.IsStroked  = arc.IsStroked;
            }
            else
            {
                var pf = new System.Windows.Media.PathFigure()
                {
                    StartPoint = new Point(a.Start.X, a.Start.Y),
                    IsFilled   = arc.IsFilled
                };

                var segment = new ArcSegment(
                    new Point(a.End.X, a.End.Y),
                    new Size(a.Radius.Width, a.Radius.Height),
                    0.0,
                    a.IsLargeArc, System.Windows.Media.SweepDirection.Clockwise,
                    arc.IsStroked);

                //segment.Freeze();
                pf.Segments.Add(segment);
                //pf.Freeze();
                pg = new System.Windows.Media.PathGeometry();
                pg.Figures.Add(pf);
                //pg.Freeze();

                _arcCache.Set(arc, pg);
            }

            DrawPathGeometryInternal(_dc, half, fill, stroke, arc.IsStroked, arc.IsFilled, pg);
        }
Example #22
0
        public static SKPath ToSKPath(this IEnumerable <IBaseShape> shapes, double dx, double dy, Func <double, float> scale)
        {
            var path = new SKPath
            {
                FillType = SKPathFillType.Winding
            };
            var previous = default(IPointShape);

            foreach (var shape in shapes)
            {
                switch (shape)
                {
                case ILineShape lineShape:
                {
                    if (previous == null || previous != lineShape.Start)
                    {
                        path.MoveTo(
                            scale(lineShape.Start.X + dx),
                            scale(lineShape.Start.Y + dy));
                    }
                    path.LineTo(
                        scale(lineShape.End.X + dx),
                        scale(lineShape.End.Y + dy));
                    previous = lineShape.End;
                }
                break;

                case IRectangleShape rectangleShape:
                {
                    path.AddRect(
                        SkiaSharpRenderer.CreateRect(rectangleShape.TopLeft, rectangleShape.BottomRight, dx, dy, scale),
                        SKPathDirection.Clockwise);
                }
                break;

                case IEllipseShape ellipseShape:
                {
                    path.AddOval(
                        SkiaSharpRenderer.CreateRect(ellipseShape.TopLeft, ellipseShape.BottomRight, dx, dy, scale),
                        SKPathDirection.Clockwise);
                }
                break;

                case IArcShape arcShape:
                {
                    var a = new GdiArc(
                        Point2.FromXY(arcShape.Point1.X, arcShape.Point1.Y),
                        Point2.FromXY(arcShape.Point2.X, arcShape.Point2.Y),
                        Point2.FromXY(arcShape.Point3.X, arcShape.Point3.Y),
                        Point2.FromXY(arcShape.Point4.X, arcShape.Point4.Y));
                    var rect = new SKRect(
                        scale(a.X + dx),
                        scale(a.Y + dy),
                        scale(a.X + dx + a.Width),
                        scale(a.Y + dy + a.Height));
                    path.AddArc(rect, (float)a.StartAngle, (float)a.SweepAngle);
                }
                break;

                case ICubicBezierShape cubicBezierShape:
                {
                    if (previous == null || previous != cubicBezierShape.Point1)
                    {
                        path.MoveTo(
                            scale(cubicBezierShape.Point1.X + dx),
                            scale(cubicBezierShape.Point1.Y + dy));
                    }
                    path.CubicTo(
                        scale(cubicBezierShape.Point2.X + dx),
                        scale(cubicBezierShape.Point2.Y + dy),
                        scale(cubicBezierShape.Point3.X + dx),
                        scale(cubicBezierShape.Point3.Y + dy),
                        scale(cubicBezierShape.Point4.X + dx),
                        scale(cubicBezierShape.Point4.Y + dy));
                    previous = cubicBezierShape.Point4;
                }
                break;

                case IQuadraticBezierShape quadraticBezierShape:
                {
                    if (previous == null || previous != quadraticBezierShape.Point1)
                    {
                        path.MoveTo(
                            scale(quadraticBezierShape.Point1.X + dx),
                            scale(quadraticBezierShape.Point1.Y + dy));
                    }
                    path.QuadTo(
                        scale(quadraticBezierShape.Point2.X + dx),
                        scale(quadraticBezierShape.Point2.Y + dy),
                        scale(quadraticBezierShape.Point3.X + dx),
                        scale(quadraticBezierShape.Point3.Y + dy));
                    previous = quadraticBezierShape.Point3;
                }
                break;

                case ITextShape textShape:
                {
                    var resultPath = ToSKPath(textShape, dx, dy, scale);
                    if (resultPath != null && !resultPath.IsEmpty)
                    {
                        path.AddPath(resultPath, SKPathAddMode.Append);
                    }
                }
                break;

                case IPathShape pathShape:
                {
                    var resultPath = ToSKPath(pathShape, dx, dy, scale);
                    if (resultPath != null && !resultPath.IsEmpty)
                    {
                        path.AddPath(resultPath, SKPathAddMode.Append);
                    }
                }
                break;

                case IGroupShape groupShape:
                {
                    var resultPath = ToSKPath(groupShape.Shapes, dx, dy, scale);
                    if (resultPath != null && !resultPath.IsEmpty)
                    {
                        path.AddPath(resultPath, SKPathAddMode.Append);
                    }
                }
                break;
                }
            }
            return(path);
        }
Example #23
0
        /// <summary>
        /// Parse a SVG path geometry string.
        /// </summary>
        /// <param name="context">The geometry context.</param>
        /// <param name="pathString">The path geometry string</param>
        /// <param name="startIndex">The string start index.</param>
        public void Parse(GeometryContext context, string pathString, int startIndex)
        {
            _context         = context;
            _pathString      = pathString;
            _pathLength      = pathString.Length;
            _curIndex        = startIndex;
            _secondLastPoint = Point2.FromXY(0, 0);
            _lastPoint       = Point2.FromXY(0, 0);
            _lastStart       = Point2.FromXY(0, 0);
            _figureStarted   = false;
            bool first    = true;
            char last_cmd = ' ';

            while (ReadToken())
            {
                char cmd = _token;

                if (first)
                {
                    if ((cmd != 'M') && (cmd != 'm'))
                    {
                        InvalidToken();
                    }

                    first = false;
                }

                switch (cmd)
                {
                case 'm':
                case 'M':
                    _lastPoint = ReadPoint(cmd, !_allowComma);

                    _context.BeginFigure(_lastPoint.AsPointShape(), _isFilled, !_isClosed);
                    _figureStarted = true;
                    _lastStart     = _lastPoint;
                    last_cmd       = 'M';

                    while (IsNumber(_allowComma))
                    {
                        _lastPoint = ReadPoint(cmd, !_allowComma);
                        _context.LineTo(_lastPoint.AsPointShape(), _isStroked, !_isSmoothJoin);
                        last_cmd = 'L';
                    }
                    break;

                case 'l':
                case 'L':
                case 'h':
                case 'H':
                case 'v':
                case 'V':
                    EnsureFigure();

                    do
                    {
                        switch (cmd)
                        {
                        case 'l':
                            _lastPoint = ReadPoint(cmd, !_allowComma);
                            break;

                        case 'L':
                            _lastPoint = ReadPoint(cmd, !_allowComma);
                            break;

                        case 'h':
                            _lastPoint = Point2.FromXY(_lastPoint.X + ReadNumber(!_allowComma), _lastPoint.Y);
                            break;

                        case 'H':
                            _lastPoint = Point2.FromXY(_lastPoint.X + ReadNumber(!_allowComma), _lastPoint.Y);
                            break;

                        case 'v':
                            _lastPoint = Point2.FromXY(_lastPoint.X, _lastPoint.Y + ReadNumber(!_allowComma));
                            break;

                        case 'V':
                            _lastPoint = Point2.FromXY(_lastPoint.X, _lastPoint.Y + ReadNumber(!_allowComma));
                            break;
                        }

                        _context.LineTo(_lastPoint.AsPointShape(), _isStroked, !_isSmoothJoin);
                    }while (IsNumber(_allowComma));

                    last_cmd = 'L';
                    break;

                case 'c':
                case 'C':
                case 's':
                case 'S':
                    EnsureFigure();

                    do
                    {
                        Point2 p;

                        if ((cmd == 's') || (cmd == 'S'))
                        {
                            if (last_cmd == 'C')
                            {
                                p = Reflect();
                            }
                            else
                            {
                                p = _lastPoint;
                            }

                            _secondLastPoint = ReadPoint(cmd, !_allowComma);
                        }
                        else
                        {
                            p = ReadPoint(cmd, !_allowComma);

                            _secondLastPoint = ReadPoint(cmd, _allowComma);
                        }

                        _lastPoint = ReadPoint(cmd, _allowComma);
                        _context.CubicBezierTo(
                            p.AsPointShape(),
                            _secondLastPoint.AsPointShape(),
                            _lastPoint.AsPointShape(),
                            _isStroked,
                            !_isSmoothJoin);

                        last_cmd = 'C';
                    }while (IsNumber(_allowComma));

                    break;

                case 'q':
                case 'Q':
                case 't':
                case 'T':
                    EnsureFigure();

                    do
                    {
                        if ((cmd == 't') || (cmd == 'T'))
                        {
                            if (last_cmd == 'Q')
                            {
                                _secondLastPoint = Reflect();
                            }
                            else
                            {
                                _secondLastPoint = _lastPoint;
                            }

                            _lastPoint = ReadPoint(cmd, !_allowComma);
                        }
                        else
                        {
                            _secondLastPoint = ReadPoint(cmd, !_allowComma);
                            _lastPoint       = ReadPoint(cmd, _allowComma);
                        }

                        _context.QuadraticBezierTo(
                            _secondLastPoint.AsPointShape(),
                            _lastPoint.AsPointShape(),
                            _isStroked,
                            !_isSmoothJoin);

                        last_cmd = 'Q';
                    }while (IsNumber(_allowComma));

                    break;

                case 'a':
                case 'A':
                    EnsureFigure();

                    do
                    {
                        double w        = ReadNumber(!_allowComma);
                        double h        = ReadNumber(_allowComma);
                        double rotation = ReadNumber(_allowComma);
                        bool   large    = ReadBool();
                        bool   sweep    = ReadBool();

                        _lastPoint = ReadPoint(cmd, _allowComma);

                        _context.ArcTo(
                            _lastPoint.AsPointShape(),
                            PathSize.Create(w, h),
                            rotation,
                            large,
                            sweep ? SweepDirection.Clockwise : SweepDirection.Counterclockwise,
                            _isStroked,
                            !_isSmoothJoin);
                    }while (IsNumber(_allowComma));

                    last_cmd = 'A';
                    break;

                case 'z':
                case 'Z':
                    EnsureFigure();
                    _context.SetClosedState(_isClosed);

                    _figureStarted = false;
                    last_cmd       = 'Z';
                    _lastPoint     = _lastStart;
                    break;

                default:
                    InvalidToken();
                    break;
                }
            }
        }
Example #24
0
    public static SKPath ToSKPath(this IEnumerable <BaseShapeViewModel> shapes)
    {
        var path = new SKPath
        {
            FillType = SKPathFillType.Winding
        };
        var previous = default(PointShapeViewModel);

        foreach (var shape in shapes)
        {
            switch (shape)
            {
            case LineShapeViewModel lineShape:
            {
                if (previous is null || previous != lineShape.Start)
                {
                    path.MoveTo(
                        (float)(lineShape.Start.X),
                        (float)(lineShape.Start.Y));
                }
                path.LineTo(
                    (float)(lineShape.End.X),
                    (float)(lineShape.End.Y));
                previous = lineShape.End;
            }
            break;

            case RectangleShapeViewModel rectangleShape:
            {
                path.AddRect(
                    SkiaSharpDrawUtil.CreateRect(rectangleShape.TopLeft, rectangleShape.BottomRight),
                    SKPathDirection.Clockwise);
            }
            break;

            case EllipseShapeViewModel ellipseShape:
            {
                path.AddOval(
                    SkiaSharpDrawUtil.CreateRect(ellipseShape.TopLeft, ellipseShape.BottomRight),
                    SKPathDirection.Clockwise);
            }
            break;

            case ArcShapeViewModel arcShape:
            {
                var a = new GdiArc(
                    Point2.FromXY(arcShape.Point1.X, arcShape.Point1.Y),
                    Point2.FromXY(arcShape.Point2.X, arcShape.Point2.Y),
                    Point2.FromXY(arcShape.Point3.X, arcShape.Point3.Y),
                    Point2.FromXY(arcShape.Point4.X, arcShape.Point4.Y));
                var rect = new SKRect(
                    (float)(a.X),
                    (float)(a.Y),
                    (float)(a.X + a.Width),
                    (float)(a.Y + a.Height));
                path.AddArc(rect, (float)a.StartAngle, (float)a.SweepAngle);
            }
            break;

            case CubicBezierShapeViewModel cubicBezierShape:
            {
                if (previous is null || previous != cubicBezierShape.Point1)
                {
                    path.MoveTo(
                        (float)(cubicBezierShape.Point1.X),
                        (float)(cubicBezierShape.Point1.Y));
                }
                path.CubicTo(
                    (float)(cubicBezierShape.Point2.X),
                    (float)(cubicBezierShape.Point2.Y),
                    (float)(cubicBezierShape.Point3.X),
                    (float)(cubicBezierShape.Point3.Y),
                    (float)(cubicBezierShape.Point4.X),
                    (float)(cubicBezierShape.Point4.Y));
                previous = cubicBezierShape.Point4;
            }
            break;

            case QuadraticBezierShapeViewModel quadraticBezierShape:
            {
                if (previous is null || previous != quadraticBezierShape.Point1)
                {
                    path.MoveTo(
                        (float)(quadraticBezierShape.Point1.X),
                        (float)(quadraticBezierShape.Point1.Y));
                }
                path.QuadTo(
                    (float)(quadraticBezierShape.Point2.X),
                    (float)(quadraticBezierShape.Point2.Y),
                    (float)(quadraticBezierShape.Point3.X),
                    (float)(quadraticBezierShape.Point3.Y));
                previous = quadraticBezierShape.Point3;
            }
            break;

            case TextShapeViewModel textShape:
            {
                var resultPath = ToSKPath(textShape);
                if (resultPath is { } && !resultPath.IsEmpty)
                {
                    path.AddPath(resultPath, SKPathAddMode.Append);
                }
            }
            break;