Skip to content

Commit 172f00e

Browse files
committed
decouple line height from the font size
1 parent ff2dfc8 commit 172f00e

File tree

7 files changed

+89
-77
lines changed

7 files changed

+89
-77
lines changed

imgui.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4012,7 +4012,7 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
40124012
Initialized = false;
40134013
Font = NULL;
40144014
FontBaked = NULL;
4015-
FontSize = FontSizeBase = FontBakedScale = CurrentDpiScale = 0.0f;
4015+
FontSize = FontSizeBase = FontBakedScale = LineHeight = CurrentDpiScale = 0.0f;
40164016
FontRasterizerDensity = 1.0f;
40174017
IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)();
40184018
if (shared_font_atlas == NULL)
@@ -5961,10 +5961,9 @@ ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_tex
59615961
text_display_end = text_end;
59625962

59635963
ImFont* font = g.Font;
5964-
const float font_size = g.FontSize;
59655964
if (text == text_display_end)
5966-
return ImVec2(0.0f, font_size);
5967-
ImVec2 text_size = font->CalcTextSizeA(font_size, FLT_MAX, wrap_width, text, text_display_end, NULL);
5965+
return ImVec2(0.0f, g.LineHeight);
5966+
ImVec2 text_size = font->CalcTextSizeA(g.FontSize, FLT_MAX, wrap_width, text, text_display_end, NULL);
59685967

59695968
// Round
59705969
// FIXME: This has been here since Dec 2015 (7b0bf230) but down the line we want this out.
@@ -7057,7 +7056,7 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl
70577056
// FIXME: Would be nice to generalize the subtleties expressed here into reusable code.
70587057
float pad_l = style.FramePadding.x;
70597058
float pad_r = style.FramePadding.x;
7060-
float button_sz = g.FontSize;
7059+
float button_sz = g.LineHeight;
70617060
ImVec2 close_button_pos;
70627061
ImVec2 collapse_button_pos;
70637062
if (has_close_button)
@@ -7085,7 +7084,7 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl
70857084
if (has_close_button)
70867085
{
70877086
g.CurrentItemFlags |= ImGuiItemFlags_NoFocus;
7088-
if (CloseButton(window->GetID("#CLOSE"), close_button_pos))
7087+
if (CloseButton(window->GetID("#CLOSE"), close_button_pos, button_sz))
70897088
*p_open = false;
70907089
g.CurrentItemFlags &= ~ImGuiItemFlags_NoFocus;
70917090
}
@@ -7440,8 +7439,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
74407439
// Lock menu offset so size calculation can use it as menu-bar windows need a minimum size.
74417440
window->DC.MenuBarOffset.x = ImMax(ImMax(window->WindowPadding.x, style.ItemSpacing.x), g.NextWindowData.MenuBarOffsetMinVal.x);
74427441
window->DC.MenuBarOffset.y = g.NextWindowData.MenuBarOffsetMinVal.y;
7443-
window->TitleBarHeight = (flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : g.FontSize + g.Style.FramePadding.y * 2.0f;
7444-
window->MenuBarHeight = (flags & ImGuiWindowFlags_MenuBar) ? window->DC.MenuBarOffset.y + g.FontSize + g.Style.FramePadding.y * 2.0f : 0.0f;
7442+
window->TitleBarHeight = (flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : g.LineHeight + g.Style.FramePadding.y * 2.0f;
7443+
window->MenuBarHeight = (flags & ImGuiWindowFlags_MenuBar) ? window->DC.MenuBarOffset.y + g.LineHeight + g.Style.FramePadding.y * 2.0f : 0.0f;
74457444
window->FontRefSize = g.FontSize; // Lock this to discourage calling window->CalcFontSize() outside of current window.
74467445

74477446
// Depending on condition we use previous or current window size to compare against contents size to decide if a scrollbar should be visible.
@@ -7574,9 +7573,9 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
75747573
// Large values tend to lead to variety of artifacts and are not recommended.
75757574
window->WindowRounding = (flags & ImGuiWindowFlags_ChildWindow) ? style.ChildRounding : ((flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiWindowFlags_Modal)) ? style.PopupRounding : style.WindowRounding;
75767575

7577-
// For windows with title bar or menu bar, we clamp to FrameHeight(FontSize + FramePadding.y * 2.0f) to completely hide artifacts.
7576+
// For windows with title bar or menu bar, we clamp to FrameHeight(LineHeight + FramePadding.y * 2.0f) to completely hide artifacts.
75787577
//if ((window->Flags & ImGuiWindowFlags_MenuBar) || !(window->Flags & ImGuiWindowFlags_NoTitleBar))
7579-
// window->WindowRounding = ImMin(window->WindowRounding, g.FontSize + style.FramePadding.y * 2.0f);
7578+
// window->WindowRounding = ImMin(window->WindowRounding, g.LineHeight + style.FramePadding.y * 2.0f);
75807579

75817580
// Apply window focus (new and reactivated windows are moved to front)
75827581
bool want_focus = false;
@@ -8745,8 +8744,8 @@ void ImGui::UpdateFontsNewFrame()
87458744
g.Font = font;
87468745
g.FontSizeBase = g.Style.FontSizeBase;
87478746
g.FontSize = 0.0f;
8748-
ImFontStackData font_stack_data = { font, g.Style.FontSizeBase, g.Style.FontSizeBase }; // <--- Will restore FontSize
8749-
SetCurrentFont(font_stack_data.Font, font_stack_data.FontSizeBeforeScaling, 0.0f); // <--- but use 0.0f to enable scale
8747+
ImFontStackData font_stack_data = { font, g.Style.FontSizeBase, g.Style.FontSizeBase }; // <--- Will restore FontSize
8748+
SetCurrentFont(font_stack_data.Font, font_stack_data.FontSizeBeforeScaling, 0.0f); // <--- but use 0.0f to enable scale
87508749
g.FontStack.push_back(font_stack_data);
87518750
IM_ASSERT(g.Font->IsLoaded());
87528751
}
@@ -8875,6 +8874,7 @@ void ImGui::UpdateCurrentFontSize(float restore_font_size_after_scaling)
88758874
g.Font->CurrentRasterizerDensity = g.FontRasterizerDensity;
88768875
g.FontSize = final_size;
88778876
g.FontBaked = (g.Font != NULL && window != NULL) ? g.Font->GetFontBaked(final_size) : NULL;
8877+
g.LineHeight = g.FontBaked ? g.FontBaked->LineHeight : g.FontSize;
88788878
g.FontBakedScale = (g.Font != NULL && window != NULL) ? (g.FontSize / g.FontBaked->Size) : 0.0f;
88798879
g.DrawListSharedData.FontSize = g.FontSize;
88808880
g.DrawListSharedData.FontScale = g.FontBakedScale;
@@ -11365,25 +11365,25 @@ ImVec2 ImGui::CalcItemSize(ImVec2 size, float default_w, float default_h)
1136511365
float ImGui::GetTextLineHeight()
1136611366
{
1136711367
ImGuiContext& g = *GImGui;
11368-
return g.FontSize;
11368+
return g.LineHeight;
1136911369
}
1137011370

1137111371
float ImGui::GetTextLineHeightWithSpacing()
1137211372
{
1137311373
ImGuiContext& g = *GImGui;
11374-
return g.FontSize + g.Style.ItemSpacing.y;
11374+
return g.LineHeight + g.Style.ItemSpacing.y;
1137511375
}
1137611376

1137711377
float ImGui::GetFrameHeight()
1137811378
{
1137911379
ImGuiContext& g = *GImGui;
11380-
return g.FontSize + g.Style.FramePadding.y * 2.0f;
11380+
return g.LineHeight + g.Style.FramePadding.y * 2.0f;
1138111381
}
1138211382

1138311383
float ImGui::GetFrameHeightWithSpacing()
1138411384
{
1138511385
ImGuiContext& g = *GImGui;
11386-
return g.FontSize + g.Style.FramePadding.y * 2.0f + g.Style.ItemSpacing.y;
11386+
return g.LineHeight + g.Style.FramePadding.y * 2.0f + g.Style.ItemSpacing.y;
1138711387
}
1138811388

1138911389
ImVec2 ImGui::GetContentRegionAvail()
@@ -16660,8 +16660,7 @@ void ImGui::ShowMetricsWindow(bool* p_open)
1666016660
{
1666116661
char buf[32];
1666216662
ImFormatString(buf, IM_ARRAYSIZE(buf), "%d", window->BeginOrderWithinContext);
16663-
float font_size = GetFontSize();
16664-
draw_list->AddRectFilled(window->Pos, window->Pos + ImVec2(font_size, font_size), IM_COL32(200, 100, 100, 255));
16663+
draw_list->AddRectFilled(window->Pos, window->Pos + ImVec2(g.FontSize, g.LineHeight), IM_COL32(200, 100, 100, 255));
1666516664
draw_list->AddText(window->Pos, IM_COL32(255, 255, 255, 255), buf);
1666616665
}
1666716666
}
@@ -17044,7 +17043,7 @@ void ImGui::DebugNodeFont(ImFont* font)
1704417043
baked->FindGlyph((ImWchar)base);
1704517044

1704617045
const int surface_sqrt = (int)ImSqrt((float)baked->MetricsTotalSurface);
17047-
Text("Ascent: %f, Descent: %f, Ascent-Descent: %f", baked->Ascent, baked->Descent, baked->Ascent - baked->Descent);
17046+
Text("Ascent: %f, Descent: %f, Ascent-Descent: %f, LineHeight: %f", baked->Ascent, baked->Descent, baked->Ascent - baked->Descent, baked->LineHeight);
1704817047
Text("Texture Area: about %d px ~%dx%d px", baked->MetricsTotalSurface, surface_sqrt, surface_sqrt);
1704917048
for (int src_n = 0; src_n < font->Sources.Size; src_n++)
1705017049
{
@@ -17389,7 +17388,7 @@ void ImGui::ShowDebugLogWindow(bool* p_open)
1738917388
{
1739017389
ImGuiContext& g = *GImGui;
1739117390
if ((g.NextWindowData.HasFlags & ImGuiNextWindowDataFlags_HasSize) == 0)
17392-
SetNextWindowSize(ImVec2(0.0f, GetFontSize() * 12.0f), ImGuiCond_FirstUseEver);
17391+
SetNextWindowSize(ImVec2(0.0f, GetTextLineHeight() * 12.0f), ImGuiCond_FirstUseEver);
1739317392
if (!Begin("Dear ImGui Debug Log", p_open) || GetCurrentWindow()->BeginCount > 1)
1739417393
{
1739517394
End();
@@ -17709,7 +17708,7 @@ void ImGui::ShowIDStackToolWindow(bool* p_open)
1770917708
{
1771017709
ImGuiContext& g = *GImGui;
1771117710
if ((g.NextWindowData.HasFlags & ImGuiNextWindowDataFlags_HasSize) == 0)
17712-
SetNextWindowSize(ImVec2(0.0f, GetFontSize() * 8.0f), ImGuiCond_FirstUseEver);
17711+
SetNextWindowSize(ImVec2(0.0f, GetTextLineHeight() * 8.0f), ImGuiCond_FirstUseEver);
1771317712
if (!Begin("Dear ImGui ID Stack Tool", p_open) || GetCurrentWindow()->BeginCount > 1)
1771417713
{
1771517714
End();

imgui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3755,6 +3755,7 @@ struct ImFontBaked
37553755

37563756
// [Internal] Members: Cold
37573757
float Ascent, Descent; // 4+4 // out // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] (unscaled)
3758+
float LineHeight; // 4 // out // Vertical distance between two baselines
37583759
unsigned int MetricsTotalSurface:26;// 3 // out // Total surface in pixels to get an idea of the font rasterization/texture cost (not exact, we approximate the cost of padding between glyphs)
37593760
unsigned int WantDestroy:1; // 0 // // Queued for destroy
37603761
unsigned int LoadNoFallback:1; // 0 // // Disable loading fallback in lower-level calls.

imgui_draw.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4616,6 +4616,7 @@ static bool ImGui_ImplStbTrueType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig
46164616
stbtt_GetFontVMetrics(&bd_font_data->FontInfo, &unscaled_ascent, &unscaled_descent, &unscaled_line_gap);
46174617
baked->Ascent = ImCeil(unscaled_ascent * scale_for_layout);
46184618
baked->Descent = ImFloor(unscaled_descent * scale_for_layout);
4619+
baked->LineHeight = baked->Size;
46194620
}
46204621
return true;
46214622
}
@@ -5060,7 +5061,7 @@ void ImFontBaked::ClearOutputData()
50605061
IndexAdvanceX.clear();
50615062
IndexLookup.clear();
50625063
FallbackGlyphIndex = -1;
5063-
Ascent = Descent = 0.0f;
5064+
Ascent = Descent = LineHeight = 0.0f;
50645065
MetricsTotalSurface = 0;
50655066
}
50665067

@@ -5460,8 +5461,8 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons
54605461
if (!text_end)
54615462
text_end = text_begin + ImStrlen(text_begin); // FIXME-OPT: Need to avoid this.
54625463

5463-
const float line_height = size;
54645464
ImFontBaked* baked = GetFontBaked(size);
5465+
const float line_height = baked->LineHeight;
54655466
const float scale = size / baked->Size;
54665467

54675468
ImVec2 text_size = ImVec2(0, 0);
@@ -5591,9 +5592,9 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im
55915592
if (!text_end)
55925593
text_end = text_begin + ImStrlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls.
55935594

5594-
const float line_height = size;
55955595
ImFontBaked* baked = GetFontBaked(size);
55965596

5597+
const float line_height = baked->LineHeight;
55975598
const float scale = size / baked->Size;
55985599
const float origin_x = x;
55995600
const bool word_wrap_enabled = (wrap_width > 0.0f);

imgui_internal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,8 +2145,9 @@ struct ImGuiContext
21452145
ImVector<ImFontAtlas*> FontAtlases; // List of font atlases used by the context (generally only contains g.IO.Fonts aka the main font atlas)
21462146
ImFont* Font; // Currently bound font. (== FontStack.back().Font)
21472147
ImFontBaked* FontBaked; // Currently bound font at currently bound size. (== Font->GetFontBaked(FontSize))
2148-
float FontSize; // Currently bound font size == line height (== FontSizeBase + externals scales applied in the UpdateCurrentFontSize() function).
2148+
float FontSize; // Currently bound font size (== FontSizeBase + externals scales applied in the UpdateCurrentFontSize() function).
21492149
float FontSizeBase; // Font size before scaling == style.FontSizeBase == value passed to PushFont() when specified.
2150+
float LineHeight; // Currently bound font's line height (+ externals scales applied in the UpdateCurrentFontSize() function).
21502151
float FontBakedScale; // == FontBaked->Size / FontSize. Scale factor over baked size. Rarely used nowadays, very often == 1.0f.
21512152
float FontRasterizerDensity; // Current font density. Used by all calls to GetFontBaked().
21522153
float CurrentDpiScale; // Current window/viewport DpiScale == CurrentViewport->DpiScale
@@ -3551,7 +3552,7 @@ namespace ImGui
35513552
IMGUI_API bool CheckboxFlags(const char* label, ImU64* flags, ImU64 flags_value);
35523553

35533554
// Widgets: Window Decorations
3554-
IMGUI_API bool CloseButton(ImGuiID id, const ImVec2& pos);
3555+
IMGUI_API bool CloseButton(ImGuiID id, ImVec2 pos, float size);
35553556
IMGUI_API bool CollapseButton(ImGuiID id, const ImVec2& pos);
35563557
IMGUI_API void Scrollbar(ImGuiAxis axis);
35573558
IMGUI_API bool ScrollbarEx(const ImRect& bb, ImGuiID id, ImGuiAxis axis, ImS64* p_scroll_v, ImS64 avail_v, ImS64 contents_v, ImDrawFlags draw_rounding_flags = 0);

imgui_tables.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3075,7 +3075,7 @@ float ImGui::TableGetHeaderRowHeight()
30753075
// In your custom header row you may omit this all together and just call TableNextRow() without a height...
30763076
ImGuiContext& g = *GImGui;
30773077
ImGuiTable* table = g.CurrentTable;
3078-
float row_height = g.FontSize;
3078+
float row_height = g.LineHeight;
30793079
for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
30803080
if (IM_BITARRAY_TESTBIT(table->EnabledMaskByIndex, column_n))
30813081
if ((table->Columns[column_n].Flags & ImGuiTableColumnFlags_NoHeaderLabel) == 0)

0 commit comments

Comments
 (0)