From 014f88b1fcf8275bd92dba87477d28c4deba10bc Mon Sep 17 00:00:00 2001 From: ocornut Date: Sat, 17 Jan 2015 15:44:25 +0000 Subject: [PATCH] Font fixes for horizontal centering within frames --- imgui.cpp | 21 +++++++++++++++------ imgui.h | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index b33e6a18f..db449086f 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -876,7 +876,7 @@ struct ImGuiState ImGuiIO IO; ImGuiStyle Style; ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back() - float FontSize; // (Shortcut) == IO.FontGlobalScale * (Font->Scale * Font->FontSize). Vertical distance between two lines of text, aka == CalcTextSize(" ").y + float FontSize; // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Size of characters. ImVec2 FontTexUvWhitePixel; // (Shortcut) == Font->TexUvForWhite float Time; @@ -2025,7 +2025,7 @@ static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs) continue; if (excluding_childs && (window->Flags & ImGuiWindowFlags_ChildWindow) != 0) continue; - ImGuiAabb bb(window->Pos - g.Style.TouchExtraPadding, window->Pos+window->Size + g.Style.TouchExtraPadding); + ImGuiAabb bb(window->Pos - g.Style.TouchExtraPadding, window->Pos + window->Size + g.Style.TouchExtraPadding); if (bb.Contains(pos)) return window; } @@ -6270,7 +6270,6 @@ void ImDrawList::AddImage(ImTextureID user_texture_id, const ImVec2& a, const Im ImFont::ImFont() { Scale = 1.0f; - DisplayOffset = ImVec2(0.5f, 0.5f); FallbackChar = (ImWchar)'?'; TexPixelsAlpha8 = NULL; @@ -6328,14 +6327,14 @@ void ImFont::ClearTextureData() void ImFont::Clear() { - DisplayOffset = ImVec2(0.5f, 0.5f); + FontSize = 0.0f; + DisplayOffset = ImVec2(-0.5f, 0.5f); ClearTextureData(); TexID = NULL; TexWidth = TexHeight = 0; TexExtraDataPos = TexUvWhitePixel = ImVec2(0, 0); - FontSize = 0.0f; Glyphs.clear(); IndexLookup.clear(); FallbackGlyph = NULL; @@ -6947,6 +6946,11 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons text_size.y += line_height; } + // Cancel out character spacing for the last character of a line (it is baked into glyph->XAdvance field) + const float character_spacing_x = 1.0f * scale; + if (text_size.x > 0.0f) + text_size.x -= character_spacing_x; + if (remaining) *remaining = s; @@ -7003,6 +7007,11 @@ ImVec2 ImFont::CalcTextSizeW(float size, float max_width, const ImWchar* text_be text_size.y += line_height; } + // Cancel out character spacing for the last character of a line (it is baked into glyph->XAdvance field) + const float character_spacing_x = 1.0f * scale; + if (text_size.x > 0.0f) + text_size.x -= character_spacing_x; + if (remaining) *remaining = s; @@ -7321,7 +7330,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) { static float window_scale = 1.0f; ImFont* font = ImGui::GetIO().Font; - ImGui::Text("Font Size: %.2f", font->FontSize); + ImGui::Text("Base Font Size: %.2f", font->FontSize); ImGui::SliderFloat("window scale", &window_scale, 0.3f, 2.0f, "%.1f"); // scale only this window ImGui::SliderFloat("font scale", &font->Scale, 0.3f, 2.0f, "%.1f"); // scale only this font ImGui::SliderFloat("global scale", &ImGui::GetIO().FontGlobalScale, 0.3f, 2.0f, "%.1f"); // scale everything diff --git a/imgui.h b/imgui.h index a2512df16..216d5a42d 100644 --- a/imgui.h +++ b/imgui.h @@ -755,6 +755,7 @@ struct ImDrawList struct ImFont { // Settings + float FontSize; // // Height of characters, set during loading (don't change after loading) float Scale; // = 1.0f // Base font scale, multiplied by the per-window font scale which you can adjust with SetFontScale() ImVec2 DisplayOffset; // = (0.0f,0.0f) // Offset font rendering by xx pixels ImWchar FallbackChar; // = '?' // Replacement glyph if one isn't found. @@ -812,7 +813,6 @@ struct ImFont }; // Runtime data - float FontSize; // Height of characters ImVector Glyphs; ImVector IndexLookup; const Glyph* FallbackGlyph; // == FindGlyph(FontFallbackChar)