jellyfin-server/Emby.Drawing.ImageMagick/PlayedIndicatorDrawer.cs

126 lines
4.1 KiB
C#
Raw Normal View History

using ImageMagickSharp;
2015-03-09 03:56:42 +00:00
using MediaBrowser.Common.Configuration;
2015-06-19 16:54:39 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Drawing;
2015-03-09 03:56:42 +00:00
using System;
using System.IO;
2015-06-19 16:54:39 +00:00
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
2015-04-08 14:38:02 +00:00
namespace Emby.Drawing.ImageMagick
{
public class PlayedIndicatorDrawer
{
private const int FontSize = 52;
private const int OffsetFromTopRightCorner = 38;
2015-03-09 03:56:42 +00:00
private readonly IApplicationPaths _appPaths;
2015-09-13 21:32:02 +00:00
private readonly IHttpClient _iHttpClient;
private readonly IFileSystem _fileSystem;
2015-03-09 03:56:42 +00:00
2015-09-13 21:32:02 +00:00
public PlayedIndicatorDrawer(IApplicationPaths appPaths, IHttpClient iHttpClient, IFileSystem fileSystem)
2015-03-09 03:56:42 +00:00
{
_appPaths = appPaths;
2015-06-19 16:54:39 +00:00
_iHttpClient = iHttpClient;
2015-09-13 21:32:02 +00:00
_fileSystem = fileSystem;
2015-03-09 03:56:42 +00:00
}
2015-06-19 16:54:39 +00:00
public async Task DrawPlayedIndicator(MagickWand wand, ImageSize imageSize)
{
var x = imageSize.Width - OffsetFromTopRightCorner;
using (var draw = new DrawingWand())
{
using (PixelWand pixel = new PixelWand())
{
pixel.Color = "#52B54B";
pixel.Opacity = 0.2;
draw.FillColor = pixel;
draw.DrawCircle(x, OffsetFromTopRightCorner, x - 20, OffsetFromTopRightCorner - 20);
pixel.Opacity = 0;
pixel.Color = "white";
draw.FillColor = pixel;
2015-09-13 21:32:02 +00:00
draw.Font = await DownloadFont("webdings.ttf", "https://github.com/MediaBrowser/Emby.Resources/raw/master/fonts/webdings.ttf", _appPaths, _iHttpClient, _fileSystem).ConfigureAwait(false);
draw.FontSize = FontSize;
draw.FontStyle = FontStyleType.NormalStyle;
draw.TextAlignment = TextAlignType.CenterAlign;
draw.FontWeight = FontWeightType.RegularStyle;
draw.TextAntialias = true;
draw.DrawAnnotation(x + 4, OffsetFromTopRightCorner + 14, "a");
draw.FillColor = pixel;
wand.CurrentImage.DrawImage(draw);
}
2015-03-09 03:56:42 +00:00
}
}
2015-09-13 21:32:02 +00:00
internal static string ExtractFont(string name, IApplicationPaths paths, IFileSystem fileSystem)
2015-03-09 03:56:42 +00:00
{
var filePath = Path.Combine(paths.ProgramDataPath, "fonts", name);
2015-09-13 21:32:02 +00:00
if (fileSystem.FileExists(filePath))
2015-03-09 03:56:42 +00:00
{
return filePath;
}
var namespacePath = typeof(PlayedIndicatorDrawer).Namespace + ".fonts." + name;
var tempPath = Path.Combine(paths.TempDirectory, Guid.NewGuid().ToString("N") + ".ttf");
2015-09-13 21:32:02 +00:00
fileSystem.CreateDirectory(Path.GetDirectoryName(tempPath));
2015-03-09 03:56:42 +00:00
using (var stream = typeof(PlayedIndicatorDrawer).Assembly.GetManifestResourceStream(namespacePath))
{
using (var fileStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
stream.CopyTo(fileStream);
}
}
2015-09-13 21:32:02 +00:00
fileSystem.CreateDirectory(Path.GetDirectoryName(filePath));
2015-03-09 03:56:42 +00:00
try
{
2015-09-13 21:32:02 +00:00
fileSystem.CopyFile(tempPath, filePath, false);
}
2015-03-09 03:56:42 +00:00
catch (IOException)
{
2015-06-19 16:54:39 +00:00
}
return tempPath;
}
2015-09-13 21:32:02 +00:00
internal static async Task<string> DownloadFont(string name, string url, IApplicationPaths paths, IHttpClient httpClient, IFileSystem fileSystem)
2015-06-19 16:54:39 +00:00
{
var filePath = Path.Combine(paths.ProgramDataPath, "fonts", name);
2015-09-13 21:32:02 +00:00
if (fileSystem.FileExists(filePath))
2015-06-19 16:54:39 +00:00
{
return filePath;
}
var tempPath = await httpClient.GetTempFile(new HttpRequestOptions
{
Url = url,
Progress = new Progress<double>()
}).ConfigureAwait(false);
2015-09-13 21:32:02 +00:00
fileSystem.CreateDirectory(Path.GetDirectoryName(filePath));
2015-06-19 16:54:39 +00:00
try
{
2015-09-13 21:32:02 +00:00
fileSystem.CopyFile(tempPath, filePath, false);
2015-06-19 16:54:39 +00:00
}
catch (IOException)
{
2015-03-09 03:56:42 +00:00
}
return tempPath;
}
}
}