Beispiel #1
0
        public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, string text, float x, float y, SKPaint paint)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            if (canvas == null)
            {
                throw new ArgumentNullException(nameof(canvas));
            }
            if (shaper == null)
            {
                throw new ArgumentNullException(nameof(shaper));
            }
            if (paint == null)
            {
                throw new ArgumentNullException(nameof(paint));
            }

            using var font = paint.ToFont();
            font.Typeface  = shaper.Typeface;

            // shape the text
            var result = shaper.Shape(text, x, y, paint);

            // create the text blob
            using var builder = new SKTextBlobBuilder();
            var run = builder.AllocatePositionedRun(font, result.Codepoints.Length);

            // copy the glyphs
            var g = run.GetGlyphSpan();
            var p = run.GetPositionSpan();

            for (var i = 0; i < result.Codepoints.Length; i++)
            {
                g[i] = (ushort)result.Codepoints[i];
                p[i] = result.Points[i];
            }

            // build
            using var textBlob = builder.Build();

            // draw the text
            canvas.DrawText(textBlob, 0, 0, paint);
        }