Exemplo n.º 1
0
        private static SKMatrix CreateRotationMatrix(IReadOnlyViewport viewport, BoundingBox boundingBox, SKMatrix priorMatrix)
        {
            SKMatrix matrix = SKMatrix.MakeIdentity();

            // The front-end sets up the canvas with a matrix based on screen scaling (e.g. retina).
            // We need to retain that effect by combining our matrix with the incoming matrix.

            // We'll create four matrices in addition to the incoming matrix. They perform the
            // zoom scale, focal point offset, user rotation and finally, centering in the screen.

            var userRotation     = SKMatrix.MakeRotationDegrees((float)viewport.Rotation);
            var focalPointOffset = SKMatrix.MakeTranslation(
                (float)(boundingBox.Left - viewport.Center.X),
                (float)(viewport.Center.Y - boundingBox.Top));
            var zoomScale      = SKMatrix.MakeScale((float)(1.0 / viewport.Resolution), (float)(1.0 / viewport.Resolution));
            var centerInScreen = SKMatrix.MakeTranslation((float)(viewport.Width / 2.0), (float)(viewport.Height / 2.0));

            // We'll concatenate them like so: incomingMatrix * centerInScreen * userRotation * zoomScale * focalPointOffset

            SKMatrix.Concat(ref matrix, zoomScale, focalPointOffset);
            SKMatrix.Concat(ref matrix, userRotation, matrix);
            SKMatrix.Concat(ref matrix, centerInScreen, matrix);
            SKMatrix.Concat(ref matrix, priorMatrix, matrix);

            return(matrix);
        }
Exemplo n.º 2
0
        public void Init()
        {
            if (isInitialized)
            {
                return;
            }
            // reset the matrix for the new sample
            Matrix = SKMatrix.MakeIdentity();

            // リソースから読み込む
            var assembly = Assembly.GetExecutingAssembly();

            using (var stream = assembly.GetManifestResourceStream(@"Tategumi.WPF.kokoro.htm"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    var text = reader.ReadToEnd();
                    Manager.ReadFromText(text);
                }
            Manager.TateviewWidth  = canvas.CanvasSize.Width;
            Manager.TateviewHeight = canvas.CanvasSize.Height;
            Manager.FontSize       = 24;

            TategumiViewCore.OpenFontStream =
                () => assembly.GetManifestResourceStream(@"Tategumi.WPF.ipaexm.ttf");

            // 組版する
            Manager.Compose();
            isInitialized = true;
        }
        void Manipulate()
        {
            TouchManipulationInfo[] infos = new TouchManipulationInfo[touchDictionary.Count];
            touchDictionary.Values.CopyTo(infos, 0);
            SKMatrix touchMatrix = SKMatrix.MakeIdentity();

            if (infos.Length == 1)
            {
                SKPoint prevPoint  = infos[0].PreviousPoint;
                SKPoint newPoint   = infos[0].NewPoint;
                SKPoint pivotPoint = Matrix.MapPoint(bitmap.Width / 2, bitmap.Height / 2);

                touchMatrix = TouchManager.OneFingerManipulate(prevPoint, newPoint, pivotPoint);
            }
            else if (infos.Length >= 2)
            {
                int     pivotIndex = infos[0].NewPoint == infos[0].PreviousPoint ? 0 : 1;
                SKPoint pivotPoint = infos[pivotIndex].NewPoint;
                SKPoint newPoint   = infos[1 - pivotIndex].NewPoint;
                SKPoint prevPoint  = infos[1 - pivotIndex].PreviousPoint;

                touchMatrix = TouchManager.TwoFingerManipulate(prevPoint, newPoint, pivotPoint);
            }

            SKMatrix matrix = Matrix;

            SKMatrix.PostConcat(ref matrix, touchMatrix);
            Matrix = matrix;
        }
Exemplo n.º 4
0
        private static SKMatrix ConvertCvMatOfDoublesToSkMatrix(Mat mat, bool discardTransX)
        {
            var skMatrix = SKMatrix.MakeIdentity();

            unsafe
            {
                var ptr = (double *)mat.DataPointer.ToPointer(); //ScaleX
                skMatrix.ScaleX = (float)*ptr;
                ptr++;                                           //SkewX
                skMatrix.SkewX = (float)*ptr;
                ptr++;                                           //TransX
                if (discardTransX)
                {
                    *ptr = 0;
                }
                skMatrix.TransX = (float)*ptr;
                ptr++; //SkewY
                skMatrix.SkewY = (float)*ptr;
                ptr++; //ScaleY
                skMatrix.ScaleY = (float)*ptr;
                ptr++; //TransY
                skMatrix.TransY = (float)*ptr;
            }

            return(skMatrix);
        }
Exemplo n.º 5
0
        public void Pinch(GestureState state, float scale, SKPoint origin)
        {
            switch (state)
            {
            case GestureState.Started:
                startPinchMatrix = Matrix;
                startPinchOrigin = origin;
                totalPinchScale  = 1f;
                break;

            case GestureState.Running:
                totalPinchScale *= scale;
                var pinchTranslation  = origin - startPinchOrigin;
                var canvasTranslation = SKMatrix.MakeTranslation(pinchTranslation.X, pinchTranslation.Y);
                var canvasScaling     = SKMatrix.MakeScale(totalPinchScale, totalPinchScale, origin.X, origin.Y);
                var canvasCombined    = SKMatrix.MakeIdentity();
                SKMatrix.Concat(ref canvasCombined, ref canvasScaling, ref canvasTranslation);
                SKMatrix.Concat(ref Matrix, ref canvasCombined, ref startPinchMatrix);
                break;

            default:
                startPinchMatrix = SKMatrix.MakeIdentity();
                startPinchOrigin = SKPoint.Empty;
                totalPinchScale  = 1f;
                break;
            }
        }
        private void HandlePan(object sender, PanUpdatedEventArgs puea)
        {
            Debug.WriteLine($"{puea.StatusType} ({puea.TotalX},{puea.TotalY})");

            switch (puea.StatusType)
            {
            case GestureStatus.Started:
                _startPanM = _m;
                break;

            case GestureStatus.Running:
                float    canvasTotalX      = (float)puea.TotalX * _screenScale;
                float    canvasTotalY      = (float)puea.TotalY * _screenScale;
                SKMatrix canvasTranslation = SKMatrix.MakeTranslation(canvasTotalX, canvasTotalY);
                SKMatrix.Concat(ref _m, ref canvasTranslation, ref _startPanM);
                _currentTransformM = canvasTranslation;
                _canvasV.InvalidateSurface();
                break;

            default:
                _startPanM = SKMatrix.MakeIdentity();

                // force textLayer to regenerate
                _textLayer?.Dispose();
                _textLayer = null;
                _canvasV.InvalidateSurface();
                break;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Configure paint wrapper for using tile brush.
        /// </summary>
        /// <param name="paintWrapper">Paint wrapper.</param>
        /// <param name="targetSize">Target size.</param>
        /// <param name="tileBrush">Tile brush to use.</param>
        /// <param name="tileBrushImage">Tile brush image.</param>
        private void ConfigureTileBrush(ref PaintWrapper paintWrapper, Size targetSize, ITileBrush tileBrush, IDrawableBitmapImpl tileBrushImage)
        {
            var calc         = new TileBrushCalculator(tileBrush, tileBrushImage.PixelSize.ToSizeWithDpi(_dpi), targetSize);
            var intermediate = CreateRenderTarget(calc.IntermediateSize);

            paintWrapper.AddDisposable(intermediate);

            using (var context = intermediate.CreateDrawingContext(null))
            {
                var sourceRect = new Rect(tileBrushImage.PixelSize.ToSizeWithDpi(96));
                var targetRect = new Rect(tileBrushImage.PixelSize.ToSizeWithDpi(_dpi));

                context.Clear(Colors.Transparent);
                context.PushClip(calc.IntermediateClip);
                context.Transform = calc.IntermediateTransform;
                context.DrawImage(
                    RefCountable.CreateUnownedNotClonable(tileBrushImage),
                    1,
                    sourceRect,
                    targetRect,
                    tileBrush.BitmapInterpolationMode);
                context.PopClip();
            }

            var tileTransform =
                tileBrush.TileMode != TileMode.None
                    ? SKMatrix.MakeTranslation(-(float)calc.DestinationRect.X, -(float)calc.DestinationRect.Y)
                    : SKMatrix.MakeIdentity();

            SKShaderTileMode tileX =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipX || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;

            SKShaderTileMode tileY =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipY || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;


            var image = intermediate.SnapshotImage();

            paintWrapper.AddDisposable(image);

            var paintTransform = default(SKMatrix);

            SKMatrix.Concat(
                ref paintTransform,
                tileTransform,
                SKMatrix.MakeScale((float)(96.0 / _dpi.X), (float)(96.0 / _dpi.Y)));

            using (var shader = image.ToShader(tileX, tileY, paintTransform))
            {
                paintWrapper.Paint.Shader = shader;
            }
        }
Exemplo n.º 8
0
        private SKMatrix GetScaleMatrix(SKImageInfo canvasInfo)
        {
            if (Stretch == SvgStretch.None || _svgRect.Width < 0.00001f || _svgRect.Height < 0.0000001f)
            {
                return(SKMatrix.MakeIdentity());
            }
            float widthRatio  = canvasInfo.Width / _svgRect.Width;
            float heightRatio = canvasInfo.Height / _svgRect.Height;

            switch (Stretch)
            {
            case SvgStretch.Uniform:
                widthRatio = heightRatio = Math.Min(widthRatio, heightRatio);
                break;

            case SvgStretch.UniformToFill:
                widthRatio = heightRatio = Math.Max(widthRatio, heightRatio);
                break;

            case SvgStretch.Fill:
                // Do nothing, use dimension ratios
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(SKMatrix.MakeScale(widthRatio, heightRatio));
        }
Exemplo n.º 9
0
        public override void MoveTo(SKRect newBox)
        {
            var oldBox = Box;

            if (oldBox.Width != newBox.Width ||
                oldBox.Height != newBox.Height)
            {
                Appearance.Normal[null] = null;
            }
            //base.MoveTo(newBox);
            var dif = SKMatrix.MakeIdentity();

            SKMatrix.PreConcat(ref dif, SKMatrix.MakeTranslation(newBox.MidX, newBox.MidY));
            SKMatrix.PreConcat(ref dif, SKMatrix.MakeScale(newBox.Width / oldBox.Width, newBox.Height / oldBox.Height));
            SKMatrix.PreConcat(ref dif, SKMatrix.MakeTranslation(-oldBox.MidX, -oldBox.MidY));
            var oldPaths = Paths;
            var newPaths = new List <SKPath>();

            foreach (var path in oldPaths)
            {
                var vertices = path.Points;
                for (int i = 0; i < vertices.Length; i++)
                {
                    vertices[i] = dif.MapPoint(vertices[i]);
                }
                var newPath = new SKPath();
                newPath.AddPoly(vertices, false);
                newPaths.Add(newPath);
            }
            Paths = newPaths;
            foreach (var oldPath in oldPaths)
            {
                oldPath.Dispose();
            }
        }
Exemplo n.º 10
0
        public static SKPath?GetClipPathClipPath(SvgClipPath svgClipPath, SKRect skBounds, HashSet <Uri> uris, CompositeDisposable disposable)
        {
            var svgClipPathRef = svgClipPath.GetUriElementReference <SvgClipPath>("clip-path", uris);

            if (svgClipPathRef == null || svgClipPathRef.Children == null)
            {
                return(null);
            }

            var clipPath = GetClipPath(svgClipPathRef, skBounds, uris, disposable);

            if (clipPath != null)
            {
                var skMatrix = SKMatrix.MakeIdentity();

                if (svgClipPathRef.ClipPathUnits == SvgCoordinateUnits.ObjectBoundingBox)
                {
                    var skScaleMatrix = SKMatrix.MakeScale(skBounds.Width, skBounds.Height);
                    SKMatrix.PostConcat(ref skMatrix, ref skScaleMatrix);

                    var skTranslateMatrix = SKMatrix.MakeTranslation(skBounds.Left, skBounds.Top);
                    SKMatrix.PostConcat(ref skMatrix, ref skTranslateMatrix);
                }

                var skTransformsMatrix = SvgTransformsExtensions.ToSKMatrix(svgClipPathRef.Transforms);
                SKMatrix.PostConcat(ref skMatrix, ref skTransformsMatrix);

                clipPath.Transform(skMatrix);
            }

            return(clipPath);
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            // Calculate perspective matrix
            SKMatrix perspectiveMatrix = SKMatrix.MakeIdentity();

            perspectiveMatrix.Persp0 = (float)persp0Slider.Value / 100;
            perspectiveMatrix.Persp1 = (float)persp1Slider.Value / 100;

            // Center of screen
            float xCenter = info.Width / 2;
            float yCenter = info.Height / 2;

            SKMatrix matrix = SKMatrix.MakeTranslation(-xCenter, -yCenter);

            SKMatrix.PostConcat(ref matrix, perspectiveMatrix);
            SKMatrix.PostConcat(ref matrix, SKMatrix.MakeTranslation(xCenter, yCenter));

            // Coordinates to center bitmap on canvas
            float x = xCenter - bitmap.Width / 2;
            float y = yCenter - bitmap.Height / 2;

            canvas.SetMatrix(matrix);
            canvas.DrawBitmap(bitmap, x, y);
        }
Exemplo n.º 12
0
    private void SaveVector(Shape shape)
    {
        var svg = new SkiaSharp.Extended.Svg.SKSvg();

        svg.Load(shape.OutputPath);

        var svgRect = svg.Picture.CullRect;

        var(w, h) = CalculateResized((int)svgRect.Width, (int)svgRect.Height);
        float svgMax = Math.Max(w, h);

        float scale   = w / svgRect.Width;
        var   matrixS = SKMatrix.MakeScale(scale, scale);
        var   matrixT = SKMatrix.MakeTranslation((MAX_SIZE - w) / 2, (MAX_SIZE - h) / 2);
        var   matrix  = SKMatrix.MakeIdentity();

        SKMatrix.Concat(ref matrix, matrixT, matrixS);

        var target = new SKBitmap(MAX_SIZE, MAX_SIZE,
                                  SKImageInfo.PlatformColorType, SKAlphaType.Premul);

        using (target)
            using (var canvas = new SKCanvas(target))
            {
                canvas.Clear();
                canvas.DrawPicture(svg.Picture, ref matrix, _skPaint);
                SaveToFile(shape.Id, "svg", target);
            }
    }
Exemplo n.º 13
0
        public void Render(SKCanvas canvas)
        {
            if (AbsoluteVisible)
            {
                var textureBox    = Texture.ViewBox;
                var textureWidth  = textureBox.Width;
                var textureHeight = textureBox.Height;

                var scaleX = this.Width / textureWidth;
                var scaleY = this.Height / textureHeight;

                SKMatrix scaleMatrix = SKMatrix.MakeScale(scaleX, scaleY);
                // Gum uses counter clockwise rotation, Skia uses clockwise, so invert:
                SKMatrix rotationMatrix  = SKMatrix.MakeRotationDegrees(-Rotation);
                SKMatrix translateMatrix = SKMatrix.MakeTranslation(this.GetAbsoluteX(), this.GetAbsoluteY());
                SKMatrix result          = SKMatrix.MakeIdentity();

                SKMatrix.Concat(
                    ref result, rotationMatrix, scaleMatrix);
                SKMatrix.Concat(
                    ref result, translateMatrix, result);

                canvas.DrawPicture(Texture.Picture, ref result);
            }
        }
        static SKMatrix Multiply(SKMatrix first, SKMatrix second)
        {
            SKMatrix target = SKMatrix.MakeIdentity();

            SKMatrix.Concat(ref target, first, second);
            return(target);
        }
Exemplo n.º 15
0
        public override void DrawBound(SKRect boundingRect, SKCanvas canvas)
        {
            SKMatrix scaleMatrix = SKMatrix.MakeScale(1, 1);
            //// Gum uses counter clockwise rotation, Skia uses clockwise, so invert:
            SKMatrix rotationMatrix  = SKMatrix.MakeRotationDegrees(-Rotation);
            SKMatrix translateMatrix = SKMatrix.MakeTranslation(this.GetAbsoluteX(), this.GetAbsoluteY());
            SKMatrix result          = SKMatrix.MakeIdentity();

            SKMatrix.Concat(
                ref result, rotationMatrix, scaleMatrix);
            SKMatrix.Concat(
                ref result, translateMatrix, result);
            canvas.Save();
            canvas.SetMatrix(result);


            SKPath path = new SKPath();

            path.MoveTo(Points[0]);

            for (int i = 0; i < Points.Count; i++)
            {
                path.LineTo(Points[i]);
            }
            path.LineTo(Points[0]);

            path.Close();
            using (var paintToUse = paint)
            {
                canvas.DrawPath(path, paintToUse);
            }

            canvas.Restore();
        }
 internal TouchManipulationBitmap(SKBitmap bitmap, BitmapType type, string text, SKColor color = default)
 {
     Matrix = SKMatrix.MakeIdentity();
     Bitmap = bitmap;
     Type   = type;
     Color  = color;
     Text   = text;
 }
            private static void CalculateBounds(bool forDrawing, SkiaSharp.Extended.Svg.SKSvg svg, Aspect aspect, double maxWidth, double maxHeight, out SKMatrix drawMatrix, out SKSizeI pixelSize)
            {
                // Determine bounds of SVG
                SKRect svgBounds = svg.ViewBox;

                if (svgBounds.Width == 0 || svgBounds.Height == 0)
                {
                    svgBounds = new SKRect(0, 0, svg.CanvasSize.Width, svg.CanvasSize.Height);
                }

                // Unable to scale?
                double scaleX, scaleY;

                if (svgBounds.Width == 0 || svgBounds.Height == 0 || double.IsInfinity(maxWidth) || double.IsInfinity(maxHeight))
                {
                    scaleX = scaleY = 1;
                }
                else
                {
                    // Scale properly
                    double ratioX = maxWidth / svgBounds.Width, ratioY = maxHeight / svgBounds.Height;
                    switch (aspect)
                    {
                    case Aspect.AspectFill:
                        scaleX = scaleY = Math.Max(ratioX, ratioY);
                        break;

                    case Aspect.AspectFit:
                        scaleX = scaleY = Math.Min(ratioX, ratioY);
                        break;

                    case Aspect.Fill:
                    default:
                        scaleX = ratioX;
                        scaleY = ratioY;
                        break;
                    }
                }

                // Increase sizing for drawing
                if (forDrawing)
                {
                    scaleX *= DemoUtilities.DisplayDensity;
                    scaleY *= DemoUtilities.DisplayDensity;
                }

                // Apply scaling
                if (svg.ViewBox.Width != 0 && svg.ViewBox.Height != 0)
                {
                    drawMatrix = SKMatrix.MakeTranslation(-svg.ViewBox.Left, -svg.ViewBox.Top);
                }
                else
                {
                    drawMatrix = SKMatrix.MakeIdentity();
                }
                SKMatrix.PreConcat(ref drawMatrix, SKMatrix.MakeScale((float)scaleX, (float)scaleY));
                pixelSize = new SKSizeI((int)Math.Ceiling(scaleX * svgBounds.Width), (int)Math.Ceiling(scaleY * svgBounds.Height));
            }
        public TouchManipulationBitmap(SKBitmap bitmap)
        {
            this.bitmap = bitmap;
            Matrix      = SKMatrix.MakeIdentity();

            TouchManager = new TouchManipulationManager
            {
                Mode = TouchManipulationMode.ScaleRotate
            };
        }
Exemplo n.º 19
0
        public TouchManipulationBitmap(SKBitmap bitmap, int id)
        {
            this.bitmap = bitmap;
            ImageId     = id;
            Matrix      = SKMatrix.MakeIdentity();

            TouchManager = new TouchManipulationManager
            {
                Mode = TouchManipulationMode.PanOnly
            };
        }
Exemplo n.º 20
0
        public void SetScaleTranslateWorksCorrectly()
        {
            var tempMatrix = SKMatrix.MakeIdentity();

            tempMatrix.Values = new float[] { 1, 0, 0, 0, 1, 0, 0, 0, 1 };

            SKMatrix.RotateDegrees(ref tempMatrix, 0);

            tempMatrix.SetScaleTranslate(1.2f, 1.0f, 0, 0);

            Assert.Equal(1.2f, tempMatrix.Values[0]);
        }
Exemplo n.º 21
0
        private void DoPanZoom(SKMatrix startM, SKPoint anchorPt, SKPoint totalTranslation, double totalScale)
        {
            SKPoint  canvasAnchorPt         = new SKPoint(anchorPt.X * _screenScale, anchorPt.Y * _screenScale);
            SKPoint  totalCanvasTranslation = new SKPoint(totalTranslation.X * _screenScale, totalTranslation.Y * _screenScale);
            SKMatrix canvasTranslation      = SKMatrix.MakeTranslation((float)totalCanvasTranslation.X, (float)totalCanvasTranslation.Y);
            SKMatrix canvasScaling          = SKMatrix.MakeScale((float)totalScale, (float)totalScale, (float)canvasAnchorPt.X, (float)canvasAnchorPt.Y);
            SKMatrix canvasCombined         = SKMatrix.MakeIdentity();

            SKMatrix.Concat(ref canvasCombined, ref canvasTranslation, ref canvasScaling);
            SKMatrix.Concat(ref _m, ref canvasCombined, ref startM);
            CanvasView.SetNeedsDisplayInRect(CanvasView.Bounds);
        }
Exemplo n.º 22
0
        protected override void OnPaintSurface(SKPaintSurfaceEventArgs args)
        {
            base.OnPaintSurface(args);

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

            canvas.Clear(SKColors.Gray);

            // Calculate rectangle for displaying bitmap
            float  scale      = Math.Min((float)info.Width / bitmap.Width, (float)info.Height / bitmap.Height);
            float  x          = (info.Width - scale * bitmap.Width) / 2;
            float  y          = (info.Height - scale * bitmap.Height) / 2;
            SKRect bitmapRect = new SKRect(x, y, x + scale * bitmap.Width, y + scale * bitmap.Height);

            canvas.DrawBitmap(bitmap, bitmapRect);

            // Calculate a matrix transform for displaying the cropping rectangle
            SKMatrix bitmapScaleMatrix = SKMatrix.MakeIdentity();

            bitmapScaleMatrix.SetScaleTranslate(scale, scale, x, y);

            // Display rectangle
            SKRect scaledCropRect = bitmapScaleMatrix.MapRect(croppingRect.Rect);

            canvas.DrawRect(scaledCropRect, edgeStroke);

            // Display heavier corners
            using (SKPath path = new SKPath())
            {
                path.MoveTo(scaledCropRect.Left, scaledCropRect.Top + CORNER);
                path.LineTo(scaledCropRect.Left, scaledCropRect.Top);
                path.LineTo(scaledCropRect.Left + CORNER, scaledCropRect.Top);

                path.MoveTo(scaledCropRect.Right - CORNER, scaledCropRect.Top);
                path.LineTo(scaledCropRect.Right, scaledCropRect.Top);
                path.LineTo(scaledCropRect.Right, scaledCropRect.Top + CORNER);

                path.MoveTo(scaledCropRect.Right, scaledCropRect.Bottom - CORNER);
                path.LineTo(scaledCropRect.Right, scaledCropRect.Bottom);
                path.LineTo(scaledCropRect.Right - CORNER, scaledCropRect.Bottom);

                path.MoveTo(scaledCropRect.Left + CORNER, scaledCropRect.Bottom);
                path.LineTo(scaledCropRect.Left, scaledCropRect.Bottom);
                path.LineTo(scaledCropRect.Left, scaledCropRect.Bottom - CORNER);

                canvas.DrawPath(path, cornerStroke);
            }

            // Invert the transform for touch tracking
            bitmapScaleMatrix.TryInvert(out inverseBitmapMatrix);
        }
Exemplo n.º 23
0
        public MainPage()
        {
            InitializeComponent();
            _currentMatrix = SKMatrix.MakeIdentity();
            _images        = new List <MyImage>();
            _quality       = SKFilterQuality.None;
            var pinch = new PinchGestureRecognizer();

            pinch.PinchUpdated += Pinch_PinchUpdated;
            SkiaView.GestureRecognizers.Add(pinch);
            SkiaView.EnableTouchEvents = true;
            SkiaView.Touch            += SkiaView_Touch;

            var assembly = typeof(MainPage).GetTypeInfo().Assembly;

            _buffer = new List <byte[]>();
            for (int i = 0; i < 24; i++)
            {
                var names = assembly.GetManifestResourceNames();
                using (Stream s = assembly.GetManifestResourceStream("SkiaXFPlayground.img" + i + ".jpg"))
                {
                    if (s != null)
                    {
                        long length = s.Length;
                        var  b      = new byte[length];
                        s.Read(b, 0, (int)length);
                        _buffer.Add(b);
                    }
                }
                LogLabel.Text = "Loading image: " + i;
            }
            LogLabel.Text = "buffering finished";
            var x = 0;
            var y = 0;



            var im = SKImage.FromEncodedData(_buffer.ElementAt(0));

            var img = new MyImage()
            {
                Image = im
            };

            img.Destination = new SKRect(x, y, x + imagesize, y + imagesize);

            LogLabel.Text = "image added ";


            LogLabel.Text = "decoding finished " + _images.Count;
        }
Exemplo n.º 24
0
        /// <summary>
        /// Configure paint wrapper for using tile brush.
        /// </summary>
        /// <param name="paintWrapper">Paint wrapper.</param>
        /// <param name="targetSize">Target size.</param>
        /// <param name="tileBrush">Tile brush to use.</param>
        /// <param name="tileBrushImage">Tile brush image.</param>
        private void ConfigureTileBrush(ref PaintWrapper paintWrapper, Size targetSize, ITileBrush tileBrush, IDrawableBitmapImpl tileBrushImage)
        {
            var calc = new TileBrushCalculator(tileBrush,
                                               new Size(tileBrushImage.PixelWidth, tileBrushImage.PixelHeight), targetSize);

            var intermediate = CreateRenderTarget(
                (int)calc.IntermediateSize.Width,
                (int)calc.IntermediateSize.Height, _dpi);

            paintWrapper.AddDisposable(intermediate);

            using (var context = intermediate.CreateDrawingContext(null))
            {
                var rect = new Rect(0, 0, tileBrushImage.PixelWidth, tileBrushImage.PixelHeight);

                context.Clear(Colors.Transparent);
                context.PushClip(calc.IntermediateClip);
                context.Transform = calc.IntermediateTransform;
                context.DrawImage(RefCountable.CreateUnownedNotClonable(tileBrushImage), 1, rect, rect);
                context.PopClip();
            }

            var tileTransform =
                tileBrush.TileMode != TileMode.None
                    ? SKMatrix.MakeTranslation(-(float)calc.DestinationRect.X, -(float)calc.DestinationRect.Y)
                    : SKMatrix.MakeIdentity();

            SKShaderTileMode tileX =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipX || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;

            SKShaderTileMode tileY =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipY || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;


            var image = intermediate.SnapshotImage();

            paintWrapper.AddDisposable(image);

            using (var shader = image.ToShader(tileX, tileY, tileTransform))
            {
                paintWrapper.Paint.Shader = shader;
            }
        }
Exemplo n.º 25
0
        public static SKPath?GetClipPath(SvgClipPath svgClipPath, SKRect skBounds, HashSet <Uri> uris, CompositeDisposable disposable)
        {
            var skPathClip = default(SKPath);

            var clipPathClipPath = GetClipPathClipPath(svgClipPath, skBounds, uris, disposable);

            if (clipPathClipPath != null && !clipPathClipPath.IsEmpty)
            {
                skPathClip = clipPathClipPath;
            }

            var clipPath = GetClipPath(svgClipPath.Children, skBounds, uris, disposable);

            if (clipPath != null)
            {
                var skMatrix = SKMatrix.MakeIdentity();

                if (svgClipPath.ClipPathUnits == SvgCoordinateUnits.ObjectBoundingBox)
                {
                    var skScaleMatrix = SKMatrix.MakeScale(skBounds.Width, skBounds.Height);
                    SKMatrix.PostConcat(ref skMatrix, ref skScaleMatrix);

                    var skTranslateMatrix = SKMatrix.MakeTranslation(skBounds.Left, skBounds.Top);
                    SKMatrix.PostConcat(ref skMatrix, ref skTranslateMatrix);
                }

                var skTransformsMatrix = SvgTransformsExtensions.ToSKMatrix(svgClipPath.Transforms);
                SKMatrix.PostConcat(ref skMatrix, ref skTransformsMatrix);

                clipPath.Transform(skMatrix);

                if (skPathClip == null)
                {
                    skPathClip = clipPath;
                }
                else
                {
                    var result = skPathClip.Op(clipPath, SKPathOp.Intersect);
                    disposable.Add(result);
                    skPathClip = result;
                }
            }

            if (skPathClip == null)
            {
                skPathClip = new SKPath();
                disposable.Add(skPathClip);
            }

            return(skPathClip);
        }
Exemplo n.º 26
0
        void DrawRotatedWithMatrices(SKCanvas canvas, SKPath path, SKPaint paint, float degrees, int cx, int cy)
        {
            var result     = SKMatrix.MakeIdentity();
            var translate  = SKMatrix.MakeTranslation(-cx, -cy);
            var rotate     = SKMatrix.MakeRotationDegrees(degrees);
            var translate2 = SKMatrix.MakeTranslation(cx, cy);

            SKMatrix.PostConcat(ref result, translate);
            SKMatrix.PostConcat(ref result, rotate);
            SKMatrix.PostConcat(ref result, translate2);

            path.Transform(result);
            canvas.DrawPath(path, paint);
        }
Exemplo n.º 27
0
        private SKMatrix GetScaleMatrix(SKPoint center, float radiusX, float radiusY)
        {
            if (radiusX > radiusY)
            {
                return(SKMatrix.MakeScale(radiusX / radiusY, 1f, center.X, center.Y));
            }

            if (radiusY > radiusX)
            {
                return(SKMatrix.MakeScale(1f, radiusY / radiusX, center.X, center.Y));
            }

            return(SKMatrix.MakeIdentity());
        }
Exemplo n.º 28
0
        public async void Init()
        {
            // reset the matrix for the new sample
            Matrix = SKMatrix.MakeIdentity();

            if (!IsInitialized)
            {
                await OnInit();

                IsInitialized = true;

                Refresh();
            }
        }
Exemplo n.º 29
0
 private void updateBitmap()
 {
     if (renderingMode == RenderingMode.DoubleBuffering)
     {
         bitmapCanvas.SetMatrix(matrix);
         LongUzorGraphic.Draw(bitmapCanvas, canvasView);
         bitmapMatrix = SKMatrix.MakeIdentity();
     }
     else if (renderingMode == RenderingMode.FullDoubleBuffering)
     {
         bitmapCanvas.ResetMatrix();
         LongUzorGraphic.Draw(bitmapCanvas, bitmap.Width, bitmap.Height);
     }
 }
        public SKMatrix OneFingerManipulate(SKPoint prevPoint, SKPoint newPoint, SKPoint pivotPoint)
        {
            if (Mode == TouchManipulationMode.None)
            {
                return(SKMatrix.MakeIdentity());
            }

            SKMatrix touchMatrix = SKMatrix.MakeIdentity();
            SKPoint  delta       = newPoint - prevPoint;

            if (Mode == TouchManipulationMode.ScaleDualRotate)  // One-finger rotation
            {
                SKPoint oldVector = prevPoint - pivotPoint;
                SKPoint newVector = newPoint - pivotPoint;

                float scale = Magnitude(newVector) / Magnitude(oldVector);


                // Avoid rotation if fingers are too close to center
                if (Magnitude(newVector) > 30 && Magnitude(oldVector) > 30)
                {
                    float prevAngle = (float)Math.Atan2(oldVector.Y, oldVector.X);
                    float newAngle  = (float)Math.Atan2(newVector.Y, newVector.X);

                    // Calculate rotation matrix
                    float angle = newAngle - prevAngle;
                    touchMatrix = SKMatrix.MakeRotation(angle, pivotPoint.X, pivotPoint.Y);

                    // Effectively rotate the old vector
                    float magnitudeRatio = Magnitude(oldVector) / Magnitude(newVector);
                    oldVector.X = magnitudeRatio * newVector.X;
                    oldVector.Y = magnitudeRatio * newVector.Y;

                    // Recalculate delta
                    delta = newVector - oldVector;
                }

                if (!float.IsNaN(scale) && !float.IsInfinity(scale))
                {
                    SKMatrix.PostConcat(ref touchMatrix,
                                        SKMatrix.MakeScale(scale, scale, pivotPoint.X, pivotPoint.Y));
                }
            }

            // Multiply the rotation matrix by a translation matrix
            SKMatrix.PostConcat(ref touchMatrix, SKMatrix.MakeTranslation(delta.X, delta.Y));

            return(touchMatrix);
        }