Reuse paint objects.

This commit is contained in:
Patrick Barron 2020-07-19 14:39:11 -04:00
parent d983d65d8a
commit 2569793ff0
2 changed files with 41 additions and 49 deletions

View File

@ -22,31 +22,27 @@ namespace Jellyfin.Drawing.Skia
{ {
var x = imageSize.Width - OffsetFromTopRightCorner; var x = imageSize.Width - OffsetFromTopRightCorner;
using (var paint = new SKPaint()) using var paint = new SKPaint
{ {
paint.Color = SKColor.Parse("#CC00A4DC"); Color = SKColor.Parse("#CC00A4DC"),
paint.Style = SKPaintStyle.Fill; Style = SKPaintStyle.Fill
canvas.DrawCircle(x, OffsetFromTopRightCorner, 20, paint); };
}
using (var paint = new SKPaint()) canvas.DrawCircle(x, OffsetFromTopRightCorner, 20, paint);
{
paint.Color = new SKColor(255, 255, 255, 255);
paint.Style = SKPaintStyle.Fill;
paint.TextSize = 30; paint.Color = new SKColor(255, 255, 255, 255);
paint.IsAntialias = true; paint.TextSize = 30;
paint.IsAntialias = true;
// or: // or:
// var emojiChar = 0x1F680; // var emojiChar = 0x1F680;
const string Text = "✔️"; const string Text = "✔️";
var emojiChar = StringUtilities.GetUnicodeCharacterCode(Text, SKTextEncoding.Utf32); var emojiChar = StringUtilities.GetUnicodeCharacterCode(Text, SKTextEncoding.Utf32);
// ask the font manager for a font with that character // ask the font manager for a font with that character
paint.Typeface = SKFontManager.Default.MatchCharacter(emojiChar); paint.Typeface = SKFontManager.Default.MatchCharacter(emojiChar);
canvas.DrawText(Text, (float)x - 20, OffsetFromTopRightCorner + 12, paint); canvas.DrawText(Text, (float)x - 20, OffsetFromTopRightCorner + 12, paint);
}
} }
} }
} }

View File

@ -28,41 +28,37 @@ namespace Jellyfin.Drawing.Skia
var x = imageSize.Width - OffsetFromTopRightCorner; var x = imageSize.Width - OffsetFromTopRightCorner;
var text = count.ToString(CultureInfo.InvariantCulture); var text = count.ToString(CultureInfo.InvariantCulture);
using (var paint = new SKPaint()) using var paint = new SKPaint
{ {
paint.Color = SKColor.Parse("#CC00A4DC"); Color = SKColor.Parse("#CC00A4DC"),
paint.Style = SKPaintStyle.Fill; Style = SKPaintStyle.Fill
canvas.DrawCircle(x, OffsetFromTopRightCorner, 20, paint); };
canvas.DrawCircle(x, OffsetFromTopRightCorner, 20, paint);
paint.Color = new SKColor(255, 255, 255, 255);
paint.TextSize = 24;
paint.IsAntialias = true;
var y = OffsetFromTopRightCorner + 9;
if (text.Length == 1)
{
x -= 7;
} }
using (var paint = new SKPaint()) if (text.Length == 2)
{ {
paint.Color = new SKColor(255, 255, 255, 255); x -= 13;
paint.Style = SKPaintStyle.Fill;
paint.TextSize = 24;
paint.IsAntialias = true;
var y = OffsetFromTopRightCorner + 9;
if (text.Length == 1)
{
x -= 7;
}
if (text.Length == 2)
{
x -= 13;
}
else if (text.Length >= 3)
{
x -= 15;
y -= 2;
paint.TextSize = 18;
}
canvas.DrawText(text, x, y, paint);
} }
else if (text.Length >= 3)
{
x -= 15;
y -= 2;
paint.TextSize = 18;
}
canvas.DrawText(text, x, y, paint);
} }
} }
} }