Internals: extracted ImFont::CalcTextSizeA() into ImFontCalcTextSizeEx() so we can make change to its signature.
(for #3237, #952, #1062, #7363)
diff --git a/imgui_draw.cpp b/imgui_draw.cpp
index 51633c6..a7c5fcb 100644
--- a/imgui_draw.cpp
+++ b/imgui_draw.cpp
@@ -5459,13 +5459,13 @@
return ImFontCalcWordWrapPositionEx(this, size, text, text_end, wrap_width);
}
-ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** out_remaining)
+ImVec2 ImFontCalcTextSizeEx(ImFont* font, float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** out_remaining)
{
if (!text_end)
text_end = text_begin + ImStrlen(text_begin); // FIXME-OPT: Need to avoid this.
const float line_height = size;
- ImFontBaked* baked = GetFontBaked(size);
+ ImFontBaked* baked = font->GetFontBaked(size);
const float scale = size / baked->Size;
ImVec2 text_size = ImVec2(0, 0);
@@ -5477,11 +5477,12 @@
const char* s = text_begin;
while (s < text_end)
{
+ // Word-wrapping
if (word_wrap_enabled)
{
// Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature.
if (!word_wrap_eol)
- word_wrap_eol = ImFontCalcWordWrapPositionEx(this, size, s, text_end, wrap_width - line_width);
+ word_wrap_eol = ImFontCalcWordWrapPositionEx(font, size, s, text_end, wrap_width - line_width);
if (s >= word_wrap_eol)
{
@@ -5503,18 +5504,15 @@
else
s += ImTextCharFromUtf8(&c, s, text_end);
- if (c < 32)
+ if (c == '\n')
{
- if (c == '\n')
- {
- text_size.x = ImMax(text_size.x, line_width);
- text_size.y += line_height;
- line_width = 0.0f;
- continue;
- }
- if (c == '\r')
- continue;
+ text_size.x = ImMax(text_size.x, line_width);
+ text_size.y += line_height;
+ line_width = 0.0f;
+ continue;
}
+ if (c == '\r')
+ continue;
// Optimized inline version of 'float char_width = GetCharAdvance((ImWchar)c);'
float char_width = (c < (unsigned int)baked->IndexAdvanceX.Size) ? baked->IndexAdvanceX.Data[c] : -1.0f;
@@ -5543,6 +5541,11 @@
return text_size;
}
+ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** out_remaining)
+{
+ return ImFontCalcTextSizeEx(this, size, max_width, wrap_width, text_begin, text_end, out_remaining);
+}
+
// Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound.
void ImFont::RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, ImWchar c, const ImVec4* cpu_fine_clip)
{
diff --git a/imgui_internal.h b/imgui_internal.h
index cd4fa46..efb947a 100644
--- a/imgui_internal.h
+++ b/imgui_internal.h
@@ -430,6 +430,7 @@
IMGUI_API int ImTextCountLines(const char* in_text, const char* in_text_end); // return number of lines taken by text. trailing carriage return doesn't count as an extra line.
// Helpers: High-level text functions (DO NOT USE!!! THIS IS A MINIMAL SUBSET OF LARGER UPCOMING CHANGES)
+IMGUI_API ImVec2 ImFontCalcTextSizeEx(ImFont* font, float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** out_remaining);
IMGUI_API const char* ImFontCalcWordWrapPositionEx(ImFont* font, float size, const char* text, const char* text_end, float wrap_width);
IMGUI_API const char* ImTextCalcWordWrapNextLineStart(const char* text, const char* text_end); // trim trailing space and find beginning of next line