Skip to content

Commit 715c954

Browse files
committed
Implement ExtraLineHeight and ExtraLineAdvance (fixup: Add BaselineOffset)
1 parent a9129bb commit 715c954

5 files changed

Lines changed: 30 additions & 14 deletions

File tree

imgui.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,7 +2813,7 @@ void ImGui::RenderText(ImVec2 pos, const char* text, const char* text_end, bool
28132813

28142814
if (text != text_display_end)
28152815
{
2816-
window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontLineAdvance, pos, GetColorU32(ImGuiCol_Text), text, text_display_end);
2816+
window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontLineAdvance, g.FontBaselineOffset, pos, GetColorU32(ImGuiCol_Text), text, text_display_end);
28172817
if (g.LogEnabled)
28182818
LogRenderedText(&pos, text, text_display_end);
28192819
}
@@ -2829,7 +2829,7 @@ void ImGui::RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end
28292829

28302830
if (text != text_end)
28312831
{
2832-
window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontLineAdvance, pos, GetColorU32(ImGuiCol_Text), text, text_end, wrap_width);
2832+
window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontLineAdvance, g.FontBaselineOffset, pos, GetColorU32(ImGuiCol_Text), text, text_end, wrap_width);
28332833
if (g.LogEnabled)
28342834
LogRenderedText(&pos, text, text_end);
28352835
}
@@ -6626,6 +6626,7 @@ void ImGui::SetCurrentFont(ImFont* font)
66266626
g.FontSize = g.Font->FontSize * font_scale;
66276627
g.FontLineHeight = (g.Font->FontSize + g.Font->ExtraLineHeight) * font_scale;
66286628
g.FontLineAdvance = (g.Font->FontSize + g.Font->ExtraLineHeight + g.Font->ExtraLineAdvance) * font_scale;
6629+
g.FontBaselineOffset = g.Font->BaselineOffset * font_scale;
66296630

66306631
ImFontAtlas* atlas = g.Font->ContainerAtlas;
66316632
g.DrawListSharedData.TexUvWhitePixel = atlas->TexUvWhitePixel;
@@ -6634,6 +6635,7 @@ void ImGui::SetCurrentFont(ImFont* font)
66346635
g.DrawListSharedData.FontSize = g.FontSize;
66356636
g.DrawListSharedData.FontLineHeight = g.FontLineHeight;
66366637
g.DrawListSharedData.FontLineAdvance = g.FontLineAdvance;
6638+
g.DrawListSharedData.FontBaselineOffset = g.FontBaselineOffset;
66376639
}
66386640

66396641
void ImGui::PushFont(ImFont* font)
@@ -7107,6 +7109,11 @@ float ImGui::GetFontLineAdvance()
71077109
return GImGui->FontLineAdvance;
71087110
}
71097111

7112+
float ImGui::GetFontBaselineOffset()
7113+
{
7114+
return GImGui->FontBaselineOffset;
7115+
}
7116+
71107117
ImVec2 ImGui::GetFontTexUvWhitePixel()
71117118
{
71127119
return GImGui->DrawListSharedData.TexUvWhitePixel;
@@ -11365,7 +11372,7 @@ void ImGui::DebugRenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP*
1136511372
window->DrawList->AddRectFilled(thumb_r.Min, thumb_r.Max, GetColorU32(ImGuiCol_WindowBg, alpha_mul));
1136611373
window->DrawList->AddRectFilled(title_r.Min, title_r.Max, GetColorU32(window_is_focused ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg, alpha_mul));
1136711374
window->DrawList->AddRect(thumb_r.Min, thumb_r.Max, GetColorU32(ImGuiCol_Border, alpha_mul));
11368-
window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontLineAdvance, title_r.Min, GetColorU32(ImGuiCol_Text, alpha_mul), thumb_window->Name, FindRenderedTextEnd(thumb_window->Name));
11375+
window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontLineAdvance, g.FontBaselineOffset, title_r.Min, GetColorU32(ImGuiCol_Text, alpha_mul), thumb_window->Name, FindRenderedTextEnd(thumb_window->Name));
1136911376
}
1137011377
draw_list->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_Border, alpha_mul));
1137111378
}
@@ -11972,6 +11979,8 @@ void ImGui::DebugNodeFont(ImFont* font)
1197211979
DragFloat("Extra line height", &font->ExtraLineHeight, 1.0f, -font->FontSize, FLT_MAX, "%g");
1197311980
SetNextItemWidth(GetFontSize() * 8);
1197411981
DragFloat("Extra line advance", &font->ExtraLineAdvance, 1.0f, -FLT_MAX, FLT_MAX, "%g");
11982+
SetNextItemWidth(GetFontSize() * 8);
11983+
DragFloat("Baseline offset", &font->BaselineOffset, 0.1f, -FLT_MAX, FLT_MAX, "%g");
1197511984
Text("Ascent: %f, Descent: %f, Height: %f", font->Ascent, font->Descent, font->Ascent - font->Descent);
1197611985
Text("Line Height: %f", font->FontSize + font->ExtraLineHeight);
1197711986
Text("Line Advance: %f", font->FontSize + font->ExtraLineHeight + font->ExtraLineAdvance);

imgui.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ namespace ImGui
426426
IMGUI_API float GetFontSize(); // get current font size (= size in pixels) of current font with current scale applied
427427
IMGUI_API float GetFontLineHeight(); // get current font line height (in pixels) of current font with current scale applied
428428
IMGUI_API float GetFontLineAdvance(); // get current font line advance (in pixels) of current font with current scale applied
429+
IMGUI_API float GetFontBaselineOffset(); // get current font baseline offset (in pixels) of current font with current scale applied
429430
IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a while pixel, useful to draw custom shapes via the ImDrawList API
430431
IMGUI_API ImU32 GetColorU32(ImGuiCol idx, float alpha_mul = 1.0f); // retrieve given style color with style alpha applied and optional extra alpha multiplier, packed as a 32-bit value suitable for ImDrawList
431432
IMGUI_API ImU32 GetColorU32(const ImVec4& col); // retrieve given color with style alpha applied, packed as a 32-bit value suitable for ImDrawList
@@ -2447,7 +2448,7 @@ struct ImDrawList
24472448
IMGUI_API void AddNgonFilled(const ImVec2& center, float radius, ImU32 col, int num_segments);
24482449
IMGUI_API void AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL);
24492450
IMGUI_API void AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL);
2450-
IMGUI_API void AddText(const ImFont* font, float font_size, float font_line_height, float font_line_advance, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL);
2451+
IMGUI_API void AddText(const ImFont* font, float font_size, float font_line_height, float font_line_advance, float font_baseline_offset, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL);
24512452
IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, ImDrawFlags flags, float thickness);
24522453
IMGUI_API void AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); // Note: Anti-aliased filling requires points to be in clockwise order.
24532454
IMGUI_API void AddBezierCubic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0); // Cubic Bezier (4 control points)
@@ -2762,6 +2763,7 @@ struct ImFont
27622763
float Scale; // 4 // in // = 1.f // Base font scale, multiplied by the per-window font scale which you can adjust with SetWindowFontScale()
27632764
float ExtraLineHeight; // 4 // in // // Amount of space to be added to line height. Using negative values are allowed. (You need to call ImGui::SetCurrentFont() to populate these values when you're expecting immediate response.)
27642765
float ExtraLineAdvance; // 4 // in // // Amount of extra vertical advance when moving cursor to next line, apply to multiline texts drawn with single instruction. (You need to call ImGui::SetCurrentFont() to populate these values when you're expecting immediate response.)
2766+
float BaselineOffset; // 4 // in // // Font baseline will be modified by this value while drawing. (You need to call ImGui::SetCurrentFont() to populate these values when you're expecting immediate response.)
27652767
float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize]
27662768
int MetricsTotalSurface;// 4 // 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)
27672769
ImU8 Used4kPagesMap[(IM_UNICODE_CODEPOINT_MAX+1)/4096/8]; // 2 bytes if ImWchar=ImWchar16, 34 bytes if ImWchar==ImWchar32. Store 1-bit for each block of 4K codepoints that has one active glyph. This is mainly used to facilitate iterations across all used codepoints.
@@ -2781,7 +2783,7 @@ struct ImFont
27812783
IMGUI_API const char* CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const;
27822784
IMGUI_API void RenderChar(ImDrawList* draw_list, float size, float line_height, ImVec2 pos, ImU32 col, ImWchar c) const;
27832785
IMGUI_API void RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width = 0.0f, bool cpu_fine_clip = false) const;
2784-
IMGUI_API void RenderText(ImDrawList* draw_list, float size, float line_height, float line_advance, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width = 0.0f, bool cpu_fine_clip = false) const;
2786+
IMGUI_API void RenderText(ImDrawList* draw_list, float size, float line_height, float line_advance, float baseline_offset, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width = 0.0f, bool cpu_fine_clip = false) const;
27852787

27862788
// [Internal] Don't use!
27872789
IMGUI_API void BuildLookupTable();

imgui_draw.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,11 +1579,12 @@ void ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos,
15791579
const float scale = font_size / font->FontSize;
15801580
const float line_height = (font->FontSize + font->ExtraLineHeight) * scale;
15811581
const float line_advance = (font->FontSize + font->ExtraLineAdvance) * scale;
1582+
const float baseline_offset = font->BaselineOffset * scale;
15821583

1583-
AddText(font, font_size, line_height, line_advance, pos, col, text_begin, text_end, wrap_width, cpu_fine_clip_rect);
1584+
AddText(font, font_size, line_height, line_advance, baseline_offset, pos, col, text_begin, text_end, wrap_width, cpu_fine_clip_rect);
15841585
}
15851586

1586-
void ImDrawList::AddText(const ImFont* font, float font_size, float font_line_height, float font_line_advance, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end, float wrap_width, const ImVec4* cpu_fine_clip_rect)
1587+
void ImDrawList::AddText(const ImFont* font, float font_size, float font_line_height, float font_line_advance, float font_baseline_offset, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end, float wrap_width, const ImVec4* cpu_fine_clip_rect)
15871588
{
15881589
if ((col & IM_COL32_A_MASK) == 0)
15891590
return;
@@ -1609,7 +1610,7 @@ void ImDrawList::AddText(const ImFont* font, float font_size, float font_line_he
16091610
clip_rect.z = ImMin(clip_rect.z, cpu_fine_clip_rect->z);
16101611
clip_rect.w = ImMin(clip_rect.w, cpu_fine_clip_rect->w);
16111612
}
1612-
font->RenderText(this, font_size, font_line_height, font_line_advance, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip_rect != NULL);
1613+
font->RenderText(this, font_size, font_line_height, font_line_advance, font_baseline_offset, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip_rect != NULL);
16131614
}
16141615

16151616
void ImDrawList::AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end)
@@ -3117,6 +3118,7 @@ ImFont::ImFont()
31173118
FontSize = 0.0f;
31183119
ExtraLineHeight = 0.0f;
31193120
ExtraLineAdvance = 0.0f;
3121+
BaselineOffset = 0.0f;
31203122
FallbackAdvanceX = 0.0f;
31213123
FallbackChar = (ImWchar)-1;
31223124
EllipsisChar = (ImWchar)-1;
@@ -3554,18 +3556,19 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col
35543556
const float scale = size / FontSize;
35553557
const float line_height = (FontSize + ExtraLineHeight) * scale;
35563558
const float line_advance = (FontSize + ExtraLineAdvance) * scale;
3559+
const float baseline_offset = BaselineOffset * scale;
35573560

3558-
RenderText(draw_list, size, line_height, line_advance, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip);
3561+
RenderText(draw_list, size, line_height, line_advance, baseline_offset, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip);
35593562
}
35603563

3561-
void ImFont::RenderText(ImDrawList* draw_list, float size, float line_height, float line_advance, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip) const
3564+
void ImFont::RenderText(ImDrawList* draw_list, float size, float line_height, float line_advance, float baseline_offset, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip) const
35623565
{
35633566
if (!text_end)
35643567
text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls.
35653568

35663569
// Align to be pixel perfect
35673570
pos.x = IM_FLOOR(pos.x);
3568-
pos.y = IM_FLOOR(pos.y + (line_height - size) * 0.5f);
3571+
pos.y = IM_FLOOR(pos.y + (line_height - size) * 0.5f + baseline_offset);
35693572
float x = pos.x;
35703573
float y = pos.y;
35713574
if (y > clip_rect.w)

imgui_internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ struct IMGUI_API ImDrawListSharedData
705705
float FontSize; // Current/default font size (optional, for simplified AddText overload)
706706
float FontLineHeight; // Current/default font line height (optional, for simplified AddText overload)
707707
float FontLineAdvance; // Current/default font line advance (optional, for simplified AddText overload)
708+
float FontBaselineOffset; // Current/default font baseline offset (optional, for simplified AddText overload)
708709
float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo()
709710
float CircleSegmentMaxError; // Number of circle segments to use per pixel of radius for AddCircle() etc
710711
ImVec4 ClipRectFullscreen; // Value for PushClipRectFullscreen()
@@ -1497,6 +1498,7 @@ struct ImGuiContext
14971498
float FontSize; // (Shortcut) == FontBaseScale * g.CurrentWindow->FontWindowScale * Font->FontSize == window->FontSize(). Text height for current window.
14981499
float FontLineHeight; // (Shortcut) == FontBaseScale * g.CurrentWindow->FontWindowScale * (Font->FontSize + Font->ExtraLineHeight) (optional, for simplified AddText overload)
14991500
float FontLineAdvance; // (Shortcut) == FontBaseScale * g.CurrentWindow->FontWindowScale * (Font->FontSize + Font->ExtraLineHeight + Font->ExtraLineAdvance) (optional, for simplified AddText overload)
1501+
float FontBaselineOffset; // (Shortcut) == FontBaseScale * g.CurrentWindow->FontWindowScale * Font->BaselineOffset (optional, for simplified AddText overload)
15001502
float FontBaseScale; // (Shortcut) == IO.FontGlobalScale * Font->Scale. Base text scale.
15011503
ImDrawListSharedData DrawListSharedData;
15021504
double Time;
@@ -1747,7 +1749,7 @@ struct ImGuiContext
17471749
Initialized = false;
17481750
FontAtlasOwnedByContext = shared_font_atlas ? false : true;
17491751
Font = NULL;
1750-
FontSize = FontBaseScale = FontLineHeight = FontLineAdvance = 0.0f;
1752+
FontSize = FontBaseScale = FontLineHeight = FontLineAdvance = FontBaselineOffset = 0.0f;
17511753
IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)();
17521754
Time = 0.0f;
17531755
FrameCount = 0;

imgui_widgets.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4734,7 +4734,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
47344734
if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length)
47354735
{
47364736
ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text);
4737-
draw_window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontLineAdvance, draw_pos - draw_scroll, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect);
4737+
draw_window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontLineAdvance, g.FontBaselineOffset, draw_pos - draw_scroll, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect);
47384738
}
47394739

47404740
// Draw blinking cursor
@@ -4765,7 +4765,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
47654765
if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length)
47664766
{
47674767
ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text);
4768-
draw_window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontLineAdvance, draw_pos, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect);
4768+
draw_window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontLineAdvance, g.FontBaselineOffset, draw_pos, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect);
47694769
}
47704770
}
47714771

0 commit comments

Comments
 (0)