public void TestEmptyBuilderReturnsNull() { var builder = new SKTextBlobBuilder(); var blob = builder.Build(); Assert.Null(blob); }
public void TestExplicitBounds() { var builder = new SKTextBlobBuilder(); var font = new SKPaint(); font.TextEncoding = SKTextEncoding.GlyphId; { var blob = builder.Build(); Assert.Null(blob); } { var r1 = SKRect.Create(10, 10, 20, 20); builder.AllocateRun(font, 16, 0, 0, 0, r1); var blob = builder.Build(); Assert.Equal(r1, blob.Bounds); } { var r1 = SKRect.Create(10, 10, 20, 20); builder.AllocateHorizontalRun(font, 16, 0, 0, r1); var blob = builder.Build(); Assert.Equal(r1, blob.Bounds); } { var r1 = SKRect.Create(10, 10, 20, 20); builder.AllocatePositionedRun(font, 16, 0, r1); var blob = builder.Build(); Assert.Equal(r1, blob.Bounds); } { var r1 = SKRect.Create(10, 10, 20, 20); var r2 = SKRect.Create(15, 20, 50, 50); var r3 = SKRect.Create(0, 5, 10, 5); builder.AllocateRun(font, 16, 0, 0, 0, r1); builder.AllocateHorizontalRun(font, 16, 0, 0, r2); builder.AllocatePositionedRun(font, 16, 0, r3); var blob = builder.Build(); Assert.Equal(SKRect.Create(0, 5, 65, 65), blob.Bounds); } { var blob = builder.Build(); Assert.Null(blob); } }
public void NonGlyphTextEncodingDoesNotThrow() { using var paint = new SKPaint { TextEncoding = SKTextEncoding.Utf16 }; using var builder = new SKTextBlobBuilder(); var run = builder.AllocateRun(paint, 100, 0, 0, 50); Assert.Equal(100, run.GetGlyphSpan().Length); Assert.Equal(50, run.GetTextSpan().Length); using var blob = builder.Build(); Assert.NotNull(blob); Assert.Equal(SKTextEncoding.Utf16, paint.TextEncoding); }
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); }
public void TestImplicitBounds() { var builder = new SKTextBlobBuilder(); var font = new SKPaint(); font.TextSize = 0; var txt = "BOOO"; var glyphs = font.GetGlyphs(txt); font.TextEncoding = SKTextEncoding.GlyphId; builder.AddPositionedRun(font, glyphs, new SKPoint[glyphs.Length]); var blob = builder.Build(); Assert.True(blob.Bounds.IsEmpty); }
public void DrawTextBlobIsTheSameAsDrawText() { var info = new SKImageInfo(300, 300); var text = "SkiaSharp"; byte[] textPixels; using (var bmp = new SKBitmap(info)) using (var canvas = new SKCanvas(bmp)) using (var paint = new SKPaint()) { paint.TextSize = 50; paint.TextAlign = SKTextAlign.Center; canvas.Clear(SKColors.White); canvas.DrawText(text, 150, 175, paint); textPixels = bmp.Bytes; } byte[] glyphsPixels; using (var bmp = new SKBitmap(info)) using (var canvas = new SKCanvas(bmp)) using (var paint = new SKPaint()) { ushort[] glyphs; using (var glyphsp = new SKPaint()) glyphs = glyphsp.GetGlyphs(text); paint.TextSize = 50; paint.TextAlign = SKTextAlign.Center; paint.TextEncoding = SKTextEncoding.GlyphId; canvas.Clear(SKColors.White); using (var builder = new SKTextBlobBuilder()) { builder.AddRun(paint, 0, 0, glyphs); canvas.DrawText(builder.Build(), 150, 175, paint); } glyphsPixels = bmp.Bytes; } Assert.Equal(textPixels, glyphsPixels); }
public unsafe void TestPositionedRunIsBothPointsAndFloats() { var font = new SKPaint(); font.TextEncoding = SKTextEncoding.GlyphId; var builder = new SKTextBlobBuilder(); var run = builder.AllocatePositionedRun(font, 3); var positions = new[] { new SKPoint(1, 2), new SKPoint(3, 4), new SKPoint(5, 6) }; var positionsRaw = new float[] { 1, 2, 3, 4, 5, 6 }; run.SetPositions(positions); var span = run.GetPositionSpan(); Assert.Equal(positions, span.ToArray()); var floats = new float[6]; Marshal.Copy((IntPtr)run.internalBuffer.pos, floats, 0, 6); Assert.Equal(positionsRaw, floats); }
/// <inheritdoc /> public IGlyphRunImpl CreateGlyphRun(GlyphRun glyphRun, out double width) { var count = glyphRun.GlyphIndices.Length; var glyphTypeface = (GlyphTypefaceImpl)glyphRun.GlyphTypeface.PlatformImpl; var typeface = glyphTypeface.Typeface; var paint = new SKPaint { TextSize = (float)glyphRun.FontRenderingEmSize, Typeface = typeface, TextEncoding = SKTextEncoding.GlyphId, IsAntialias = true, IsStroke = false, SubpixelText = true }; using (var textBlobBuilder = new SKTextBlobBuilder()) { var scale = (float)(glyphRun.FontRenderingEmSize / glyphTypeface.DesignEmHeight); if (glyphRun.GlyphOffsets.IsEmpty) { width = 0; var buffer = textBlobBuilder.AllocateHorizontalRun(paint, count, 0); if (!glyphTypeface.IsFixedPitch) { var positions = buffer.GetPositionSpan(); for (var i = 0; i < count; i++) { positions[i] = (float)width; if (glyphRun.GlyphAdvances.IsEmpty) { width += glyphTypeface.GetGlyphAdvance(glyphRun.GlyphIndices[i]) * scale; } else { width += glyphRun.GlyphAdvances[i]; } } } buffer.SetGlyphs(glyphRun.GlyphIndices.Buffer.Span); } else { var buffer = textBlobBuilder.AllocatePositionedRun(paint, count); var glyphPositions = buffer.GetPositionSpan(); var currentX = 0.0; for (var i = 0; i < count; i++) { var glyphOffset = glyphRun.GlyphOffsets[i]; glyphPositions[i] = new SKPoint((float)(currentX + glyphOffset.X), (float)glyphOffset.Y); if (glyphRun.GlyphAdvances.IsEmpty) { currentX += glyphTypeface.GetGlyphAdvance(glyphRun.GlyphIndices[i]) * scale; } else { currentX += glyphRun.GlyphAdvances[i]; } } buffer.SetGlyphs(glyphRun.GlyphIndices.Buffer.Span); width = currentX; } var textBlob = textBlobBuilder.Build(); return(new GlyphRunImpl(paint, textBlob)); } }