Example #1
0
        private static void DrawLabel(SKCanvas target, float x, float y, LabelStyle style, string text)
        {
            var paint = CreatePaint(style);

            var rect = new SKRect();

            paint.MeasureText(text, ref rect);

            var horizontalAlign = CalcHorizontalAlignment(style.HorizontalAlignment);
            var verticalAlign   = CalcVerticalAlignment(style.VerticalAlignment);

            var backRectXOffset = -rect.Left;
            var backRectYOffset = rect.Bottom;

            rect.Offset(
                x - rect.Width * horizontalAlign + (float)style.Offset.X,
                y + rect.Height * verticalAlign + (float)style.Offset.Y);

            var backRect = rect;                             // copy

            rect.Offset(-backRectXOffset, -backRectYOffset); // correct for text specific offset returned paint.Measure

            backRect.Inflate(3, 3);
            DrawBackground(style, backRect, target);

            target.DrawText(text, rect.Left, rect.Bottom, paint);
        }
        /// <summary>
        /// Moves the cropping rectangle by a delta value in x and y direction, while keeping the
        /// rectangle inside the maximum rectangle and keeping the aspect ratio.
        /// </summary>
        /// <param name="delta">rectangle delta</param>
        public void MoveRect(SKPoint delta)
        {
            SKRect rect = this.Rect;

            rect.Offset(delta);

            float width  = rect.Width;
            float height = rect.Height;

            // limit moving the rectangle
            rect.Left = Math.Max(rect.Left, 0.0f);
            rect.Top  = Math.Max(rect.Top, 0.0f);

            if (this.aspectRatio.HasValue)
            {
                rect.Right  = rect.Left + width;
                rect.Bottom = rect.Top + height;
            }

            rect.Right  = Math.Min(rect.Right, this.maxRect.Right);
            rect.Bottom = Math.Min(rect.Bottom, this.maxRect.Bottom);

            if (this.aspectRatio.HasValue)
            {
                rect.Left = rect.Right - width;
                rect.Top  = rect.Bottom - height;
            }

            this.Rect = rect;
        }
Example #3
0
        public void Draw(SKCanvas canvas, IReadOnlyViewport viewport, IWidget widget, float layerOpacity)
        {
            var hyperlink = (Hyperlink)widget;

            if (string.IsNullOrEmpty(hyperlink.Text))
            {
                return;
            }
            using var textPaint = new SKPaint { Color = hyperlink.TextColor.ToSkia(layerOpacity), IsAntialias = true };
            using var backPaint = new SKPaint { Color = hyperlink.BackColor.ToSkia(layerOpacity) };
            // The textRect has an offset which can be confusing.
            // This is because DrawText's origin is the baseline of the text, not the bottom.
            // Read more here: https://developer.xamarin.com/guides/xamarin-forms/advanced/skiasharp/basics/text/
            var textRect = new SKRect();

            textPaint.MeasureText(hyperlink.Text, ref textRect);
            // The backRect is straight forward. It is leading for our purpose.
            var backRect = new SKRect(0, 0,
                                      textRect.Width + hyperlink.PaddingX * 2,
                                      textPaint.TextSize + hyperlink.PaddingY * 2); // Use the font's TextSize for consistency
            var offsetX = GetOffsetX(backRect.Width, hyperlink.MarginX, hyperlink.HorizontalAlignment, viewport.Width);
            var offsetY = GetOffsetY(backRect.Height, hyperlink.MarginY, hyperlink.VerticalAlignment, viewport.Height);

            backRect.Offset(offsetX, offsetY);
            canvas.DrawRoundRect(backRect, hyperlink.CornerRadius, hyperlink.CornerRadius, backPaint);
            hyperlink.Envelope = backRect.ToMRect();
            // To position the text within the backRect correct using the textRect's offset.
            canvas.DrawText(hyperlink.Text,
                            offsetX - textRect.Left + hyperlink.PaddingX,
                            offsetY - textRect.Top + hyperlink.PaddingY, textPaint);
        }
Example #4
0
        private static void DrawHyperlink(SKCanvas canvas, double screenWidth, double screenHeight, Hyperlink textBox)
        {
            if (textBox.Text == null)
            {
                return;                       // todo: fix this for widgets without text
            }
            var textPaint = new SKPaint {
                Color = textBox.TextColor.ToSkia(), IsAntialias = true
            };
            var backPaint = new SKPaint {
                Color = textBox.BackColor.ToSkia(),
            };
            // The textRect has an offset which can be confusing.
            // This is because DrawText's origin is the baseline of the text, not the bottom.
            // Read more here: https://developer.xamarin.com/guides/xamarin-forms/advanced/skiasharp/basics/text/
            var textRect = new SKRect();

            textPaint.MeasureText(textBox.Text, ref textRect);
            // The backRect is straight forward. It is leading for our purpose.
            var backRect = new SKRect(0, 0,
                                      textRect.Width + textBox.PaddingX * 2,
                                      textPaint.TextSize + textBox.PaddingY * 2); // Use the font's TextSize for consistency
            var offsetX = GetOffsetX(backRect.Width, textBox.MarginX, textBox.HorizontalAlignment, screenWidth);
            var offsetY = GetOffsetY(backRect.Height, textBox.MarginY, textBox.VerticalAlignment, screenHeight);

            backRect.Offset(offsetX, offsetY);
            canvas.DrawRoundRect(backRect, textBox.CornerRadius, textBox.CornerRadius, backPaint);
            textBox.Envelope = backRect.ToMapsui();
            // To position the text within the backRect correct using the textRect's offset.
            canvas.DrawText(textBox.Text,
                            offsetX - textRect.Left + textBox.PaddingX,
                            offsetY - textRect.Top + textBox.PaddingY, textPaint);
        }
Example #5
0
        public static void DrawBitmapFont(this SKCanvas canvas, BitmapFont bmFont, string str, float x, float y, SKPaint paint = null)
        {
            if (bmFont == null || bmFont.Bitmap == null)
            {
                return;
            }

            var dic = bmFont.Chars;

            str.ForEach(ch =>
            {
                int id = (int)ch;
                Abstractions.Char c;

                if (!dic.TryGetValue(id, out c))
                {
                    return;
                }

                SKRect src = new SKRect(0, 0, c.Width - 1, c.Height - 1);
                src.Offset(c.X, c.Y);

                SKRect dst = new SKRect(0, 0, c.Width - 1, c.Height - 1);
                dst.Offset(x + c.XOffset, y + c.YOffset);

                canvas.DrawBitmap(bmFont.Bitmap, src, dst);

                x += c.XAdvance;
            });
        }
Example #6
0
        private void DrawPencils(SKCanvas canvas, SKPaint paint)
        {
            // draw pencils, assigning the fields used for touch detection
            _numPencils = SwatchColor.ColorsByName.Count;
            if (_currentColorIndex == -1)
            {
                InitCurrentColor();
            }

            var marginAlloc = (_numPencils + 1) * PENCIL_MARGIN;

            _pencilWidth = (canvas.ClipBounds.Width - marginAlloc) / _numPencils; // see opposite calc in TouchInControlArea
            var pencilHeight  = _pencilWidth * 334.0f / 112.0f;                   // scale as per originals
            var runningLeft   = PENCIL_MARGIN;
            var pencilsBottom = canvas.ClipBounds.Height;

            _pencilsTop = pencilsBottom - pencilHeight;
            int _pencilIndex = 0;

            foreach (var swatchBM in _pencilBitmaps)
            {
                var pencilRect = new SKRect(runningLeft, _pencilsTop, runningLeft + _pencilWidth, pencilsBottom);
                if (_pencilIndex++ == _currentColorIndex)
                {
                    var offsetY = -Math.Max(20.0f, pencilHeight / 4.0f);
                    pencilRect.Offset(0.0f, offsetY);  // show selected color
                }

                canvas.DrawBitmap(swatchBM, pencilRect, paint);
                runningLeft += PENCIL_MARGIN + _pencilWidth;
            }
        }
Example #7
0
        private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();
            // trova rettangolo per adattarlo alla bitmap
            float  scale = Math.Min((float)info.Width / bitmap1.Width, (float)info.Height / bitmap1.Height);
            SKRect rect  = SKRect.Create(scale * bitmap1.Width, scale * bitmap1.Height);
            float  x     = (info.Width - rect.Width) / 2;
            float  y     = (info.Height - rect.Height) / 2;

            rect.Offset(x, y);
            // ottenere il valore di avanzamento da Slider
            float progress = (float)progressSlider.Value;

            // visualizza due bitmap con trasparenza
            using (SKPaint paint = new SKPaint())
            {
                paint.Color = paint.Color.WithAlpha((byte)(0xFF * (1 - progress)));
                canvas.DrawBitmap(bitmap1, rect, paint);
                paint.Color = paint.Color.WithAlpha((byte)(0xFF * progress));
                canvas.DrawBitmap(bitmap2, rect, paint);
            }
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            if (_bytes == null)
            {
                return;
            }

            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            // Find rectangle to fit bitmap
            float scale = Math.Min((float)info.Width / ImageBitmap.Width,
                                   (float)info.Height / ImageBitmap.Height);
            SKRect rect = SKRect.Create(scale * ImageBitmap.Width,
                                        scale * ImageBitmap.Height);
            float x = (info.Width - rect.Width) / 2;
            float y = (info.Height - rect.Height) / 2;

            rect.Offset(x, y);

            // Get progress value from Slider
            float progress = (float)progressSlider.Value;

            // Display one bitmaps with transparency
            using (SKPaint paint = new SKPaint())
            {
                paint.Color = paint.Color.WithAlpha((byte)(0xFF * (1 - progress)));
                canvas.DrawBitmap(ImageBitmap, rect, paint);
            }
        }
        private SKRect GetAnotationScreenRect(FishModel fish)
        {
            // get marker location
            var markerScreenPos = GetMarkerOnScreenPos(fish);

            // get annotation
            SKBitmap annotation = null;

            var annotationType = GetAnnotationDisplayType(fish);

            switch (annotationType)
            {
            case AnnotationDisplayType.Center:
                annotation = fishAnnotations[fish].OnscreenAnnotation;
                SKRect rect = new SKRect(0, 0, annotation.Width, annotation.Height);
                rect.Location = markerScreenPos;
                rect.Offset(-(annotation.Width / 2), -(annotation.Height / 2 + lineLength));
                return(rect);

            case AnnotationDisplayType.Left:
                annotation = fishAnnotations[fish].LeftAnnotation;
                SKRect left = new SKRect(0, 0, annotation.Width, annotation.Height);
                left.Offset(0, markerScreenPos.Y - (annotation.Height / 2 + lineLength));
                return(left);

            case AnnotationDisplayType.Right:
                annotation = fishAnnotations[fish].RightAnnotation;
                SKRect right = new SKRect(0, 0, annotation.Width, annotation.Height);
                right.Offset(ScreenWindowRect.Width - annotation.Width, markerScreenPos.Y - (annotation.Height / 2 + lineLength));
                return(right);
            }
            return(SKRect.Empty);
        }
Example #10
0
        private static void DrawLabel(SKCanvas target, float x, float y, LabelStyle style, string text, float layerOpacity)
        {
            var paint = CreatePaint(style, layerOpacity);

            var rect = new SKRect();

            paint.MeasureText(text, ref rect);

            var horizontalAlign = CalcHorizontalAlignment(style.HorizontalAlignment);
            var verticalAlign   = CalcVerticalAlignment(style.VerticalAlignment);

            var backRectXOffset = rect.Left;
            var backRectYOffset = rect.Bottom;
            var offsetX         = style.Offset.IsRelative ? rect.Width * style.Offset.X : style.Offset.X;
            var offsetY         = style.Offset.IsRelative ? rect.Height * style.Offset.Y : style.Offset.Y;

            rect.Offset(
                x - rect.Width * horizontalAlign + (float)offsetX,
                y + rect.Height * verticalAlign + (float)offsetY);

            var backRect = rect;                             // copy

            rect.Offset(-backRectXOffset, -backRectYOffset); // correct for text specific offset returned paint.Measure

            backRect.Inflate(3, 3);
            DrawBackground(style, backRect, target, layerOpacity);

            if (style.Halo != null)
            {
                var haloPaint = CreatePaint(style, layerOpacity);
                haloPaint.Style = SKPaintStyle.StrokeAndFill;
                haloPaint.Color = style.Halo.Color.ToSkia(layerOpacity);

                // TODO: PenStyle

                /*
                 * float[] intervals = { 10.0f, 5.0f, 2.0f, 5.0f };
                 * haloPaint.SetPathEffect(SkDashPathEffect::Make(intervals, count, 0.0f));
                 */

                haloPaint.StrokeWidth = (float)style.Halo.Width * 2;

                target.DrawText(text, rect.Left, rect.Bottom, haloPaint);
            }

            target.DrawText(text, rect.Left, rect.Bottom, paint);
        }
Example #11
0
        public void CreateThumbnailsNetStandard2()
        {
            //ExStart
            //ExFor:Document.RenderToScale
            //ExSummary:Renders individual pages to graphics to create one image with thumbnails of all pages (.NetStandard 2.0).
            Document doc = new Document(MyDir + "Rendering.docx");

            // Calculate the number of rows and columns that we will fill with thumbnails.
            const int thumbnailColumnsNum = 2;
            int       thumbRows           = Math.DivRem(doc.PageCount, thumbnailColumnsNum, out int remainder);

            if (remainder > 0)
            {
                thumbRows++;
            }

            // Scale the thumbnails relative to the size of the first page.
            const float scale     = 0.25f;
            Size        thumbSize = doc.GetPageInfo(0).GetSizeInPixels(scale, 96);

            // Calculate the size of the image that will contain all the thumbnails.
            int imgWidth  = thumbSize.Width * thumbnailColumnsNum;
            int imgHeight = thumbSize.Height * thumbRows;

            using (SKBitmap bitmap = new SKBitmap(imgWidth, imgHeight))
            {
                using (SKCanvas canvas = new SKCanvas(bitmap))
                {
                    // Fill the background, which is transparent by default, in white.
                    canvas.Clear(SKColors.White);

                    for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)
                    {
                        int rowIdx = Math.DivRem(pageIndex, thumbnailColumnsNum, out int columnIdx);

                        // Specify where we want the thumbnail to appear.
                        float thumbLeft = columnIdx * thumbSize.Width;
                        float thumbTop  = rowIdx * thumbSize.Height;

                        SizeF size = doc.RenderToScale(pageIndex, canvas, thumbLeft, thumbTop, scale);

                        // Render a page as a thumbnail, and then frame it in a rectangle of the same size.
                        SKRect rect = new SKRect(0, 0, size.Width, size.Height);
                        rect.Offset(thumbLeft, thumbTop);
                        canvas.DrawRect(rect, new SKPaint
                        {
                            Color = SKColors.Black,
                            Style = SKPaintStyle.Stroke
                        });
                    }

                    using (SKFileWStream fs = new SKFileWStream(ArtifactsDir + "Rendering.CreateThumbnailsNetStandard2.png"))
                    {
                        bitmap.PeekPixels().Encode(fs, SKEncodedImageFormat.Png, 100);
                    }
                }
            }
            //ExEnd
        }
Example #12
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            string str = "Hello SkiaSharp!";

            // Create an SKPaint object to display the text
            SKPaint textPaint = new SKPaint
            {
                Color = SKColors.Chocolate
            };

            // Adjust TextSize property so text is 90% of screen width
            float textWidth = textPaint.MeasureText(str);

            textPaint.TextSize = 0.9f * info.Width * textPaint.TextSize / textWidth;

            // Find the text bounds
            SKRect textBounds;

            textPaint.MeasureText(str, ref textBounds);

            // Calculate offsets to center the text on the screen
            float xText = info.Width / 2 - textBounds.MidX;
            float yText = info.Height / 2 - textBounds.MidY;

            // And draw the text
            canvas.DrawText(str, xText, yText, textPaint);

            // Create a new SKRect object for the frame around the text
            SKRect frameRect = textBounds;

            frameRect.Offset(xText, yText);
            frameRect.Inflate(10, 10);

            // Create an SKPaint object to display the frame
            SKPaint framePaint = new SKPaint
            {
                Style       = SKPaintStyle.Stroke,
                StrokeWidth = 5,
                Color       = SKColors.Blue
            };

            // Draw one frame
            canvas.DrawRoundRect(frameRect, 20, 20, framePaint);

            // Inflate the frameRect and draw another
            frameRect.Inflate(10, 10);
            framePaint.Color = SKColors.DarkBlue;
            canvas.DrawRoundRect(frameRect, 30, 30, framePaint);
        }
Example #13
0
        public override void DrawText(char[] buffer, int x, int y)
        {
            SKRect clipRect = currentClipRect;

            clipRect.Offset(canvasOriginX, canvasOriginY);
            //1.
            //skCanvas.ClipRect(clipRect);
            //2.
            skCanvas.DrawText(new string(buffer), x, y, textFill);
            ////3.
            //ClearCurrentClipRect();
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            using (SKPaint paint = new SKPaint())
            {
                // Set text color to blue
                paint.Color = SKColors.Blue;

                // Set text size to fill 90% of width
                paint.TextSize = 100;
                float width = paint.MeasureText(TEXT);
                float scale = 0.9f * info.Width / width;
                paint.TextSize *= scale;

                // Get text bounds
                SKRect textBounds = new SKRect();
                paint.MeasureText(TEXT, ref textBounds);

                // Calculate offsets to position text above center
                float xText = info.Width / 2 - textBounds.MidX;
                float yText = info.Height / 2;

                // Draw unreflected text
                canvas.DrawText(TEXT, xText, yText, paint);

                // Shift textBounds to match displayed text
                textBounds.Offset(xText, yText);

                // Use those offsets to create a gradient for the reflected text
                paint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(0, textBounds.Top),
                    new SKPoint(0, textBounds.Bottom),
                    new SKColor[] { paint.Color.WithAlpha(0),
                                    paint.Color.WithAlpha(0x80) },
                    null,
                    SKShaderTileMode.Clamp);

                // Create a blur mask filter
                paint.MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, paint.TextSize / 36);

                // Scale the canvas to flip upside-down around the vertical center
                canvas.Scale(1, -1, 0, yText);

                // Draw reflected text
                canvas.DrawText(TEXT, xText, yText, paint);
            }
        }
Example #15
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            using (SKPaint paint = new SKPaint())
            {
                // Create gradient for background
                paint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(0, 0),
                    new SKPoint(info.Width, info.Height),
                    new SKColor[] { new SKColor(0x40, 0x40, 0x40),
                                    new SKColor(0xC0, 0xC0, 0xC0) },
                    null,
                    SKShaderTileMode.Clamp);

                // Draw background
                canvas.DrawRect(info.Rect, paint);

                // Set TextSize to fill 90% of width
                paint.TextSize = 100;
                float width = paint.MeasureText(TEXT);
                float scale = 0.9f * info.Width / width;
                paint.TextSize *= scale;

                // Get text bounds
                SKRect textBounds = new SKRect();
                paint.MeasureText(TEXT, ref textBounds);

                // Calculate offsets to center the text on the screen
                float xText = info.Width / 2 - textBounds.MidX;
                float yText = info.Height / 2 - textBounds.MidY;

                // Shift textBounds by that amount
                textBounds.Offset(xText, yText);

                // Create gradient for text
                paint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(textBounds.Left, textBounds.Top),
                    new SKPoint(textBounds.Right, textBounds.Bottom),
                    new SKColor[] { new SKColor(0x40, 0x40, 0x40),
                                    new SKColor(0xC0, 0xC0, 0xC0) },
                    null,
                    SKShaderTileMode.Clamp);

                // Draw text
                canvas.DrawText(TEXT, xText, yText, paint);
            }
        }
Example #16
0
        public static SKRect MovedInsideOf(this SKRect smallRect, SKRect bigRect)
        {
            if (smallRect.Left < bigRect.Left)
            {
                smallRect.Offset(bigRect.Left - smallRect.Left, 0);
            }
            else if (smallRect.Right > bigRect.Right)
            {
                smallRect.Offset(bigRect.Right - smallRect.Right, 0);
            }

            if (smallRect.Top < bigRect.Top)
            {
                smallRect.Offset(0, bigRect.Top - smallRect.Top);
            }
            else if (smallRect.Bottom > bigRect.Bottom)
            {
                smallRect.Offset(0, bigRect.Bottom - smallRect.Bottom);
            }

            return(smallRect);
        }
Example #17
0
        protected void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            var length = Math.Min(width / 6, height / 6);
            var rect   = new SKRect(-length, -length, length, length);
            var side   = rotationMatrix.MapPoint(new SKPoint(1, 0)).X > 0;

            canvas.Clear(SampleMedia.Colors.XamarinLightBlue);

            // first do 2D translation to the center of the screen
            canvas.Translate(width / 2, height / 2);

            // then apply the 3D rotation
            var matrix = rotationMatrix.Matrix;

            canvas.Concat(ref matrix);

            var paint = new SKPaint
            {
                Color       = side ? SampleMedia.Colors.XamarinPurple : SampleMedia.Colors.XamarinGreen,
                Style       = SKPaintStyle.Fill,
                IsAntialias = true
            };

            canvas.DrawRoundRect(rect, 30, 30, paint);

            var shadow = SKShader.CreateLinearGradient(
                new SKPoint(0, 0), new SKPoint(0, length * 2),
                new[] { paint.Color.WithAlpha(127), paint.Color.WithAlpha(0) },
                null,
                SKShaderTileMode.Clamp);

            paint = new SKPaint
            {
                Shader      = shadow,
                Style       = SKPaintStyle.Fill,
                IsAntialias = true
            };

            rect.Offset(0, length * 2 + 5);
            canvas.DrawRoundRect(rect, 30, 30, paint);

            Log.Debug("Demo", "Enter");

            if (cnt == 0)
            {
                Log.Debug("Demo", "Enter");
                InitEvent();
            }
        }
Example #18
0
        public void RectangleOffsetsCorrectly()
        {
            var expected = new SKRect(25, 30, 65, 80);

            var rect1 = new SKRect(15, 25, 55, 75);

            rect1.Location = new SKPoint(25, 30);

            var rect2 = new SKRect(15, 25, 55, 75);

            rect2.Offset(10, 5);

            Assert.AreEqual(expected, rect1);
            Assert.AreEqual(expected, rect2);
        }
Example #19
0
        protected override void OnDraw(object sender, DrawEventArgs args)
        {
            if (IsVisible)
            {
                var canvas = args.Canvas;
                canvas.DrawImage(Resource.Butter, Geometry);
                geometry.Offset(0, 15);

                if (geometry.Location.Y > target.Location.Y)
                {
                    tcs.TrySetResult(true);
                    IsVisible = false;
                }
            }
        }
Example #20
0
        public override void move()
        {
            if (rect1.Right < 0)
            {
                rect1.Location = new SKPoint(rect2.Location.X + rect2.Width - 1, rect2.Location.Y);
            }

            if (rect2.Right < 0)
            {
                rect2.Location = new SKPoint(rect1.Location.X + rect1.Width - 1, rect1.Location.Y);
            }

            rect1.Offset((float)xSpeed, 0);
            rect2.Offset((float)xSpeed, 0);
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            if (webBitmap != null)
            {
                float x = (info.Width - webBitmap.Width) / 2;
                float y = (info.Height / 3 - webBitmap.Height) / 2;
                canvas.DrawBitmap(webBitmap, x, y);
            }

            if (resourceBitmap != null)
            {
                canvas.DrawBitmap(resourceBitmap,
                                  new SKRect(0, info.Height / 3, info.Width, 2 * info.Height / 3));
            }

            if (libraryBitmap != null)
            {
                float scale = Math.Min((float)info.Width / libraryBitmap.Width,
                                       info.Height / 3f / libraryBitmap.Height);

                float  left   = (info.Width - scale * libraryBitmap.Width) / 2;
                float  top    = (info.Height / 3 - scale * libraryBitmap.Height) / 2;
                float  right  = left + scale * libraryBitmap.Width;
                float  bottom = top + scale * libraryBitmap.Height;
                SKRect rect   = new SKRect(left, top, right, bottom);
                rect.Offset(0, 2 * info.Height / 3);

                canvas.DrawBitmap(libraryBitmap, rect);
            }
            else
            {
                using (SKPaint paint = new SKPaint())
                {
                    paint.Color     = SKColors.Blue;
                    paint.TextAlign = SKTextAlign.Center;
                    paint.TextSize  = 48;

                    canvas.DrawText("Tap to load bitmap",
                                    info.Width / 2, 5 * info.Height / 6, paint);
                }
            }
        }
Example #22
0
        protected override HandleRef BuildWindowCore(HandleRef hwndParent)
        {
            var wndclass = new User32.WNDCLASSEX();

            wndclass.cbSize    = Marshal.SizeOf <User32.WNDCLASSEX>();
            wndclass.style     = 0;
            wndclass.hInstance = Kernel32.GetModuleHandle(null).DangerousGetHandle();
            User32.RegisterClassEx(ref wndclass);
            var windowHandle = User32.CreateWindowEx((User32.WindowStylesEx)WS_EX_NOREDIRECTIONBITMAP, "static", "", User32.WindowStyles.WS_CHILD | User32.WindowStyles.WS_CLIPCHILDREN | User32.WindowStyles.WS_VISIBLE, 0, 0, (int)Width, (int)Height, hwndParent.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            User32.SetWindowPos(windowHandle, hwndParent.Handle, 0, 0, (int)Width, (int)Height, User32.SetWindowPosFlags.SWP_SHOWWINDOW);
            ANGLE.InitializeContext((int)Width, (int)Height, true, windowHandle, User32.GetDC(windowHandle).DangerousGetHandle());
            var angleInterface = GRGlInterface.CreateAngle(ANGLE.AngleGetProcAddress);
            var grContext      = GRContext.CreateGl(angleInterface);
            var buffer         = ANGLE.GetFrameBuffer();
            var fbinfo         = new GRGlFramebufferInfo(buffer, ANGLE.GetFramebufferFormat());
            var backendRT      = new GRBackendRenderTarget((int)Width, (int)Height, 4, 8, fbinfo);
            var surfaceprops   = new SKSurfaceProperties(SKSurfacePropsFlags.UseDeviceIndependentFonts, SKPixelGeometry.Unknown);
            var surface        = SKSurface.Create(grContext, backendRT, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888, null, surfaceprops);
            var canvas         = surface.Canvas;

            canvas.Clear(SKColor.Empty);
            SKPaint paint = new SKPaint();

            paint.Style       = SKPaintStyle.Fill;
            paint.IsAntialias = true;
            paint.StrokeWidth = 4;
            paint.Color       = new SKColor(0xff4285f4);
            SKRect rect = SKRect.Create(10, 10, 100, 160);

            canvas.DrawRect(rect, paint);
            SKRoundRect oval = new SKRoundRect();

            oval.SetOval(rect);
            oval.Offset(40, 80);
            paint.Color = new SKColor(0xffdb4437);
            canvas.DrawRoundRect(oval, paint);
            paint.Color = new SKColor(0xff0f9d58);
            canvas.DrawCircle(180, 50, 25, paint);
            rect.Offset(80, 50);
            paint.Color = new SKColor(0xfff4b400);
            paint.Style = SKPaintStyle.Stroke;
            canvas.DrawRoundRect(rect, 10, 10, paint);
            canvas.Flush();
            ANGLE.Swap();
            return(new HandleRef(this, windowHandle));
        }
Example #23
0
        private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();
            string str = "Hello SkiaSharp!";
            // crea un SKPaint oggetto per visualizzare il testo
            SKPaint textPaint = new SKPaint
            {
                Color = SKColors.Chocolate
            };
            // regola la proprietà TextSize in modo che il testo sia 90% della larghezza dello schermo
            float textWidth = textPaint.MeasureText(str);

            textPaint.TextSize = 0.9f * info.Width * textPaint.TextSize / textWidth;
            // trovare i limiti del testo
            SKRect textBounds = new SKRect();

            textPaint.MeasureText(str, ref textBounds);
            // calcola gli offset per centrare il testo sullo schermo
            float xText = info.Width / 2 - textBounds.MidX;
            float yText = info.Height / 2 - textBounds.MidY;

            // e disegna il testo
            canvas.DrawText(str, xText, yText, textPaint);
            // crea un nuovo skRect oggetto per la cornice intorno al testo
            SKRect frameRect = textBounds;

            frameRect.Offset(xText, yText);
            frameRect.Inflate(10, 10);
            // crea un SKPaint oggetto per visualizzare il frame
            SKPaint framePaint = new SKPaint
            {
                Style       = SKPaintStyle.Stroke,
                StrokeWidth = 5,
                Color       = SKColors.Blue
            };

            // disegna un frame
            canvas.DrawRoundRect(frameRect, 20, 20, framePaint);
            // gonfiare il frameRect e disegnarne un altro
            frameRect.Inflate(10, 10);
            framePaint.Color = SKColors.DarkBlue;
            canvas.DrawRoundRect(frameRect, 30, 30, framePaint);
        }
Example #24
0
        public override void DrawText(char[] buffer, Rectangle logicalTextBox, int textAlignment)
        {
            SKRect clipRect = currentClipRect;

            //1.
            clipRect.Offset(canvasOriginX, canvasOriginY);
            //2.
            //skCanvas.ClipRect(clipRect);
            //3.
            //TODO: review here
            skCanvas.DrawText(new string(buffer),
                              logicalTextBox.X,
                              logicalTextBox.Bottom,
                              textFill);
            //4.
            //ClearCurrentClipRect();
        }
        private static void MeasureText(SKImageInfo info, SKCanvas canvas)
        {
            string str = "SkiaSharp Demo";

            SKRect textBounds = new SKRect();
            float  xText;
            float  yText;

            using (SKPaint textPaint = new SKPaint()
            {
                Color = SKColors.Chocolate
            })
            {
                float textWidth = textPaint.MeasureText(str);
                textPaint.TextSize = 0.9f * info.Width * textPaint.TextSize / textWidth;

                // Find the text bounds
                textPaint.MeasureText(str, ref textBounds);

                // Calculate offsets to center the text on the screen
                xText = info.Width / 2 - textBounds.MidX;
                yText = info.Height / 2 - textBounds.MidY;

                // And draw the text
                canvas.DrawText(str, xText, yText, textPaint);
            }


            using (SKPaint framePaint = new SKPaint()
            {
                Color = SKColors.Blue, StrokeWidth = 5, Style = SKPaintStyle.Stroke
            })
            {
                SKRect frameRect = textBounds;
                frameRect.Offset(xText, yText);
                frameRect.Inflate(10, 10);

                canvas.DrawRoundRect(frameRect, 20, 20, framePaint);

                // Inflate the frameRect and draw another
                frameRect.Inflate(10, 10);
                framePaint.Color = SKColors.DarkBlue;

                canvas.DrawRoundRect(frameRect, 30, 30, framePaint);
            }
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            // Find rectangle to display bitmap
            float scale = Math.Min((float)info.Width / bitmap.Width,
                                   (float)info.Height / bitmap.Height);

            SKRect rect = SKRect.Create(scale * bitmap.Width, scale * bitmap.Height);

            float x = (info.Width - rect.Width) / 2;
            float y = (info.Height - rect.Height) / 2;

            rect.Offset(x, y);

            // Display bitmap in rectangle
            canvas.DrawBitmap(bitmap, rect);

            // Adjust center and radius for scaled and offset bitmap
            SKPoint center = new SKPoint(scale * CENTER.X + x,
                                         scale * CENTER.Y + y);
            float radius = scale * RADIUS;

            using (SKPaint paint = new SKPaint())
            {
                paint.Shader = SKShader.CreateRadialGradient(
                    center,
                    radius,
                    new SKColor[] { SKColors.Black,
                                    SKColors.Transparent },
                    new float[] { 0.6f, 1 },
                    SKShaderTileMode.Clamp);

                paint.BlendMode = SKBlendMode.DstIn;

                // Display rectangle using that gradient and blend mode
                canvas.DrawRect(rect, paint);
            }

            canvas.DrawColor(SKColors.Pink, SKBlendMode.DstOver);
        }
        //Flow.SkiaUnrefQueue _queue = new Flow.SkiaUnrefQueue();
        public void AddPicture(double dx, double dy, SKPicture picture, int hints)
        {
            if (current_layer_ == null)
            {
                return;
            }
            SKPoint offset      = new SKPoint((float)dx, (float)dy);
            SKRect  pictureRect = picture.CullRect;

            pictureRect.Offset(offset.X, offset.Y);
            var layer = new PictureLayer();

            layer.set_offset(offset);
            layer.set_picture(picture);
            layer.set_is_complex((hints & 1) == 1);
            layer.set_will_change((hints & 2) == 2);
            current_layer_.Add(layer);
        }
        public void AddPicture(double dx, double dy, SKPicture picture, int hints)
        {
            if (current_layer_ == null)
            {
                return;
            }
            SKPoint offset      = new SKPoint((float)dx, (float)dy);
            SKRect  pictureRect = picture.CullRect;

            pictureRect.Offset(offset.X, offset.Y);
            var layer = new PictureLayer();

            layer.set_offset(offset);
            layer.set_picture(new Flow.SkiaGPUObject <SKPicture>(picture, _queue)); // UIDartState::CreateGPUObject(picture->picture()));
            layer.set_is_complex(true);                                             // !!(hints & 1));
            layer.set_will_change(true);                                            // !!(hints & 2));
            current_layer_.Add(layer);
        }
Example #29
0
        public override void Preroll(PrerollContext context, SKMatrix matrix)
        {
            SKPicture sk_picture = picture();

            var      cache = context.raster_cache;
            SKMatrix ctm   = matrix;

            ctm.SetScaleTranslate(ctm.ScaleX, ctm.ScaleY, offset_.X, offset_.Y);
#if !SUPPORT_FRACTIONAL_TRANSLATION
            ctm = RasterCache.GetIntegralTransCTM(ctm);
#endif
            cache.Prepare(context.gr_context, sk_picture, ctm, context.dst_color_space, is_complex_, will_change_);


            SKRect bounds = sk_picture.CullRect;
            bounds.Offset(offset_.X, offset_.Y);
            set_paint_bounds(bounds);
        }
Example #30
0
        public override HitTestResults HitTest(SKPointI point)
        {
            if (!ScreenRect.Contains(point))
            {
                return(null);
            }

            for (int i = 0; i < MaxNumberOfSkills; ++i)
            {
                SKRect buttonRect = RectForSkill(i);
                buttonRect.Offset(Position.X, Position.Y);
                if (buttonRect.Contains(point))
                {
                    return(new HitTestResults(this, i));
                }
            }
            return(null);
        }