Merge pull request #1407 from Soreepeong/fix/noto-glyph-ranges

This commit is contained in:
goat 2023-09-19 19:29:17 +02:00 committed by GitHub
commit 9b31a7ff39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 16 deletions

View file

@ -171,6 +171,42 @@ internal class GameFontManager : IServiceType
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>

View file

@ -790,10 +790,10 @@ internal class InterfaceManager : IDisposable, IServiceType
}
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 @@ internal class InterfaceManager : IDisposable, IServiceType
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));
List<(ushort, ushort)> codepointRanges = new(4 + requests.Sum(x => x.CodepointRanges.Count))
{
new(Fallback1Codepoint, Fallback1Codepoint),
new(Fallback2Codepoint, Fallback2Codepoint),
// ImGui default ellipsis characters
new(0x2026, 0x2026),
new(0x0085, 0x0085),
};
foreach (var request in requests)
{
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)));
codepointRanges.AddRange(request.CodepointRanges.Select(x => (From: x.Item1, To: x.Item2)));
codepointRanges.Sort();
List<ushort> flattenedRanges = new();
foreach (var range in codepointRanges)
{