Skip to content

Commit e668890

Browse files
adrienlapoulenoireocornut
authored andcommitted
Debug: added encoding viewer in Metrics.
(reworked by omar from original/old commit)
1 parent e66fc22 commit e668890

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

imgui.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -12127,6 +12127,81 @@ static void RenderViewportsThumbnails()
1212712127
ImGui::Dummy(bb_full.GetSize() * SCALE);
1212812128
}
1212912129

12130+
static void ShowEncodingViewerChar(ImFont* font, ImWchar c, const char* c_utf8)
12131+
{
12132+
ImGui::TableNextColumn();
12133+
if (font->FindGlyphNoFallback(c))
12134+
ImGui::TextUnformatted(c_utf8);
12135+
else
12136+
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "(not in font)");
12137+
12138+
ImGui::TableNextColumn();
12139+
char utf8_code[] = "0x.. 0x.. 0x.. 0x..";
12140+
for (int byte_index = 0; c_utf8[byte_index]; byte_index++)
12141+
{
12142+
if (byte_index > 0)
12143+
utf8_code[byte_index * 5 - 1] = ' ';
12144+
ImFormatString(utf8_code + (byte_index * 5) + 2, 3, "%02X", (int)(unsigned char)c_utf8[byte_index]);
12145+
}
12146+
ImGui::TextUnformatted(utf8_code);
12147+
ImGui::TableNextColumn();
12148+
ImGui::Text("U+%04X", (int)c);
12149+
}
12150+
12151+
static void ShowUTF8EncodingViewer()
12152+
{
12153+
static char buf[256] = "";
12154+
static ImFontGlyphRangesBuilder range_builder;
12155+
static ImVector<ImWchar> ranges;
12156+
static bool unique_glyphs = false;
12157+
12158+
ImGui::SetNextItemWidth(-FLT_MIN);
12159+
12160+
bool rebuild = false;
12161+
rebuild |= ImGui::InputText("##Sample Text", buf, IM_ARRAYSIZE(buf));
12162+
rebuild |= ImGui::Checkbox("Sorted unique glyphs", &unique_glyphs);
12163+
if (rebuild && unique_glyphs)
12164+
{
12165+
range_builder.Clear();
12166+
range_builder.AddText(buf);
12167+
ranges.clear();
12168+
range_builder.BuildRanges(&ranges);
12169+
}
12170+
if (ImGui::BeginTable("list", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_ScrollY, ImVec2(0.0f, ImGui::GetFontSize() * 15)))
12171+
{
12172+
ImGui::TableSetupColumn("Glyph");
12173+
ImGui::TableSetupColumn("UTF-8");
12174+
ImGui::TableSetupColumn("Codepoint");
12175+
ImGui::TableHeadersRow();
12176+
12177+
ImFont* font = ImGui::GetFont();
12178+
if (unique_glyphs)
12179+
{
12180+
for (int range_index = 0; range_index < ranges.Size && ranges[range_index] != 0; range_index += 2)
12181+
for (ImWchar c = ranges[range_index]; c <= ranges[range_index + 1]; c++)
12182+
{
12183+
char c_utf8[4 + 1];
12184+
ImTextStrToUtf8(c_utf8, IM_ARRAYSIZE(c_utf8), &c, &c + 1);
12185+
ShowEncodingViewerChar(font, c, c_utf8);
12186+
}
12187+
}
12188+
else
12189+
{
12190+
for (const char* p = buf; p[0] != 0;)
12191+
{
12192+
unsigned int c;
12193+
int c_utf8_len = ImTextCharFromUtf8(&c, p, NULL);
12194+
char c_utf8[4 + 1];
12195+
memcpy(c_utf8, p, c_utf8_len);
12196+
c_utf8[c_utf8_len] = 0;
12197+
ShowEncodingViewerChar(font, (ImWchar)c, c_utf8);
12198+
p += c_utf8_len;
12199+
}
12200+
}
12201+
ImGui::EndTable();
12202+
}
12203+
}
12204+
1213012205
// Avoid naming collision with imgui_demo.cpp's HelpMarker() for unity builds.
1213112206
static void MetricsHelpMarker(const char* desc)
1213212207
{
@@ -12285,6 +12360,12 @@ void ImGui::ShowMetricsWindow(bool* p_open)
1228512360
}
1228612361
}
1228712362

12363+
if (TreeNode("UTF-8 Encoding viewer"))
12364+
{
12365+
ShowUTF8EncodingViewer();
12366+
TreePop();
12367+
}
12368+
1228812369
// The Item Picker tool is super useful to visually select an item and break into the call-stack of where it was submitted.
1228912370
if (Button("Item Picker.."))
1229012371
DebugStartItemPicker();

0 commit comments

Comments
 (0)