public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, string text, float x, float y, SKPaint paint) { if (canvas == null) throw new ArgumentNullException(nameof(canvas)); if (shaper == null) throw new ArgumentNullException(nameof(shaper)); if (text == null) throw new ArgumentNullException(nameof(text)); if (paint == null) throw new ArgumentNullException(nameof(paint)); if (string.IsNullOrEmpty(text)) return; // shape the text var result = shaper.Shape(text, x, y, paint); // draw the text using (var paintClone = paint.Clone()) { paintClone.TextEncoding = SKTextEncoding.GlyphId; paintClone.Typeface = shaper.Typeface; var bytes = result.Codepoints.Select(cp => BitConverter.GetBytes((ushort)cp)).SelectMany(b => b).ToArray(); canvas.DrawPositionedText(bytes, result.Points, paintClone); } }
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); }