Esempio n. 1
0
 public static void Draw(this SKPicture skPicture, SKColor background, float scaleX, float scaleY, SKCanvas skCanvas)
 {
     skCanvas.DrawColor(background);
     skCanvas.Save();
     skCanvas.Scale(scaleX, scaleY);
     skCanvas.DrawPicture(skPicture);
     skCanvas.Restore();
 }
 public void DrawPicture(SKPicture picture, ref SKMatrix matrix, SKPaint paint = null)
 {
     canvas.DrawPicture(picture, ref matrix, paint);
     if (calculateBounds)
     {
         displayObject.addBoundingRect(matrix.TransX, matrix.TransY, picture.CullRect.Width, picture.CullRect.Height);
     }
 }
Esempio n. 3
0
 private void DrawPicture(SKCanvas canvas, SKPicture picture, double dx, double dy, double zx, double zy)
 {
     canvas.Save();
     canvas.Translate((float)dx, (float)dy);
     canvas.Scale((float)zx, (float)zy);
     canvas.DrawPicture(picture);
     canvas.Restore();
 }
 public void DrawPicture(SKPicture picture, SKPaint paint = null)
 {
     canvas.DrawPicture(picture, paint);
     if (calculateBounds)
     {
         displayObject.addBoundingRect(picture.CullRect);
     }
 }
        public SKPicture EndRecording()
        {
            var picture = new SKPicture(CullRect, RecordingCanvas?.Commands);

            CullRect        = SKRect.Empty;
            RecordingCanvas = null;

            return(picture);
        }
        public SKSvgElement(Stream resourceStream)
        {
            _svg        = new SKSvg();
            _skPicuture = _svg.Load(resourceStream);

            var bounds = _svg.Picture.CullRect;

            Limites = new Retangulo(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom);
        }
        public ConcernBodySelection(ConcernItem concernSelected)
        {
            InitializeComponent();

            DrawData.Init();
            SetConcernCriteria(concernSelected);
            pictureFront = DrawData.Front_SVG;
            pictureBack  = DrawData.Back_SVG;
        }
Esempio n. 8
0
 public void Render(SKCanvas canvas, float angleInRadians, SKPoint center, float scale)
 {
     if (_svgScene == null)
     {
         _svgScene = LoadScene();
     }
     canvas.Clear(SKColors.White);
     canvas.DrawPicture(_svgScene);
 }
Esempio n. 9
0
        public static RasterCacheResult RasterizePicture(SKPicture picture, GRContext context, SKMatrix ctm, SKColorSpace dst_color_space, bool checkerboard)
        {
            TRACE_EVENT0("flutter", "RasterCachePopulate");

            return(Rasterize(context, ctm, dst_color_space, checkerboard, picture.CullRect, (SKCanvas canvas) =>
            {
                canvas.DrawPicture(picture);
            }));
        }
Esempio n. 10
0
        private void SkCanvas_OnPaintSurface(object sender, SKPaintGLSurfaceEventArgs e)
        {
            SKImageInfo info = new SKImageInfo(e.BackendRenderTarget.Width, e.BackendRenderTarget.Height, e.ColorType);

            SKSurface surface       = e.Surface;
            SKCanvas  surfaceCanvas = surface.Canvas;

            surfaceCanvas.Clear();

            surfaceCanvas.SetMatrix(_canvasMatrix);


            if (_picture == null)
            {
                using (SKPictureRecorder pictureRecorder = new SKPictureRecorder())
                    using (SKCanvas canvas = pictureRecorder.BeginRecording(info.Rect))
                        using (SKPath path = new SKPath())
                        {
                            /*
                             * Draw path with multiple segments(>=3).
                             * Then every time you translate the surfaceCanvas, it consumes more GPU memory.
                             */
                            path.CubicTo(150, 50, 200, 125, 300, 25);

                            // path.LineTo(80, 125);
                            // path.LineTo(130, 75);
                            // path.LineTo(200, 205);


                            canvas.DrawPath(path, _paint);

                            // Fine with DrawRoundRect or DrawText
                            // canvas.DrawText("TEXT", new SKPoint(50, 50), _paint);
                            // canvas.DrawRoundRect(120,120,300,220,12,12,paint);

                            _picture = pictureRecorder.EndRecording();
                        }
            }


            surfaceCanvas.DrawPicture(_picture);

            /*
             * Directly drawing on surfaceCanvas is fine.
             */
            // using (SKPath path = new SKPath())
            // {
            //     path.CubicTo(150, 50, 200, 125, 300, 25);
            //
            //     path.LineTo(80, 125);
            //     path.LineTo(130, 75);
            //     path.LineTo(200, 205);
            //
            //     surfaceCanvas.DrawPath(path, _paint);
            // }
        }
Esempio n. 11
0
 public static bool ToImage(this SKPicture skPicture, Stream stream, SKColor background, SKEncodedImageFormat format, int quality, float scaleX, float scaleY, SKColorType skColorType, SKAlphaType skAlphaType, SKColorSpace skColorSpace)
 {
     using var skBitmap = skPicture.ToBitmap(background, scaleX, scaleY, skColorType, skAlphaType, skColorSpace);
     if (skBitmap is null)
     {
         return(false);
     }
     using var skImage = SKImage.FromBitmap(skBitmap);
     using var skData  = skImage.Encode(format, quality);
     if (skData is { })
Esempio n. 12
0
        public static SKMatrix GetAreaSvgScaledMatrix(int width, int height, SKPicture picture, BodyArea areaUsed)
        {
            float imageScale = (height / picture.CullRect.Height) * 4.0f;
            float tY         = (float)GetOffsetCalculations(areaUsed, height);

            float axisScale = picture.CullRect.Width * imageScale;
            float xPosition = (width - axisScale) / 2;

            return(SKMatrix.CreateScaleTranslation(imageScale, imageScale, xPosition, tY));
        }
Esempio n. 13
0
        public void Setup(SKCanvas canvas, double scale)
        {
            if (!Enabled)
            {
                return;
            }
            strokes.Clear();
            // try to get roughly 15 vertical lines in portrait, rounding to the nearest 10 pixel
            //			if (Config.GridSize < 0)
            {
                var theLength = Math.Min(canvas.ClipBounds.Width, canvas.ClipBounds.Height);
                Config.GridSize = ((((int)theLength / 15) + 5) / 10) * 10;
            }
            var baseStroke = new Stroke {
                Size = LineWidth * scale, ToolSettings = new ToolSettings {
                    SelectedColor = LineColor
                }
            };
            int counter = 0;

            do
            {
                var stroke = new Stroke(baseStroke);
                stroke.Points.Add(new Point {
                    X = counter * Config.GridSize, Y = 0
                });
                stroke.Points.Add(new Point {
                    X = counter * Config.GridSize, Y = canvas.ClipBounds.Height
                });
                strokes.Add(stroke);
                counter++;
            } while ((counter * Config.GridSize) < canvas.ClipBounds.Width);
            counter = 0;
            do
            {
                var stroke = new Stroke(baseStroke);
                stroke.Points.Add(new Point {
                    X = 0, Y = counter * Config.GridSize
                });
                stroke.Points.Add(new Point {
                    X = canvas.ClipBounds.Width, Y = counter * Config.GridSize
                });
                strokes.Add(stroke);
                counter++;
            } while (counter * Config.GridSize < canvas.ClipBounds.Height);
            // Drawing the grid to a picture.
            // A tad faster than drawing directly on the canvas. No big impact though
            using (var recorder = new SKPictureRecorder())
            {
                recorder.BeginRecording(canvas.ClipBounds);
                DrawBackbuffer(recorder.RecordingCanvas);
                _gridPicture = recorder.EndRecording();
            }
        }
Esempio n. 14
0
        public static RasterCacheResult RasterizePicture(SKPicture picture, GRContext context, SKMatrix ctm, SKColorSpace dst_color_space, bool checkerboard)
        {
            TRACE_EVENT0("flutter", "RasterCachePopulate");

            //C++ TO C# CONVERTER TODO TASK: Only lambda expressions having all locals passed by reference can be converted to C#:
            //ORIGINAL LINE: return Rasterize(context, ctm, dst_color_space, checkerboard, picture->cullRect(), [=](SKCanvas* canvas)
            return(Rasterize(context, ctm, dst_color_space, checkerboard, picture.CullRect, (SKCanvas canvas) =>
            {
                canvas.DrawPicture(picture);
            }));
        }
Esempio n. 15
0
        public ConcernBodyAnnotation(SKColor colorOfBrushToUse, string brushType, bool useFrontOfBodyImage, BodyArea areaSelection)
        {
            InitializeComponent();

            brushColourToBeUsed = colorOfBrushToUse;
            brushName           = brushType;
            areaUsed            = areaSelection;
            isFrontBody         = useFrontOfBodyImage;
            picture             = isFrontBody ? DrawData.Front_SVG : DrawData.Back_SVG;
            brushTypeText       = brushType;
            txtSubject.Text     = brushType;
        }
Esempio n. 16
0
        public void CanSerializeAndDeserializePicture()
        {
            using var recorder = new SKPictureRecorder();
            using var canvas   = recorder.BeginRecording(SKRect.Create(0, 0, 40, 40));
            using var picture  = recorder.EndRecording();

            using var data = picture.Serialize();

            using var deserialized = SKPicture.Deserialize(data);

            Assert.NotNull(deserialized);
        }
Esempio n. 17
0
        public static bool ToSvg(this SKPicture skPicture, string path, SKColor background, float scaleX, float scaleY)
        {
            float width  = skPicture.CullRect.Width * scaleX;
            float height = skPicture.CullRect.Height * scaleY;

            if (width <= 0 || height <= 0)
            {
                return(false);
            }
            using var skFileWStream = new SKFileWStream(path);
            using var skCanvas      = SKSvgCanvas.Create(SKRect.Create(0, 0, width, height), skFileWStream);
            Draw(skPicture, background, scaleX, scaleY, skCanvas);
            return(true);
        }
Esempio n. 18
0
        public void CanDeserializeFromData()
        {
            using var picture = CreateTestPicture();
            using var data    = picture.Serialize();

            using var bmp = new SKBitmap(40, 40);
            using var cnv = new SKCanvas(bmp);

            using var deserialized = SKPicture.Deserialize(data);
            Assert.Equal(SKRect.Create(0, 0, 40, 40), deserialized.CullRect);
            cnv.DrawPicture(deserialized);

            ValidateTestBitmap(bmp);
        }
Esempio n. 19
0
        /// Adds a [Picture] to the scene.
        ///
        /// The picture is rasterized at the given offset.
        public void addPicture(Offset offset, SKPicture picture, bool isComplexHint = false, bool willChangeHint = false)
        {
            int hints = 0;

            if (isComplexHint)
            {
                hints |= 1;
            }
            if (willChangeHint)
            {
                hints |= 2;
            }
            _addPicture(offset.dx, offset.dy, picture, hints);
        }
Esempio n. 20
0
        public void Dispose()
        {
            _previousZX = double.NaN;
            _previousZY = double.NaN;

            if (_skiaRenderer != null)
            {
                _skiaRenderer.Dispose();
            }

            if (_paintCache != null)
            {
                foreach (var cache in _paintCache)
                {
                    cache.Value.Dispose();
                }
                _paintCache = null;
            }

            if (_picturePoints != null)
            {
                _picturePoints.Dispose();
                _picturePoints = null;
            }

            if (_pictureDecorators != null)
            {
                _pictureDecorators.Dispose();
                _pictureDecorators = null;
            }

            if (_pictureShapesWorking != null)
            {
                _pictureShapesWorking.Dispose();
                _pictureShapesWorking = null;
            }

            if (_pictureShapesCurrent != null)
            {
                _pictureShapesCurrent.Dispose();
                _pictureShapesCurrent = null;
            }

            if (_disposable != null)
            {
                _disposable.Dispose();
                _disposable = null;
            }
        }
Esempio n. 21
0
        public SKPicture GetPicture()
        {
            if (picture != null)
            {
                return(picture);
            }
            var box = Box;

            using (var recorder = new SKPictureRecorder()) // SKBitmap((int)box.Width, (int)box.Height);
                using (var canvas = recorder.BeginRecording(SKRect.Create(box.Width, box.Height)))
                {
                    Render(canvas, box.Size, false);
                    return(picture = recorder.EndRecording());
                }
        }
        private SKSurface RasterizeSegment(SKPicture segment, RasterizeDimensions dim, float skewAngle, params SKPaint[] paints)
        {
            if (dim.SvgWidth <= 0 || dim.SvgWidth <= 0)
            {
                return(null);
            }
            var surface = SKSurface.Create(dim.SvgInfo);

            surface.Canvas.Translate(dim.Translate.X, dim.Translate.Y);
            Skew(surface.Canvas, skewAngle, 0);
            foreach (var paint in paints)
            {
                surface.Canvas.DrawPicture(segment, ref dim.SvgMatrix, paint);
            }
            return(surface);
        }
Esempio n. 23
0
        public static bool ToPdf(this SKPicture skPicture, string path, SKColor background, float scaleX, float scaleY)
        {
            float width  = skPicture.CullRect.Width * scaleX;
            float height = skPicture.CullRect.Height * scaleY;

            if (width <= 0 || height <= 0)
            {
                return(false);
            }
            using var skFileWStream = new SKFileWStream(path);
            using var skDocument    = SKDocument.CreatePdf(skFileWStream, SKDocument.DefaultRasterDpi);
            using var skCanvas      = skDocument.BeginPage(width, height);
            Draw(skPicture, background, scaleX, scaleY, skCanvas);
            skDocument.Close();
            return(true);
        }
Esempio n. 24
0
        public static SKBitmap?ToBitmap(this SKPicture skPicture, SKColor background, float scaleX, float scaleY, SKColorType skColorType, SKAlphaType skAlphaType, SKColorSpace skColorSpace)
        {
            var width  = skPicture.CullRect.Width * scaleX;
            var height = skPicture.CullRect.Height * scaleY;

            if (!(width > 0) || !(height > 0))
            {
                return(null);
            }
            var skImageInfo = new SKImageInfo((int)width, (int)height, skColorType, skAlphaType, skColorSpace);
            var skBitmap    = new SKBitmap(skImageInfo);

            using var skCanvas = new SKCanvas(skBitmap);
            Draw(skPicture, background, scaleX, scaleY, skCanvas);
            return(skBitmap);
        }
Esempio n. 25
0
        }         // SVGPlan constructor

        /// <summary>
        /// This function reads the plan from an SVG file.  It is a helper function for the Plan constructor.
        /// </summary>
        /// <param name="filePath">Full path incl. filename</param>
        /// <returns></returns>
        private SKPicture loadPlan(string filePath)
        {
            SKPicture LoadedPlan = null;

            try
            {
                LoadedPlan = SVGCanvas.Load(filePath);
            }
            catch (Exception ex)
            {
                string errorString = "Error loading SVG plan! ";
                LogFile.WriteError(errorString + ex.Message);
                throw new Exception(errorString, ex);
            }

            return(LoadedPlan);
        }         // loadPlan()
Esempio n. 26
0
        public static string Generate(SKPicture picture, string namespaceName, string className)
        {
            var counter = new SkiaCSharpCodeGenCounter();

            var sb = new StringBuilder();

            sb.AppendLine("// <auto-generated />");
            sb.AppendLine($"");
            sb.AppendLine($"namespace {namespaceName}");
            sb.AppendLine($"{{");
            sb.AppendLine($"    using System;");
            sb.AppendLine($"    using SkiaSharp;");
            sb.AppendLine($"");
            sb.AppendLine($"    public static class {className}");
            sb.AppendLine($"    {{");
            sb.AppendLine($"        public static SKPicture Picture {{ get; }}");
            sb.AppendLine($"");
            sb.AppendLine($"        static {className}()");
            sb.AppendLine($"        {{");
            sb.AppendLine($"            Picture = Record();");
            sb.AppendLine($"        }}");
            sb.AppendLine($"");
            sb.AppendLine($"        private static SKPicture Record()");
            sb.AppendLine($"        {{");

            var indent = "            ";

            var counterPicture = ++counter.Picture;

            picture.ToSKPicture(counter, sb, indent);

            sb.AppendLine($"{indent}return {counter.PictureVarName}{counterPicture};");

            sb.AppendLine($"        }}");
            sb.AppendLine($"");
            sb.AppendLine($"        public static void Draw(SKCanvas {counter.CanvasVarName})");
            sb.AppendLine($"        {{");
            sb.AppendLine($"            {counter.CanvasVarName}.DrawPicture(Picture);");
            sb.AppendLine($"        }}");
            sb.AppendLine($"    }}");
            sb.AppendLine($"}}");

            var code = sb.ToString();

            return(code);
        }
Esempio n. 27
0
        /// <summary>
        /// The Plan constructor loads the plan from file and figures out its width and height.
        /// It also saves various variables which are needed later for displaying the plan.
        /// The parent constructor is called with the canvasView. canvasView is not required in
        /// this derived class.
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="canvasView"></param>
        /// <param name="planPathName"></param>
        internal SVGPlan(AppCompatActivity activity, SKCanvasView canvasView, string planPathName) : base(activity, canvasView)
        {
#if STOPWATCH
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
#endif
            // Create the SVG resource
            SVGCanvas = new SkiaSharp.Extended.Svg.SKSvg();

            // Check if SVG file exists. This should already have been done before getting here, but just in case...
            Java.IO.File planFile = new Java.IO.File(planPathName);
            if (planFile.Exists())
            {
                // Load the plan
                planSKPicture = loadPlan(planPathName);
                if (planSKPicture != null)
                {
                    drawingWidth  = planSKPicture.CullRect.Width;
                    drawingHeight = planSKPicture.CullRect.Height;
                    LogFile.WriteInfo(System.String.Format("planSKPicture size: {0} x {1}", drawingWidth, drawingHeight));

#if STOPWATCH
                    stopwatch.Stop();
                    LogFile.WriteInfo(System.String.Format("Path.LoadPlan(): {0}ms", stopwatch.ElapsedMilliseconds));
                    stopwatch.Restart();
#endif
                }                 // if planSKPicture!=null
                else
                {
                    // Do nothing. LoadPlan() throws an exception if plan can't be loaded.
                    drawingWidth  = 0;
                    drawingHeight = 0;
                }
#if STOPWATCH
                stopwatch.Stop();
                LogFile.WriteInfo(System.String.Format("Plan() - completion: {0}ms", stopwatch.ElapsedMilliseconds));
#endif
            }             // if (file.exists)
            else
            {
                String errorString = "File " + planPathName + " does not exist. ";
                LogFile.WriteError(errorString);
                throw new FileNotFoundException(errorString);
            }
        }         // SVGPlan constructor
        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);
        }
Esempio n. 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);
        }
        //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);
        }