PlayedIndicatorDrawer.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using SkiaSharp;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Model.Drawing;
  5. using System;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Controller.IO;
  9. using MediaBrowser.Model.IO;
  10. using System.Reflection;
  11. using MediaBrowser.Common.Progress;
  12. namespace Emby.Drawing.Skia
  13. {
  14. public class PlayedIndicatorDrawer
  15. {
  16. private const int OffsetFromTopRightCorner = 38;
  17. private readonly IApplicationPaths _appPaths;
  18. private readonly IHttpClient _iHttpClient;
  19. private readonly IFileSystem _fileSystem;
  20. public PlayedIndicatorDrawer(IApplicationPaths appPaths, IHttpClient iHttpClient, IFileSystem fileSystem)
  21. {
  22. _appPaths = appPaths;
  23. _iHttpClient = iHttpClient;
  24. _fileSystem = fileSystem;
  25. }
  26. public async Task DrawPlayedIndicator(SKCanvas canvas, ImageSize imageSize)
  27. {
  28. var x = imageSize.Width - OffsetFromTopRightCorner;
  29. using (var paint = new SKPaint())
  30. {
  31. paint.Color = SKColor.Parse("#CC52B54B");
  32. paint.Style = SKPaintStyle.Fill;
  33. canvas.DrawCircle((float)x, OffsetFromTopRightCorner, 20, paint);
  34. }
  35. using (var paint = new SKPaint())
  36. {
  37. paint.Color = new SKColor(255, 255, 255, 255);
  38. paint.Style = SKPaintStyle.Fill;
  39. paint.TextSize = 30;
  40. paint.IsAntialias = true;
  41. var text = "✔️";
  42. var emojiChar = StringUtilities.GetUnicodeCharacterCode(text, SKTextEncoding.Utf32);
  43. // or:
  44. //var emojiChar = 0x1F680;
  45. // ask the font manager for a font with that character
  46. var fontManager = SKFontManager.Default;
  47. var emojiTypeface = fontManager.MatchCharacter(emojiChar);
  48. paint.Typeface = emojiTypeface;
  49. canvas.DrawText(text, (float)x-20, OffsetFromTopRightCorner + 12, paint);
  50. }
  51. }
  52. internal static async Task<string> DownloadFont(string name, string url, IApplicationPaths paths, IHttpClient httpClient, IFileSystem fileSystem)
  53. {
  54. var filePath = Path.Combine(paths.ProgramDataPath, "fonts", name);
  55. if (fileSystem.FileExists(filePath))
  56. {
  57. return filePath;
  58. }
  59. var tempPath = await httpClient.GetTempFile(new HttpRequestOptions
  60. {
  61. Url = url,
  62. Progress = new SimpleProgress<double>()
  63. }).ConfigureAwait(false);
  64. fileSystem.CreateDirectory(fileSystem.GetDirectoryName(filePath));
  65. try
  66. {
  67. fileSystem.CopyFile(tempPath, filePath, false);
  68. }
  69. catch (IOException)
  70. {
  71. }
  72. return tempPath;
  73. }
  74. }
  75. }