static void TestDrawUnderline(string text, float fontSize, TextFormatFlags flags) { var font = new Font(Typeface, fontSize, FontStyle.Underline); var fileName = CleanFileName($"underline-{text}-{fontSize}-{flags}.png"); foreach (char c in System.IO.Path.GetInvalidFileNameChars()) { fileName = fileName.Replace(c, '_'); } var size = TextRendererSk.MeasureText(text, font, 0, flags); var BackColour = SKColors.Black; using (SKBitmap bitmap = new SKBitmap((int)size.Width, (int)size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul)) using (var canvas = new SKCanvas(bitmap)) { canvas.Clear(BackColour); TextRendererSk.DrawText(canvas, text, font, SKRect.Create(0, -1, size.Width, size.Height), SKColors.White, flags); using (Stream s = File.Open(fileName, FileMode.Create)) { SKData d = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100); d.SaveTo(s); } } Console.WriteLine("Drawing Underline {0} (fontSize {1}, flags {2}), measured size: {3}", text, fontSize, flags, size); }
static void TestDrawSelectionMultiline(string text, float fontSize, TextFormatFlags flags, TextPaintOptions options) { var font = new Font(Typeface, fontSize); var fileName = CleanFileName($"DrawSelectionMultiline-start({options.SelectionStart})-end({options.SelectionEnd})-{text}-{fontSize}-{flags}.png"); var size = TextRendererSk.MeasureText(text, font, 0, flags); var BackColour = SKColors.Black; using (SKBitmap bitmap = new SKBitmap((int)size.Width, (int)size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul)) using (var canvas = new SKCanvas(bitmap)) { canvas.Clear(BackColour); 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)) { SKData d = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100); d.SaveTo(s); } } Console.WriteLine("Drawing with selection and multiline {0} (fontSize {1}, flags {2}), measured size: {3}", text, fontSize, flags, size); }
public void TestGetCursorFromPoint() { var font = new Font(Typeface, 20); var flags = TextFormatFlags.Default; var text = "Hello 你好 world!"; var size = TextRendererSk.MeasureText(text, font, 0, flags); var point = new SKPoint(0, 0); var cursor = TextRendererSk.GetCursorFromPoint(text, font, SKRect.Create(0, 0, size.Width, size.Height), flags, point); Assert.AreEqual(0, cursor); point = new SKPoint(6, 10); cursor = TextRendererSk.GetCursorFromPoint(text, font, SKRect.Create(0, 0, size.Width, size.Height), flags, point); Assert.AreEqual(0, cursor); point = new SKPoint(12, 17); cursor = TextRendererSk.GetCursorFromPoint(text, font, SKRect.Create(0, 0, size.Width, size.Height), flags, point); Assert.AreEqual(1, cursor); point = new SKPoint(23, 17); cursor = TextRendererSk.GetCursorFromPoint(text, font, SKRect.Create(0, 0, size.Width, size.Height), flags, point); Assert.AreEqual(2, cursor); point = new SKPoint(81, 9); cursor = TextRendererSk.GetCursorFromPoint(text, font, SKRect.Create(0, 0, size.Width, size.Height), flags, point); Assert.AreEqual(7, cursor); point = new SKPoint(136, 14); cursor = TextRendererSk.GetCursorFromPoint(text, font, SKRect.Create(0, 0, size.Width, size.Height), flags, point); Assert.AreEqual(11, cursor); point = new SKPoint(141, 11); cursor = TextRendererSk.GetCursorFromPoint(text, font, SKRect.Create(0, 0, size.Width, size.Height), flags, point); Assert.AreEqual(12, cursor); point = new SKPoint(170, 16); cursor = TextRendererSk.GetCursorFromPoint(text, font, SKRect.Create(0, 0, size.Width, size.Height), flags, point); Assert.AreEqual(15, cursor); // Click on the right of last character point = new SKPoint(178, 13); cursor = TextRendererSk.GetCursorFromPoint(text, font, SKRect.Create(0, 0, size.Width, size.Height), flags, point); Assert.AreEqual(15, cursor); point = new SKPoint(11, -1); cursor = TextRendererSk.GetCursorFromPoint(text, font, SKRect.Create(0, 0, size.Width, size.Height), flags, point); Assert.AreEqual(1, cursor); point = new SKPoint(181, 12); cursor = TextRendererSk.GetCursorFromPoint(text, font, SKRect.Create(0, 0, size.Width, size.Height), flags, point); Assert.AreEqual(15, cursor); }
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((int)size.Width, (int)size.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul)) using (var canvas = new SKCanvas(bitmap)) { canvas.Clear(BackColour); 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)) { SKData d = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100); d.SaveTo(s); } } Console.WriteLine("Drawing sized with cursor {0} (fontSize {1}, flags {2}), measured size: {3}", text, fontSize, flags, size); }