Implement feature to use game resource fonts

This commit is contained in:
Soreepeong 2022-02-24 18:56:34 +09:00
parent b7c47b4c97
commit f3588dfe23
9 changed files with 1137 additions and 2 deletions

View file

@ -16,6 +16,7 @@ using Dalamud.Game.Gui.Internal;
using Dalamud.Game.Internal.DXGI;
using Dalamud.Hooking;
using Dalamud.Hooking.Internal;
using Dalamud.Interface.GameFonts;
using Dalamud.Interface.Internal.ManagedAsserts;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Internal.Windows.StyleEditor;
@ -56,6 +57,9 @@ namespace Dalamud.Interface.Internal
private readonly SwapChainVtableResolver address;
private RawDX11Scene? scene;
private GameFont overwriteDefaultFontFromGameFont = GameFont.Undefined;
private GameFontHandle? overwriteDefaultFontFromGameFontHandle;
// can't access imgui IO before first present call
private bool lastWantCapture = false;
private bool isRebuildingFonts = false;
@ -128,10 +132,15 @@ namespace Dalamud.Interface.Internal
public event Action ResizeBuffers;
/// <summary>
/// Gets or sets an action that is executed when fonts are rebuilt.
/// Gets or sets an action that is executed right before fonts are rebuilt.
/// </summary>
public event Action BuildFonts;
/// <summary>
/// Gets or sets an action that is executed right after fonts are rebuilt.
/// </summary>
public event Action AfterBuildFonts;
/// <summary>
/// Gets the default ImGui font.
/// </summary>
@ -304,6 +313,16 @@ namespace Dalamud.Interface.Internal
if (!this.isRebuildingFonts)
{
Log.Verbose("[FONT] RebuildFonts() trigger");
var configuration = Service<DalamudConfiguration>.Get();
if (this.overwriteDefaultFontFromGameFont != configuration.DefaultFontFromGame)
{
this.overwriteDefaultFontFromGameFont = configuration.DefaultFontFromGame;
this.overwriteDefaultFontFromGameFontHandle?.Dispose();
if (configuration.DefaultFontFromGame == GameFont.Undefined)
this.overwriteDefaultFontFromGameFontHandle = null;
else
this.overwriteDefaultFontFromGameFontHandle = Service<GameFontManager>.Get().NewFontRef(configuration.DefaultFontFromGame);
}
this.isRebuildingFonts = true;
this.scene.OnNewRenderFrame += this.RebuildFontsInternal;
@ -384,6 +403,16 @@ namespace Dalamud.Interface.Internal
this.scene.OnBuildUI += this.Display;
this.scene.OnNewInputFrame += this.OnNewInputFrame;
if (this.overwriteDefaultFontFromGameFont != configuration.DefaultFontFromGame)
{
this.overwriteDefaultFontFromGameFont = configuration.DefaultFontFromGame;
this.overwriteDefaultFontFromGameFontHandle?.Dispose();
if (configuration.DefaultFontFromGame == GameFont.Undefined)
this.overwriteDefaultFontFromGameFontHandle = null;
else
this.overwriteDefaultFontFromGameFontHandle = Service<GameFontManager>.Get().NewFontRef(configuration.DefaultFontFromGame);
}
this.SetupFonts();
StyleModel.TransferOldModels();
@ -496,7 +525,6 @@ namespace Dalamud.Interface.Internal
ImGui.GetIO().Fonts.Clear();
ImFontConfigPtr fontConfig = ImGuiNative.ImFontConfig_ImFontConfig();
fontConfig.MergeMode = true;
fontConfig.PixelSnapH = true;
var fontPathJp = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "NotoSansCJKjp-Medium.otf");
@ -522,7 +550,9 @@ namespace Dalamud.Interface.Internal
},
GCHandleType.Pinned);
fontConfig.MergeMode = false;
ImGui.GetIO().Fonts.AddFontFromFileTTF(fontPathGame, 17.0f, fontConfig, gameRangeHandle.AddrOfPinnedObject());
fontConfig.MergeMode = true;
var fontPathIcon = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "FontAwesome5FreeSolid.otf");
@ -546,6 +576,9 @@ namespace Dalamud.Interface.Internal
MonoFont = ImGui.GetIO().Fonts.AddFontFromFileTTF(fontPathMono, 16.0f);
var gameFontManager = Service<GameFontManager>.Get();
gameFontManager.BuildFonts();
Log.Verbose("[FONT] Invoke OnBuildFonts");
this.BuildFonts?.Invoke();
Log.Verbose("[FONT] OnBuildFonts OK!");
@ -557,6 +590,13 @@ namespace Dalamud.Interface.Internal
ImGui.GetIO().Fonts.Build();
gameFontManager.AfterBuildFonts();
GameFontManager.CopyGlyphsAcrossFonts(this.overwriteDefaultFontFromGameFontHandle?.Get(), DefaultFont, false, true);
Log.Verbose("[FONT] Invoke OnAfterBuildFonts");
this.AfterBuildFonts?.Invoke();
Log.Verbose("[FONT] OnAfterBuildFonts OK!");
Log.Verbose("[FONT] Fonts built!");
this.fontBuildSignal.Set();

View file

@ -12,6 +12,7 @@ using Dalamud.Game.Gui.Dtr;
using Dalamud.Game.Text;
using Dalamud.Interface.Colors;
using Dalamud.Interface.Components;
using Dalamud.Interface.GameFonts;
using Dalamud.Interface.Windowing;
using Dalamud.Plugin.Internal;
using Dalamud.Utility;
@ -27,6 +28,8 @@ namespace Dalamud.Interface.Internal.Windows
private const float MinScale = 0.3f;
private const float MaxScale = 2.0f;
private readonly List<GameFont> validFontChoices;
private readonly string[] validFontNames;
private readonly string[] languages;
private readonly string[] locLanguages;
private int langIndex;
@ -66,6 +69,8 @@ namespace Dalamud.Interface.Internal.Windows
private bool doButtonsSystemMenu;
private bool disableRmtFiltering;
private int validFontIndex;
#region Experimental
private bool doPluginTest;
@ -111,6 +116,10 @@ namespace Dalamud.Interface.Internal.Windows
this.doButtonsSystemMenu = configuration.DoButtonsSystemMenu;
this.disableRmtFiltering = configuration.DisableRmtFiltering;
this.validFontChoices = Enum.GetValues<GameFont>().Where(x => x == GameFont.Undefined || GameFontManager.IsGenericPurposeFont(x)).ToList();
this.validFontNames = this.validFontChoices.Select(x => GameFontManager.DescribeFont(x)).ToArray();
this.validFontIndex = Math.Max(0, this.validFontChoices.IndexOf(configuration.DefaultFontFromGame));
this.languages = Localization.ApplicableLangCodes.Prepend("en").ToArray();
try
{
@ -286,6 +295,11 @@ namespace Dalamud.Interface.Internal.Windows
ImGuiHelpers.ScaledDummy(10, 16);
ImGui.Text(Loc.Localize("DalamudSettingsGlobalFont", "Global Font"));
ImGui.Combo("##DalamudSettingsGlobalFontDrag", ref this.validFontIndex, this.validFontNames, this.validFontNames.Length);
ImGuiHelpers.ScaledDummy(10, 16);
if (ImGui.Button(Loc.Localize("DalamudSettingsOpenStyleEditor", "Open Style Editor")))
{
Service<DalamudInterface>.Get().OpenStyleEditor();
@ -800,6 +814,8 @@ namespace Dalamud.Interface.Internal.Windows
configuration.IsFocusManagementEnabled = this.doFocus;
configuration.ShowTsm = this.doTsm;
configuration.DefaultFontFromGame = this.validFontChoices[this.validFontIndex];
// This is applied every frame in InterfaceManager::CheckViewportState()
configuration.IsDisableViewport = !this.doViewport;
@ -842,6 +858,8 @@ namespace Dalamud.Interface.Internal.Windows
configuration.Save();
Service<InterfaceManager>.Get().RebuildFonts();
_ = Service<PluginManager>.Get().ReloadPluginMastersAsync();
}
}