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