예제 #1
0
        /// <summary>
        /// Draw an ink stroke
        /// </summary>
        /// <param name="canvas">the canvas</param>
        /// <param name="stroke">the stroke</param>
        /// <param name="useCache">true to use cached data</param>
        public static void Draw(this SKCanvas canvas, XInkStroke stroke, bool useCache)
        {
            _ = canvas ?? throw new ArgumentNullException(nameof(canvas));
            _ = stroke ?? throw new ArgumentNullException(nameof(stroke));

            var inkPoints = stroke.GetInkPoints();

            if (useCache)
            {
                if (inkPoints.Count == 1)
                {
                    if (stroke.Paint == null)
                    {
                        var paintResult = stroke.CreatePaint(SKPaintStyle.Fill);

                        stroke.Paint = paintResult.Item1;

                        stroke.Resources = paintResult.Item2;
                    }

                    canvas.DrawCircle(
                        stroke.GetInkPoints()[0].Position.ToSKPoint(),
                        stroke.DrawingAttributes.Size / 4.0f,
                        stroke.Paint);
                }
                else
                {
                    stroke.Path ??= stroke.DrawingAttributes.IgnorePressure
                        ? stroke.CreatePath()
                        : stroke.CreateVariableThicknessPath();

                    if (stroke.Paint == null)
                    {
                        var paintResult = stroke.CreatePaint();
                        stroke.Resources = paintResult.Item2;
                        stroke.Paint     = paintResult.Item1;
                    }

                    if (stroke.Path != null)
                    {
                        canvas.DrawPath(stroke.Path, stroke.Paint);
                    }
                }
            }
            else
            {
                if (inkPoints.Count == 1)
                {
                    var paintResult = stroke.CreatePaint(SKPaintStyle.Fill);

                    using (var paint = paintResult.Item1)
                    {
                        paint.IsStroke = false;

                        var radius = stroke.DrawingAttributes.Size / 4.0f;

                        //System.Diagnostics.Debug.WriteLine($"Drawing dot radius {radius}");
                        canvas.DrawCircle(
                            inkPoints[0].Position.ToSKPoint(),
                            radius,
                            paint);
                    }

                    if (paintResult.Item2 != null)
                    {
                        foreach (var item in paintResult.Item2)
                        {
                            item.Dispose();
                        }
                    }
                }
                else
                {
                    using var path = stroke.DrawingAttributes.IgnorePressure ? stroke.CreatePath() : stroke.CreateVariableThicknessPath();
                    if (path == null)
                    {
                        return;
                    }

                    var paintResult = stroke.CreatePaint();

                    using (var paint = paintResult.Item1)
                    {
                        canvas.DrawPath(path, paint);
                    }

                    if (paintResult.Item2 != null)
                    {
                        foreach (var item in paintResult.Item2)
                        {
                            item.Dispose();
                        }
                    }
                }
            }
        }