PlayedIndicatorDrawer.cs 4.0 KB

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