Skip to content

Commit

Permalink
Merge pull request #1407 from Soreepeong/fix/noto-glyph-ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
goaaats committed Sep 19, 2023
2 parents 5a3196e + 84c5982 commit 9b31a7f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
36 changes: 36 additions & 0 deletions Dalamud/Interface/GameFonts/GameFontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,42 @@ public static void UnscaleFont(ImFontPtr fontPtr, float fontScale, bool rebuildL
fontPtr.BuildLookupTable();
}

/// <summary>
/// Create a glyph range for use with ImGui AddFont.
/// </summary>
/// <param name="family">Font family and size.</param>
/// <param name="mergeDistance">Merge two ranges into one if distance is below the value specified in this parameter.</param>
/// <returns>Glyph ranges.</returns>
public GCHandle ToGlyphRanges(GameFontFamilyAndSize family, int mergeDistance = 8)
{
var fdt = this.fdts[(int)family]!;
var ranges = new List<ushort>(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);
}

/// <summary>
/// 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.
/// </summary>
Expand Down
29 changes: 13 additions & 16 deletions Dalamud/Interface/Internal/InterfaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -850,22 +850,19 @@ private unsafe void SetupFonts()

foreach (var (fontSize, requests) in extraFontRequests)
{
List<Tuple<ushort, ushort>> codepointRanges = new();
codepointRanges.Add(Tuple.Create(Fallback1Codepoint, Fallback1Codepoint));
codepointRanges.Add(Tuple.Create(Fallback2Codepoint, Fallback2Codepoint));

// ImGui default ellipsis characters
codepointRanges.Add(Tuple.Create<ushort, ushort>(0x2026, 0x2026));
codepointRanges.Add(Tuple.Create<ushort, ushort>(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<ushort> flattenedRanges = new();
foreach (var range in codepointRanges)
{
Expand Down

0 comments on commit 9b31a7f

Please sign in to comment.