Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade FontAwesome5 #202

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Pinpoint.Core/Pinpoint.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FontAwesome5" Version="2.0.8" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="FontAwesome5" Version="2.1.11" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Svg" Version="3.1.1" />
</ItemGroup>

Expand Down
39 changes: 20 additions & 19 deletions Pinpoint.Core/Results/FontAwesomeBitmapRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@
using FontAwesome5.Extensions;
using Svg;

namespace Pinpoint.Core.Results
namespace Pinpoint.Core.Results;

public static class FontAwesomeBitmapRepository
{
public static class FontAwesomeBitmapRepository
{
private static readonly Dictionary<EFontAwesomeIcon, Bitmap> IconCache = new();
private static readonly Dictionary<EFontAwesomeIcon, Bitmap> IconCache = new();

public static Bitmap Get(EFontAwesomeIcon icon)
public static Bitmap Get(EFontAwesomeIcon icon)
{
// Check if we already converted FA icon -> Bitmap and if not, do it
if (!IconCache.ContainsKey(icon))
{
// Check if we already converted FA icon -> Bitmap and if not, do it
if (!IconCache.ContainsKey(icon))
if (icon.GetSvg(out string path, out int width, out int height))
{
var attr = icon.GetInformationAttribute<FontAwesomeSvgInformationAttribute>();
var xml = CreateXmlDocument(attr.Width, attr.Height, attr.Path);
var xml = CreateXmlDocument(width, height, path);
var document = SvgDocument.FromSvg<SvgDocument>(xml);
IconCache[icon] = document.Draw();
}

return IconCache.ContainsKey(icon) ? IconCache[icon] : default;
}

private static string CreateXmlDocument(int width, int height, string path)
{
var sb = new StringBuilder();
sb.Append($"<svg width=\"{width}\" height=\"{height}\" viewBox=\"0 0 {width} {height}\" xmlns=\"http://www.w3.org/2000/svg\">");
sb.Append($"<path d=\"{path}\"/>");
sb.Append("</svg>");
return sb.ToString();
}
return IconCache.TryGetValue(icon, out Bitmap value) ? value : default;
}

private static string CreateXmlDocument(int width, int height, string path)
{
var sb = new StringBuilder();
sb.Append($"<svg width=\"{width}\" height=\"{height}\" viewBox=\"0 0 {width} {height}\" xmlns=\"http://www.w3.org/2000/svg\">");
sb.Append($"<path d=\"{path}\"/>");
sb.Append("</svg>");
return sb.ToString();
}
}