From b376b683263aabc69fa8a6983b93a0238458232f Mon Sep 17 00:00:00 2001 From: ocornut Date: Sun, 6 Nov 2016 21:50:18 +0100 Subject: [PATCH] Font: Added io.FontDefault in ImGuiIO structure to make it easier to change default font from third-party or demo code --- imgui.cpp | 15 ++++++++++++--- imgui.h | 1 + imgui_demo.cpp | 5 +++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index c143d0b0..e7984b9b 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -658,6 +658,7 @@ static float GetDraggedColumnOffset(int column_index); static bool IsKeyPressedMap(ImGuiKey key, bool repeat = true); +static ImFont* GetDefaultFont(); static void SetCurrentFont(ImFont* font); static void SetCurrentWindow(ImGuiWindow* window); static void SetWindowScrollY(ImGuiWindow* window, float new_scroll_y); @@ -809,6 +810,7 @@ ImGuiIO::ImGuiIO() LogFilename = "imgui_log.txt"; Fonts = &GImDefaultFontAtlas; FontGlobalScale = 1.0f; + FontDefault = NULL; DisplayFramebufferScale = ImVec2(1.0f, 1.0f); MousePos = ImVec2(-1,-1); MousePosPrev = ImVec2(-1,-1); @@ -2106,7 +2108,8 @@ void ImGui::NewFrame() g.Initialized = true; } - SetCurrentFont(g.IO.Fonts->Fonts[0]); + SetCurrentFont(GetDefaultFont()); + IM_ASSERT(g.Font->IsLoaded()); g.Time += g.IO.DeltaTime; g.FrameCount += 1; @@ -4586,6 +4589,12 @@ float ImGui::CalcItemWidth() return w; } +static ImFont* GetDefaultFont() +{ + ImGuiContext& g = *GImGui; + return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0]; +} + static void SetCurrentFont(ImFont* font) { ImGuiContext& g = *GImGui; @@ -4601,7 +4610,7 @@ void ImGui::PushFont(ImFont* font) { ImGuiContext& g = *GImGui; if (!font) - font = g.IO.Fonts->Fonts[0]; + font = GetDefaultFont(); SetCurrentFont(font); g.FontStack.push_back(font); g.CurrentWindow->DrawList->PushTextureID(font->ContainerAtlas->TexID); @@ -4612,7 +4621,7 @@ void ImGui::PopFont() ImGuiContext& g = *GImGui; g.CurrentWindow->DrawList->PopTextureID(); g.FontStack.pop_back(); - SetCurrentFont(g.FontStack.empty() ? g.IO.Fonts->Fonts[0] : g.FontStack.back()); + SetCurrentFont(g.FontStack.empty() ? GetDefaultFont() : g.FontStack.back()); } void ImGui::PushAllowKeyboardFocus(bool allow_keyboard_focus) diff --git a/imgui.h b/imgui.h index edb1d68d..776b4d5c 100644 --- a/imgui.h +++ b/imgui.h @@ -739,6 +739,7 @@ struct ImGuiIO ImFontAtlas* Fonts; // // Load and assemble one or more fonts into a single tightly packed texture. Output to Fonts array. float FontGlobalScale; // = 1.0f // Global scale all fonts bool FontAllowUserScaling; // = false // Allow user scaling text of individual window with CTRL+Wheel. + ImFont* FontDefault; // = NULL // Font to use on NewFrame(). Use NULL to uses Fonts->Fonts[0]. ImVec2 DisplayFramebufferScale; // = (1.0f,1.0f) // For retina display or other situations where window coordinates are different from framebuffer coordinates. User storage only, presently not used by ImGui. ImVec2 DisplayVisibleMin; // (0.0f,0.0f) // If you use DisplaySize as a virtual space larger than your screen, set DisplayVisibleMin/Max to the visible area. ImVec2 DisplayVisibleMax; // (0.0f,0.0f) // If the values are the same, we defaults to Min=(0.0f) and Max=DisplaySize diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 4ad5e647..e5215141 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -1721,7 +1721,8 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) if (ImGui::TreeNode("Fonts", "Fonts (%d)", ImGui::GetIO().Fonts->Fonts.Size)) { ImGui::SameLine(); ShowHelpMarker("Tip: Load fonts with io.Fonts->AddFontFromFileTTF()\nbefore calling io.Fonts->GetTex* functions."); - ImFontAtlas* atlas = ImGui::GetIO().Fonts; + ImGuiIO& io = ImGui::GetIO(); + ImFontAtlas* atlas = io.Fonts; if (ImGui::TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight)) { ImGui::Image(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0,0), ImVec2(1,1), ImColor(255,255,255,255), ImColor(255,255,255,128)); @@ -1733,7 +1734,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImFont* font = atlas->Fonts[i]; ImGui::BulletText("Font %d: \'%s\', %.2f px, %d glyphs", i, font->ConfigData ? font->ConfigData[0].Name : "", font->FontSize, font->Glyphs.Size); ImGui::TreePush((void*)(intptr_t)i); - if (i > 0) { ImGui::SameLine(); if (ImGui::SmallButton("Set as default")) { atlas->Fonts[i] = atlas->Fonts[0]; atlas->Fonts[0] = font; } } + ImGui::SameLine(); if (ImGui::SmallButton("Set as default")) io.FontDefault = font; ImGui::PushFont(font); ImGui::Text("The quick brown fox jumps over the lazy dog"); ImGui::PopFont();