mirror of
https://github.com/kwsch/PKHeX.git
synced 2026-08-22 06:57:42 -05:00
Simplify font loading
Don't care about operating system; use the AddFontFile method instead. Add fallback for font load failure. This should be easier to maintain in the future, if multiple char->glyph mappings are to be supported (gen6 is already different from gen7 with some swapped glyphs).
This commit is contained in:
@@ -2,53 +2,36 @@
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.IO;
|
||||
using PKHeX.WinForms.Properties;
|
||||
|
||||
namespace PKHeX.WinForms
|
||||
{
|
||||
public static class SafeNativeMethods
|
||||
{
|
||||
#if WINDOWS
|
||||
[DllImport("gdi32.dll")]
|
||||
internal static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static class FontUtil
|
||||
{
|
||||
private static readonly PrivateFontCollection s_FontCollection = new PrivateFontCollection();
|
||||
private static readonly PrivateFontCollection CustomFonts = new PrivateFontCollection();
|
||||
|
||||
private static FontFamily[] FontFamilies
|
||||
{
|
||||
get
|
||||
{
|
||||
if (s_FontCollection.Families.Length == 0) SetPKXFont();
|
||||
return s_FontCollection.Families;
|
||||
}
|
||||
}
|
||||
|
||||
public static Font GetPKXFont(float size) => new Font(FontFamilies[0], size);
|
||||
|
||||
private static void SetPKXFont()
|
||||
static FontUtil()
|
||||
{
|
||||
string g6path = Path.Combine(Path.GetTempPath(), "pgldings6.ttf");
|
||||
try
|
||||
{
|
||||
byte[] fontData = Resources.pgldings_normalregular;
|
||||
#if WINDOWS
|
||||
IntPtr fontPtr = Marshal.AllocCoTaskMem(fontData.Length);
|
||||
Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
|
||||
s_FontCollection.AddMemoryFont(fontPtr, Resources.pgldings_normalregular.Length); uint dummy = 0;
|
||||
SafeNativeMethods.AddFontMemResourceEx(fontPtr, (uint)Resources.pgldings_normalregular.Length, IntPtr.Zero, ref dummy);
|
||||
Marshal.FreeCoTaskMem(fontPtr);
|
||||
#else
|
||||
GCHandle fontHandle = GCHandle.Alloc(fontData, GCHandleType.Pinned);
|
||||
s_FontCollection.AddMemoryFont(fontHandle.AddrOfPinnedObject(), fontData.Length);
|
||||
fontHandle.Free();
|
||||
#endif
|
||||
|
||||
if (!File.Exists(g6path))
|
||||
File.WriteAllBytes(g6path, Resources.pgldings_normalregular);
|
||||
}
|
||||
catch (Exception ex) { Debug.WriteLine($"Unable to add ingame font: {ex.Message}"); }
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"Unable to add in-game font: {ex.Message}");
|
||||
return;
|
||||
}
|
||||
|
||||
CustomFonts.AddFontFile(g6path);
|
||||
}
|
||||
|
||||
public static Font GetPKXFont(float size)
|
||||
{
|
||||
var family = CustomFonts.Families.Length == 0 ? FontFamily.GenericSansSerif : CustomFonts.Families[0];
|
||||
return new Font(family, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user