PlayedIndicatorDrawer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 FontSize = 42;
  17. private const int OffsetFromTopRightCorner = 38;
  18. private readonly IApplicationPaths _appPaths;
  19. private readonly IHttpClient _iHttpClient;
  20. private readonly IFileSystem _fileSystem;
  21. public PlayedIndicatorDrawer(IApplicationPaths appPaths, IHttpClient iHttpClient, IFileSystem fileSystem)
  22. {
  23. _appPaths = appPaths;
  24. _iHttpClient = iHttpClient;
  25. _fileSystem = fileSystem;
  26. }
  27. public async Task DrawPlayedIndicator(SKCanvas canvas, ImageSize imageSize)
  28. {
  29. var x = imageSize.Width - OffsetFromTopRightCorner;
  30. using (var paint = new SKPaint())
  31. {
  32. paint.Color = SKColor.Parse("#CC52B54B");
  33. paint.Style = SKPaintStyle.Fill;
  34. canvas.DrawCircle((float)x, OffsetFromTopRightCorner, 20, paint);
  35. }
  36. using (var paint = new SKPaint())
  37. {
  38. paint.Color = new SKColor(255, 255, 255, 255);
  39. paint.Style = SKPaintStyle.Fill;
  40. paint.Typeface = SKTypeface.FromFile(await DownloadFont("webdings.ttf", "https://github.com/MediaBrowser/Emby.Resources/raw/master/fonts/webdings.ttf",
  41. _appPaths, _iHttpClient, _fileSystem).ConfigureAwait(false));
  42. paint.TextSize = FontSize;
  43. paint.IsAntialias = true;
  44. canvas.DrawText("a", (float)x-20, OffsetFromTopRightCorner + 12, paint);
  45. }
  46. }
  47. internal static string ExtractFont(string name, IApplicationPaths paths, IFileSystem fileSystem)
  48. {
  49. var filePath = Path.Combine(paths.ProgramDataPath, "fonts", name);
  50. if (fileSystem.FileExists(filePath))
  51. {
  52. return filePath;
  53. }
  54. var namespacePath = typeof(PlayedIndicatorDrawer).Namespace + ".fonts." + name;
  55. var tempPath = Path.Combine(paths.TempDirectory, Guid.NewGuid().ToString("N") + ".ttf");
  56. fileSystem.CreateDirectory(fileSystem.GetDirectoryName(tempPath));
  57. using (var stream = typeof(PlayedIndicatorDrawer).GetTypeInfo().Assembly.GetManifestResourceStream(namespacePath))
  58. {
  59. using (var fileStream = fileSystem.GetFileStream(tempPath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
  60. {
  61. stream.CopyTo(fileStream);
  62. }
  63. }
  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. internal static async Task<string> DownloadFont(string name, string url, IApplicationPaths paths, IHttpClient httpClient, IFileSystem fileSystem)
  75. {
  76. var filePath = Path.Combine(paths.ProgramDataPath, "fonts", name);
  77. if (fileSystem.FileExists(filePath))
  78. {
  79. return filePath;
  80. }
  81. var tempPath = await httpClient.GetTempFile(new HttpRequestOptions
  82. {
  83. Url = url,
  84. Progress = new SimpleProgress<double>()
  85. }).ConfigureAwait(false);
  86. fileSystem.CreateDirectory(fileSystem.GetDirectoryName(filePath));
  87. try
  88. {
  89. fileSystem.CopyFile(tempPath, filePath, false);
  90. }
  91. catch (IOException)
  92. {
  93. }
  94. return tempPath;
  95. }
  96. }
  97. }