From 84c5982c258c4bf714278aa4cf6cdac3182e5676 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Tue, 19 Sep 2023 15:17:15 +0900 Subject: [PATCH] Use axis12 glyph ranges when loading noto12 as Dalamud default font --- .../Interface/GameFonts/GameFontManager.cs | 36 +++++++++++++++++++ .../Interface/Internal/InterfaceManager.cs | 29 +++++++-------- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/Dalamud/Interface/GameFonts/GameFontManager.cs b/Dalamud/Interface/GameFonts/GameFontManager.cs index ad0e47273..3a1ab737e 100644 --- a/Dalamud/Interface/GameFonts/GameFontManager.cs +++ b/Dalamud/Interface/GameFonts/GameFontManager.cs @@ -171,6 +171,42 @@ public static void UnscaleFont(ImFontPtr fontPtr, float fontScale, bool rebuildL fontPtr.BuildLookupTable(); } + /// + /// Create a glyph range for use with ImGui AddFont. + /// + /// Font family and size. + /// Merge two ranges into one if distance is below the value specified in this parameter. + /// Glyph ranges. + public GCHandle ToGlyphRanges(GameFontFamilyAndSize family, int mergeDistance = 8) + { + var fdt = this.fdts[(int)family]!; + var ranges = new List(fdt.Glyphs.Count) + { + checked((ushort)fdt.Glyphs[0].CharInt), + checked((ushort)fdt.Glyphs[0].CharInt), + }; + + foreach (var glyph in fdt.Glyphs.Skip(1)) + { + var c32 = glyph.CharInt; + if (c32 >= 0x10000) + break; + + var c16 = unchecked((ushort)c32); + if (ranges[^1] + mergeDistance >= c16 && c16 > ranges[^1]) + { + ranges[^1] = c16; + } + else if (ranges[^1] + 1 < c16) + { + ranges.Add(c16); + ranges.Add(c16); + } + } + + return GCHandle.Alloc(ranges.ToArray(), GCHandleType.Pinned); + } + /// /// Creates a new GameFontHandle, and increases internal font reference counter, and if it's first time use, then the font will be loaded on next font building process. /// diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index 841511f55..559207ed6 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -790,10 +790,10 @@ private unsafe void SetupFonts() } else { - var japaneseRangeHandle = GCHandle.Alloc(GlyphRangesJapanese.GlyphRanges, GCHandleType.Pinned); - garbageList.Add(japaneseRangeHandle); + var rangeHandle = gameFontManager.ToGlyphRanges(GameFontFamilyAndSize.Axis12); + garbageList.Add(rangeHandle); - fontConfig.GlyphRanges = japaneseRangeHandle.AddrOfPinnedObject(); + fontConfig.GlyphRanges = rangeHandle.AddrOfPinnedObject(); fontConfig.PixelSnapH = true; DefaultFont = ioFonts.AddFontFromFileTTF(fontPathJp, fontConfig.SizePixels, fontConfig); this.loadedFontInfo[DefaultFont] = fontInfo; @@ -850,22 +850,19 @@ private unsafe void SetupFonts() foreach (var (fontSize, requests) in extraFontRequests) { - 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) + List<(ushort, ushort)> codepointRanges = new(4 + requests.Sum(x => x.CodepointRanges.Count)) { - foreach (var range in request.CodepointRanges) - codepointRanges.Add(range); - } + new(Fallback1Codepoint, Fallback1Codepoint), + new(Fallback2Codepoint, Fallback2Codepoint), + // ImGui default ellipsis characters + new(0x2026, 0x2026), + new(0x0085, 0x0085), + }; - 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))); + foreach (var request in requests) + codepointRanges.AddRange(request.CodepointRanges.Select(x => (From: x.Item1, To: x.Item2))); + codepointRanges.Sort(); List flattenedRanges = new(); foreach (var range in codepointRanges) {