PlayedIndicatorDrawer.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using ImageMagickSharp;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Model.Drawing;
  4. using System;
  5. using System.IO;
  6. namespace Emby.Drawing.ImageMagick
  7. {
  8. public class PlayedIndicatorDrawer
  9. {
  10. private const int FontSize = 52;
  11. private const int OffsetFromTopRightCorner = 38;
  12. private readonly IApplicationPaths _appPaths;
  13. public PlayedIndicatorDrawer(IApplicationPaths appPaths)
  14. {
  15. _appPaths = appPaths;
  16. }
  17. public void DrawPlayedIndicator(MagickWand wand, ImageSize imageSize)
  18. {
  19. var x = imageSize.Width - OffsetFromTopRightCorner;
  20. using (var draw = new DrawingWand())
  21. {
  22. using (PixelWand pixel = new PixelWand())
  23. {
  24. pixel.Color = "#52B54B";
  25. pixel.Opacity = 0.2;
  26. draw.FillColor = pixel;
  27. draw.DrawCircle(x, OffsetFromTopRightCorner, x - 20, OffsetFromTopRightCorner - 20);
  28. pixel.Opacity = 0;
  29. pixel.Color = "white";
  30. draw.FillColor = pixel;
  31. draw.Font = ExtractFont("webdings.ttf", _appPaths);
  32. draw.FontSize = FontSize;
  33. draw.FontStyle = FontStyleType.NormalStyle;
  34. draw.TextAlignment = TextAlignType.CenterAlign;
  35. draw.FontWeight = FontWeightType.RegularStyle;
  36. draw.TextAntialias = true;
  37. draw.DrawAnnotation(x + 4, OffsetFromTopRightCorner + 14, "a");
  38. draw.FillColor = pixel;
  39. wand.CurrentImage.DrawImage(draw);
  40. }
  41. }
  42. }
  43. internal static string ExtractFont(string name, IApplicationPaths paths)
  44. {
  45. var filePath = Path.Combine(paths.ProgramDataPath, "fonts", name);
  46. if (File.Exists(filePath))
  47. {
  48. return filePath;
  49. }
  50. var namespacePath = typeof(PlayedIndicatorDrawer).Namespace + ".fonts." + name;
  51. var tempPath = Path.Combine(paths.TempDirectory, Guid.NewGuid().ToString("N") + ".ttf");
  52. Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
  53. using (var stream = typeof(PlayedIndicatorDrawer).Assembly.GetManifestResourceStream(namespacePath))
  54. {
  55. using (var fileStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.Read))
  56. {
  57. stream.CopyTo(fileStream);
  58. }
  59. }
  60. Directory.CreateDirectory(Path.GetDirectoryName(filePath));
  61. try
  62. {
  63. File.Copy(tempPath, filePath, false);
  64. }
  65. catch (IOException)
  66. {
  67. }
  68. return tempPath;
  69. }
  70. }
  71. }