From afd3a36f6989d09fa785bb27ffb9537c23333ea9 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 5 May 2025 19:39:56 +0200 Subject: [PATCH] Demo: added basic Fonts section under main demo (same as Metrics one) for visibility. --- imgui.cpp | 24 ++++++++++++++++++++---- imgui_demo.cpp | 20 ++++++++++++++++++++ imgui_internal.h | 1 + 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index ad9132f23..cb36ac536 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -15491,6 +15491,18 @@ static void MetricsHelpMarker(const char* desc) // [DEBUG] List fonts in a font atlas and display its texture void ImGui::ShowFontAtlas(ImFontAtlas* atlas) { + ImGuiContext& g = *GImGui; + + Text("Read "); + SameLine(0, 0); + TextLinkOpenURL("https://www.dearimgui.com/faq/"); + SameLine(0, 0); + Text(" for details on font loading."); + + ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig; + Checkbox("Show font preview", &cfg->ShowFontPreview); + + // Font list for (ImFont* font : atlas->Fonts) { PushID(font); @@ -15499,7 +15511,6 @@ void ImGui::ShowFontAtlas(ImFontAtlas* atlas) } if (TreeNode("Font Atlas", "Font Atlas (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight)) { - ImGuiContext& g = *GImGui; PushStyleVar(ImGuiStyleVar_ImageBorderSize, ImMax(1.0f, g.Style.ImageBorderSize)); ImageWithBg(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f), ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); PopStyleVar(); @@ -16290,6 +16301,8 @@ void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, co // [DEBUG] Display details for a single font, called by ShowStyleEditor(). void ImGui::DebugNodeFont(ImFont* font) { + ImGuiContext& g = *GImGui; + ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig; bool opened = TreeNode(font, "Font: \"%s\": %.2f px, %d glyphs, %d sources(s)", font->Sources ? font->Sources[0].Name : "", font->FontSize, font->Glyphs.Size, font->SourcesCount); @@ -16297,9 +16310,12 @@ void ImGui::DebugNodeFont(ImFont* font) if (!opened) Indent(); Indent(); - PushFont(font); - Text("The quick brown fox jumps over the lazy dog"); - PopFont(); + if (cfg->ShowFontPreview) + { + PushFont(font); + Text("The quick brown fox jumps over the lazy dog"); + PopFont(); + } if (!opened) { Unindent(); diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 24ea67fa2..091a52569 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -82,6 +82,7 @@ Index of this file: // [SECTION] DemoWindowWidgetsDisableBlocks() // [SECTION] DemoWindowWidgetsDragAndDrop() // [SECTION] DemoWindowWidgetsDragsAndSliders() +// [SECTION] DemoWindowWidgetsFonts() // [SECTION] DemoWindowWidgetsImages() // [SECTION] DemoWindowWidgetsListBoxes() // [SECTION] DemoWindowWidgetsMultiComponents() @@ -1719,6 +1720,24 @@ static void DemoWindowWidgetsDragsAndSliders() } } +//----------------------------------------------------------------------------- +// [SECTION] DemoWindowWidgetsFonts() +//----------------------------------------------------------------------------- + +// Forward declare ShowFontAtlas() which isn't worth putting in public API yet +namespace ImGui { IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); } + +static void DemoWindowWidgetsFonts() +{ + IMGUI_DEMO_MARKER("Widgets/Fonts"); + if (ImGui::TreeNode("Fonts")) + { + ImFontAtlas* atlas = ImGui::GetIO().Fonts; + ImGui::ShowFontAtlas(atlas); + ImGui::TreePop(); + } +} + //----------------------------------------------------------------------------- // [SECTION] DemoWindowWidgetsImages() //----------------------------------------------------------------------------- @@ -4182,6 +4201,7 @@ static void DemoWindowWidgets(ImGuiDemoWindowData* demo_data) DemoWindowWidgetsDragAndDrop(); DemoWindowWidgetsDragsAndSliders(); + DemoWindowWidgetsFonts(); DemoWindowWidgetsImages(); DemoWindowWidgetsListBoxes(); DemoWindowWidgetsMultiComponents(); diff --git a/imgui_internal.h b/imgui_internal.h index 1c60ddf18..996eb82a2 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -2020,6 +2020,7 @@ struct ImGuiMetricsConfig int ShowTablesRectsType = -1; int HighlightMonitorIdx = -1; ImGuiID HighlightViewportID = 0; + bool ShowFontPreview = true; }; struct ImGuiStackLevelInfo