Exemple #1
0
        public void CanDrawRoundRectDifference()
        {
            using var outer = new SKRoundRect(SKRect.Create(50, 50, 200, 200), 20);
            using var inner = new SKRoundRect(SKRect.Create(100, 100, 100, 100), 20);

            using var paint = new SKPaint();

            SKColor[] diff;
            using (var bmp = new SKBitmap(new SKImageInfo(300, 300)))
                using (var canvas = new SKCanvas(bmp))
                {
                    canvas.Clear(SKColors.Transparent);
                    canvas.DrawRoundRectDifference(outer, inner, paint);

                    diff = bmp.Pixels;
                }

            SKColor[] paths;
            using (var bmp = new SKBitmap(new SKImageInfo(300, 300)))
                using (var canvas = new SKCanvas(bmp))
                    using (var path = new SKPath())
                    {
                        canvas.Clear(SKColors.Transparent);

                        path.AddRoundRect(outer);
                        path.AddRoundRect(inner);
                        path.FillType = SKPathFillType.EvenOdd;

                        canvas.DrawPath(path, paint);

                        paths = bmp.Pixels;
                    }

            Assert.Equal(paths, diff);
        }
Exemple #2
0
        void UpdateShape()
        {
            var path = new SKPath();

            path.AddRoundRect(new SKRect(0, 0, 1, 1), RadiusX, RadiusY, SKPathDirection.Clockwise);
            UpdateShape(path);
        }
        public void ParsePathReturnsValidPath()
        {
            // based on ParsePath

            foreach (var test in parsePathTestCases)
            {
                var path = SKPath.ParseSvgPathData(test.Item1);

                Assert.NotNull(path);
                Assert.Equal(test.Item2, path.Bounds);

                TestToFromSvgPath(path);
            }

            var r = SKRect.Create(0, 0, 10, 10.5f);

            using (var p = new SKPath()) {
                p.AddRect(r);
                TestToFromSvgPath(p);

                p.AddOval(r);
                TestToFromSvgPath(p);

                p.AddRoundRect(r, 4, 4.5f);
                TestToFromSvgPath(p);
            }
        }
Exemple #4
0
        private SKPath NonPathShapeToSkiaPath(IShape shape)
        {
            SKPath skPath = new SKPath();

            if (shape is IRectangle rectangle)
            {
                SKRect skRect = SKRect.Create((float)rectangle.Left, (float)rectangle.Top, (float)rectangle.Width, (float)rectangle.Height);
                if (rectangle.RadiusX > 0 || rectangle.RadiusY > 0)
                {
                    skPath.AddRoundRect(skRect, (float)rectangle.RadiusX, (float)rectangle.RadiusY);
                }
                else
                {
                    skPath.AddRect(skRect);
                }
            }
            else if (shape is ILine line)
            {
                skPath.MoveTo((float)line.X1, (float)line.Y1);
                skPath.LineTo((float)line.X2, (float)line.Y2);
            }
            else if (shape is IEllipse ellipse)
            {
                SKRect skRect = SKRect.Create((float)ellipse.Left, (float)ellipse.Top, (float)ellipse.Width, (float)ellipse.Height);
                skPath.AddOval(skRect);
            }
            return(skPath);
        }
Exemple #5
0
        public static SKPath ToPath(this SKRoundRect bounds)
        {
            var path = new SKPath();

            path.AddRoundRect(bounds);
            path.Close();
            return(path);
        }
Exemple #6
0
        public static SKPath CreateRoundedRectPath(SKRoundRect skroundRect)
        {
            var skPath = new SKPath();

            skPath.AddRoundRect(skroundRect, SKPathDirection.Clockwise);
            skPath.Close();

            return(skPath);
        }
        public void PathContainsPointInRoundRect()
        {
            using (var path = new SKPath()) {
                var rrect = new SKRoundRect(SKRect.Create(10, 10, 100, 100), 5, 5);
                path.AddRoundRect(rrect);

                Assert.True(path.Contains(30, 30));
                Assert.False(path.Contains(5, 30));
            }
        }
        public void RoundRectPathIsRoundRect()
        {
            using (var path = new SKPath())
            {
                var rrect = new SKRoundRect(SKRect.Create(10, 10, 100, 100), 5, 5);
                path.AddRoundRect(rrect);

                Assert.False(path.IsOval);
                Assert.False(path.IsLine);
                Assert.False(path.IsRect);
                Assert.True(path.IsRoundRect);
                Assert.Equal(rrect.Rect, path.GetRoundRect().Rect);
                Assert.Equal(rrect.Radii, path.GetRoundRect().Radii);
            }
        }
Exemple #9
0
        public void DrawRectangle(IRectangle rectangle)
        {
            SKPath skPath = new SKPath();
            SKRect skRect = SKRect.Create(0, 0, (float)rectangle.Width, (float)rectangle.Height);

            if (rectangle.RadiusX > 0 || rectangle.RadiusY > 0)
            {
                skPath.AddRoundRect(skRect, (float)rectangle.RadiusX, (float)rectangle.RadiusY);
            }
            else
            {
                skPath.AddRect(skRect);
            }

            DrawShapePath(skPath, rectangle);
        }
Exemple #10
0
        public override void Awake(NSObject context)
        {
            base.Awake(context);

            var scale = WKInterfaceDevice.CurrentDevice.ScreenScale;

            var bitmap = new SKBitmap((int)(ContentFrame.Width * scale), (int)(ContentFrame.Height * scale));

            var colors = new[] { SKColors.Cyan, SKColors.Magenta, SKColors.Yellow, SKColors.Cyan };
            var center = new SKPoint(bitmap.Width / 2, bitmap.Height / 2);

            using (var canvas = new SKCanvas(bitmap))
            {
                canvas.Clear(SKColors.Transparent);

                using (var path = new SKPath())
                {
                    path.AddRoundRect(new SKRect(5, 5, bitmap.Width - 5, bitmap.Height - 5), 5, 5);
                    canvas.ClipPath(path);
                }

                using (var paint = new SKPaint())
                    using (var gradient = SKShader.CreateSweepGradient(center, colors, null))
                    {
                        paint.IsAntialias = true;
                        paint.Shader      = gradient;
                        canvas.DrawPaint(paint);
                        paint.Shader = null;
                    }

                using (var paint = new SKPaint())
                    using (var tf = SKTypeface.FromFamilyName("San Fransisco"))
                    {
                        paint.IsAntialias = true;
                        paint.Color       = SKColors.DarkBlue;
                        paint.TextSize    = (float)(20 * scale);
                        paint.TextAlign   = SKTextAlign.Center;
                        paint.Typeface    = tf;

                        canvas.DrawText("SkiaSharp", center.X, (center.Y / 2) + (paint.TextSize / 2), paint);
                    }
            }

            var image = bitmap.ToUIImage(scale, UIKit.UIImageOrientation.Up);

            imageView.SetImage(image);
        }
        static public SKPath CreateRoundedRectPath(int left, int top, int width, int height, CornerRadius cornerRadius)
        {
            var path        = new SKPath();
            var skRoundRect = new SKRoundRect(new SKRect(left, top, width, height));

            SKPoint[] radii = new SKPoint[4]
            {
                new SKPoint((float)cornerRadius.TopLeft, (float)cornerRadius.TopLeft),
                new SKPoint((float)cornerRadius.TopRight, (float)cornerRadius.TopRight),
                new SKPoint((float)cornerRadius.BottomRight, (float)cornerRadius.BottomRight),
                new SKPoint((float)cornerRadius.BottomLeft, (float)cornerRadius.BottomLeft)
            };
            skRoundRect.SetRectRadii(skRoundRect.Rect, radii);
            path.AddRoundRect(skRoundRect);
            path.Close();
            return(path);
        }
Exemple #12
0
        public static SKPath ToRoundedRectPath(this SKRectI bounds, CornerRadius cornerRadius)
        {
            var path        = new SKPath();
            var skRoundRect = new SKRoundRect(bounds);

            SKPoint[] radii = new SKPoint[4]
            {
                new SKPoint((float)cornerRadius.TopLeft, (float)cornerRadius.TopLeft),
                new SKPoint((float)cornerRadius.TopRight, (float)cornerRadius.TopRight),
                new SKPoint((float)cornerRadius.BottomRight, (float)cornerRadius.BottomRight),
                new SKPoint((float)cornerRadius.BottomLeft, (float)cornerRadius.BottomLeft)
            };
            skRoundRect.SetRectRadii(skRoundRect.Rect, radii);
            path.AddRoundRect(skRoundRect);
            path.Close();
            return(path);
        }
Exemple #13
0
        private void DrawInnerBlurRectangle(SKCanvas canvas, SKRect rect)
        {
            // create the rounded rectangle
            var roundedRect = new SKPath();

            roundedRect.AddRoundRect(rect, 10, 10);

            // draw the white background
            var p = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                Color       = SKColors.White
            };

            canvas.DrawPath(roundedRect, p);

            using (new SKAutoCanvasRestore(canvas))
            {
                // clip the canvas to stop the blur from appearing outside
                canvas.ClipPath(roundedRect, SKClipOperation.Intersect, true);

                // draw the wide blur all around
                p.Color       = SKColors.Black;
                p.Style       = SKPaintStyle.Stroke;
                p.StrokeWidth = 2;
                p.MaskFilter  = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 2);
                canvas.Translate(0.5f, 1.5f);
                canvas.DrawPath(roundedRect, p);

                // draw the narrow blur at the top
                p.StrokeWidth = 1;
                p.MaskFilter  = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 1);
                canvas.DrawPath(roundedRect, p);
            }

            // draw the border
            p.StrokeWidth = 2;
            p.MaskFilter  = null;
            p.Color       = SampleMedia.Colors.XamarinGreen;
            canvas.DrawPath(roundedRect, p);
        }
        private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            (SKBitmap, SKRect) GetCurrentBitmap()
            {
                if (_previewBitmap != null)
                {
                    return(_previewBitmap.Bitmap, _previewBitmap.SkRect);
                }
                if (_contentBitmap != null)
                {
                    return(_contentBitmap, _contentBitmap.Info.Rect);
                }
                return(null, SKRect.Empty);
            }

            var info    = e.Info;
            var surface = e.Surface;
            var canvas  = surface.Canvas;

            canvas.Clear();

            var(bitmap, srcRect) = GetCurrentBitmap();
            if (bitmap == null)
            {
                return;
            }

            var borderColor = _isFocused ? FocusedColor : UnfocusedColor;

            using (var path = new SKPath())
                using (var roundRect = new SKRoundRect(info.Rect, 30, 30))
                    using (var paint = new SKPaint
                    {
                        Color = borderColor, IsAntialias = true, Style = SKPaintStyle.Stroke, StrokeWidth = 3
                    })
                    {
                        path.AddRoundRect(roundRect);
                        canvas.ClipPath(path, antialias: true);
                        canvas.DrawBitmap(bitmap, srcRect, info.Rect);
                        canvas.DrawRoundRect(roundRect, paint);
                    }
        }
Exemple #15
0
        public virtual void DrawProgress(SKCanvas canvas, float progress, float hSpace, RectangleF rectangle)
        {
            using var paint = new SKPaint();

            var fillColor     = TypedVirtualView.GetProgressColor(defaultProgressColor, CurrentState).ToSKColor();
            var width         = (rectangle.Width - (hSpace * 2)) * progress;
            var height        = defaultHeight;
            var y             = rectangle.Y + ((rectangle.Height - height) / 2);
            var highlightRect = new RectangleF(hSpace, y, width, height).ToSKRect();

            var trackRect = highlightRect;

            using var trackPath = new SKPath();
            trackPath.Reset();
            trackPath.AddRoundRect(trackRect, 1f, 1f, SKPathDirection.Clockwise);
            paint.Reset();
            paint.IsAntialias = true;
            paint.Style       = SKPaintStyle.Fill;
            paint.Color       = fillColor;
            canvas.DrawPath(trackPath, paint);
        }
Exemple #16
0
        public virtual void DrawTrack(SKCanvas canvas, float hSpace, RectangleF rectangle)
        {
            using var paint = new SKPaint();
            var fillColor = TypedVirtualView.GetTrackColor(defaultTrackColor, CurrentState).ToSKColor();

            TrackRect.Width  = rectangle.Width - (hSpace * 2);
            TrackRect.Height = defaultHeight;
            TrackRect.Y      = rectangle.Y + ((rectangle.Height - TrackRect.Height) / 2);
            TrackRect.X      = hSpace;
            var highlightRect = TrackRect.ToSKRect();

            //var highlightRect = rectangle.ApplyPadding(new Thickness(hSpace, vSpace)).ToSKRect();
            using var highlightPath = new SKPath();
            highlightPath.Reset();
            highlightPath.AddRoundRect(highlightRect, 1f, 1f, SKPathDirection.Clockwise);

            paint.Reset();
            paint.IsAntialias = true;
            paint.Style       = SKPaintStyle.Fill;
            paint.Color       = fillColor;
            canvas.DrawPath(highlightPath, paint);
        }
        private SKPath NonPathShapeToSkiaPath(IShape shape)
        {
            SKPath skPath = new SKPath();

            if (shape is IRectangle rectangle)
            {
                SKRect skRect = SKRect.Create((float)rectangle.Left, (float)rectangle.Top, (float)rectangle.Width, (float)rectangle.Height);
                if (rectangle.RadiusX > 0 || rectangle.RadiusY > 0)
                {
                    skPath.AddRoundRect(skRect, (float)rectangle.RadiusX, (float)rectangle.RadiusY);
                }
                else
                {
                    skPath.AddRect(skRect);
                }
            }
            else if (shape is ILine line)
            {
                skPath.MoveTo((float)line.X1, (float)line.Y1);
                skPath.LineTo((float)line.X2, (float)line.Y2);
            }
            else if (shape is IEllipse ellipse)
            {
                SKRect skRect = SKRect.Create((float)ellipse.Left, (float)ellipse.Top, (float)ellipse.Width, (float)ellipse.Height);
                skPath.AddOval(skRect);
            }
            else if (shape is IPolygon polygon)
            {
                skPath.FillType = FillRuleToSkiaPathFillType(polygon.FillRule);
                skPath.AddPoly(PointsToSkiaPoints(polygon.Points), close: true);
            }
            else if (shape is IPolyline polyline)
            {
                skPath.FillType = FillRuleToSkiaPathFillType(polyline.FillRule);
                skPath.AddPoly(PointsToSkiaPoints(polyline.Points), close: false);
            }
            return(skPath);
        }
Exemple #18
0
 public void DrawRounRect(SKRoundRect roundRect)
 {
     clip.AddRoundRect(roundRect);
 }
Exemple #19
0
        public override void AddRoundRect(float x, float y, float w, float h, float nwRadius, float neRadius, float seRadius, float swRadius)
        {
            var rect = new SKRect(x, y, w, h);

            _graphicsPath.AddRoundRect(rect, nwRadius, seRadius);
        }
Exemple #20
0
        private void Load(Stream stream)
        {
            var matrix = SKMatrix.CreateIdentity();
            Dictionary <int, SKMatrix> stack = new Dictionary <int, SKMatrix>();

            using (var reader = XmlReader.Create(stream))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (stack.TryGetValue(reader.Depth, out var stackMatrix))
                        {
                            stack.Remove(reader.Depth);
                            matrix = stackMatrix;
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (string.Equals(reader.Name, "svg", StringComparison.Ordinal))
                        {
                            var preserveAspectRatio = reader["preserveAspectRatio"];
                            // get the SVG dimensions
                            var viewBoxA = reader["viewBox"] ?? reader["viewPort"];
                            if (viewBoxA != null)
                            {
                                ViewBox = ReadRectangle(viewBoxA);
                            }

                            var widthA  = reader["width"];
                            var heightA = reader["height"];
                            var width   = ReadNumber(widthA);
                            var height  = ReadNumber(heightA);
                            var size    = new SKSize(width, height);

                            if (widthA == null)
                            {
                                size.Width = ViewBox.Width;
                            }
                            else if (widthA.IndexOf('%') > -1)
                            {
                                size.Width *= ViewBox.Width;
                            }
                            if (heightA == null)
                            {
                                size.Height = ViewBox.Height;
                            }
                            else if (heightA != null && heightA.IndexOf('%') > -1)
                            {
                                size.Height *= ViewBox.Height;
                            }
                            CanvasSize = size;

                            if (!ViewBox.IsEmpty && (ViewBox.Width != CanvasSize.Width || ViewBox.Height != CanvasSize.Height))
                            {
                                if (preserveAspectRatio == "none")
                                {
                                    matrix = matrix.PostConcat(SKMatrix.CreateScale(CanvasSize.Width / ViewBox.Width, CanvasSize.Height / ViewBox.Height));
                                }
                                else
                                {
                                    // TODO: just center scale for now
                                    var scale    = Math.Min(CanvasSize.Width / ViewBox.Width, CanvasSize.Height / ViewBox.Height);
                                    var centered = SKRect.Create(CanvasSize).AspectFit(ViewBox.Size);
                                    matrix = matrix.PostConcat(SKMatrix.CreateTranslation(centered.Left, centered.Top))
                                             .PostConcat(SKMatrix.CreateScale(scale, scale));
                                }
                            }

                            // translate the canvas by the viewBox origin
                            matrix = matrix.PostConcat(SKMatrix.CreateTranslation(-ViewBox.Left, -ViewBox.Top));
                        }
                        else
                        {
                            var transform = reader["transform"];
                            if (transform != null)
                            {
                                stack[reader.Depth] = matrix;
                                var trMatrix = ReadTransform(transform);
                                matrix = matrix.PostConcat(trMatrix);
                            }

                            if (string.Equals(reader.Name, "path", StringComparison.Ordinal))
                            {
                                var pathData = reader["d"];
                                using (var path = SKPath.ParseSvgPathData(pathData))
                                    Path.AddPath(path, ref matrix);
                            }
                            else if (string.Equals(reader.Name, "polyline", StringComparison.Ordinal))
                            {
                                var pathData = "M" + reader["points"];
                                using (var path = SKPath.ParseSvgPathData(pathData))
                                    Path.AddPath(path, ref matrix);
                            }
                            else if (string.Equals(reader.Name, "polygon", StringComparison.Ordinal))
                            {
                                var pathData = "M" + reader["points"] + " Z";
                                using (var path = SKPath.ParseSvgPathData(pathData))
                                    Path.AddPath(path, ref matrix);
                            }
                            else if (string.Equals(reader.Name, "line", StringComparison.Ordinal))
                            {
                                var x1 = ReadNumber(reader["x1"]);
                                var x2 = ReadNumber(reader["x2"]);
                                var y1 = ReadNumber(reader["y1"]);
                                var y2 = ReadNumber(reader["y2"]);
                                using (var path = new SKPath())
                                {
                                    path.MoveTo(x1, y1);
                                    path.LineTo(x2, y2);
                                    Path.AddPath(path, ref matrix);
                                }
                            }
                            else if (string.Equals(reader.Name, "circle", StringComparison.Ordinal))
                            {
                                var cx = ReadNumber(reader["cx"]);
                                var cy = ReadNumber(reader["cy"]);
                                var rr = ReadNumber(reader["r"]);
                                using (var path = new SKPath())
                                {
                                    path.AddCircle(cx, cy, rr);
                                    Path.AddPath(path, ref matrix);
                                }
                            }
                            else if (string.Equals(reader.Name, "ellipse", StringComparison.Ordinal))
                            {
                                var cx = ReadNumber(reader["cx"]);
                                var cy = ReadNumber(reader["cy"]);
                                var rx = ReadNumber(reader["rx"]);
                                var ry = ReadNumber(reader["ry"]);
                                using (var path = new SKPath())
                                {
                                    path.AddOval(new SKRect(cx, cy, rx, ry));
                                    Path.AddPath(path, ref matrix);
                                }
                            }
                            else if (string.Equals(reader.Name, "rect", StringComparison.Ordinal))
                            {
                                var x      = ReadNumber(reader["x"]);
                                var y      = ReadNumber(reader["y"]);
                                var width  = ReadNumber(reader["width"]);
                                var height = ReadNumber(reader["height"]);
                                var rx     = ReadOptionalNumber(reader["rx"]);
                                var ry     = ReadOptionalNumber(reader["ry"]);
                                var rect   = SKRect.Create(x, y, width, height);
                                using (var path = new SKPath())
                                {
                                    if (rx != null)
                                    {
                                        path.AddRoundRect(rect, rx ?? 0, ry ?? 0);
                                    }
                                    else
                                    {
                                        path.AddRect(rect);
                                    }
                                    Path.AddPath(path, ref matrix);
                                }
                            }
                        }
                    }
                }
            }
        }