public void BreakTextReturnsTheCorrectNumberOfBytes(SKTextEncoding encoding, string text, int extectedRead) { var paint = new SKPaint(); paint.TextEncoding = encoding; // get bytes var bytes = encoding == SKTextEncoding.GlyphId ? GetGlyphBytes(text) : StringUtilities.GetEncodedText(text, encoding); var read = paint.BreakText(bytes, 50.0f, out var measured); Assert.Equal(extectedRead, read); Assert.True(measured > 0); byte[] GetGlyphBytes(string text) { var glyphs = paint.GetGlyphs(text); var bytes = new byte[Buffer.ByteLength(glyphs)]; Buffer.BlockCopy(glyphs, 0, bytes, 0, bytes.Length); return(bytes); } }
public bool PrepareLetterDefinitions(string text) { List <char> newChars = new List <char>(); FindNewCharacters(text, ref newChars); if (newChars.Count == 0) { return(false); } var newCharString = new string(newChars.ToArray()); var glyphs = _TextPaint.GetGlyphs(newCharString); var glyphWidths = _TextPaint.GetGlyphWidths(newCharString); for (int i = 0; i < glyphs.Length; i++) { FontLetterDefinition tempDef = new FontLetterDefinition(); if (glyphs[i] == 0) { _LetterDefinitions[newCharString[i]] = tempDef; } else { tempDef.ValidDefinition = true; tempDef.AdvanceX = glyphWidths[i]; _LetterDefinitions[newCharString[i]] = tempDef; } } return(true); }
public void UnicodeGlyphsReturnsTheCorrectNumberOfCharacters() { const string text = "🚀"; var emojiChar = StringUtilities.GetUnicodeCharacterCode(text, SKTextEncoding.Utf32); var typeface = SKFontManager.Default.MatchCharacter(emojiChar); Assert.NotNull(typeface); var paint = new SKPaint(); paint.TextEncoding = SKTextEncoding.Utf32; paint.Typeface = typeface; Assert.Equal(1, paint.CountGlyphs(text)); Assert.Single(paint.GetGlyphs(text)); Assert.NotEqual(0, paint.GetGlyphs(text)[0]); }
public void PlainGlyphsReturnsTheCorrectNumberOfCharacters() { const string text = "Hello World!"; var paint = new SKPaint(); Assert.Equal(text.Length, paint.CountGlyphs(text)); Assert.Equal(text.Length, paint.GetGlyphs(text).Length); }
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); }