Fix fallback font not working correctly (#839)

* Fix GetGameFontHandle calls during BuildFonts

* Add option to draw ImGui test windows in Monospace font

* Add warning if first font is not the default font as we would like it

* Add better description for Font Resolution Level

* Fix scaling when using Korean fonts with AXIS on
This commit is contained in:
kizer 2022-05-13 17:13:49 +09:00 committed by GitHub
parent cadcddf7b1
commit e1df496e2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 15 deletions

View file

@ -39,7 +39,7 @@ namespace Dalamud.Interface.GameFonts
private readonly Dictionary<GameFontStyle, int> fontUseCounter = new();
private readonly Dictionary<GameFontStyle, Dictionary<char, Tuple<int, FdtReader.FontTableEntry>>> glyphRectIds = new();
private bool isBetweenBuildFontsAndAfterBuildFonts = false;
private bool isBetweenBuildFontsAndRightAfterImGuiIoFontsBuild = false;
private bool isBuildingAsFallbackFontMode = false;
/// <summary>
@ -174,7 +174,7 @@ namespace Dalamud.Interface.GameFonts
needRebuild = !this.fonts.ContainsKey(style);
if (needRebuild)
{
if (Service<InterfaceManager>.Get().IsBuildingFontsBeforeAtlasBuild && this.isBetweenBuildFontsAndAfterBuildFonts)
if (Service<InterfaceManager>.Get().IsBuildingFontsBeforeAtlasBuild && this.isBetweenBuildFontsAndRightAfterImGuiIoFontsBuild)
{
Log.Information("[GameFontManager] NewFontRef: Building {0} right now, as it is called while BuildFonts is already in progress yet atlas build has not been called yet.", style.ToString());
this.EnsureFont(style);
@ -246,7 +246,7 @@ namespace Dalamud.Interface.GameFonts
public void BuildFonts(bool forceMinSize)
{
this.isBuildingAsFallbackFontMode = forceMinSize;
this.isBetweenBuildFontsAndAfterBuildFonts = true;
this.isBetweenBuildFontsAndRightAfterImGuiIoFontsBuild = true;
this.glyphRectIds.Clear();
this.fonts.Clear();
@ -255,6 +255,21 @@ namespace Dalamud.Interface.GameFonts
this.EnsureFont(style);
}
/// <summary>
/// Record that ImGui.GetIO().Fonts.Build() has been called.
/// </summary>
public void AfterIoFontsBuild()
{
this.isBetweenBuildFontsAndRightAfterImGuiIoFontsBuild = false;
}
/// <summary>
/// Checks whether GameFontMamager owns an ImFont.
/// </summary>
/// <param name="fontPtr">ImFontPtr to check.</param>
/// <returns>Whether it owns.</returns>
public bool OwnsFont(ImFontPtr fontPtr) => this.fonts.ContainsValue(fontPtr);
/// <summary>
/// Post-build fonts before plugins do something more. To be called from InterfaceManager.
/// </summary>
@ -270,6 +285,9 @@ namespace Dalamud.Interface.GameFonts
var fdt = this.fdts[(int)(this.isBuildingAsFallbackFontMode ? style.FamilyWithMinimumSize : style.FamilyAndSize)];
var scale = style.SizePt / fdt.FontHeader.Size;
var fontPtr = font.NativePtr;
Log.Verbose("[GameFontManager] AfterBuildFonts: Scaling {0} from {1}pt to {2}pt (scale: {3})", style.ToString(), fdt.FontHeader.Size, style.SizePt, scale);
fontPtr->FontSize = fdt.FontHeader.Size * 4 / 3;
if (fontPtr->ConfigData != null)
fontPtr->ConfigData->SizePixels = fontPtr->FontSize;
@ -364,12 +382,8 @@ namespace Dalamud.Interface.GameFonts
}
}
ImGuiHelpers.CopyGlyphsAcrossFonts(InterfaceManager.DefaultFont, font, true, false);
UnscaleFont(font, 1 / scale, false);
font.BuildLookupTable();
}
this.isBetweenBuildFontsAndAfterBuildFonts = false;
}
/// <summary>

View file

@ -67,6 +67,7 @@ namespace Dalamud.Interface.Internal
#endif
private bool isImGuiDrawDemoWindow = false;
private bool isImGuiTestWindowsInMonospace = false;
private bool isImGuiDrawMetricsWindow = false;
/// <summary>
@ -363,12 +364,18 @@ namespace Dalamud.Interface.Internal
this.WindowSystem.Draw();
if (this.isImGuiTestWindowsInMonospace)
ImGui.PushFont(InterfaceManager.MonoFont);
if (this.isImGuiDrawDemoWindow)
ImGui.ShowDemoWindow(ref this.isImGuiDrawDemoWindow);
if (this.isImGuiDrawMetricsWindow)
ImGui.ShowMetricsWindow(ref this.isImGuiDrawMetricsWindow);
if (this.isImGuiTestWindowsInMonospace)
ImGui.PopFont();
// Release focus of any ImGui window if we click into the game.
var io = ImGui.GetIO();
if (!io.WantCaptureMouse && (User32.GetKeyState((int)User32.VirtualKey.VK_LBUTTON) & 0x8000) != 0)
@ -566,8 +573,8 @@ namespace Dalamud.Interface.Internal
if (ImGui.BeginMenu("GUI"))
{
ImGui.MenuItem("Use Monospace font for following windows", string.Empty, ref this.isImGuiTestWindowsInMonospace);
ImGui.MenuItem("Draw ImGui demo", string.Empty, ref this.isImGuiDrawDemoWindow);
ImGui.MenuItem("Draw metrics", string.Empty, ref this.isImGuiDrawMetricsWindow);
ImGui.Separator();

View file

@ -524,8 +524,6 @@ namespace Dalamud.Interface.Internal
this.scene.OnBuildUI += this.Display;
this.scene.OnNewInputFrame += this.OnNewInputFrame;
this.SetupFonts();
StyleModel.TransferOldModels();
if (configuration.SavedStyles == null || configuration.SavedStyles.All(x => x.Name != StyleModelV1.DalamudStandard.Name))
@ -557,6 +555,8 @@ namespace Dalamud.Interface.Internal
ImGui.GetIO().FontGlobalScale = configuration.GlobalUiScale;
this.SetupFonts();
if (!configuration.IsDocking)
{
ImGui.GetIO().ConfigFlags &= ~ImGuiConfigFlags.DockingEnable;
@ -833,10 +833,15 @@ namespace Dalamud.Interface.Internal
for (int i = customFontFirstConfigIndex, i_ = ioFonts.ConfigData.Size; i < i_; i++)
{
var config = ioFonts.ConfigData[i];
if (gameFontManager.OwnsFont(config.DstFont))
continue;
config.OversampleH = 1;
config.OversampleV = 1;
var name = Encoding.UTF8.GetString((byte*)config.Name.Data, config.Name.Count).TrimEnd('\0');
if (name.IsNullOrEmpty())
name = $"{config.SizePixels}px";
// ImFont information is reflected only if corresponding ImFontConfig has MergeMode not set.
if (config.MergeMode)
@ -874,6 +879,7 @@ namespace Dalamud.Interface.Internal
Log.Verbose("[FONT] ImGui.IO.Build will be called.");
ioFonts.Build();
gameFontManager.AfterIoFontsBuild();
Log.Verbose("[FONT] ImGui.IO.Build OK!");
if (ioFonts.TexHeight > maxTexDimension)
@ -891,6 +897,7 @@ namespace Dalamud.Interface.Internal
if (possibilityForScaling && !disableBigFonts)
{
Log.Information("[FONT] Atlas size is {0}x{1} which is bigger than allowed {2}x{3}. Retrying with minimized font sizes.", ioFonts.TexWidth, ioFonts.TexHeight, maxTexDimension, maxTexDimension);
this.IsFallbackFontMode = true;
this.SetupFonts(true);
return;
}
@ -900,7 +907,8 @@ namespace Dalamud.Interface.Internal
}
}
this.IsFallbackFontMode = disableBigFonts;
if (!disableBigFonts)
this.IsFallbackFontMode = false;
if (Math.Abs(fontGamma - 1.0f) >= 0.001)
{
@ -934,9 +942,10 @@ namespace Dalamud.Interface.Internal
if (mod.Axis == TargetFontModification.AxisMode.Overwrite)
{
Log.Verbose("[FONT] {0}: Overwrite from AXIS of size {1}px (was {2}px)", mod.Name, mod.SourceAxis.ImFont.FontSize, font.FontSize);
font.FontSize = mod.SourceAxis.ImFont.FontSize;
font.Ascent = mod.SourceAxis.ImFont.Ascent;
font.Descent = mod.SourceAxis.ImFont.Descent;
GameFontManager.UnscaleFont(font, font.FontSize / mod.SourceAxis.ImFont.FontSize, false);
var ascentDiff = mod.SourceAxis.ImFont.Ascent - font.Ascent;
font.Ascent += ascentDiff;
font.Descent = ascentDiff;
font.FallbackChar = mod.SourceAxis.ImFont.FallbackChar;
font.EllipsisChar = mod.SourceAxis.ImFont.EllipsisChar;
ImGuiHelpers.CopyGlyphsAcrossFonts(mod.SourceAxis.ImFont, font, false, false);
@ -969,6 +978,9 @@ namespace Dalamud.Interface.Internal
this.AfterBuildFonts?.Invoke();
Log.Verbose("[FONT] OnAfterBuildFonts OK!");
if (ioFonts.Fonts[0].NativePtr != DefaultFont.NativePtr)
Log.Warning("[FONT] First font is not DefaultFont");
Log.Verbose("[FONT] Fonts built!");
this.fontBuildSignal.Set();

View file

@ -375,7 +375,9 @@ namespace Dalamud.Interface.Internal.Windows
ImGui.PushStyleColor(ImGuiCol.Text, ImGuiColors.DalamudGrey);
ImGui.TextWrapped(string.Format(
Loc.Localize("DalamudSettingsFontResolutionLevelHint", "This option allows Dalamud fonts to look better. If your game crashes when changing this option, your PC does not support high font resolutions in Dalamud - you will have to use a lower one.\nCurrent font atlas size is {0}px * {1}px."),
Loc.Localize(
"DalamudSettingsFontResolutionLevelHint",
"This option allows Dalamud fonts to look better.\n* If your game crashes right away when changing this option, your PC does not support high font resolutions in Dalamud - you will have to use a lower one.\n* If it doesn't crash immediately, then you can keep the new choice indefinitely as it's not going to crash your game once it worked.\n* Either choose the 3rd or 5th option. Use other options only when neither works well.\n* Current font atlas size is {0}px * {1}px."),
ImGui.GetIO().Fonts.TexWidth,
ImGui.GetIO().Fonts.TexHeight));
ImGui.PopStyleColor();