Exemplo n.º 1
0
        public static Task <bool> PointIsInPath(
            this IJSRuntime runtime,
            PathF path,
            float x,
            float y)
        {
            var pathDefinition = path.ToDefinitionString();

            return(PointIsInPath(runtime, pathDefinition, x, y));
        }
Exemplo n.º 2
0
        public static GraphicsPath AsGDIPath(this PathF target, float ppu, float ox, float oy, float fx, float fy)
        {
            var path = new GraphicsPath();

#if DEBUG
            try
            {
#endif

            float ppux = ppu * fx;
            float ppuy = ppu * fy;

            int pointIndex        = 0;
            var arcAngleIndex     = 0;
            var arcClockwiseIndex = 0;

            foreach (var type in target.SegmentTypes)
            {
                if (type == PathOperation.Move)
                {
                    path.StartFigure();
                    pointIndex++;
                }
                else if (type == PathOperation.Line)
                {
                    var startPoint = target[pointIndex - 1];
                    var endPoint   = target[pointIndex++];
                    path.AddLine(
                        ox + startPoint.X * ppux,
                        oy + startPoint.Y * ppuy,
                        ox + endPoint.X * ppux,
                        oy + endPoint.Y * ppuy);
                }
                else if (type == PathOperation.Quad)
                {
                    var startPoint       = target[pointIndex - 1];
                    var quadControlPoint = target[pointIndex++];
                    var endPoint         = target[pointIndex++];

                    var cubicControlPoint1X = startPoint.X + 2.0f * (quadControlPoint.X - startPoint.X) / 3.0f;
                    var cubicControlPoint1Y = startPoint.Y + 2.0f * (quadControlPoint.Y - startPoint.Y) / 3.0f;

                    var cubicControlPoint2X = endPoint.X + 2.0f * (quadControlPoint.X - endPoint.X) / 3.0f;
                    var cubicControlPoint2Y = endPoint.Y + 2.0f * (quadControlPoint.Y - endPoint.Y) / 3.0f;

                    path.AddBezier(
                        ox + startPoint.X * ppux,
                        oy + startPoint.Y * ppuy,
                        ox + cubicControlPoint1X * ppux,
                        oy + cubicControlPoint1Y * ppuy,
                        ox + cubicControlPoint2X * ppux,
                        oy + cubicControlPoint2Y * ppuy,
                        ox + endPoint.X * ppux,
                        oy + endPoint.Y * ppuy);
                }
                else if (type == PathOperation.Cubic)
                {
                    var startPoint         = target[pointIndex - 1];
                    var cubicControlPoint1 = target[pointIndex++];
                    var cubicControlPoint2 = target[pointIndex++];
                    var endPoint           = target[pointIndex++];

                    path.AddBezier(
                        ox + startPoint.X * ppux,
                        oy + startPoint.Y * ppuy,
                        ox + cubicControlPoint1.X * ppux,
                        oy + cubicControlPoint1.Y * ppuy,
                        ox + cubicControlPoint2.X * ppux,
                        oy + cubicControlPoint2.Y * ppuy,
                        ox + endPoint.X * ppux,
                        oy + endPoint.Y * ppuy);
                }
                else if (type == PathOperation.Arc)
                {
                    var topLeft     = target[pointIndex++];
                    var bottomRight = target[pointIndex++];
                    var startAngle  = target.GetArcAngle(arcAngleIndex++);
                    var endAngle    = target.GetArcAngle(arcAngleIndex++);
                    var clockwise   = target.GetArcClockwise(arcClockwiseIndex++);

                    while (startAngle < 0)
                    {
                        startAngle += 360;
                    }

                    while (endAngle < 0)
                    {
                        endAngle += 360;
                    }

                    var x      = ox + topLeft.X * ppux;
                    var y      = oy + topLeft.Y * ppuy;
                    var width  = (bottomRight.X - topLeft.X) * ppux;
                    var height = (bottomRight.Y - topLeft.Y) * ppuy;

                    float sweep = Geometry.GetSweep(startAngle, endAngle, clockwise);
                    if (!clockwise)
                    {
                        startAngle = endAngle;
                    }

                    startAngle *= -1;

                    path.AddArc(x, y, width, height, startAngle, sweep);
                }
                else if (type == PathOperation.Close)
                {
                    path.CloseFigure();
                }
            }

            return(path);

#if DEBUG
        }

        catch (Exception exc)
        {
            path.Dispose();

            var definition = target.ToDefinitionString();
            Logger.Debug(string.Format("Unable to convert the path to a GDIPath: {0}", definition), exc);
            return(null);
        }
#endif
        }
Exemplo n.º 3
0
        public static PathGeometry AsDxPath(this PathF path, float ppu, float ox, float oy, float fx, float fy, Factory factory, FillMode fillMode = FillMode.Winding)
        {
            var geometry = new PathGeometry(factory);

#if DEBUG
            try
            {
#endif

            var sink = geometry.Open();
            sink.SetFillMode(fillMode);

            var ppux = ppu * fx;
            var ppuy = ppu * fy;

            var pointIndex        = 0;
            var arcAngleIndex     = 0;
            var arcClockwiseIndex = 0;
            var figureOpen        = false;
            var segmentIndex      = -1;

            var lastOperation = PathOperation.Move;

            foreach (var type in path.SegmentTypes)
            {
                segmentIndex++;

                if (type == PathOperation.Move)
                {
                    if (lastOperation != PathOperation.Close && lastOperation != PathOperation.Move)
                    {
                        sink.EndFigure(FigureEnd.Open);
                        //vPath = vPathGeometry.Open();
                    }

                    var point = path[pointIndex++];

                    /*var vBegin = FigureBegin.Hollow;
                     * if (path.IsSubPathClosed(vSegmentIndex))
                     * {
                     *  vBegin = FigureBegin.Filled;
                     * }*/
                    var begin = FigureBegin.Filled;
                    sink.BeginFigure(ox + point.X * ppux, oy + point.Y * ppuy, begin);
                    figureOpen = true;
                }
                else if (type == PathOperation.Line)
                {
                    var point = path[pointIndex++];
                    sink.LineTo(ox + point.X * ppux, oy + point.Y * ppuy);
                }

                else if (type == PathOperation.Quad)
                {
                    var controlPoint = path[pointIndex++];
                    var endPoint     = path[pointIndex++];

                    sink.QuadTo(
                        ox + controlPoint.X * ppux,
                        oy + controlPoint.Y * ppuy,
                        ox + endPoint.X * ppux,
                        oy + endPoint.Y * ppuy);
                }
                else if (type == PathOperation.Cubic)
                {
                    var controlPoint1 = path[pointIndex++];
                    var controlPoint2 = path[pointIndex++];
                    var endPoint      = path[pointIndex++];
                    sink.CubicTo(
                        ox + controlPoint1.X * ppux,
                        oy + controlPoint1.Y * ppuy,
                        ox + controlPoint2.X * ppux,
                        oy + controlPoint2.Y * ppuy,
                        ox + endPoint.X * ppux,
                        oy + endPoint.Y * ppuy);
                }
                else if (type == PathOperation.Arc)
                {
                    var topLeft     = path[pointIndex++];
                    var bottomRight = path[pointIndex++];
                    var startAngle  = path.GetArcAngle(arcAngleIndex++);
                    var endAngle    = path.GetArcAngle(arcAngleIndex++);
                    var clockwise   = path.GetArcClockwise(arcClockwiseIndex++);

                    while (startAngle < 0)
                    {
                        startAngle += 360;
                    }

                    while (endAngle < 0)
                    {
                        endAngle += 360;
                    }

                    var rotation    = Geometry.GetSweep(startAngle, endAngle, clockwise);
                    var absRotation = Math.Abs(rotation);

                    var rectX      = ox + topLeft.X * ppux;
                    var rectY      = oy + topLeft.Y * ppuy;
                    var rectWidth  = (ox + bottomRight.X * ppux) - rectX;
                    var rectHeight = (oy + bottomRight.Y * ppuy) - rectY;

                    var startPoint = Geometry.EllipseAngleToPoint(rectX, rectY, rectWidth, rectHeight, -startAngle);
                    var endPoint   = Geometry.EllipseAngleToPoint(rectX, rectY, rectWidth, rectHeight, -endAngle);


                    if (!figureOpen)
                    {
                        /*var vBegin = FigureBegin.Hollow;
                         * if (path.IsSubPathClosed(vSegmentIndex))
                         * {
                         *  vBegin = FigureBegin.Filled;
                         * }*/
                        var begin = FigureBegin.Filled;
                        sink.BeginFigure(startPoint.X, startPoint.Y, begin);
                        figureOpen = true;
                    }
                    else
                    {
                        sink.LineTo(startPoint.X, startPoint.Y);
                    }

                    var arcSegment = new ArcSegment
                    {
                        Point          = new Vector2(endPoint.X, endPoint.Y),
                        Size           = new Size2F(rectWidth / 2, rectHeight / 2),
                        SweepDirection = clockwise ? SweepDirection.Clockwise : SweepDirection.CounterClockwise,
                        ArcSize        = absRotation >= 180 ? ArcSize.Large : ArcSize.Small
                    };
                    sink.AddArc(arcSegment);
                }
                else if (type == PathOperation.Close)
                {
                    sink.EndFigure(FigureEnd.Closed);
                }

                lastOperation = type;
            }

            if (segmentIndex >= 0 && lastOperation != PathOperation.Close)
            {
                sink.EndFigure(FigureEnd.Open);
            }

            sink.Close();

            return(geometry);

#if DEBUG
        }

        catch (Exception exc)
        {
            geometry.Dispose();

            var definition = path.ToDefinitionString();
            Logger.Debug($"Unable to convert the path to a DXPath: {definition}", exc);
            return(null);
        }
#endif
        }
Exemplo n.º 4
0
        public static CanvasGeometry AsPath(this PathF path, float ox, float oy, float fx, float fy, ICanvasResourceCreator creator, CanvasFilledRegionDetermination fillMode = CanvasFilledRegionDetermination.Winding)
        {
            var builder = new CanvasPathBuilder(creator);

#if DEBUG
            try
            {
#endif
            builder.SetFilledRegionDetermination(fillMode);

            var pointIndex        = 0;
            var arcAngleIndex     = 0;
            var arcClockwiseIndex = 0;
            var figureOpen        = false;
            var segmentIndex      = -1;

            var lastOperation = PathOperation.Move;

            foreach (var type in path.SegmentTypes)
            {
                segmentIndex++;

                if (type == PathOperation.Move)
                {
                    if (lastOperation != PathOperation.Close && lastOperation != PathOperation.Move)
                    {
                        builder.EndFigure(CanvasFigureLoop.Open);
                    }

                    var point = path[pointIndex++];
                    var begin = CanvasFigureFill.Default;
                    builder.BeginFigure(ox + point.X * fx, oy + point.Y * fy, begin);
                    figureOpen = true;
                }
                else if (type == PathOperation.Line)
                {
                    var point = path[pointIndex++];
                    builder.AddLine(ox + point.X * fx, oy + point.Y * fy);
                }

                else if (type == PathOperation.Quad)
                {
                    var controlPoint = path[pointIndex++];
                    var endPoint     = path[pointIndex++];

                    builder.AddQuadraticBezier(
                        new Vector2(ox + controlPoint.X * fx, oy + controlPoint.Y * fy),
                        new Vector2(ox + endPoint.X * fx, oy + endPoint.Y * fy));
                }
                else if (type == PathOperation.Cubic)
                {
                    var controlPoint1 = path[pointIndex++];
                    var controlPoint2 = path[pointIndex++];
                    var endPoint      = path[pointIndex++];
                    builder.AddCubicBezier(
                        new Vector2(ox + controlPoint1.X * fx, oy + controlPoint1.Y * fy),
                        new Vector2(ox + controlPoint2.X * fx, oy + controlPoint2.Y * fy),
                        new Vector2(ox + endPoint.X * fx, oy + endPoint.Y * fy));
                }
                else if (type == PathOperation.Arc)
                {
                    var topLeft     = path[pointIndex++];
                    var bottomRight = path[pointIndex++];
                    var startAngle  = path.GetArcAngle(arcAngleIndex++);
                    var endAngle    = path.GetArcAngle(arcAngleIndex++);
                    var clockwise   = path.GetArcClockwise(arcClockwiseIndex++);

                    while (startAngle < 0)
                    {
                        startAngle += 360;
                    }

                    while (endAngle < 0)
                    {
                        endAngle += 360;
                    }

                    var rotation    = Geometry.GetSweep(startAngle, endAngle, clockwise);
                    var absRotation = Math.Abs(rotation);

                    var rectX      = ox + topLeft.X * fx;
                    var rectY      = oy + topLeft.Y * fy;
                    var rectWidth  = (ox + bottomRight.X * fx) - rectX;
                    var rectHeight = (oy + bottomRight.Y * fy) - rectY;

                    var startPoint = Geometry.EllipseAngleToPoint(rectX, rectY, rectWidth, rectHeight, -startAngle);
                    var endPoint   = Geometry.EllipseAngleToPoint(rectX, rectY, rectWidth, rectHeight, -endAngle);


                    if (!figureOpen)
                    {
                        var begin = CanvasFigureFill.Default;
                        builder.BeginFigure(startPoint.X, startPoint.Y, begin);
                        figureOpen = true;
                    }
                    else
                    {
                        builder.AddLine(startPoint.X, startPoint.Y);
                    }

                    builder.AddArc(
                        new Vector2(endPoint.X, endPoint.Y),
                        rectWidth / 2,
                        rectHeight / 2,
                        0,
                        clockwise ? CanvasSweepDirection.Clockwise : CanvasSweepDirection.CounterClockwise,
                        absRotation >= 180 ? CanvasArcSize.Large : CanvasArcSize.Small
                        );
                }
                else if (type == PathOperation.Close)
                {
                    builder.EndFigure(CanvasFigureLoop.Closed);
                }

                lastOperation = type;
            }

            if (segmentIndex >= 0 && lastOperation != PathOperation.Close)
            {
                builder.EndFigure(CanvasFigureLoop.Open);
            }

            var geometry = CanvasGeometry.CreatePath(builder);
            return(geometry);

#if DEBUG
        }

        catch (Exception exc)
        {
            builder.Dispose();

            var definition = path.ToDefinitionString();
            Logger.Debug(string.Format("Unable to convert the path to a Win2D Path: {0}", definition), exc);
            return(null);
        }
#endif
        }