Skip to content

Commit

Permalink
Replace ExtraFontAdvance with ExtraFontHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
thedmd committed Nov 22, 2021
1 parent 715c954 commit 818b0ac
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 65 deletions.
26 changes: 7 additions & 19 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2813,7 +2813,7 @@ void ImGui::RenderText(ImVec2 pos, const char* text, const char* text_end, bool

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

if (text != text_end)
{
window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontLineAdvance, g.FontBaselineOffset, pos, GetColorU32(ImGuiCol_Text), text, text_end, wrap_width);
window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontBaselineOffset, pos, GetColorU32(ImGuiCol_Text), text, text_end, wrap_width);
if (g.LogEnabled)
LogRenderedText(&pos, text, text_end);
}
Expand Down Expand Up @@ -2905,7 +2905,6 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con
const ImFont* font = draw_list->_Data->Font;
const float font_size = draw_list->_Data->FontSize;
const float font_line_height = draw_list->_Data->FontLineHeight;
const float font_line_advance = draw_list->_Data->FontLineAdvance;
const char* text_end_ellipsis = NULL;

ImWchar ellipsis_char = font->EllipsisChar;
Expand All @@ -2930,18 +2929,18 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con

// We can now claim the space between pos_max.x and ellipsis_max.x
const float text_avail_width = ImMax((ImMax(pos_max.x, ellipsis_max_x) - ellipsis_total_width) - pos_min.x, 1.0f);
float text_size_clipped_x = font->CalcTextSizeA(font_size, font_line_height, font_line_advance, text_avail_width, 0.0f, text, text_end_full, &text_end_ellipsis).x;
float text_size_clipped_x = font->CalcTextSizeA(font_size, font_line_height, text_avail_width, 0.0f, text, text_end_full, &text_end_ellipsis).x;
if (text == text_end_ellipsis && text_end_ellipsis < text_end_full)
{
// Always display at least 1 character if there's no room for character + ellipsis
text_end_ellipsis = text + ImTextCountUtf8BytesFromChar(text, text_end_full);
text_size_clipped_x = font->CalcTextSizeA(font_size, font_line_height, font_line_advance, FLT_MAX, 0.0f, text, text_end_ellipsis).x;
text_size_clipped_x = font->CalcTextSizeA(font_size, font_line_height, FLT_MAX, 0.0f, text, text_end_ellipsis).x;
}
while (text_end_ellipsis > text && ImCharIsBlankA(text_end_ellipsis[-1]))
{
// Trim trailing space before ellipsis (FIXME: Supporting non-ascii blanks would be nice, for this we need a function to backtrack in UTF-8 text)
text_end_ellipsis--;
text_size_clipped_x -= font->CalcTextSizeA(font_size, font_line_height, font_line_advance, FLT_MAX, 0.0f, text_end_ellipsis, text_end_ellipsis + 1).x; // Ascii blanks are always 1 byte
text_size_clipped_x -= font->CalcTextSizeA(font_size, font_line_height, FLT_MAX, 0.0f, text_end_ellipsis, text_end_ellipsis + 1).x; // Ascii blanks are always 1 byte
}

// Render text, render ellipsis
Expand Down Expand Up @@ -4596,10 +4595,9 @@ ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_tex
ImFont* font = g.Font;
const float font_size = g.FontSize;
const float font_line_height = g.FontLineHeight;
const float font_line_advance = g.FontLineAdvance;
if (text == text_display_end)
return ImVec2(0.0f, font_line_height);
ImVec2 text_size = font->CalcTextSizeA(font_size, font_line_height, font_line_advance, FLT_MAX, wrap_width, text, text_display_end, NULL);
ImVec2 text_size = font->CalcTextSizeA(font_size, font_line_height, FLT_MAX, wrap_width, text, text_display_end, NULL);

// Round
// FIXME: This has been here since Dec 2015 (7b0bf230) but down the line we want this out.
Expand Down Expand Up @@ -6625,7 +6623,6 @@ void ImGui::SetCurrentFont(ImFont* font)
const float font_scale = g.FontBaseScale * (g.CurrentWindow ? g.CurrentWindow->CalcFontScale() : 1.0f);
g.FontSize = g.Font->FontSize * font_scale;
g.FontLineHeight = (g.Font->FontSize + g.Font->ExtraLineHeight) * font_scale;
g.FontLineAdvance = (g.Font->FontSize + g.Font->ExtraLineHeight + g.Font->ExtraLineAdvance) * font_scale;
g.FontBaselineOffset = g.Font->BaselineOffset * font_scale;

ImFontAtlas* atlas = g.Font->ContainerAtlas;
Expand All @@ -6634,7 +6631,6 @@ void ImGui::SetCurrentFont(ImFont* font)
g.DrawListSharedData.Font = g.Font;
g.DrawListSharedData.FontSize = g.FontSize;
g.DrawListSharedData.FontLineHeight = g.FontLineHeight;
g.DrawListSharedData.FontLineAdvance = g.FontLineAdvance;
g.DrawListSharedData.FontBaselineOffset = g.FontBaselineOffset;
}

Expand Down Expand Up @@ -7104,11 +7100,6 @@ float ImGui::GetFontLineHeight()
return GImGui->FontLineHeight;
}

float ImGui::GetFontLineAdvance()
{
return GImGui->FontLineAdvance;
}

float ImGui::GetFontBaselineOffset()
{
return GImGui->FontBaselineOffset;
Expand Down Expand Up @@ -11372,7 +11363,7 @@ void ImGui::DebugRenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP*
window->DrawList->AddRectFilled(thumb_r.Min, thumb_r.Max, GetColorU32(ImGuiCol_WindowBg, alpha_mul));
window->DrawList->AddRectFilled(title_r.Min, title_r.Max, GetColorU32(window_is_focused ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg, alpha_mul));
window->DrawList->AddRect(thumb_r.Min, thumb_r.Max, GetColorU32(ImGuiCol_Border, alpha_mul));
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));
window->DrawList->AddText(g.Font, g.FontSize, g.FontLineHeight, g.FontBaselineOffset, title_r.Min, GetColorU32(ImGuiCol_Text, alpha_mul), thumb_window->Name, FindRenderedTextEnd(thumb_window->Name));
}
draw_list->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_Border, alpha_mul));
}
Expand Down Expand Up @@ -11978,12 +11969,9 @@ void ImGui::DebugNodeFont(ImFont* font)
SetNextItemWidth(GetFontSize() * 8);
DragFloat("Extra line height", &font->ExtraLineHeight, 1.0f, -font->FontSize, FLT_MAX, "%g");
SetNextItemWidth(GetFontSize() * 8);
DragFloat("Extra line advance", &font->ExtraLineAdvance, 1.0f, -FLT_MAX, FLT_MAX, "%g");
SetNextItemWidth(GetFontSize() * 8);
DragFloat("Baseline offset", &font->BaselineOffset, 0.1f, -FLT_MAX, FLT_MAX, "%g");
Text("Ascent: %f, Descent: %f, Height: %f", font->Ascent, font->Descent, font->Ascent - font->Descent);
Text("Line Height: %f", font->FontSize + font->ExtraLineHeight);
Text("Line Advance: %f", font->FontSize + font->ExtraLineHeight + font->ExtraLineAdvance);
char c_str[5];
Text("Fallback character: '%s' (U+%04X)", ImTextCharToUtf8(c_str, font->FallbackChar), font->FallbackChar);
Text("Ellipsis character: '%s' (U+%04X)", ImTextCharToUtf8(c_str, font->EllipsisChar), font->EllipsisChar);
Expand Down
10 changes: 4 additions & 6 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,8 @@ namespace ImGui
// Style read access
// - Use the style editor (ShowStyleEditor() function) to interactively see what the colors are)
IMGUI_API ImFont* GetFont(); // get current font
IMGUI_API float GetFontSize(); // get current font size (= size in pixels) of current font with current scale applied
IMGUI_API float GetFontSize(); // get current font size, mostly used in context where width should be determined (= size in pixels) of current font with current scale applied
IMGUI_API float GetFontLineHeight(); // get current font line height (in pixels) of current font with current scale applied
IMGUI_API float GetFontLineAdvance(); // get current font line advance (in pixels) of current font with current scale applied
IMGUI_API float GetFontBaselineOffset(); // get current font baseline offset (in pixels) of current font with current scale applied
IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a while pixel, useful to draw custom shapes via the ImDrawList API
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
Expand Down Expand Up @@ -2448,7 +2447,7 @@ struct ImDrawList
IMGUI_API void AddNgonFilled(const ImVec2& center, float radius, ImU32 col, int num_segments);
IMGUI_API void AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL);
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);
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);
IMGUI_API void AddText(const ImFont* font, float font_size, float font_line_height, 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);
IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, ImDrawFlags flags, float thickness);
IMGUI_API void AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); // Note: Anti-aliased filling requires points to be in clockwise order.
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)
Expand Down Expand Up @@ -2762,7 +2761,6 @@ struct ImFont
bool DirtyLookupTables; // 1 // out //
float Scale; // 4 // in // = 1.f // Base font scale, multiplied by the per-window font scale which you can adjust with SetWindowFontScale()
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.)
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.)
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.)
float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize]
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)
Expand All @@ -2779,11 +2777,11 @@ struct ImFont

// 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.
// 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.
IMGUI_API ImVec2 CalcTextSizeA(float size, float line_height, float line_advance, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8
IMGUI_API ImVec2 CalcTextSizeA(float size, float line_height, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8
IMGUI_API const char* CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const;
IMGUI_API void RenderChar(ImDrawList* draw_list, float size, float line_height, ImVec2 pos, ImU32 col, ImWchar c) const;
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;
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;
IMGUI_API void RenderText(ImDrawList* draw_list, float size, float line_height, 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;

// [Internal] Don't use!
IMGUI_API void BuildLookupTable();
Expand Down
27 changes: 12 additions & 15 deletions imgui_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1578,13 +1578,12 @@ void ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos,

const float scale = font_size / font->FontSize;
const float line_height = (font->FontSize + font->ExtraLineHeight) * scale;
const float line_advance = (font->FontSize + font->ExtraLineAdvance) * scale;
const float baseline_offset = font->BaselineOffset * scale;

AddText(font, font_size, line_height, line_advance, baseline_offset, pos, col, text_begin, text_end, wrap_width, cpu_fine_clip_rect);
AddText(font, font_size, line_height, baseline_offset, pos, col, text_begin, text_end, wrap_width, cpu_fine_clip_rect);
}

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)
void ImDrawList::AddText(const ImFont* font, float font_size, float font_line_height, 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)
{
if ((col & IM_COL32_A_MASK) == 0)
return;
Expand All @@ -1610,7 +1609,7 @@ void ImDrawList::AddText(const ImFont* font, float font_size, float font_line_he
clip_rect.z = ImMin(clip_rect.z, cpu_fine_clip_rect->z);
clip_rect.w = ImMin(clip_rect.w, cpu_fine_clip_rect->w);
}
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);
font->RenderText(this, font_size, font_line_height, font_baseline_offset, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip_rect != NULL);
}

void ImDrawList::AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end)
Expand Down Expand Up @@ -3117,7 +3116,6 @@ ImFont::ImFont()
{
FontSize = 0.0f;
ExtraLineHeight = 0.0f;
ExtraLineAdvance = 0.0f;
BaselineOffset = 0.0f;
FallbackAdvanceX = 0.0f;
FallbackChar = (ImWchar)-1;
Expand Down Expand Up @@ -3442,7 +3440,7 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c
return s;
}

ImVec2 ImFont::CalcTextSizeA(float size, float line_height, float line_advance, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining) const
ImVec2 ImFont::CalcTextSizeA(float size, float line_height, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining) const
{
if (!text_end)
text_end = text_begin + strlen(text_begin); // FIXME-OPT: Need to avoid this.
Expand Down Expand Up @@ -3472,7 +3470,7 @@ ImVec2 ImFont::CalcTextSizeA(float size, float line_height, float line_advance,
{
if (text_size.x < line_width)
text_size.x = line_width;
text_size.y += line_advance;
text_size.y += line_height;
line_width = 0.0f;
word_wrap_eol = NULL;

Expand Down Expand Up @@ -3505,7 +3503,7 @@ ImVec2 ImFont::CalcTextSizeA(float size, float line_height, float line_advance,
if (c == '\n')
{
text_size.x = ImMax(text_size.x, line_width);
text_size.y += line_advance;
text_size.y += line_height;
line_width = 0.0f;
continue;
}
Expand Down Expand Up @@ -3555,13 +3553,12 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col
{
const float scale = size / FontSize;
const float line_height = (FontSize + ExtraLineHeight) * scale;
const float line_advance = (FontSize + ExtraLineAdvance) * scale;
const float baseline_offset = BaselineOffset * scale;

RenderText(draw_list, size, line_height, line_advance, baseline_offset, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip);
RenderText(draw_list, size, line_height, baseline_offset, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip);
}

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
void ImFont::RenderText(ImDrawList* draw_list, float size, float line_height, 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
{
if (!text_end)
text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls.
Expand All @@ -3585,7 +3582,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, float line_height, fl
{
s = (const char*)memchr(s, '\n', text_end - s);
s = s ? s + 1 : text_end;
y += line_advance;
y += line_height;
}

// For large text, scan for the last visible line in order to avoid over-reserving in the call to PrimReserve()
Expand All @@ -3598,7 +3595,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, float line_height, fl
{
s_end = (const char*)memchr(s_end, '\n', text_end - s_end);
s_end = s_end ? s_end + 1 : text_end;
y_end += line_advance;
y_end += line_height;
}
text_end = s_end;
}
Expand Down Expand Up @@ -3632,7 +3629,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, float line_height, fl
if (s >= word_wrap_eol)
{
x = pos.x;
y += line_advance;
y += line_height;
word_wrap_eol = NULL;

// Wrapping skips upcoming blanks
Expand Down Expand Up @@ -3663,7 +3660,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, float line_height, fl
if (c == '\n')
{
x = pos.x;
y += line_advance;
y += line_height;
if (y > clip_rect.w)
break; // break out of main loop
continue;
Expand Down
Loading

0 comments on commit 818b0ac

Please sign in to comment.