PlayedIndicatorDrawer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using ImageMagickSharp;
  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.Common.Progress;
  9. using MediaBrowser.Controller.IO;
  10. using MediaBrowser.Model.IO;
  11. namespace Emby.Drawing.ImageMagick
  12. {
  13. public class PlayedIndicatorDrawer
  14. {
  15. private const int FontSize = 52;
  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(MagickWand wand, ImageSize imageSize)
  27. {
  28. var x = imageSize.Width - OffsetFromTopRightCorner;
  29. using (var draw = new DrawingWand())
  30. {
  31. using (PixelWand pixel = new PixelWand())
  32. {
  33. pixel.Color = "#52B54B";
  34. pixel.Opacity = 0.2;
  35. draw.FillColor = pixel;
  36. draw.DrawCircle(x, OffsetFromTopRightCorner, x - 20, OffsetFromTopRightCorner - 20);
  37. pixel.Opacity = 0;
  38. pixel.Color = "white";
  39. draw.FillColor = pixel;
  40. draw.Font = await DownloadFont("webdings.ttf", "https://github.com/MediaBrowser/Emby.Resources/raw/master/fonts/webdings.ttf", _appPaths, _iHttpClient, _fileSystem).ConfigureAwait(false);
  41. draw.FontSize = FontSize;
  42. draw.FontStyle = FontStyleType.NormalStyle;
  43. draw.TextAlignment = TextAlignType.CenterAlign;
  44. draw.FontWeight = FontWeightType.RegularStyle;
  45. draw.TextAntialias = true;
  46. draw.DrawAnnotation(x + 4, OffsetFromTopRightCorner + 14, "a");
  47. draw.FillColor = pixel;
  48. wand.CurrentImage.DrawImage(draw);
  49. }
  50. }
  51. }
  52. internal static string ExtractFont(string name, IApplicationPaths paths, IFileSystem fileSystem)
  53. {
  54. var filePath = Path.Combine(paths.ProgramDataPath, "fonts", name);
  55. if (fileSystem.FileExists(filePath))
  56. {
  57. return filePath;
  58. }
  59. var namespacePath = typeof(PlayedIndicatorDrawer).Namespace + ".fonts." + name;
  60. var tempPath = Path.Combine(paths.TempDirectory, Guid.NewGuid().ToString("N") + ".ttf");
  61. fileSystem.CreateDirectory(fileSystem.GetDirectoryName(tempPath));
  62. using (var stream = typeof(PlayedIndicatorDrawer).Assembly.GetManifestResourceStream(namespacePath))
  63. {
  64. using (var fileStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.Read))
  65. {
  66. stream.CopyTo(fileStream);
  67. }
  68. }
  69. fileSystem.CreateDirectory(fileSystem.GetDirectoryName(filePath));
  70. try
  71. {
  72. fileSystem.CopyFile(tempPath, filePath, false);
  73. }
  74. catch (IOException)
  75. {
  76. }
  77. return tempPath;
  78. }
  79. internal static async Task<string> DownloadFont(string name, string url, IApplicationPaths paths, IHttpClient httpClient, IFileSystem fileSystem)
  80. {
  81. var filePath = Path.Combine(paths.ProgramDataPath, "fonts", name);
  82. if (fileSystem.FileExists(filePath))
  83. {
  84. return filePath;
  85. }
  86. var tempPath = await httpClient.GetTempFile(new HttpRequestOptions
  87. {
  88. Url = url,
  89. Progress = new SimpleProgress<double>()
  90. }).ConfigureAwait(false);
  91. fileSystem.CreateDirectory(fileSystem.GetDirectoryName(filePath));
  92. try
  93. {
  94. fileSystem.CopyFile(tempPath, filePath, false);
  95. }
  96. catch (IOException)
  97. {
  98. }
  99. return tempPath;
  100. }
  101. }
  102. }