2015-02-20 01:57:10 +00:00
|
|
|
|
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;
|
2015-02-20 01:57:10 +00:00
|
|
|
|
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;
|
2017-06-23 16:04:45 +00:00
|
|
|
|
using MediaBrowser.Common.Progress;
|
2016-10-25 19:02:04 +00:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
2013-09-19 17:45:48 +00:00
|
|
|
|
|
2015-04-08 14:38:02 +00:00
|
|
|
|
namespace Emby.Drawing.ImageMagick
|
2013-09-19 17:45:48 +00:00
|
|
|
|
{
|
2014-01-01 18:26:31 +00:00
|
|
|
|
public class PlayedIndicatorDrawer
|
2013-09-19 17:45:48 +00:00
|
|
|
|
{
|
2015-02-20 01:57:10 +00:00
|
|
|
|
private const int FontSize = 52;
|
|
|
|
|
private const int OffsetFromTopRightCorner = 38;
|
2013-09-19 17:45:48 +00:00
|
|
|
|
|
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)
|
2013-09-19 17:45:48 +00:00
|
|
|
|
{
|
2015-02-20 01:57:10 +00:00
|
|
|
|
var x = imageSize.Width - OffsetFromTopRightCorner;
|
2013-09-19 17:45:48 +00:00
|
|
|
|
|
2015-02-20 01:57:10 +00:00
|
|
|
|
using (var draw = new DrawingWand())
|
2013-09-19 17:45:48 +00:00
|
|
|
|
{
|
2015-02-20 01:57:10 +00:00
|
|
|
|
using (PixelWand pixel = new PixelWand())
|
|
|
|
|
{
|
|
|
|
|
pixel.Color = "#52B54B";
|
|
|
|
|
pixel.Opacity = 0.2;
|
|
|
|
|
draw.FillColor = pixel;
|
|
|
|
|
draw.DrawCircle(x, OffsetFromTopRightCorner, x - 20, OffsetFromTopRightCorner - 20);
|
2013-09-19 17:45:48 +00:00
|
|
|
|
|
2015-02-20 01:57:10 +00:00
|
|
|
|
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);
|
2015-02-20 01:57:10 +00:00
|
|
|
|
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");
|
2013-09-19 17:45:48 +00:00
|
|
|
|
|
2015-02-20 01:57:10 +00:00
|
|
|
|
draw.FillColor = pixel;
|
|
|
|
|
wand.CurrentImage.DrawImage(draw);
|
2013-09-19 17:45:48 +00:00
|
|
|
|
}
|
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");
|
2017-05-04 18:14:45 +00:00
|
|
|
|
fileSystem.CreateDirectory(fileSystem.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-02-20 01:57:10 +00:00
|
|
|
|
|
2017-05-04 18:14:45 +00:00
|
|
|
|
fileSystem.CreateDirectory(fileSystem.GetDirectoryName(filePath));
|
2015-03-09 03:56:42 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-09-13 21:32:02 +00:00
|
|
|
|
fileSystem.CopyFile(tempPath, filePath, false);
|
2013-09-19 17:45:48 +00:00
|
|
|
|
}
|
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,
|
2017-06-23 16:04:45 +00:00
|
|
|
|
Progress = new SimpleProgress<double>()
|
2015-06-19 16:54:39 +00:00
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
2017-05-04 18:14:45 +00:00
|
|
|
|
fileSystem.CreateDirectory(fileSystem.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;
|
2013-09-19 17:45:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|