From b2ff63a4cc6b0589a41c9170da5bd6c33bcf14c8 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Mon, 7 Mar 2022 13:47:37 +0900 Subject: [PATCH 1/8] Show size appropriate fonts (cherry picked from commit be65907beb7c0ab9041f1e13d2d121a273a4ec52) --- .../Interface/GameFonts/GameFontManager.cs | 60 +++- .../Interface/Internal/InterfaceManager.cs | 280 ++++++++++++++---- .../Internal/Windows/SettingsWindow.cs | 2 +- .../Internal/Windows/TitleScreenMenuWindow.cs | 75 +++-- 4 files changed, 320 insertions(+), 97 deletions(-) diff --git a/Dalamud/Interface/GameFonts/GameFontManager.cs b/Dalamud/Interface/GameFonts/GameFontManager.cs index ba9253a9b..b7e208af1 100644 --- a/Dalamud/Interface/GameFonts/GameFontManager.cs +++ b/Dalamud/Interface/GameFonts/GameFontManager.cs @@ -117,18 +117,21 @@ namespace Dalamud.Interface.GameFonts /// Target font. /// Whether to copy missing glyphs only. /// Whether to call target.BuildLookupTable(). - public static void CopyGlyphsAcrossFonts(ImFontPtr? source, ImFontPtr? target, bool missingOnly, bool rebuildLookupTable) + /// Low codepoint range to copy. + /// High codepoing range to copy. + public static void CopyGlyphsAcrossFonts(ImFontPtr? source, ImFontPtr? target, bool missingOnly, bool rebuildLookupTable, int rangeLow = 32, int rangeHigh = 0xFFFE) { if (!source.HasValue || !target.HasValue) return; + var scale = target.Value!.FontSize / source.Value!.FontSize; unsafe { var glyphs = (ImFontGlyphReal*)source.Value!.Glyphs.Data; for (int j = 0, j_ = source.Value!.Glyphs.Size; j < j_; j++) { var glyph = &glyphs[j]; - if (glyph->Codepoint < 32 || glyph->Codepoint >= 0xFFFF) + if (glyph->Codepoint < rangeLow || glyph->Codepoint > rangeHigh) continue; var prevGlyphPtr = (ImFontGlyphReal*)target.Value!.FindGlyphNoFallback((ushort)glyph->Codepoint).NativePtr; @@ -137,27 +140,27 @@ namespace Dalamud.Interface.GameFonts target.Value!.AddGlyph( target.Value!.ConfigData, (ushort)glyph->Codepoint, - glyph->X0, - glyph->Y0, - glyph->X0 + ((glyph->X1 - glyph->X0) * target.Value!.FontSize / source.Value!.FontSize), - glyph->Y0 + ((glyph->Y1 - glyph->Y0) * target.Value!.FontSize / source.Value!.FontSize), + glyph->X0 * scale, + glyph->Y0 * scale, + glyph->X1 * scale, + glyph->Y1 * scale, glyph->U0, glyph->V0, glyph->U1, glyph->V1, - glyph->AdvanceX * target.Value!.FontSize / source.Value!.FontSize); + glyph->AdvanceX * scale); } else if (!missingOnly) { - prevGlyphPtr->X0 = glyph->X0; - prevGlyphPtr->Y0 = glyph->Y0; - prevGlyphPtr->X1 = glyph->X0 + ((glyph->X1 - glyph->X0) * target.Value!.FontSize / source.Value!.FontSize); - prevGlyphPtr->Y1 = glyph->Y0 + ((glyph->Y1 - glyph->Y0) * target.Value!.FontSize / source.Value!.FontSize); + prevGlyphPtr->X0 = glyph->X0 * scale; + prevGlyphPtr->Y0 = glyph->Y0 * scale; + prevGlyphPtr->X1 = glyph->X1 * scale; + prevGlyphPtr->Y1 = glyph->Y1 * scale; prevGlyphPtr->U0 = glyph->U0; prevGlyphPtr->V0 = glyph->V0; prevGlyphPtr->U1 = glyph->U1; prevGlyphPtr->V1 = glyph->V1; - prevGlyphPtr->AdvanceX = glyph->AdvanceX * target.Value!.FontSize / source.Value!.FontSize; + prevGlyphPtr->AdvanceX = glyph->AdvanceX * scale; } } } @@ -166,6 +169,39 @@ namespace Dalamud.Interface.GameFonts target.Value!.BuildLookupTable(); } + /// + /// Unscales fonts after they have been rendered onto atlas. + /// + /// Font to unscale. + /// Scale factor. + /// Whether to call target.BuildLookupTable(). + public static void UnscaleFont(ImFontPtr fontPtr, float fontScale, bool rebuildLookupTable = true) + { + unsafe + { + var font = fontPtr.NativePtr; + for (int i = 0, i_ = font->IndexAdvanceX.Size; i < i_; ++i) + ((float*)font->IndexAdvanceX.Data)[i] /= fontScale; + font->FallbackAdvanceX /= fontScale; + font->FontSize /= fontScale; + font->Ascent /= fontScale; + font->Descent /= fontScale; + var glyphs = (ImFontGlyphReal*)font->Glyphs.Data; + for (int i = 0, i_ = font->Glyphs.Size; i < i_; i++) + { + var glyph = &glyphs[i]; + glyph->X0 /= fontScale; + glyph->X1 /= fontScale; + glyph->Y0 /= fontScale; + glyph->Y1 /= fontScale; + glyph->AdvanceX /= fontScale; + } + } + + if (rebuildLookupTable) + fontPtr.BuildLookupTable(); + } + /// public void Dispose() { diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index 0c6d99efa..6f5477463 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -47,8 +47,13 @@ namespace Dalamud.Interface.Internal /// internal class InterfaceManager : IDisposable { + private const float DefaultFontSizePt = 12.0f; + private const float DefaultFontSizePx = DefaultFontSizePt * 4.0f / 3.0f; + private readonly string rtssPath; + private readonly HashSet glyphRequests = new(); + private readonly Hook presentHook; private readonly Hook resizeBuffersHook; private readonly Hook setCursorHook; @@ -58,6 +63,7 @@ namespace Dalamud.Interface.Internal private RawDX11Scene? scene; private GameFontHandle? axisFontHandle; + private bool overwriteAllNotoGlyphsWithAxis; // can't access imgui IO before first present call private bool lastWantCapture = false; @@ -337,6 +343,69 @@ namespace Dalamud.Interface.Internal this.fontBuildSignal.WaitOne(); } + /// + /// Requests a default font of specified size to exist. + /// + /// Font size in pixels. + /// Ranges of glyphs. + /// Requets handle. + public SpecialGlyphRequest NewFontSizeRef(float size, List> ranges) + { + var allContained = true; + var fonts = ImGui.GetIO().Fonts.Fonts; + ImFontPtr foundFont = null; + unsafe + { + for (int i = 0, i_ = fonts.Size; allContained && i < i_; i++) + { + if (!this.glyphRequests.Any(x => x.FontInternal.NativePtr == fonts[i].NativePtr)) + continue; + + foreach (var range in ranges) + { + if (!allContained) + break; + + for (var j = range.Item1; j <= range.Item2 && allContained; j++) + allContained &= fonts[i].FindGlyphNoFallback(j).NativePtr != null; + } + + if (allContained) + foundFont = fonts[i]; + + break; + } + } + + var req = new SpecialGlyphRequest(this, size, ranges); + req.FontInternal = foundFont; + + if (!allContained) + this.RebuildFonts(); + + return req; + } + + /// + /// Requests a default font of specified size to exist. + /// + /// Font size in pixels. + /// Text to calculate glyph ranges from. + /// Requets handle. + public SpecialGlyphRequest NewFontSizeRef(float size, string text) + { + List> ranges = new(); + foreach (var c in new SortedSet(text.ToHashSet())) + { + if (ranges.Any() && ranges[^1].Item2 + 1 == c) + ranges[^1] = Tuple.Create(ranges[^1].Item1, c); + else + ranges.Add(Tuple.Create(c, c)); + } + + return this.NewFontSizeRef(size, ranges); + } + private static void ShowFontError(string path) { Util.Fatal($"One or more files required by XIVLauncher were not found.\nPlease restart and report this error if it occurs again.\n\n{path}", "Error"); @@ -345,21 +414,15 @@ namespace Dalamud.Interface.Internal private void SetAxisFonts() { var configuration = Service.Get(); - if (configuration.UseAxisFontsFromGame) - { - var currentFamilyAndSize = GameFontStyle.GetRecommendedFamilyAndSize(GameFontFamily.Axis, this.axisFontHandle?.Style.Size ?? 0f); - var expectedFamilyAndSize = GameFontStyle.GetRecommendedFamilyAndSize(GameFontFamily.Axis, 12 * ImGui.GetIO().FontGlobalScale); - if (currentFamilyAndSize == expectedFamilyAndSize) - return; + this.overwriteAllNotoGlyphsWithAxis = configuration.UseAxisFontsFromGame; + var currentFamilyAndSize = GameFontStyle.GetRecommendedFamilyAndSize(GameFontFamily.Axis, this.axisFontHandle?.Style.Size ?? 0f); + var expectedFamilyAndSize = GameFontStyle.GetRecommendedFamilyAndSize(GameFontFamily.Axis, DefaultFontSizePt * ImGui.GetIO().FontGlobalScale); + if (currentFamilyAndSize != expectedFamilyAndSize) + { this.axisFontHandle?.Dispose(); this.axisFontHandle = Service.Get().NewFontRef(new(expectedFamilyAndSize)); } - else - { - this.axisFontHandle?.Dispose(); - this.axisFontHandle = null; - } } /* @@ -531,64 +594,95 @@ namespace Dalamud.Interface.Internal private unsafe void SetupFonts() { var dalamud = Service.Get(); - var ioFonts = ImGui.GetIO().Fonts; + var io = ImGui.GetIO(); + var ioFonts = io.Fonts; + var fontScale = io.FontGlobalScale; var fontGamma = this.FontGamma; + List fontsToUnscale = new(); this.fontBuildSignal.Reset(); - ioFonts.Clear(); - ImFontConfigPtr fontConfig = ImGuiNative.ImFontConfig_ImFontConfig(); - fontConfig.PixelSnapH = true; - var fontPathJp = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "NotoSansCJKjp-Medium.otf"); - if (!File.Exists(fontPathJp)) ShowFontError(fontPathJp); - var japaneseRangeHandle = GCHandle.Alloc(GlyphRangesJapanese.GlyphRanges, GCHandleType.Pinned); + // Default font + { + var japaneseRangeHandle = GCHandle.Alloc(GlyphRangesJapanese.GlyphRanges, GCHandleType.Pinned); + DefaultFont = ioFonts.AddFontFromFileTTF(fontPathJp, (DefaultFontSizePx + 1) * fontScale, null, japaneseRangeHandle.AddrOfPinnedObject()); + japaneseRangeHandle.Free(); + fontsToUnscale.Add(DefaultFont); + } - DefaultFont = ioFonts.AddFontFromFileTTF(fontPathJp, 17.0f, null, japaneseRangeHandle.AddrOfPinnedObject()); + // FontAwesome icon font + { + var fontPathIcon = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "FontAwesome5FreeSolid.otf"); + if (!File.Exists(fontPathIcon)) + ShowFontError(fontPathIcon); - var fontPathGame = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "gamesym.ttf"); + var iconRangeHandle = GCHandle.Alloc(new ushort[] { 0xE000, 0xF8FF, 0, }, GCHandleType.Pinned); + IconFont = ioFonts.AddFontFromFileTTF(fontPathIcon, DefaultFontSizePx * fontScale, null, iconRangeHandle.AddrOfPinnedObject()); + iconRangeHandle.Free(); + fontsToUnscale.Add(IconFont); + } - if (!File.Exists(fontPathGame)) - ShowFontError(fontPathGame); + // Monospace font + { + var fontPathMono = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "Inconsolata-Regular.ttf"); + if (!File.Exists(fontPathMono)) + ShowFontError(fontPathMono); + MonoFont = ioFonts.AddFontFromFileTTF(fontPathMono, DefaultFontSizePx * fontScale); + fontsToUnscale.Add(MonoFont); + } - var gameRangeHandle = GCHandle.Alloc( - new ushort[] + // Default font but in requested size for requested glyphs + { + Dictionary> extraFontRequests = new(); + foreach (var extraFontRequest in this.glyphRequests) { - 0xE020, - 0xE0DB, - 0, - }, - GCHandleType.Pinned); + if (!extraFontRequests.ContainsKey(extraFontRequest.Size)) + extraFontRequests[extraFontRequest.Size] = new(); + extraFontRequests[extraFontRequest.Size].Add(extraFontRequest); + } - fontConfig.MergeMode = false; - ioFonts.AddFontFromFileTTF(fontPathGame, 17.0f, fontConfig, gameRangeHandle.AddrOfPinnedObject()); - fontConfig.MergeMode = true; - - var fontPathIcon = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "FontAwesome5FreeSolid.otf"); - - if (!File.Exists(fontPathIcon)) - ShowFontError(fontPathIcon); - - var iconRangeHandle = GCHandle.Alloc( - new ushort[] + foreach (var (fontSize, requests) in extraFontRequests) { - 0xE000, - 0xF8FF, - 0, - }, - GCHandleType.Pinned); - IconFont = ioFonts.AddFontFromFileTTF(fontPathIcon, 17.0f, null, iconRangeHandle.AddrOfPinnedObject()); + List> codepointRanges = new(); + foreach (var request in requests) + { + foreach (var range in request.CodepointRanges) + codepointRanges.Add(range); + } - var fontPathMono = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "Inconsolata-Regular.ttf"); + codepointRanges.Sort((x, y) => (x.Item1 == y.Item1 ? (x.Item2 < y.Item2 ? -1 : (x.Item2 == y.Item2 ? 0 : 1)) : (x.Item1 < y.Item1 ? -1 : 1))); - if (!File.Exists(fontPathMono)) - ShowFontError(fontPathMono); + List flattenedRanges = new(); + foreach (var range in codepointRanges) + { + if (flattenedRanges.Any() && flattenedRanges[^1] >= range.Item1 - 1) + { + flattenedRanges[^1] = Math.Max(flattenedRanges[^1], range.Item2); + } + else + { + flattenedRanges.Add(range.Item1); + flattenedRanges.Add(range.Item2); + } + } - MonoFont = ioFonts.AddFontFromFileTTF(fontPathMono, 16.0f); + flattenedRanges.Add(0); + + var rangeHandle = GCHandle.Alloc(flattenedRanges.ToArray(), GCHandleType.Pinned); + var sizedFont = ioFonts.AddFontFromFileTTF(fontPathJp, fontSize * fontScale, null, rangeHandle.AddrOfPinnedObject()); + rangeHandle.Free(); + + fontsToUnscale.Add(sizedFont); + + foreach (var request in requests) + request.FontInternal = sizedFont; + } + } var gameFontManager = Service.Get(); gameFontManager.BuildFonts(); @@ -612,8 +706,27 @@ namespace Dalamud.Interface.Internal texPixels[i] = (byte)(Math.Pow(texPixels[i] / 255.0f, 1.0f / fontGamma) * 255.0f); } + foreach (var font in fontsToUnscale) + GameFontManager.UnscaleFont(font, fontScale, false); + gameFontManager.AfterBuildFonts(); - GameFontManager.CopyGlyphsAcrossFonts(this.axisFontHandle?.ImFont, DefaultFont, false, true); + + foreach (var font in fontsToUnscale) + { + if (font.NativePtr == MonoFont.NativePtr || font.NativePtr == IconFont.NativePtr) + continue; + + if (this.overwriteAllNotoGlyphsWithAxis) + GameFontManager.CopyGlyphsAcrossFonts(this.axisFontHandle?.ImFont, font, false, false); + else + GameFontManager.CopyGlyphsAcrossFonts(this.axisFontHandle?.ImFont, font, false, false, 0xE020, 0xE0DB); + } + + // Fill missing glyphs in MonoFont from DefaultFont + GameFontManager.CopyGlyphsAcrossFonts(DefaultFont, MonoFont, true, false); + + foreach (var font in fontsToUnscale) + font.BuildLookupTable(); Log.Verbose("[FONT] Invoke OnAfterBuildFonts"); this.AfterBuildFonts?.Invoke(); @@ -623,11 +736,6 @@ namespace Dalamud.Interface.Internal this.fontBuildSignal.Set(); - fontConfig.Destroy(); - japaneseRangeHandle.Free(); - gameRangeHandle.Free(); - iconRangeHandle.Free(); - this.FontsReady = true; } @@ -765,5 +873,65 @@ namespace Dalamud.Interface.Internal Service.Get().Draw(); } + + /// + /// Represents a glyph request. + /// + public class SpecialGlyphRequest : IDisposable + { + /// + /// Initializes a new instance of the class. + /// + /// InterfaceManager to associate. + /// Font size in pixels. + /// Codepoint ranges. + internal SpecialGlyphRequest(InterfaceManager manager, float size, List> ranges) + { + this.Manager = manager; + this.Size = size; + this.CodepointRanges = ranges; + this.Manager.glyphRequests.Add(this); + } + + /// + /// Gets the font of specified size, or DefaultFont if it's not ready yet. + /// + public ImFontPtr Font + { + get + { + unsafe + { + return this.FontInternal.NativePtr == null ? DefaultFont : this.FontInternal; + } + } + } + + /// + /// Gets or sets the associated ImFont. + /// + internal ImFontPtr FontInternal { get; set; } + + /// + /// Gets associated InterfaceManager. + /// + internal InterfaceManager Manager { get; init; } + + /// + /// Gets font size. + /// + internal float Size { get; init; } + + /// + /// Gets codepoint ranges. + /// + internal List> CodepointRanges { get; init; } + + /// + public void Dispose() + { + this.Manager.glyphRequests.Remove(this); + } + } } } diff --git a/Dalamud/Interface/Internal/Windows/SettingsWindow.cs b/Dalamud/Interface/Internal/Windows/SettingsWindow.cs index 48104ed6a..9ff94f42e 100644 --- a/Dalamud/Interface/Internal/Windows/SettingsWindow.cs +++ b/Dalamud/Interface/Internal/Windows/SettingsWindow.cs @@ -26,7 +26,7 @@ namespace Dalamud.Interface.Internal.Windows internal class SettingsWindow : Window { private const float MinScale = 0.3f; - private const float MaxScale = 2.0f; + private const float MaxScale = 3.0f; private readonly string[] languages; private readonly string[] locLanguages; diff --git a/Dalamud/Interface/Internal/Windows/TitleScreenMenuWindow.cs b/Dalamud/Interface/Internal/Windows/TitleScreenMenuWindow.cs index b4c089fbe..7ea7b64dd 100644 --- a/Dalamud/Interface/Internal/Windows/TitleScreenMenuWindow.cs +++ b/Dalamud/Interface/Internal/Windows/TitleScreenMenuWindow.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Numerics; using Dalamud.Configuration.Internal; @@ -20,17 +21,18 @@ namespace Dalamud.Interface.Internal.Windows /// internal class TitleScreenMenuWindow : Window, IDisposable { - private const float TargetFontSize = 16.2f; + private const float TargetFontSizePt = 18f; + private const float TargetFontSizePx = TargetFontSizePt * 4 / 3; + private readonly TextureWrap shadeTexture; private readonly Dictionary shadeEasings = new(); private readonly Dictionary moveEasings = new(); private readonly Dictionary logoEasings = new(); + private readonly Dictionary specialGlyphRequests = new(); private InOutCubic? fadeOutEasing; - private GameFontHandle? axisFontHandle; - private State state = State.Hide; /// @@ -71,19 +73,14 @@ namespace Dalamud.Interface.Internal.Windows /// public override void PreDraw() { - this.SetAxisFonts(); ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0, 0)); ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, new Vector2(0, 0)); - if (this.axisFontHandle?.Available ?? false) - ImGui.PushFont(this.axisFontHandle.ImFont); base.PreDraw(); } /// public override void PostDraw() { - if (this.axisFontHandle?.Available ?? false) - ImGui.PopFont(); ImGui.PopStyleVar(2); base.PostDraw(); } @@ -99,7 +96,7 @@ namespace Dalamud.Interface.Internal.Windows /// public override void Draw() { - ImGui.SetWindowFontScale(TargetFontSize / ImGui.GetFont().FontSize * 4 / 3); + var scale = ImGui.GetIO().FontGlobalScale; var tsm = Service.Get(); @@ -129,7 +126,7 @@ namespace Dalamud.Interface.Internal.Windows moveEasing.Update(); - var finalPos = (i + 1) * this.shadeTexture.Height; + var finalPos = (i + 1) * this.shadeTexture.Height * scale; var pos = moveEasing.Value * finalPos; // FIXME(goat): Sometimes, easings can overshoot and bring things out of alignment. @@ -180,7 +177,7 @@ namespace Dalamud.Interface.Internal.Windows { var entry = tsm.Entries[i]; - var finalPos = (i + 1) * this.shadeTexture.Height; + var finalPos = (i + 1) * this.shadeTexture.Height * scale; this.DrawEntry(entry, i != 0, true, i == 0, false); @@ -222,26 +219,35 @@ namespace Dalamud.Interface.Internal.Windows break; } } - } - private void SetAxisFonts() - { - var configuration = Service.Get(); - if (configuration.UseAxisFontsFromGame) + var srcText = tsm.Entries.Select(e => e.Name).ToHashSet(); + var keys = this.specialGlyphRequests.Keys.ToHashSet(); + keys.RemoveWhere(x => srcText.Contains(x)); + foreach (var key in keys) { - if (this.axisFontHandle == null) - this.axisFontHandle = Service.Get().NewFontRef(new(GameFontFamily.Axis, TargetFontSize)); - } - else - { - this.axisFontHandle?.Dispose(); - this.axisFontHandle = null; + this.specialGlyphRequests[key].Dispose(); + this.specialGlyphRequests.Remove(key); } } private bool DrawEntry( TitleScreenMenu.TitleScreenMenuEntry entry, bool inhibitFadeout, bool showText, bool isFirst, bool overrideAlpha) { + InterfaceManager.SpecialGlyphRequest fontHandle; + if (this.specialGlyphRequests.TryGetValue(entry.Name, out fontHandle) && fontHandle.Size != TargetFontSizePx) + { + fontHandle.Dispose(); + this.specialGlyphRequests.Remove(entry.Name); + fontHandle = null; + } + + if (fontHandle == null) + this.specialGlyphRequests[entry.Name] = fontHandle = Service.Get().NewFontSizeRef(TargetFontSizePx, entry.Name); + + ImGui.PushFont(fontHandle.Font); + + var scale = ImGui.GetIO().FontGlobalScale; + if (!this.shadeEasings.TryGetValue(entry.Id, out var shadeEasing)) { shadeEasing = new InOutCubic(TimeSpan.FromMilliseconds(350)); @@ -251,7 +257,7 @@ namespace Dalamud.Interface.Internal.Windows var initialCursor = ImGui.GetCursorPos(); ImGui.PushStyleVar(ImGuiStyleVar.Alpha, (float)shadeEasing.Value); - ImGui.Image(this.shadeTexture.ImGuiHandle, new Vector2(this.shadeTexture.Width, this.shadeTexture.Height)); + ImGui.Image(this.shadeTexture.ImGuiHandle, new Vector2(this.shadeTexture.Width * scale, this.shadeTexture.Height * scale)); ImGui.PopStyleVar(); var isHover = ImGui.IsItemHovered(); @@ -305,7 +311,7 @@ namespace Dalamud.Interface.Internal.Windows ImGui.PushStyleVar(ImGuiStyleVar.Alpha, 1f); } - ImGui.Image(entry.Texture.ImGuiHandle, new Vector2(TitleScreenMenu.TextureSize)); + ImGui.Image(entry.Texture.ImGuiHandle, new Vector2(TitleScreenMenu.TextureSize * scale)); if (overrideAlpha || isFirst) { ImGui.PopStyleVar(); @@ -319,23 +325,36 @@ namespace Dalamud.Interface.Internal.Windows var textHeight = ImGui.GetTextLineHeightWithSpacing(); var cursor = ImGui.GetCursorPos(); - cursor.Y += (entry.Texture.Height / 2) - (textHeight / 2); - ImGui.SetCursorPos(cursor); + cursor.Y += (entry.Texture.Height * scale / 2) - (textHeight / 2); if (overrideAlpha) { ImGui.PushStyleVar(ImGuiStyleVar.Alpha, showText ? (float)logoEasing.Value : 0f); } + // Drop shadow + ImGui.PushStyleColor(ImGuiCol.Text, 0xFF000000); + for (int i = 0, i_ = (int)Math.Ceiling(1 * scale); i < i_; i++) + { + ImGui.SetCursorPos(new Vector2(cursor.X, cursor.Y + i)); + ImGui.Text(entry.Name); + } + + ImGui.PopStyleColor(); + + ImGui.SetCursorPos(cursor); ImGui.Text(entry.Name); + if (overrideAlpha) { ImGui.PopStyleVar(); } - initialCursor.Y += entry.Texture.Height; + initialCursor.Y += entry.Texture.Height * scale; ImGui.SetCursorPos(initialCursor); + ImGui.PopFont(); + return isHover; } From 1f20b3c35af682e8cb69af27579e0db619895763 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Mon, 7 Mar 2022 14:05:20 +0900 Subject: [PATCH 2/8] Don't oversample fonts, and use geta mark to indicate unavailable glyph (cherry picked from commit e22b168c531b349a52e3577f2520c787e55efcd4) --- .../Interface/GameFonts/GameFontManager.cs | 62 +++++++++++-------- .../Interface/Internal/InterfaceManager.cs | 32 ++++++++-- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/Dalamud/Interface/GameFonts/GameFontManager.cs b/Dalamud/Interface/GameFonts/GameFontManager.cs index b7e208af1..6b00be2e7 100644 --- a/Dalamud/Interface/GameFonts/GameFontManager.cs +++ b/Dalamud/Interface/GameFonts/GameFontManager.cs @@ -284,39 +284,49 @@ namespace Dalamud.Interface.GameFonts /// public void BuildFonts() { - var io = ImGui.GetIO(); - io.Fonts.TexDesiredWidth = 4096; - - this.glyphRectIds.Clear(); - this.fonts.Clear(); - - foreach (var style in this.fontUseCounter.Keys) + unsafe { - var rectIds = this.glyphRectIds[style] = new(); + ImFontConfigPtr fontConfig = ImGuiNative.ImFontConfig_ImFontConfig(); + fontConfig.OversampleH = 1; + fontConfig.OversampleV = 1; + fontConfig.PixelSnapH = true; - var fdt = this.fdts[(int)style.FamilyAndSize]; - if (fdt == null) - continue; + var io = ImGui.GetIO(); + io.Fonts.TexDesiredWidth = 4096; - var font = io.Fonts.AddFontDefault(); - this.fonts[style] = font; - foreach (var glyph in fdt.Glyphs) + this.glyphRectIds.Clear(); + this.fonts.Clear(); + + foreach (var style in this.fontUseCounter.Keys) { - var c = glyph.Char; - if (c < 32 || c >= 0xFFFF) + var rectIds = this.glyphRectIds[style] = new(); + + var fdt = this.fdts[(int)style.FamilyAndSize]; + if (fdt == null) continue; - var widthAdjustment = style.CalculateWidthAdjustment(fdt, glyph); - rectIds[c] = Tuple.Create( - io.Fonts.AddCustomRectFontGlyph( - font, - c, - glyph.BoundingWidth + widthAdjustment + 1, - glyph.BoundingHeight + 1, - glyph.AdvanceWidth, - new Vector2(0, glyph.CurrentOffsetY)), - glyph); + var font = io.Fonts.AddFontDefault(fontConfig); + this.fonts[style] = font; + foreach (var glyph in fdt.Glyphs) + { + var c = glyph.Char; + if (c < 32 || c >= 0xFFFF) + continue; + + var widthAdjustment = style.CalculateWidthAdjustment(fdt, glyph); + rectIds[c] = Tuple.Create( + io.Fonts.AddCustomRectFontGlyph( + font, + c, + glyph.BoundingWidth + widthAdjustment + 1, + glyph.BoundingHeight + 1, + glyph.AdvanceWidth, + new Vector2(0, glyph.CurrentOffsetY)), + glyph); + } } + + fontConfig.Destroy(); } } diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index 6f5477463..9c7d5b151 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -49,6 +49,8 @@ namespace Dalamud.Interface.Internal { private const float DefaultFontSizePt = 12.0f; private const float DefaultFontSizePx = DefaultFontSizePt * 4.0f / 3.0f; + private const ushort Fallback1Codepoint = 0x3013; // Geta mark; FFXIV uses this to indicate that a glyph is missing. + private const ushort Fallback2Codepoint = '-'; // FFXIV uses dash if Geta mark is unavailable. private readonly string rtssPath; @@ -603,6 +605,11 @@ namespace Dalamud.Interface.Internal this.fontBuildSignal.Reset(); ioFonts.Clear(); + ImFontConfigPtr fontConfig = ImGuiNative.ImFontConfig_ImFontConfig(); + fontConfig.OversampleH = 1; + fontConfig.OversampleV = 1; + fontConfig.PixelSnapH = true; + var fontPathJp = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "NotoSansCJKjp-Medium.otf"); if (!File.Exists(fontPathJp)) ShowFontError(fontPathJp); @@ -610,7 +617,7 @@ namespace Dalamud.Interface.Internal // Default font { var japaneseRangeHandle = GCHandle.Alloc(GlyphRangesJapanese.GlyphRanges, GCHandleType.Pinned); - DefaultFont = ioFonts.AddFontFromFileTTF(fontPathJp, (DefaultFontSizePx + 1) * fontScale, null, japaneseRangeHandle.AddrOfPinnedObject()); + DefaultFont = ioFonts.AddFontFromFileTTF(fontPathJp, (DefaultFontSizePx + 1) * fontScale, fontConfig, japaneseRangeHandle.AddrOfPinnedObject()); japaneseRangeHandle.Free(); fontsToUnscale.Add(DefaultFont); } @@ -622,7 +629,7 @@ namespace Dalamud.Interface.Internal ShowFontError(fontPathIcon); var iconRangeHandle = GCHandle.Alloc(new ushort[] { 0xE000, 0xF8FF, 0, }, GCHandleType.Pinned); - IconFont = ioFonts.AddFontFromFileTTF(fontPathIcon, DefaultFontSizePx * fontScale, null, iconRangeHandle.AddrOfPinnedObject()); + IconFont = ioFonts.AddFontFromFileTTF(fontPathIcon, DefaultFontSizePx * fontScale, fontConfig, iconRangeHandle.AddrOfPinnedObject()); iconRangeHandle.Free(); fontsToUnscale.Add(IconFont); } @@ -632,7 +639,7 @@ namespace Dalamud.Interface.Internal var fontPathMono = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "Inconsolata-Regular.ttf"); if (!File.Exists(fontPathMono)) ShowFontError(fontPathMono); - MonoFont = ioFonts.AddFontFromFileTTF(fontPathMono, DefaultFontSizePx * fontScale); + MonoFont = ioFonts.AddFontFromFileTTF(fontPathMono, DefaultFontSizePx * fontScale, fontConfig); fontsToUnscale.Add(MonoFont); } @@ -649,6 +656,9 @@ namespace Dalamud.Interface.Internal foreach (var (fontSize, requests) in extraFontRequests) { List> codepointRanges = new(); + codepointRanges.Add(Tuple.Create(Fallback1Codepoint, Fallback1Codepoint)); + codepointRanges.Add(Tuple.Create(Fallback2Codepoint, Fallback2Codepoint)); + foreach (var request in requests) { foreach (var range in request.CodepointRanges) @@ -674,7 +684,7 @@ namespace Dalamud.Interface.Internal flattenedRanges.Add(0); var rangeHandle = GCHandle.Alloc(flattenedRanges.ToArray(), GCHandleType.Pinned); - var sizedFont = ioFonts.AddFontFromFileTTF(fontPathJp, fontSize * fontScale, null, rangeHandle.AddrOfPinnedObject()); + var sizedFont = ioFonts.AddFontFromFileTTF(fontPathJp, fontSize * fontScale, fontConfig, rangeHandle.AddrOfPinnedObject()); rangeHandle.Free(); fontsToUnscale.Add(sizedFont); @@ -713,7 +723,12 @@ namespace Dalamud.Interface.Internal foreach (var font in fontsToUnscale) { - if (font.NativePtr == MonoFont.NativePtr || font.NativePtr == IconFont.NativePtr) + // Leave IconFont alone. + if (font.NativePtr == IconFont.NativePtr) + continue; + + // MonoFont will be filled later from DefaultFont. + if (font.NativePtr == MonoFont.NativePtr) continue; if (this.overwriteAllNotoGlyphsWithAxis) @@ -722,11 +737,17 @@ namespace Dalamud.Interface.Internal GameFontManager.CopyGlyphsAcrossFonts(this.axisFontHandle?.ImFont, font, false, false, 0xE020, 0xE0DB); } + // Fill missing glyphs in DefaultFont from Axis + GameFontManager.CopyGlyphsAcrossFonts(this.axisFontHandle?.ImFont, DefaultFont, true, false); + // Fill missing glyphs in MonoFont from DefaultFont GameFontManager.CopyGlyphsAcrossFonts(DefaultFont, MonoFont, true, false); foreach (var font in fontsToUnscale) + { + font.FallbackChar = Fallback1Codepoint; font.BuildLookupTable(); + } Log.Verbose("[FONT] Invoke OnAfterBuildFonts"); this.AfterBuildFonts?.Invoke(); @@ -734,6 +755,7 @@ namespace Dalamud.Interface.Internal Log.Verbose("[FONT] Fonts built!"); + fontConfig.Destroy(); this.fontBuildSignal.Set(); this.FontsReady = true; From cbc0896fade01f7011dd14abf98e3f25a05ac1f7 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Mon, 7 Mar 2022 14:14:30 +0900 Subject: [PATCH 3/8] A bit more of sensible defaults (cherry picked from commit 14dfbf7bcfe17c581331c50254fa1ea5dc9c2c66) --- Dalamud/Interface/GameFonts/GameFontManager.cs | 1 - Dalamud/Interface/Internal/InterfaceManager.cs | 5 +++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Dalamud/Interface/GameFonts/GameFontManager.cs b/Dalamud/Interface/GameFonts/GameFontManager.cs index 6b00be2e7..83178acc9 100644 --- a/Dalamud/Interface/GameFonts/GameFontManager.cs +++ b/Dalamud/Interface/GameFonts/GameFontManager.cs @@ -292,7 +292,6 @@ namespace Dalamud.Interface.GameFonts fontConfig.PixelSnapH = true; var io = ImGui.GetIO(); - io.Fonts.TexDesiredWidth = 4096; this.glyphRectIds.Clear(); this.fonts.Clear(); diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index 9c7d5b151..e5c4cbdb4 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -604,6 +604,7 @@ namespace Dalamud.Interface.Internal this.fontBuildSignal.Reset(); ioFonts.Clear(); + ioFonts.TexDesiredWidth = 4096; ImFontConfigPtr fontConfig = ImGuiNative.ImFontConfig_ImFontConfig(); fontConfig.OversampleH = 1; @@ -658,6 +659,10 @@ namespace Dalamud.Interface.Internal List> codepointRanges = new(); codepointRanges.Add(Tuple.Create(Fallback1Codepoint, Fallback1Codepoint)); codepointRanges.Add(Tuple.Create(Fallback2Codepoint, Fallback2Codepoint)); + + // ImGui default ellipsis characters + codepointRanges.Add(Tuple.Create(0x2026, 0x2026)); + codepointRanges.Add(Tuple.Create(0x0085, 0x0085)); foreach (var request in requests) { From 267a4b0047a88e178affa9a69c70d752fe827b30 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Mon, 7 Mar 2022 14:41:55 +0900 Subject: [PATCH 4/8] Always keep all AXIS fonts in memory (cherry picked from commit f5c75e4cd58b8bbed2e51fe22944b19043a6abaa) --- .../Interface/Internal/InterfaceManager.cs | 47 ++++++++++++------- .../Internal/Windows/TitleScreenMenuWindow.cs | 1 + 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index e5c4cbdb4..a58260c0a 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -64,7 +64,7 @@ namespace Dalamud.Interface.Internal private readonly SwapChainVtableResolver address; private RawDX11Scene? scene; - private GameFontHandle? axisFontHandle; + private GameFontHandle[] axisFontHandles; private bool overwriteAllNotoGlyphsWithAxis; // can't access imgui IO before first present call @@ -353,16 +353,17 @@ namespace Dalamud.Interface.Internal /// Requets handle. public SpecialGlyphRequest NewFontSizeRef(float size, List> ranges) { - var allContained = true; + var allContained = false; var fonts = ImGui.GetIO().Fonts.Fonts; ImFontPtr foundFont = null; unsafe { - for (int i = 0, i_ = fonts.Size; allContained && i < i_; i++) + for (int i = 0, i_ = fonts.Size; i < i_; i++) { if (!this.glyphRequests.Any(x => x.FontInternal.NativePtr == fonts[i].NativePtr)) continue; + allContained = true; foreach (var range in ranges) { if (!allContained) @@ -418,12 +419,16 @@ namespace Dalamud.Interface.Internal var configuration = Service.Get(); this.overwriteAllNotoGlyphsWithAxis = configuration.UseAxisFontsFromGame; - var currentFamilyAndSize = GameFontStyle.GetRecommendedFamilyAndSize(GameFontFamily.Axis, this.axisFontHandle?.Style.Size ?? 0f); - var expectedFamilyAndSize = GameFontStyle.GetRecommendedFamilyAndSize(GameFontFamily.Axis, DefaultFontSizePt * ImGui.GetIO().FontGlobalScale); - if (currentFamilyAndSize != expectedFamilyAndSize) + if (this.axisFontHandles == null) { - this.axisFontHandle?.Dispose(); - this.axisFontHandle = Service.Get().NewFontRef(new(expectedFamilyAndSize)); + this.axisFontHandles = new GameFontHandle[] + { + Service.Get().NewFontRef(new(GameFontFamilyAndSize.Axis96)), + Service.Get().NewFontRef(new(GameFontFamilyAndSize.Axis12)), + Service.Get().NewFontRef(new(GameFontFamilyAndSize.Axis14)), + Service.Get().NewFontRef(new(GameFontFamilyAndSize.Axis18)), + Service.Get().NewFontRef(new(GameFontFamilyAndSize.Axis36)), + }; } } @@ -659,7 +664,7 @@ namespace Dalamud.Interface.Internal List> codepointRanges = new(); codepointRanges.Add(Tuple.Create(Fallback1Codepoint, Fallback1Codepoint)); codepointRanges.Add(Tuple.Create(Fallback2Codepoint, Fallback2Codepoint)); - + // ImGui default ellipsis characters codepointRanges.Add(Tuple.Create(0x2026, 0x2026)); codepointRanges.Add(Tuple.Create(0x0085, 0x0085)); @@ -736,14 +741,24 @@ namespace Dalamud.Interface.Internal if (font.NativePtr == MonoFont.NativePtr) continue; - if (this.overwriteAllNotoGlyphsWithAxis) - GameFontManager.CopyGlyphsAcrossFonts(this.axisFontHandle?.ImFont, font, false, false); - else - GameFontManager.CopyGlyphsAcrossFonts(this.axisFontHandle?.ImFont, font, false, false, 0xE020, 0xE0DB); - } + var axisFont = this.axisFontHandles[^1]; + for (var i = this.axisFontHandles.Length - 2; i >= 0; i--) + { + if (this.axisFontHandles[i].Style.Size >= (font.FontSize - 1) * fontScale * 3 / 4) + axisFont = this.axisFontHandles[i]; + else + break; + } - // Fill missing glyphs in DefaultFont from Axis - GameFontManager.CopyGlyphsAcrossFonts(this.axisFontHandle?.ImFont, DefaultFont, true, false); + if (this.overwriteAllNotoGlyphsWithAxis) + GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, font, false, false); + else + GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, font, false, false, 0xE020, 0xE0DB); + + // Fill missing glyphs in DefaultFont from Axis + if (font.NativePtr == DefaultFont.NativePtr) + GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, DefaultFont, true, false); + } // Fill missing glyphs in MonoFont from DefaultFont GameFontManager.CopyGlyphsAcrossFonts(DefaultFont, MonoFont, true, false); diff --git a/Dalamud/Interface/Internal/Windows/TitleScreenMenuWindow.cs b/Dalamud/Interface/Internal/Windows/TitleScreenMenuWindow.cs index 7ea7b64dd..40bb29672 100644 --- a/Dalamud/Interface/Internal/Windows/TitleScreenMenuWindow.cs +++ b/Dalamud/Interface/Internal/Windows/TitleScreenMenuWindow.cs @@ -245,6 +245,7 @@ namespace Dalamud.Interface.Internal.Windows this.specialGlyphRequests[entry.Name] = fontHandle = Service.Get().NewFontSizeRef(TargetFontSizePx, entry.Name); ImGui.PushFont(fontHandle.Font); + ImGui.SetWindowFontScale(TargetFontSizePx / fontHandle.Size); var scale = ImGui.GetIO().FontGlobalScale; From 0cf9b80172c3514bd48392c07c9777ddbc0064d6 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Wed, 9 Mar 2022 02:54:05 +0900 Subject: [PATCH 5/8] Keep glyph range handles alive until font is built (cherry picked from commit 5baccced15055ee21fd5b848ec386e3907d49c69) --- .../Interface/Internal/InterfaceManager.cs | 299 +++++++++--------- 1 file changed, 157 insertions(+), 142 deletions(-) diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index a58260c0a..8872fc45f 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -611,174 +611,189 @@ namespace Dalamud.Interface.Internal ioFonts.Clear(); ioFonts.TexDesiredWidth = 4096; - ImFontConfigPtr fontConfig = ImGuiNative.ImFontConfig_ImFontConfig(); - fontConfig.OversampleH = 1; - fontConfig.OversampleV = 1; - fontConfig.PixelSnapH = true; + ImFontConfigPtr fontConfig = null; + List garbageList = new(); - var fontPathJp = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "NotoSansCJKjp-Medium.otf"); - if (!File.Exists(fontPathJp)) - ShowFontError(fontPathJp); - - // Default font + try { - var japaneseRangeHandle = GCHandle.Alloc(GlyphRangesJapanese.GlyphRanges, GCHandleType.Pinned); - DefaultFont = ioFonts.AddFontFromFileTTF(fontPathJp, (DefaultFontSizePx + 1) * fontScale, fontConfig, japaneseRangeHandle.AddrOfPinnedObject()); - japaneseRangeHandle.Free(); - fontsToUnscale.Add(DefaultFont); - } + fontConfig = ImGuiNative.ImFontConfig_ImFontConfig(); + fontConfig.OversampleH = 1; + fontConfig.OversampleV = 1; + fontConfig.PixelSnapH = true; - // FontAwesome icon font - { - var fontPathIcon = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "FontAwesome5FreeSolid.otf"); - if (!File.Exists(fontPathIcon)) - ShowFontError(fontPathIcon); + var fontPathJp = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "NotoSansCJKjp-Medium.otf"); + if (!File.Exists(fontPathJp)) + ShowFontError(fontPathJp); - var iconRangeHandle = GCHandle.Alloc(new ushort[] { 0xE000, 0xF8FF, 0, }, GCHandleType.Pinned); - IconFont = ioFonts.AddFontFromFileTTF(fontPathIcon, DefaultFontSizePx * fontScale, fontConfig, iconRangeHandle.AddrOfPinnedObject()); - iconRangeHandle.Free(); - fontsToUnscale.Add(IconFont); - } - - // Monospace font - { - var fontPathMono = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "Inconsolata-Regular.ttf"); - if (!File.Exists(fontPathMono)) - ShowFontError(fontPathMono); - MonoFont = ioFonts.AddFontFromFileTTF(fontPathMono, DefaultFontSizePx * fontScale, fontConfig); - fontsToUnscale.Add(MonoFont); - } - - // Default font but in requested size for requested glyphs - { - Dictionary> extraFontRequests = new(); - foreach (var extraFontRequest in this.glyphRequests) + // Default font { - if (!extraFontRequests.ContainsKey(extraFontRequest.Size)) - extraFontRequests[extraFontRequest.Size] = new(); - extraFontRequests[extraFontRequest.Size].Add(extraFontRequest); + var japaneseRangeHandle = GCHandle.Alloc(GlyphRangesJapanese.GlyphRanges, GCHandleType.Pinned); + garbageList.Add(japaneseRangeHandle); + + DefaultFont = ioFonts.AddFontFromFileTTF(fontPathJp, (DefaultFontSizePx + 1) * fontScale, fontConfig, japaneseRangeHandle.AddrOfPinnedObject()); + fontsToUnscale.Add(DefaultFont); } - foreach (var (fontSize, requests) in extraFontRequests) + // FontAwesome icon font { - List> codepointRanges = new(); - codepointRanges.Add(Tuple.Create(Fallback1Codepoint, Fallback1Codepoint)); - codepointRanges.Add(Tuple.Create(Fallback2Codepoint, Fallback2Codepoint)); + var fontPathIcon = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "FontAwesome5FreeSolid.otf"); + if (!File.Exists(fontPathIcon)) + ShowFontError(fontPathIcon); - // ImGui default ellipsis characters - codepointRanges.Add(Tuple.Create(0x2026, 0x2026)); - codepointRanges.Add(Tuple.Create(0x0085, 0x0085)); + var iconRangeHandle = GCHandle.Alloc(new ushort[] { 0xE000, 0xF8FF, 0, }, GCHandleType.Pinned); + garbageList.Add(iconRangeHandle); - foreach (var request in requests) + IconFont = ioFonts.AddFontFromFileTTF(fontPathIcon, DefaultFontSizePx * fontScale, fontConfig, iconRangeHandle.AddrOfPinnedObject()); + fontsToUnscale.Add(IconFont); + } + + // Monospace font + { + var fontPathMono = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "Inconsolata-Regular.ttf"); + if (!File.Exists(fontPathMono)) + ShowFontError(fontPathMono); + MonoFont = ioFonts.AddFontFromFileTTF(fontPathMono, DefaultFontSizePx * fontScale, fontConfig); + fontsToUnscale.Add(MonoFont); + } + + // Default font but in requested size for requested glyphs + { + Dictionary> extraFontRequests = new(); + foreach (var extraFontRequest in this.glyphRequests) { - foreach (var range in request.CodepointRanges) - codepointRanges.Add(range); + if (!extraFontRequests.ContainsKey(extraFontRequest.Size)) + extraFontRequests[extraFontRequest.Size] = new(); + extraFontRequests[extraFontRequest.Size].Add(extraFontRequest); } - codepointRanges.Sort((x, y) => (x.Item1 == y.Item1 ? (x.Item2 < y.Item2 ? -1 : (x.Item2 == y.Item2 ? 0 : 1)) : (x.Item1 < y.Item1 ? -1 : 1))); - - List flattenedRanges = new(); - foreach (var range in codepointRanges) + foreach (var (fontSize, requests) in extraFontRequests) { - if (flattenedRanges.Any() && flattenedRanges[^1] >= range.Item1 - 1) + List> codepointRanges = new(); + codepointRanges.Add(Tuple.Create(Fallback1Codepoint, Fallback1Codepoint)); + codepointRanges.Add(Tuple.Create(Fallback2Codepoint, Fallback2Codepoint)); + + // ImGui default ellipsis characters + codepointRanges.Add(Tuple.Create(0x2026, 0x2026)); + codepointRanges.Add(Tuple.Create(0x0085, 0x0085)); + + foreach (var request in requests) { - flattenedRanges[^1] = Math.Max(flattenedRanges[^1], range.Item2); + foreach (var range in request.CodepointRanges) + codepointRanges.Add(range); } + + codepointRanges.Sort((x, y) => (x.Item1 == y.Item1 ? (x.Item2 < y.Item2 ? -1 : (x.Item2 == y.Item2 ? 0 : 1)) : (x.Item1 < y.Item1 ? -1 : 1))); + + List flattenedRanges = new(); + foreach (var range in codepointRanges) + { + if (flattenedRanges.Any() && flattenedRanges[^1] >= range.Item1 - 1) + { + flattenedRanges[^1] = Math.Max(flattenedRanges[^1], range.Item2); + } + else + { + flattenedRanges.Add(range.Item1); + flattenedRanges.Add(range.Item2); + } + } + + flattenedRanges.Add(0); + + var rangeHandle = GCHandle.Alloc(flattenedRanges.ToArray(), GCHandleType.Pinned); + garbageList.Add(rangeHandle); + + var sizedFont = ioFonts.AddFontFromFileTTF(fontPathJp, fontSize * fontScale, fontConfig, rangeHandle.AddrOfPinnedObject()); + fontsToUnscale.Add(sizedFont); + + foreach (var request in requests) + request.FontInternal = sizedFont; + } + } + + var gameFontManager = Service.Get(); + gameFontManager.BuildFonts(); + + Log.Verbose("[FONT] Invoke OnBuildFonts"); + this.BuildFonts?.Invoke(); + Log.Verbose("[FONT] OnBuildFonts OK!"); + + for (var i = 0; i < ImGui.GetIO().Fonts.Fonts.Size; i++) + { + Log.Verbose("{0} - {1}", i, ImGui.GetIO().Fonts.Fonts[i].GetDebugName()); + } + + ioFonts.Build(); + + if (Math.Abs(fontGamma - 1.0f) >= 0.001) + { + // Gamma correction (stbtt/FreeType would output in linear space whereas most real world usages will apply 1.4 or 1.8 gamma; Windows/XIV prebaked uses 1.4) + ioFonts.GetTexDataAsRGBA32(out byte* texPixels, out var texWidth, out var texHeight); + for (int i = 3, i_ = texWidth * texHeight * 4; i < i_; i += 4) + texPixels[i] = (byte)(Math.Pow(texPixels[i] / 255.0f, 1.0f / fontGamma) * 255.0f); + } + + foreach (var font in fontsToUnscale) + GameFontManager.UnscaleFont(font, fontScale, false); + + gameFontManager.AfterBuildFonts(); + + foreach (var font in fontsToUnscale) + { + // Leave IconFont alone. + if (font.NativePtr == IconFont.NativePtr) + continue; + + // MonoFont will be filled later from DefaultFont. + if (font.NativePtr == MonoFont.NativePtr) + continue; + + var axisFont = this.axisFontHandles[^1]; + for (var i = this.axisFontHandles.Length - 2; i >= 0; i--) + { + if (this.axisFontHandles[i].Style.Size >= (font.FontSize - 1) * fontScale * 3 / 4) + axisFont = this.axisFontHandles[i]; else - { - flattenedRanges.Add(range.Item1); - flattenedRanges.Add(range.Item2); - } + break; } - flattenedRanges.Add(0); - - var rangeHandle = GCHandle.Alloc(flattenedRanges.ToArray(), GCHandleType.Pinned); - var sizedFont = ioFonts.AddFontFromFileTTF(fontPathJp, fontSize * fontScale, fontConfig, rangeHandle.AddrOfPinnedObject()); - rangeHandle.Free(); - - fontsToUnscale.Add(sizedFont); - - foreach (var request in requests) - request.FontInternal = sizedFont; - } - } - - var gameFontManager = Service.Get(); - gameFontManager.BuildFonts(); - - Log.Verbose("[FONT] Invoke OnBuildFonts"); - this.BuildFonts?.Invoke(); - Log.Verbose("[FONT] OnBuildFonts OK!"); - - for (var i = 0; i < ImGui.GetIO().Fonts.Fonts.Size; i++) - { - Log.Verbose("{0} - {1}", i, ImGui.GetIO().Fonts.Fonts[i].GetDebugName()); - } - - ioFonts.Build(); - - if (Math.Abs(fontGamma - 1.0f) >= 0.001) - { - // Gamma correction (stbtt/FreeType would output in linear space whereas most real world usages will apply 1.4 or 1.8 gamma; Windows/XIV prebaked uses 1.4) - ioFonts.GetTexDataAsRGBA32(out byte* texPixels, out var texWidth, out var texHeight); - for (int i = 3, i_ = texWidth * texHeight * 4; i < i_; i += 4) - texPixels[i] = (byte)(Math.Pow(texPixels[i] / 255.0f, 1.0f / fontGamma) * 255.0f); - } - - foreach (var font in fontsToUnscale) - GameFontManager.UnscaleFont(font, fontScale, false); - - gameFontManager.AfterBuildFonts(); - - foreach (var font in fontsToUnscale) - { - // Leave IconFont alone. - if (font.NativePtr == IconFont.NativePtr) - continue; - - // MonoFont will be filled later from DefaultFont. - if (font.NativePtr == MonoFont.NativePtr) - continue; - - var axisFont = this.axisFontHandles[^1]; - for (var i = this.axisFontHandles.Length - 2; i >= 0; i--) - { - if (this.axisFontHandles[i].Style.Size >= (font.FontSize - 1) * fontScale * 3 / 4) - axisFont = this.axisFontHandles[i]; + if (this.overwriteAllNotoGlyphsWithAxis) + GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, font, false, false); else - break; + GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, font, false, false, 0xE020, 0xE0DB); + + // Fill missing glyphs in DefaultFont from Axis + if (font.NativePtr == DefaultFont.NativePtr) + GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, DefaultFont, true, false); } - if (this.overwriteAllNotoGlyphsWithAxis) - GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, font, false, false); - else - GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, font, false, false, 0xE020, 0xE0DB); + // Fill missing glyphs in MonoFont from DefaultFont + GameFontManager.CopyGlyphsAcrossFonts(DefaultFont, MonoFont, true, false); - // Fill missing glyphs in DefaultFont from Axis - if (font.NativePtr == DefaultFont.NativePtr) - GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, DefaultFont, true, false); + foreach (var font in fontsToUnscale) + { + font.FallbackChar = Fallback1Codepoint; + font.BuildLookupTable(); + } + + Log.Verbose("[FONT] Invoke OnAfterBuildFonts"); + this.AfterBuildFonts?.Invoke(); + Log.Verbose("[FONT] OnAfterBuildFonts OK!"); + + Log.Verbose("[FONT] Fonts built!"); + + this.fontBuildSignal.Set(); + + this.FontsReady = true; } - - // Fill missing glyphs in MonoFont from DefaultFont - GameFontManager.CopyGlyphsAcrossFonts(DefaultFont, MonoFont, true, false); - - foreach (var font in fontsToUnscale) + finally { - font.FallbackChar = Fallback1Codepoint; - font.BuildLookupTable(); + if (fontConfig.NativePtr != null) + fontConfig.Destroy(); + + foreach (var garbage in garbageList) + garbage.Free(); } - - Log.Verbose("[FONT] Invoke OnAfterBuildFonts"); - this.AfterBuildFonts?.Invoke(); - Log.Verbose("[FONT] OnAfterBuildFonts OK!"); - - Log.Verbose("[FONT] Fonts built!"); - - fontConfig.Destroy(); - this.fontBuildSignal.Set(); - - this.FontsReady = true; } private void Disable() From 6da762dc3c1ce6eab403d82fd0b0316ba03c918c Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Thu, 31 Mar 2022 23:29:37 +0900 Subject: [PATCH 6/8] Correct return types on boot --- lib/CoreCLR/CoreCLR.cpp | 4 ++-- lib/CoreCLR/CoreCLR.h | 4 ++-- lib/CoreCLR/boot.cpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/CoreCLR/CoreCLR.cpp b/lib/CoreCLR/CoreCLR.cpp index 6f4d337b8..265f1869e 100644 --- a/lib/CoreCLR/CoreCLR.cpp +++ b/lib/CoreCLR/CoreCLR.cpp @@ -37,12 +37,12 @@ int CoreCLR::load_hostfxr(const struct get_hostfxr_parameters* parameters) && m_hostfxr_close_fptr ? 0 : -1; } -bool CoreCLR::load_runtime(const std::wstring& runtime_config_path) +int CoreCLR::load_runtime(const std::wstring& runtime_config_path) { return CoreCLR::load_runtime(runtime_config_path, nullptr); } -bool CoreCLR::load_runtime(const std::wstring& runtime_config_path, const struct hostfxr_initialize_parameters* parameters) +int CoreCLR::load_runtime(const std::wstring& runtime_config_path, const struct hostfxr_initialize_parameters* parameters) { int result; diff --git a/lib/CoreCLR/CoreCLR.h b/lib/CoreCLR/CoreCLR.h index 235bf9923..71c62d90e 100644 --- a/lib/CoreCLR/CoreCLR.h +++ b/lib/CoreCLR/CoreCLR.h @@ -12,8 +12,8 @@ class CoreCLR { int load_hostfxr(); int load_hostfxr(const get_hostfxr_parameters* parameters); - bool load_runtime(const std::wstring& runtime_config_path); - bool load_runtime( + int load_runtime(const std::wstring& runtime_config_path); + int load_runtime( const std::wstring& runtime_config_path, const struct hostfxr_initialize_parameters* parameters); diff --git a/lib/CoreCLR/boot.cpp b/lib/CoreCLR/boot.cpp index 2fde7e221..63636e709 100644 --- a/lib/CoreCLR/boot.cpp +++ b/lib/CoreCLR/boot.cpp @@ -90,7 +90,7 @@ int InitializeClrAndGetEntryPoint( printf("Loading hostfxr... "); if ((result = g_clr->load_hostfxr(&init_parameters)) != 0) { - printf("\nError: Failed to load the `hostfxr` library (err=%d)\n", result); + printf("\nError: Failed to load the `hostfxr` library (err=0x%08x)\n", result); return result; } printf("Done!\n"); From f14bf171e48c4267678de505e82dfa51530848f9 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Fri, 1 Apr 2022 01:18:28 +0900 Subject: [PATCH 7/8] Add attempts to reduce font atlas size --- .../Internal/DalamudConfiguration.cs | 7 +- .../Interface/GameFonts/GameFontManager.cs | 13 +- .../Interface/Internal/InterfaceManager.cs | 179 ++++++++++++------ .../Internal/Windows/SettingsWindow.cs | 20 +- 4 files changed, 157 insertions(+), 62 deletions(-) diff --git a/Dalamud/Configuration/Internal/DalamudConfiguration.cs b/Dalamud/Configuration/Internal/DalamudConfiguration.cs index b2a11d774..2ee31c4f7 100644 --- a/Dalamud/Configuration/Internal/DalamudConfiguration.cs +++ b/Dalamud/Configuration/Internal/DalamudConfiguration.cs @@ -141,7 +141,12 @@ namespace Dalamud.Configuration.Internal /// * ...TTF fonts loaded with stb or FreeType are in linear space. /// * ...the game's prebaked AXIS fonts are in gamma space with gamma value of 1.4. /// - public float FontGamma { get; set; } = 1.0f; + public float FontGamma { get; set; } = 1.4f; + + /// + /// Gets or sets a value indicating whether to allow big font atlas. + /// + public bool AllowBigFontAtlas { get; set; } = false; /// /// Gets or sets a value indicating whether or not plugin UI should be hidden. diff --git a/Dalamud/Interface/GameFonts/GameFontManager.cs b/Dalamud/Interface/GameFonts/GameFontManager.cs index 83178acc9..adbe6dfa4 100644 --- a/Dalamud/Interface/GameFonts/GameFontManager.cs +++ b/Dalamud/Interface/GameFonts/GameFontManager.cs @@ -141,9 +141,9 @@ namespace Dalamud.Interface.GameFonts target.Value!.ConfigData, (ushort)glyph->Codepoint, glyph->X0 * scale, - glyph->Y0 * scale, + ((glyph->Y0 - source.Value!.Ascent) * scale) + target.Value!.Ascent, glyph->X1 * scale, - glyph->Y1 * scale, + ((glyph->Y1 - source.Value!.Ascent) * scale) + target.Value!.Ascent, glyph->U0, glyph->V0, glyph->U1, @@ -153,9 +153,9 @@ namespace Dalamud.Interface.GameFonts else if (!missingOnly) { prevGlyphPtr->X0 = glyph->X0 * scale; - prevGlyphPtr->Y0 = glyph->Y0 * scale; + prevGlyphPtr->Y0 = ((glyph->Y0 - source.Value!.Ascent) * scale) + target.Value!.Ascent; prevGlyphPtr->X1 = glyph->X1 * scale; - prevGlyphPtr->Y1 = glyph->Y1 * scale; + prevGlyphPtr->Y1 = ((glyph->Y1 - source.Value!.Ascent) * scale) + target.Value!.Ascent; prevGlyphPtr->U0 = glyph->U0; prevGlyphPtr->V0 = glyph->V0; prevGlyphPtr->U1 = glyph->U1; @@ -343,7 +343,7 @@ namespace Dalamud.Interface.GameFonts { var fdt = this.fdts[(int)style.FamilyAndSize]; var fontPtr = font.NativePtr; - fontPtr->ConfigData->SizePixels = fontPtr->FontSize = fdt.FontHeader.LineHeight; + fontPtr->ConfigData->SizePixels = fontPtr->FontSize = fdt.FontHeader.Size * 4 / 3; fontPtr->Ascent = fdt.FontHeader.Ascent; fontPtr->Descent = fdt.FontHeader.Descent; fontPtr->EllipsisChar = '…'; @@ -444,6 +444,9 @@ namespace Dalamud.Interface.GameFonts { lock (this.syncRoot) { + if (!this.fontUseCounter.ContainsKey(style)) + return; + if ((this.fontUseCounter[style] -= 1) == 0) this.fontUseCounter.Remove(style); } diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index 8872fc45f..50304e89c 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -55,6 +55,7 @@ namespace Dalamud.Interface.Internal private readonly string rtssPath; private readonly HashSet glyphRequests = new(); + private readonly List axisFontHandles = new(); private readonly Hook presentHook; private readonly Hook resizeBuffersHook; @@ -64,9 +65,6 @@ namespace Dalamud.Interface.Internal private readonly SwapChainVtableResolver address; private RawDX11Scene? scene; - private GameFontHandle[] axisFontHandles; - private bool overwriteAllNotoGlyphsWithAxis; - // can't access imgui IO before first present call private bool lastWantCapture = false; private bool isRebuildingFonts = false; @@ -197,6 +195,16 @@ namespace Dalamud.Interface.Internal /// public bool IsReady => this.scene != null; + /// + /// Gets or sets a value indicating whether to override configuration for UseAxis. + /// + public bool? UseAxisOverride { get; set; } = null; + + /// + /// Gets a value indicating whether to use AXIS fonts. + /// + public bool UseAxis => this.UseAxisOverride ?? Service.Get().UseAxisFontsFromGame; + /// /// Gets or sets the overrided font gamma value, instead of using the value from configuration. /// @@ -207,6 +215,16 @@ namespace Dalamud.Interface.Internal /// public float FontGamma => Math.Max(0.1f, this.FontGammaOverride.GetValueOrDefault(Service.Get().FontGamma)); + /// + /// Gets or sets a value indicating whether to override configuration for AllowBigFontAtlas. + /// + public bool? AllowBigFontAtlasOverride { get; set; } = null; + + /// + /// Gets a value indicating whether to allow big font atlas. + /// + public bool AllowBigFontAtlas => this.AllowBigFontAtlasOverride ?? Service.Get().AllowBigFontAtlas; + /// /// Enable this module. /// @@ -330,8 +348,6 @@ namespace Dalamud.Interface.Internal if (!this.isRebuildingFonts) { Log.Verbose("[FONT] RebuildFonts() trigger"); - this.SetAxisFonts(); - this.isRebuildingFonts = true; this.scene.OnNewRenderFrame += this.RebuildFontsInternal; } @@ -414,24 +430,6 @@ namespace Dalamud.Interface.Internal Util.Fatal($"One or more files required by XIVLauncher were not found.\nPlease restart and report this error if it occurs again.\n\n{path}", "Error"); } - private void SetAxisFonts() - { - var configuration = Service.Get(); - this.overwriteAllNotoGlyphsWithAxis = configuration.UseAxisFontsFromGame; - - if (this.axisFontHandles == null) - { - this.axisFontHandles = new GameFontHandle[] - { - Service.Get().NewFontRef(new(GameFontFamilyAndSize.Axis96)), - Service.Get().NewFontRef(new(GameFontFamilyAndSize.Axis12)), - Service.Get().NewFontRef(new(GameFontFamilyAndSize.Axis14)), - Service.Get().NewFontRef(new(GameFontFamilyAndSize.Axis18)), - Service.Get().NewFontRef(new(GameFontFamilyAndSize.Axis36)), - }; - } - } - /* * NOTE(goat): When hooking ReShade DXGISwapChain::runtime_present, this is missing the syncInterval arg. * Seems to work fine regardless, I guess, so whatever. @@ -493,8 +491,6 @@ namespace Dalamud.Interface.Internal this.scene.OnBuildUI += this.Display; this.scene.OnNewInputFrame += this.OnNewInputFrame; - this.SetAxisFonts(); - this.SetupFonts(); StyleModel.TransferOldModels(); @@ -600,22 +596,40 @@ namespace Dalamud.Interface.Internal private unsafe void SetupFonts() { + var gameFontManager = Service.Get(); var dalamud = Service.Get(); var io = ImGui.GetIO(); var ioFonts = io.Fonts; - var fontScale = io.FontGlobalScale; + var fontLoadScale = this.AllowBigFontAtlas ? io.FontGlobalScale : 1; var fontGamma = this.FontGamma; List fontsToUnscale = new(); + List fontsToOverwriteFromAxis = new(); + List fontsToReassignSizes = new(); this.fontBuildSignal.Reset(); ioFonts.Clear(); - ioFonts.TexDesiredWidth = 4096; + ioFonts.TexDesiredWidth = this.AllowBigFontAtlas ? 4096 : 2048; + + Log.Verbose("[FONT] SetupFonts - 1"); + + foreach (var v in this.axisFontHandles) + { + if (v != null) + v.Dispose(); + } + + this.axisFontHandles.Clear(); + + Log.Verbose("[FONT] SetupFonts - 2"); ImFontConfigPtr fontConfig = null; List garbageList = new(); try { + var dummyRangeHandle = GCHandle.Alloc(new ushort[] { '0', '0', 0 }, GCHandleType.Pinned); + garbageList.Add(dummyRangeHandle); + fontConfig = ImGuiNative.ImFontConfig_ImFontConfig(); fontConfig.OversampleH = 1; fontConfig.OversampleV = 1; @@ -626,15 +640,31 @@ namespace Dalamud.Interface.Internal ShowFontError(fontPathJp); // Default font + Log.Verbose("[FONT] SetupFonts - Default font"); + this.axisFontHandles.Add(gameFontManager.NewFontRef(this.AllowBigFontAtlas ? new(GameFontFamily.Axis, DefaultFontSizePt * fontLoadScale) : new(GameFontFamilyAndSize.Axis12))); + if (this.UseAxis) + { + fontConfig.GlyphRanges = dummyRangeHandle.AddrOfPinnedObject(); + fontConfig.SizePixels = DefaultFontSizePx * fontLoadScale; + DefaultFont = ioFonts.AddFontDefault(fontConfig); + fontsToUnscale.Add(DefaultFont); + fontsToOverwriteFromAxis.Add(true); + fontsToReassignSizes.Add(null); + } + else { var japaneseRangeHandle = GCHandle.Alloc(GlyphRangesJapanese.GlyphRanges, GCHandleType.Pinned); garbageList.Add(japaneseRangeHandle); - DefaultFont = ioFonts.AddFontFromFileTTF(fontPathJp, (DefaultFontSizePx + 1) * fontScale, fontConfig, japaneseRangeHandle.AddrOfPinnedObject()); + fontConfig.GlyphRanges = japaneseRangeHandle.AddrOfPinnedObject(); + DefaultFont = ioFonts.AddFontFromFileTTF(fontPathJp, (DefaultFontSizePx + 1) * fontLoadScale, fontConfig); fontsToUnscale.Add(DefaultFont); + fontsToOverwriteFromAxis.Add(false); + fontsToReassignSizes.Add(null); } // FontAwesome icon font + Log.Verbose("[FONT] SetupFonts - FontAwesome icon font"); { var fontPathIcon = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "FontAwesome5FreeSolid.otf"); if (!File.Exists(fontPathIcon)) @@ -643,20 +673,31 @@ namespace Dalamud.Interface.Internal var iconRangeHandle = GCHandle.Alloc(new ushort[] { 0xE000, 0xF8FF, 0, }, GCHandleType.Pinned); garbageList.Add(iconRangeHandle); - IconFont = ioFonts.AddFontFromFileTTF(fontPathIcon, DefaultFontSizePx * fontScale, fontConfig, iconRangeHandle.AddrOfPinnedObject()); + fontConfig.GlyphRanges = iconRangeHandle.AddrOfPinnedObject(); + IconFont = ioFonts.AddFontFromFileTTF(fontPathIcon, DefaultFontSizePx * fontLoadScale, fontConfig); fontsToUnscale.Add(IconFont); + this.axisFontHandles.Add(null); + fontsToOverwriteFromAxis.Add(false); + fontsToReassignSizes.Add(null); } // Monospace font + Log.Verbose("[FONT] SetupFonts - Monospace font"); { var fontPathMono = Path.Combine(dalamud.AssetDirectory.FullName, "UIRes", "Inconsolata-Regular.ttf"); if (!File.Exists(fontPathMono)) ShowFontError(fontPathMono); - MonoFont = ioFonts.AddFontFromFileTTF(fontPathMono, DefaultFontSizePx * fontScale, fontConfig); + + fontConfig.GlyphRanges = IntPtr.Zero; + MonoFont = ioFonts.AddFontFromFileTTF(fontPathMono, DefaultFontSizePx * fontLoadScale, fontConfig); fontsToUnscale.Add(MonoFont); + this.axisFontHandles.Add(null); + fontsToOverwriteFromAxis.Add(false); + fontsToReassignSizes.Add(null); } // Default font but in requested size for requested glyphs + Log.Verbose("[FONT] SetupFonts - Default font but in requested size for requested glyphs"); { Dictionary> extraFontRequests = new(); foreach (var extraFontRequest in this.glyphRequests) @@ -700,18 +741,30 @@ namespace Dalamud.Interface.Internal flattenedRanges.Add(0); - var rangeHandle = GCHandle.Alloc(flattenedRanges.ToArray(), GCHandleType.Pinned); - garbageList.Add(rangeHandle); + ImFontPtr sizedFont; + this.axisFontHandles.Add(gameFontManager.NewFontRef(this.AllowBigFontAtlas ? new(GameFontFamily.Axis, fontSize * 3 / 4 * fontLoadScale) : new(GameFontFamilyAndSize.Axis12))); + if (this.UseAxis) + { + fontConfig.GlyphRanges = dummyRangeHandle.AddrOfPinnedObject(); + fontConfig.SizePixels = (this.AllowBigFontAtlas ? fontSize : DefaultFontSizePx) * fontLoadScale; + sizedFont = ioFonts.AddFontDefault(fontConfig); + } + else + { + var rangeHandle = GCHandle.Alloc(flattenedRanges.ToArray(), GCHandleType.Pinned); + garbageList.Add(rangeHandle); + sizedFont = ioFonts.AddFontFromFileTTF(fontPathJp, (this.AllowBigFontAtlas ? fontSize : DefaultFontSizePx) * fontLoadScale, fontConfig, rangeHandle.AddrOfPinnedObject()); + } - var sizedFont = ioFonts.AddFontFromFileTTF(fontPathJp, fontSize * fontScale, fontConfig, rangeHandle.AddrOfPinnedObject()); fontsToUnscale.Add(sizedFont); + fontsToOverwriteFromAxis.Add(true); + fontsToReassignSizes.Add(this.AllowBigFontAtlas ? null : fontSize); foreach (var request in requests) request.FontInternal = sizedFont; } } - var gameFontManager = Service.Get(); gameFontManager.BuildFonts(); Log.Verbose("[FONT] Invoke OnBuildFonts"); @@ -734,37 +787,53 @@ namespace Dalamud.Interface.Internal } foreach (var font in fontsToUnscale) - GameFontManager.UnscaleFont(font, fontScale, false); + GameFontManager.UnscaleFont(font, fontLoadScale, false); gameFontManager.AfterBuildFonts(); - foreach (var font in fontsToUnscale) + for (var i = 0; i < fontsToUnscale.Count; i++) { - // Leave IconFont alone. - if (font.NativePtr == IconFont.NativePtr) + var font = fontsToUnscale[i]; + var fontPtr = font.NativePtr; + var correspondingAxis = this.axisFontHandles[i]; + var overwrite = fontsToOverwriteFromAxis[i]; + var overwriteSize = fontsToReassignSizes[i]; + + if (correspondingAxis == null) continue; - // MonoFont will be filled later from DefaultFont. - if (font.NativePtr == MonoFont.NativePtr) - continue; - - var axisFont = this.axisFontHandles[^1]; - for (var i = this.axisFontHandles.Length - 2; i >= 0; i--) + if (overwrite) { - if (this.axisFontHandles[i].Style.Size >= (font.FontSize - 1) * fontScale * 3 / 4) - axisFont = this.axisFontHandles[i]; - else - break; + var srcPtr = correspondingAxis.ImFont.NativePtr; + var scale = fontPtr->ConfigData->SizePixels / srcPtr->ConfigData->SizePixels / fontLoadScale; + Log.Verbose("[FONT] Font {0}: scale {1}", i, scale); + fontPtr->FontSize = srcPtr->FontSize * scale; + fontPtr->Ascent = srcPtr->Ascent * scale; + fontPtr->Descent = srcPtr->Descent * scale; + fontPtr->FallbackChar = srcPtr->FallbackChar; + fontPtr->EllipsisChar = srcPtr->EllipsisChar; } - if (this.overwriteAllNotoGlyphsWithAxis) - GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, font, false, false); - else - GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, font, false, false, 0xE020, 0xE0DB); + if (overwriteSize != null) + { + var scale = overwriteSize.Value / fontPtr->ConfigData->SizePixels; + fontPtr->FontSize *= scale; + fontPtr->Ascent *= scale; + fontPtr->Descent *= scale; + } - // Fill missing glyphs in DefaultFont from Axis - if (font.NativePtr == DefaultFont.NativePtr) - GameFontManager.CopyGlyphsAcrossFonts(axisFont.ImFont, DefaultFont, true, false); + GameFontManager.CopyGlyphsAcrossFonts(correspondingAxis.ImFont, font, !overwrite, false); + + if (!this.UseAxis && fontPtr == DefaultFont.NativePtr) + { + fontPtr->FontSize -= 1; + GameFontManager.CopyGlyphsAcrossFonts(correspondingAxis.ImFont, font, true, false, 0xE020, 0xE0DB); + fontPtr->FontSize += 1; + } + else + { + GameFontManager.CopyGlyphsAcrossFonts(correspondingAxis.ImFont, font, true, false, 0xE020, 0xE0DB); + } } // Fill missing glyphs in MonoFont from DefaultFont diff --git a/Dalamud/Interface/Internal/Windows/SettingsWindow.cs b/Dalamud/Interface/Internal/Windows/SettingsWindow.cs index 9ff94f42e..493725d92 100644 --- a/Dalamud/Interface/Internal/Windows/SettingsWindow.cs +++ b/Dalamud/Interface/Internal/Windows/SettingsWindow.cs @@ -39,6 +39,7 @@ namespace Dalamud.Interface.Internal.Windows private float globalUiScale; private bool doUseAxisFontsFromGame; + private bool doAllowBigFontAtlas; private float fontGamma; private bool doToggleUiHide; private bool doToggleUiHideDuringCutscenes; @@ -96,6 +97,7 @@ namespace Dalamud.Interface.Internal.Windows this.globalUiScale = configuration.GlobalUiScale; this.fontGamma = configuration.FontGamma; this.doUseAxisFontsFromGame = configuration.UseAxisFontsFromGame; + this.doAllowBigFontAtlas = configuration.AllowBigFontAtlas; this.doToggleUiHide = configuration.ToggleUiHide; this.doToggleUiHideDuringCutscenes = configuration.ToggleUiHideDuringCutscenes; this.doToggleUiHideDuringGpose = configuration.ToggleUiHideDuringGpose; @@ -188,6 +190,8 @@ namespace Dalamud.Interface.Internal.Windows ImGui.GetIO().FontGlobalScale = configuration.GlobalUiScale; interfaceManager.FontGammaOverride = null; + interfaceManager.AllowBigFontAtlasOverride = null; + interfaceManager.UseAxisOverride = null; this.thirdRepoList = configuration.ThirdRepoList.Select(x => x.Clone()).ToList(); this.devPluginLocations = configuration.DevPluginLoadLocations.Select(x => x.Clone()).ToList(); @@ -325,9 +329,22 @@ namespace Dalamud.Interface.Internal.Windows ImGui.TextColored(ImGuiColors.DalamudGrey, Loc.Localize("DalamudSettingToggleUiHideOptOutNote", "Plugins may independently opt out of the settings below.")); - ImGui.Checkbox(Loc.Localize("DalamudSettingToggleAxisFonts", "Use AXIS fonts as default Dalamud font"), ref this.doUseAxisFontsFromGame); + if (ImGui.Checkbox(Loc.Localize("DalamudSettingToggleAxisFonts", "Use AXIS fonts as default Dalamud font"), ref this.doUseAxisFontsFromGame)) + { + interfaceManager.UseAxisOverride = this.doUseAxisFontsFromGame; + interfaceManager.RebuildFonts(); + } + ImGui.TextColored(ImGuiColors.DalamudGrey, Loc.Localize("DalamudSettingToggleUiAxisFontsHint", "Use AXIS fonts (the game's main UI fonts) as default Dalamud font.")); + if (ImGui.Checkbox(Loc.Localize("DalamudSettingAllowBigFontAtlas", "Allow big font atlas"), ref this.doAllowBigFontAtlas)) + { + interfaceManager.AllowBigFontAtlasOverride = this.doAllowBigFontAtlas; + interfaceManager.RebuildFonts(); + } + + ImGui.TextColored(ImGuiColors.DalamudGrey, string.Format(Loc.Localize("DalamudSettingAllowBigFontAtlas", "Displays text crisply, but may crash if your GPU does not support it.\nCurrent size: {0}px * {1}px"), ImGui.GetIO().Fonts.TexWidth, ImGui.GetIO().Fonts.TexHeight)); + ImGui.Checkbox(Loc.Localize("DalamudSettingToggleUiHide", "Hide plugin UI when the game UI is toggled off"), ref this.doToggleUiHide); ImGui.TextColored(ImGuiColors.DalamudGrey, Loc.Localize("DalamudSettingToggleUiHideHint", "Hide any open windows by plugins when toggling the game overlay.")); @@ -856,6 +873,7 @@ namespace Dalamud.Interface.Internal.Windows configuration.Fools22 = this.doFools22; configuration.UseAxisFontsFromGame = this.doUseAxisFontsFromGame; + configuration.AllowBigFontAtlas = this.doAllowBigFontAtlas; configuration.FontGamma = this.fontGamma; // This is applied every frame in InterfaceManager::CheckViewportState() From 216d205542d70eee7f9d49f5e03db5d13770b857 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Fri, 1 Apr 2022 01:52:58 +0900 Subject: [PATCH 8/8] Fix scaling problems --- .../Interface/Internal/InterfaceManager.cs | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index 50304e89c..c19a32601 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -748,18 +748,20 @@ namespace Dalamud.Interface.Internal fontConfig.GlyphRanges = dummyRangeHandle.AddrOfPinnedObject(); fontConfig.SizePixels = (this.AllowBigFontAtlas ? fontSize : DefaultFontSizePx) * fontLoadScale; sizedFont = ioFonts.AddFontDefault(fontConfig); + fontsToUnscale.Add(sizedFont); + fontsToOverwriteFromAxis.Add(true); + fontsToReassignSizes.Add(this.AllowBigFontAtlas ? null : fontSize); } else { var rangeHandle = GCHandle.Alloc(flattenedRanges.ToArray(), GCHandleType.Pinned); garbageList.Add(rangeHandle); - sizedFont = ioFonts.AddFontFromFileTTF(fontPathJp, (this.AllowBigFontAtlas ? fontSize : DefaultFontSizePx) * fontLoadScale, fontConfig, rangeHandle.AddrOfPinnedObject()); + sizedFont = ioFonts.AddFontFromFileTTF(fontPathJp, (this.AllowBigFontAtlas ? fontSize : DefaultFontSizePx + 1) * fontLoadScale, fontConfig, rangeHandle.AddrOfPinnedObject()); + fontsToUnscale.Add(sizedFont); + fontsToOverwriteFromAxis.Add(false); + fontsToReassignSizes.Add(this.AllowBigFontAtlas ? null : fontSize); } - fontsToUnscale.Add(sizedFont); - fontsToOverwriteFromAxis.Add(true); - fontsToReassignSizes.Add(this.AllowBigFontAtlas ? null : fontSize); - foreach (var request in requests) request.FontInternal = sizedFont; } @@ -786,9 +788,6 @@ namespace Dalamud.Interface.Internal texPixels[i] = (byte)(Math.Pow(texPixels[i] / 255.0f, 1.0f / fontGamma) * 255.0f); } - foreach (var font in fontsToUnscale) - GameFontManager.UnscaleFont(font, fontLoadScale, false); - gameFontManager.AfterBuildFonts(); for (var i = 0; i < fontsToUnscale.Count; i++) @@ -799,30 +798,34 @@ namespace Dalamud.Interface.Internal var overwrite = fontsToOverwriteFromAxis[i]; var overwriteSize = fontsToReassignSizes[i]; + GameFontManager.UnscaleFont(font, fontLoadScale, false); + if (correspondingAxis == null) continue; + var scale = 1f; + if (overwrite) { var srcPtr = correspondingAxis.ImFont.NativePtr; - var scale = fontPtr->ConfigData->SizePixels / srcPtr->ConfigData->SizePixels / fontLoadScale; - Log.Verbose("[FONT] Font {0}: scale {1}", i, scale); + scale = fontPtr->ConfigData->SizePixels / srcPtr->ConfigData->SizePixels / fontLoadScale; fontPtr->FontSize = srcPtr->FontSize * scale; fontPtr->Ascent = srcPtr->Ascent * scale; fontPtr->Descent = srcPtr->Descent * scale; fontPtr->FallbackChar = srcPtr->FallbackChar; fontPtr->EllipsisChar = srcPtr->EllipsisChar; + GameFontManager.CopyGlyphsAcrossFonts(correspondingAxis.ImFont, font, false, false); + + scale = 1f; } if (overwriteSize != null) - { - var scale = overwriteSize.Value / fontPtr->ConfigData->SizePixels; - fontPtr->FontSize *= scale; - fontPtr->Ascent *= scale; - fontPtr->Descent *= scale; - } + scale *= overwriteSize.Value / fontPtr->ConfigData->SizePixels; - GameFontManager.CopyGlyphsAcrossFonts(correspondingAxis.ImFont, font, !overwrite, false); + if (scale != 1f) + GameFontManager.UnscaleFont(font, 1 / scale, false); + + Log.Verbose("[FONT] Font {0}: result size {1}", i, fontPtr->FontSize); if (!this.UseAxis && fontPtr == DefaultFont.NativePtr) {