Skip to content

Commit

Permalink
Merge pull request #8 from thisisthekap/br_7_replace_int_with_float
Browse files Browse the repository at this point in the history
Use float instead of int to use the full precision SkiaSharp offers, introduced new class TextRendererSk, TextRenderer  is deprecated now.
  • Loading branch information
ryancheung authored Mar 10, 2020
2 parents 3c4a888 + 5fc2e5e commit 8dbc346
Show file tree
Hide file tree
Showing 5 changed files with 869 additions and 800 deletions.
81 changes: 40 additions & 41 deletions SkiaTextRenderer.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Drawing;
using System.IO;
using SkiaSharp;

Expand Down Expand Up @@ -32,15 +31,15 @@ static void TestDraw(string text, float fontSize, TextFormatFlags flags)

var fileName = CleanFileName($"{text}-{fontSize}-{flags}.png");

var size = TextRenderer.MeasureText(text, font, 0, flags);
var size = TextRendererSk.MeasureText(text, font, 0, flags);
var BackColour = SKColors.Black;

using (SKBitmap bitmap = new SKBitmap(size.Width, size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (SKBitmap bitmap = new SKBitmap((int) size.Width, (int) size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(BackColour);

TextRenderer.DrawText(canvas, text, font, new Rectangle(0, 0, size.Width, size.Height), SKColors.White, flags);
TextRendererSk.DrawText(canvas, text, font, SKRect.Create(0, 0, size.Width, size.Height), SKColors.White, flags);

using (Stream s = File.Open(fileName, FileMode.Create))
{
Expand All @@ -62,15 +61,15 @@ static void TestDrawItalic(string text, float fontSize, TextFormatFlags flags)
fileName = fileName.Replace(c, '_');
}

var size = TextRenderer.MeasureText(text, font, 0, flags);
var size = TextRendererSk.MeasureText(text, font, 0, flags);
var BackColour = SKColors.Black;

using (SKBitmap bitmap = new SKBitmap(size.Width, size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (SKBitmap bitmap = new SKBitmap((int) size.Width, (int) size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(BackColour);

TextRenderer.DrawText(canvas, text, font, new Rectangle(0, 0, size.Width, size.Height), SKColors.White, flags);
TextRendererSk.DrawText(canvas, text, font, SKRect.Create(0, 0, size.Width, size.Height), SKColors.White, flags);

using (Stream s = File.Open(fileName, FileMode.Create))
{
Expand All @@ -92,15 +91,15 @@ static void TestDrawUnderline(string text, float fontSize, TextFormatFlags flags
fileName = fileName.Replace(c, '_');
}

var size = TextRenderer.MeasureText(text, font, 0, flags);
var size = TextRendererSk.MeasureText(text, font, 0, flags);
var BackColour = SKColors.Black;

using (SKBitmap bitmap = new SKBitmap(size.Width, size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (SKBitmap bitmap = new SKBitmap((int) size.Width, (int) size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(BackColour);

TextRenderer.DrawText(canvas, text, font, new Rectangle(0, -1, size.Width, size.Height), SKColors.White, flags);
TextRendererSk.DrawText(canvas, text, font, SKRect.Create(0, -1, size.Width, size.Height), SKColors.White, flags);

using (Stream s = File.Open(fileName, FileMode.Create))
{
Expand All @@ -122,15 +121,15 @@ static void TestDrawStrikeThrough(string text, float fontSize, TextFormatFlags f
fileName = fileName.Replace(c, '_');
}

var size = TextRenderer.MeasureText(text, font, 0, flags);
var size = TextRendererSk.MeasureText(text, font, 0, flags);
var BackColour = SKColors.Black;

using (SKBitmap bitmap = new SKBitmap(size.Width, size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (SKBitmap bitmap = new SKBitmap((int) size.Width, (int) size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(BackColour);

TextRenderer.DrawText(canvas, text, font, new Rectangle(0, 0, size.Width, size.Height), SKColors.White, flags);
TextRendererSk.DrawText(canvas, text, font, SKRect.Create(0, 0, size.Width, size.Height), SKColors.White, flags);

using (Stream s = File.Open(fileName, FileMode.Create))
{
Expand All @@ -142,7 +141,7 @@ static void TestDrawStrikeThrough(string text, float fontSize, TextFormatFlags f
Console.WriteLine("Drawing StrikeThrough {0} (fontSize {1}, flags {2}), measured size: {3}", text, fontSize, flags, size);
}

static void TestDrawWithSize(string text, float fontSize, TextFormatFlags flags, Size size)
static void TestDrawWithSize(string text, float fontSize, TextFormatFlags flags, SKSize size)
{
var font = new Font(Typeface, fontSize);

Expand All @@ -154,12 +153,12 @@ static void TestDrawWithSize(string text, float fontSize, TextFormatFlags flags,

var BackColour = SKColors.Black;

using (SKBitmap bitmap = new SKBitmap(size.Width, size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (SKBitmap bitmap = new SKBitmap((int) size.Width, (int) size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(BackColour);

TextRenderer.DrawText(canvas, text, font, new Rectangle(0, 0, size.Width, size.Height), SKColors.White, flags);
TextRendererSk.DrawText(canvas, text, font, SKRect.Create(0, 0, size.Width, size.Height), SKColors.White, flags);

using (Stream s = File.Open(fileName, FileMode.Create))
{
Expand All @@ -177,15 +176,15 @@ static void TestDrawMultiline(string text, float fontSize, TextFormatFlags flags

var fileName = CleanFileName($"multiline-{text}-{fontSize}-{flags}.png");

var size = TextRenderer.MeasureText(text, font, 0, flags);
var size = TextRendererSk.MeasureText(text, font, 0, flags);
var BackColour = SKColors.Black;

using (SKBitmap bitmap = new SKBitmap(size.Width, size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (SKBitmap bitmap = new SKBitmap((int) size.Width, (int) size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(BackColour);

TextRenderer.DrawText(canvas, text, font, new Rectangle(0, 0, size.Width, size.Height), SKColors.White, flags);
TextRendererSk.DrawText(canvas, text, font, SKRect.Create(0, 0, size.Width, size.Height), SKColors.White, flags);

using (Stream s = File.Open(fileName, FileMode.Create))
{
Expand All @@ -203,15 +202,15 @@ static void TestDrawCursor(string text, float fontSize, TextFormatFlags flags, i

var fileName = CleanFileName($"DrawCursor-cursor({cursorPosition})-{text}-{fontSize}-{flags}.png");

var size = TextRenderer.MeasureText(text, font, 0, flags);
var size = TextRendererSk.MeasureText(text, font, 0, flags);
var BackColour = SKColors.Black;

using (SKBitmap bitmap = new SKBitmap(size.Width, size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (SKBitmap bitmap = new SKBitmap((int) size.Width, (int) size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(BackColour);

TextRenderer.DrawText(canvas, text, font, new Rectangle(0, 0, size.Width, size.Height), SKColors.White, flags, cursorPosition);
TextRendererSk.DrawText(canvas, text, font, SKRect.Create(0, 0, size.Width, size.Height), SKColors.White, flags, cursorPosition);

using (Stream s = File.Open(fileName, FileMode.Create))
{
Expand All @@ -223,20 +222,20 @@ static void TestDrawCursor(string text, float fontSize, TextFormatFlags flags, i
Console.WriteLine("Drawing with cursor {0} (fontSize {1}, flags {2}), measured size: {3}", text, fontSize, flags, size);
}

static void TestDrawCursorMultiline(string text, float fontSize, TextFormatFlags flags, Size size, int cursorPosition)
static void TestDrawCursorMultiline(string text, float fontSize, TextFormatFlags flags, SKSize size, int cursorPosition)
{
var font = new Font(Typeface, fontSize);

var fileName = CleanFileName($"DrawCursor-sized-cursor({cursorPosition})-{text}-{fontSize}-{flags}.png");

var BackColour = SKColors.Black;

using (SKBitmap bitmap = new SKBitmap(size.Width, size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (SKBitmap bitmap = new SKBitmap((int) size.Width, (int) size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(BackColour);

TextRenderer.DrawText(canvas, text, font, new Rectangle(0, 0, size.Width, size.Height), SKColors.White, flags, cursorPosition);
TextRendererSk.DrawText(canvas, text, font, SKRect.Create(0, 0, size.Width, size.Height), SKColors.White, flags, cursorPosition);

using (Stream s = File.Open(fileName, FileMode.Create))
{
Expand All @@ -254,15 +253,15 @@ static void TestDrawSelection(string text, float fontSize, TextFormatFlags flags

var fileName = CleanFileName($"DrawSelection-start({options.SelectionStart})-end({options.SelectionEnd})-{text}-{fontSize}-{flags}.png");

var size = TextRenderer.MeasureText(text, font, 0, flags);
var size = TextRendererSk.MeasureText(text, font, 0, flags);
var BackColour = SKColors.Black;

using (SKBitmap bitmap = new SKBitmap(size.Width, size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (SKBitmap bitmap = new SKBitmap((int) size.Width, (int) size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(BackColour);

TextRenderer.DrawText(canvas, text, font, new Rectangle(0, 0, size.Width, size.Height), SKColors.White, flags, options);
TextRendererSk.DrawText(canvas, text, font, SKRect.Create(0, 0, size.Width, size.Height), SKColors.White, flags, options);

using (Stream s = File.Open(fileName, FileMode.Create))
{
Expand All @@ -280,15 +279,15 @@ static void TestDrawSelectionMultiline(string text, float fontSize, TextFormatFl

var fileName = CleanFileName($"DrawSelectionMultiline-start({options.SelectionStart})-end({options.SelectionEnd})-{text}-{fontSize}-{flags}.png");

var size = TextRenderer.MeasureText(text, font, 0, flags);
var size = TextRendererSk.MeasureText(text, font, 0, flags);
var BackColour = SKColors.Black;

using (SKBitmap bitmap = new SKBitmap(size.Width, size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (SKBitmap bitmap = new SKBitmap((int) size.Width, (int) size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul))
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(BackColour);

TextRenderer.DrawText(canvas, text, font, new Rectangle(0, 0, size.Width, size.Height), SKColors.White, flags, options);
TextRendererSk.DrawText(canvas, text, font, SKRect.Create(0, 0, size.Width, size.Height), SKColors.White, flags, options);

using (Stream s = File.Open(fileName, FileMode.Create))
{
Expand All @@ -313,11 +312,11 @@ static void Main(string[] args)
TestDrawUnderline("Hello 你好 world!", 20, TextFormatFlags.Default);
TestDrawStrikeThrough("Hello 你好 world!", 20, TextFormatFlags.Default);

TestDrawWithSize("Hello 你好 world!", 12, TextFormatFlags.Default, new Size(200, 80));
TestDrawWithSize("Hello 你好 world!", 12, TextFormatFlags.VerticalCenter, new Size(100, 80));
TestDrawWithSize("Hello 你好 world!", 12, TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new Size(80, 80));
TestDrawWithSize("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new Size(80, 80));
TestDrawWithSize("Hel\nl\r\no 你\n好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine, new Size(80, 20));
TestDrawWithSize("Hello 你好 world!", 12, TextFormatFlags.Default, new SKSize(200, 80));
TestDrawWithSize("Hello 你好 world!", 12, TextFormatFlags.VerticalCenter, new SKSize(100, 80));
TestDrawWithSize("Hello 你好 world!", 12, TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new SKSize(80, 80));
TestDrawWithSize("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new SKSize(80, 80));
TestDrawWithSize("Hel\nl\r\no 你\n好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine, new SKSize(80, 20));

TestDrawMultiline("Hello 你\n好 world!", 20, TextFormatFlags.Default);

Expand All @@ -327,11 +326,11 @@ static void Main(string[] args)
TestDrawCursor("Hello 你好 world!", 20, TextFormatFlags.Default, 6);
TestDrawCursor("Hello 你好 world!", 20, TextFormatFlags.Default, 14);

TestDrawCursorMultiline("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new Size(80, 80), -1);
TestDrawCursorMultiline("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new Size(80, 80), 2);
TestDrawCursorMultiline("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new Size(80, 80), 6);
TestDrawCursorMultiline("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new Size(80, 80), 10);
TestDrawCursorMultiline("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new Size(80, 80), 14);
TestDrawCursorMultiline("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new SKSize(80, 80), -1);
TestDrawCursorMultiline("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new SKSize(80, 80), 2);
TestDrawCursorMultiline("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new SKSize(80, 80), 6);
TestDrawCursorMultiline("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new SKSize(80, 80), 10);
TestDrawCursorMultiline("Hello 你好 world!", 12, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak, new SKSize(80, 80), 14);

TestDrawSelection("Hello 你好 world!", 20, TextFormatFlags.Default, new TextPaintOptions() { SelectionStart = 0, SelectionEnd = 2 });
TestDrawSelection("Hello 你好 world!", 20, TextFormatFlags.Default, new TextPaintOptions() { SelectionStart = 0, SelectionEnd = 15 });
Expand Down
Loading

0 comments on commit 8dbc346

Please sign in to comment.