Fonts: added "Input Glyphs Overlap Detection Tool". Added "Clear bakes", "Clear unused" buttons. Move code.

This commit is contained in:
ocornut
2025-05-22 17:45:22 +02:00
parent 5926c877a1
commit 25f9c318e3
4 changed files with 64 additions and 7 deletions

View File

@@ -16699,6 +16699,21 @@ void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, co
out_draw_list->Flags = backup_flags;
}
// [DEBUG] Compute mask of inputs with the same codepoint.
static int CalcFontGlyphSrcOverlapMask(ImFontAtlas* atlas, ImFont* font, unsigned int codepoint)
{
int mask = 0, count = 0;
for (int src_n = 0; src_n < font->Sources.Size; src_n++)
{
ImFontConfig* src = font->Sources[src_n];
if (!(src->FontLoader ? src->FontLoader : atlas->FontLoader)->FontSrcContainsGlyph(atlas, src, (ImWchar)codepoint))
continue;
mask |= (1 << src_n);
count++;
}
return count > 1 ? mask : 0;
}
// [DEBUG] Display details for a single font, called by ShowStyleEditor().
void ImGui::DebugNodeFont(ImFont* font)
{
@@ -16730,6 +16745,12 @@ void ImGui::DebugNodeFont(ImFont* font)
if (SmallButton("Remove"))
atlas->RemoveFont(font);
EndDisabled();
SameLine();
if (SmallButton("Clear bakes"))
ImFontAtlasFontDiscardBakes(atlas, font, 0);
SameLine();
if (SmallButton("Clear unused"))
ImFontAtlasFontDiscardBakes(atlas, font, 2);
// Display details
SetNextItemWidth(GetFontSize() * 8);
@@ -16769,6 +16790,37 @@ void ImGui::DebugNodeFont(ImFont* font)
TreePop();
}
}
if (font->Sources.Size > 1 && TreeNode("Input Glyphs Overlap Detection Tool"))
{
TextWrapped("- First Input that contains the glyph is used.\n- Use ImFontConfig::GlyphExcludeRanges[] to specify ranges to ignore glyph in given Input.\n- This tool doesn't cache results and is slow, don't keep it open!");
if (BeginTable("table", 2))
{
for (unsigned int c = 0; c < 0x10000; c++)
if (int overlap_mask = CalcFontGlyphSrcOverlapMask(atlas, font, c))
{
unsigned int c_end = c + 1;
while (c_end < 0x10000 && CalcFontGlyphSrcOverlapMask(atlas, font, c_end) == overlap_mask)
c_end++;
if (TableNextColumn() && TreeNode((void*)(intptr_t)c, "U+%04X-U+%04X: %d codepoints in %d inputs", c, c_end - 1, c_end - c, ImCountSetBits(overlap_mask)))
{
char utf8_buf[5];
for (unsigned int n = c; n < c_end; n++)
BulletText("Codepoint U+%04X (%s)", n, ImTextCharToUtf8(utf8_buf, n));
TreePop();
}
TableNextColumn();
for (int src_n = 0; src_n < font->Sources.Size; src_n++)
if (overlap_mask & (1 << src_n))
{
Text("%d ", src_n);
SameLine();
}
c = c_end - 1;
}
EndTable();
}
TreePop();
}
// Display all glyphs of the fonts in separate pages of 256 characters
for (int baked_n = 0; baked_n < atlas->Builder->BakedPool.Size; baked_n++)