PKHeX/PKHeX.WinForms/Util/FontUtil.cs
Kurt d47bb1d297
Update .NET Runtime to .NET 8.0 (#4082)
With the new version of Visual Studio bringing C# 12, we can revise our logic for better readability as well as use new methods/APIs introduced in the .NET 8.0 BCL.
2023-12-03 20:13:20 -08:00

44 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using PKHeX.WinForms.Properties;
namespace PKHeX.WinForms;
public static class FontUtil
{
private static readonly PrivateFontCollection CustomFonts = new();
private static readonly Dictionary<float, Font> GeneratedFonts = [];
static FontUtil()
{
string g6path = Path.Combine(Path.GetTempPath(), "pgldings6.ttf");
try
{
if (!File.Exists(g6path))
File.WriteAllBytes(g6path, Resources.pgldings_normalregular);
CustomFonts.AddFontFile(g6path);
}
catch (FileNotFoundException ex)
{
Debug.WriteLine($"Unable to read font file: {ex.Message}");
}
catch (Exception ex)
{
Debug.WriteLine($"Unable to add in-game font: {ex.Message}");
}
}
public static Font GetPKXFont(float size = 11f)
{
if (GeneratedFonts.TryGetValue(size, out var f))
return f;
var family = CustomFonts.Families.Length == 0 ? FontFamily.GenericSansSerif : CustomFonts.Families[0];
var font = new Font(family, size);
GeneratedFonts.Add(size, font);
return font;
}
}