Exemplo n.º 1
0
        internal static CanvasGeometry GenerateGeometry(CanvasDevice device, Size size, CompositionPathInfo info, Vector2 offset)
        {
            //
            //   |--LeftTop----------------------RightTop--|
            //   |                                         |
            // TopLeft                                TopRight
            //   |                                         |
            //   |                                         |
            //   |                                         |
            //   |                                         |
            //   |                                         |
            //   |                                         |
            // BottomLeft                          BottomRight
            //   |                                         |
            //   |--LeftBottom----------------RightBottom--|
            //
            //  compute the coordinates of the key points
            var leftTop     = new Vector2(info.LeftTop.Single(), 0);
            var rightTop    = new Vector2((size.Width - info.RightTop).Single(), 0);
            var topRight    = new Vector2(size.Width.Single(), info.TopRight.Single());
            var bottomRight = new Vector2(size.Width.Single(), (size.Height - info.BottomRight).Single());
            var rightBottom = new Vector2((size.Width - info.RightBottom).Single(), size.Height.Single());
            var leftBottom  = new Vector2(info.LeftBottom.Single(), size.Height.Single());
            var bottomLeft  = new Vector2(0, (size.Height - info.BottomLeft).Single());
            var topLeft     = new Vector2(0, info.TopLeft.Single());

            //  check keypoints for overlap and resolve by partitioning corners according to
            //  the percentage of each one.

            //  top edge
            if (leftTop.X > rightTop.X)
            {
                var v = ((info.LeftTop) / (info.LeftTop + info.RightTop) * size.Width).Single();
                leftTop.X  = v;
                rightTop.X = v;
            }

            //  right edge
            if (topRight.Y > bottomRight.Y)
            {
                var v = ((info.TopRight) / (info.TopRight + info.BottomRight) * size.Height).Single();
                topRight.Y    = v;
                bottomRight.Y = v;
            }

            //  bottom edge
            if (leftBottom.X > rightBottom.X)
            {
                var v = ((info.LeftBottom) / (info.LeftBottom + info.RightBottom) * size.Width).Single();
                rightBottom.X = v;
                leftBottom.X  = v;
            }

            // left edge
            if (topLeft.Y > bottomLeft.Y)
            {
                var v = ((info.TopLeft) / (info.TopLeft + info.BottomLeft) * size.Height).Single();
                bottomLeft.Y = v;
                topLeft.Y    = v;
            }

            // Apply offset
            leftTop     += offset;
            rightTop    += offset;
            topRight    += offset;
            bottomRight += offset;
            rightBottom += offset;
            leftBottom  += offset;
            bottomLeft  += offset;
            topLeft     += offset;

            //  create the border geometry
            var pathBuilder = new CanvasPathBuilder(device);

            // Begin path
            pathBuilder.BeginFigure(leftTop);

            // Top line
            pathBuilder.AddLine(rightTop);

            // Upper-right corners
            var radiusX = size.Width - rightTop.X;
            var radiusY = (double)topRight.Y;

            if (!radiusX.IsZero() || !radiusY.IsZero())
            {
                pathBuilder.AddArc(topRight, radiusX.Single(), radiusY.Single(), (Math.PI / 2f).Single(),
                                   CanvasSweepDirection.Clockwise, CanvasArcSize.Small);
            }

            // Right line
            pathBuilder.AddLine(bottomRight);

            // Lower-right corners
            radiusX = size.Width - rightBottom.X;
            radiusY = size.Height - bottomRight.Y;
            if (!radiusX.IsZero() || !radiusY.IsZero())
            {
                pathBuilder.AddArc(rightBottom, radiusX.Single(), radiusY.Single(), (Math.PI / 2f).Single(),
                                   CanvasSweepDirection.Clockwise, CanvasArcSize.Small);
            }

            // Bottom line
            pathBuilder.AddLine(leftBottom);

            // Lower-left corners
            radiusX = leftBottom.X;
            radiusY = size.Height - bottomLeft.Y;
            if (!radiusX.IsZero() || !radiusY.IsZero())
            {
                pathBuilder.AddArc(bottomLeft, radiusX.Single(), radiusY.Single(), (Math.PI / 2f).Single(),
                                   CanvasSweepDirection.Clockwise, CanvasArcSize.Small);
            }

            // Left line
            pathBuilder.AddLine(topLeft);

            // Upper-left corners
            radiusX = leftTop.X;
            radiusY = topLeft.Y;
            if (!radiusX.IsZero() || !radiusY.IsZero())
            {
                pathBuilder.AddArc(leftTop, radiusX.Single(), radiusY.Single(), (Math.PI / 2f).Single(),
                                   CanvasSweepDirection.Clockwise, CanvasArcSize.Small);
            }

            // End path
            pathBuilder.EndFigure(CanvasFigureLoop.Closed);

            return(CanvasGeometry.CreatePath(pathBuilder));
        }
        internal static CanvasGeometry GenerateGeometry(CanvasDevice device, Size size, CompositionPathInfo info, Vector2 offset)
        {
            //
            //   |--LeftTop----------------------RightTop--|
            //   |                                         |
            // TopLeft                                TopRight
            //   |                                         |
            //   |                                         |
            //   |                                         |
            //   |                                         |
            //   |                                         |
            //   |                                         |
            // BottomLeft                          BottomRight
            //   |                                         |
            //   |--LeftBottom----------------RightBottom--|
            //
            //  compute the coordinates of the key points
            var leftTop = new Vector2(info.LeftTop.Single(), 0);
            var rightTop = new Vector2((size.Width - info.RightTop).Single(), 0);
            var topRight = new Vector2(size.Width.Single(), info.TopRight.Single());
            var bottomRight = new Vector2(size.Width.Single(), (size.Height - info.BottomRight).Single());
            var rightBottom = new Vector2((size.Width - info.RightBottom).Single(), size.Height.Single());
            var leftBottom = new Vector2(info.LeftBottom.Single(), size.Height.Single());
            var bottomLeft = new Vector2(0, (size.Height - info.BottomLeft).Single());
            var topLeft = new Vector2(0, info.TopLeft.Single());

            //  check keypoints for overlap and resolve by partitioning corners according to
            //  the percentage of each one.  

            //  top edge
            if (leftTop.X > rightTop.X)
            {
                var v = ((info.LeftTop) / (info.LeftTop + info.RightTop) * size.Width).Single();
                leftTop.X = v;
                rightTop.X = v;
            }

            //  right edge
            if (topRight.Y > bottomRight.Y)
            {
                var v = ((info.TopRight) / (info.TopRight + info.BottomRight) * size.Height).Single();
                topRight.Y = v;
                bottomRight.Y = v;
            }

            //  bottom edge
            if (leftBottom.X > rightBottom.X)
            {
                var v = ((info.LeftBottom) / (info.LeftBottom + info.RightBottom) * size.Width).Single();
                rightBottom.X = v;
                leftBottom.X = v;
            }

            // left edge
            if (topLeft.Y > bottomLeft.Y)
            {
                var v = ((info.TopLeft) / (info.TopLeft + info.BottomLeft) * size.Height).Single();
                bottomLeft.Y = v;
                topLeft.Y = v;
            }

            // Apply offset
            leftTop += offset;
            rightTop += offset;
            topRight += offset;
            bottomRight += offset;
            rightBottom += offset;
            leftBottom += offset;
            bottomLeft += offset;
            topLeft += offset;

            //  create the border geometry
            var pathBuilder = new CanvasPathBuilder(device);

            // Begin path
            pathBuilder.BeginFigure(leftTop);

            // Top line
            pathBuilder.AddLine(rightTop);

            // Upper-right corners
            var radiusX = size.Width - rightTop.X;
            var radiusY = (double)topRight.Y;
            if (!radiusX.IsZero() || !radiusY.IsZero())
            {
                pathBuilder.AddArc(topRight, radiusX.Single(), radiusY.Single(), (Math.PI / 2f).Single(),
                    CanvasSweepDirection.Clockwise, CanvasArcSize.Small);
            }

            // Right line
            pathBuilder.AddLine(bottomRight);

            // Lower-right corners
            radiusX = size.Width - rightBottom.X;
            radiusY = size.Height - bottomRight.Y;
            if (!radiusX.IsZero() || !radiusY.IsZero())
            {
                pathBuilder.AddArc(rightBottom, radiusX.Single(), radiusY.Single(), (Math.PI / 2f).Single(),
                    CanvasSweepDirection.Clockwise, CanvasArcSize.Small);
            }

            // Bottom line
            pathBuilder.AddLine(leftBottom);

            // Lower-left corners
            radiusX = leftBottom.X;
            radiusY = size.Height - bottomLeft.Y;
            if (!radiusX.IsZero() || !radiusY.IsZero())
            {
                pathBuilder.AddArc(bottomLeft, radiusX.Single(), radiusY.Single(), (Math.PI / 2f).Single(),
                    CanvasSweepDirection.Clockwise, CanvasArcSize.Small);
            }

            // Left line
            pathBuilder.AddLine(topLeft);

            // Upper-left corners
            radiusX = leftTop.X;
            radiusY = topLeft.Y;
            if (!radiusX.IsZero() || !radiusY.IsZero())
            {
                pathBuilder.AddArc(leftTop, radiusX.Single(), radiusY.Single(), (Math.PI / 2f).Single(),
                    CanvasSweepDirection.Clockwise, CanvasArcSize.Small);
            }

            // End path
            pathBuilder.EndFigure(CanvasFigureLoop.Closed);

            return CanvasGeometry.CreatePath(pathBuilder);
        }