Show size appropriate fonts

(cherry picked from commit be65907beb)
This commit is contained in:
Soreepeong 2022-03-07 13:47:37 +09:00
parent 0cd1c1b150
commit b2ff63a4cc
4 changed files with 320 additions and 97 deletions

View file

@ -117,18 +117,21 @@ namespace Dalamud.Interface.GameFonts
/// <param name="target">Target font.</param>
/// <param name="missingOnly">Whether to copy missing glyphs only.</param>
/// <param name="rebuildLookupTable">Whether to call target.BuildLookupTable().</param>
public static void CopyGlyphsAcrossFonts(ImFontPtr? source, ImFontPtr? target, bool missingOnly, bool rebuildLookupTable)
/// <param name="rangeLow">Low codepoint range to copy.</param>
/// <param name="rangeHigh">High codepoing range to copy.</param>
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();
}
/// <summary>
/// Unscales fonts after they have been rendered onto atlas.
/// </summary>
/// <param name="fontPtr">Font to unscale.</param>
/// <param name="fontScale">Scale factor.</param>
/// <param name="rebuildLookupTable">Whether to call target.BuildLookupTable().</param>
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();
}
/// <inheritdoc/>
public void Dispose()
{