PlayedIndicatorDrawer.cs 3.9 KB

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