PlayedIndicatorDrawer.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. namespace Emby.Drawing.Skia
  12. {
  13. public class PlayedIndicatorDrawer
  14. {
  15. private const int FontSize = 42;
  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.Typeface = SKTypeface.FromFile(await DownloadFont("webdings.ttf", "https://github.com/MediaBrowser/Emby.Resources/raw/master/fonts/webdings.ttf",
  40. _appPaths, _iHttpClient, _fileSystem).ConfigureAwait(false));
  41. paint.TextSize = FontSize;
  42. paint.IsAntialias = true;
  43. canvas.DrawText("a", (float)x-20, OffsetFromTopRightCorner + 12, paint);
  44. }
  45. }
  46. internal static string ExtractFont(string name, IApplicationPaths paths, IFileSystem fileSystem)
  47. {
  48. var filePath = Path.Combine(paths.ProgramDataPath, "fonts", name);
  49. if (fileSystem.FileExists(filePath))
  50. {
  51. return filePath;
  52. }
  53. var namespacePath = typeof(PlayedIndicatorDrawer).Namespace + ".fonts." + name;
  54. var tempPath = Path.Combine(paths.TempDirectory, Guid.NewGuid().ToString("N") + ".ttf");
  55. fileSystem.CreateDirectory(fileSystem.GetDirectoryName(tempPath));
  56. using (var stream = typeof(PlayedIndicatorDrawer).GetTypeInfo().Assembly.GetManifestResourceStream(namespacePath))
  57. {
  58. using (var fileStream = fileSystem.GetFileStream(tempPath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
  59. {
  60. stream.CopyTo(fileStream);
  61. }
  62. }
  63. fileSystem.CreateDirectory(fileSystem.GetDirectoryName(filePath));
  64. try
  65. {
  66. fileSystem.CopyFile(tempPath, filePath, false);
  67. }
  68. catch (IOException)
  69. {
  70. }
  71. return tempPath;
  72. }
  73. internal static async Task<string> DownloadFont(string name, string url, IApplicationPaths paths, IHttpClient httpClient, IFileSystem fileSystem)
  74. {
  75. var filePath = Path.Combine(paths.ProgramDataPath, "fonts", name);
  76. if (fileSystem.FileExists(filePath))
  77. {
  78. return filePath;
  79. }
  80. var tempPath = await httpClient.GetTempFile(new HttpRequestOptions
  81. {
  82. Url = url,
  83. Progress = new Progress<double>()
  84. }).ConfigureAwait(false);
  85. fileSystem.CreateDirectory(fileSystem.GetDirectoryName(filePath));
  86. try
  87. {
  88. fileSystem.CopyFile(tempPath, filePath, false);
  89. }
  90. catch (IOException)
  91. {
  92. }
  93. return tempPath;
  94. }
  95. }
  96. }