CalcWordWrapPosition() breaks on \n instead of relying on caller to do it + expose ImTextCalcWordWrapNextLineStart().

Should be functional no-op, fingers crossed. Breaking on \n allows caller to count lines reliably.
This commit is contained in:
ocornut
2025-08-18 14:27:15 +02:00
parent 740c566305
commit deef227ba2
3 changed files with 7 additions and 14 deletions

View File

@@ -5346,7 +5346,7 @@ ImFontBaked* ImFontAtlasBakedGetOrAdd(ImFontAtlas* atlas, ImFont* font, float fo
} }
// Trim trailing space and find beginning of next line // Trim trailing space and find beginning of next line
static inline const char* CalcWordWrapNextLineStartA(const char* text, const char* text_end) const char* ImTextCalcWordWrapNextLineStart(const char* text, const char* text_end)
{ {
while (text < text_end && ImCharIsBlankA(*text)) while (text < text_end && ImCharIsBlankA(*text))
text++; text++;
@@ -5398,12 +5398,7 @@ const char* ImFont::CalcWordWrapPosition(float size, const char* text, const cha
if (c < 32) if (c < 32)
{ {
if (c == '\n') if (c == '\n')
{ return s; // Direct return, skip "Wrap_width is too small to fit anything" path.
line_width = word_width = blank_width = 0.0f;
inside_word = true;
s = next_s;
continue;
}
if (c == '\r') if (c == '\r')
{ {
s = next_s; s = next_s;
@@ -5495,7 +5490,7 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons
text_size.y += line_height; text_size.y += line_height;
line_width = 0.0f; line_width = 0.0f;
word_wrap_eol = NULL; word_wrap_eol = NULL;
s = CalcWordWrapNextLineStartA(s, text_end); // Wrapping skips upcoming blanks s = ImTextCalcWordWrapNextLineStart(s, text_end); // Wrapping skips upcoming blanks
continue; continue;
} }
} }
@@ -5619,7 +5614,7 @@ begin:
// If the specs for CalcWordWrapPosition() were reworked to optionally return on \n we could combine both. // If the specs for CalcWordWrapPosition() were reworked to optionally return on \n we could combine both.
// However it is still better than nothing performing the fast-forward! // However it is still better than nothing performing the fast-forward!
s = CalcWordWrapPosition(size, s, line_end ? line_end : text_end, wrap_width); s = CalcWordWrapPosition(size, s, line_end ? line_end : text_end, wrap_width);
s = CalcWordWrapNextLineStartA(s, text_end); s = ImTextCalcWordWrapNextLineStart(s, text_end);
} }
else else
{ {
@@ -5673,7 +5668,7 @@ begin:
if (y > clip_rect.w) if (y > clip_rect.w)
break; // break out of main loop break; // break out of main loop
word_wrap_eol = NULL; word_wrap_eol = NULL;
s = CalcWordWrapNextLineStartA(s, text_end); // Wrapping skips upcoming blanks s = ImTextCalcWordWrapNextLineStart(s, text_end); // Wrapping skips upcoming blanks
continue; continue;
} }
} }

View File

@@ -439,6 +439,7 @@ IMGUI_API int ImTextCountUtf8BytesFromChar(const char* in_text, const
IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string in UTF-8 IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string in UTF-8
IMGUI_API const char* ImTextFindPreviousUtf8Codepoint(const char* in_text_start, const char* in_text_curr); // return previous UTF-8 code-point. IMGUI_API const char* ImTextFindPreviousUtf8Codepoint(const char* in_text_start, const char* in_text_curr); // return previous UTF-8 code-point.
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. 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.
IMGUI_API const char* ImTextCalcWordWrapNextLineStart(const char* text, const char* text_end); // trim trailing space and find beginning of next line
// Helpers: File System // Helpers: File System
#ifdef IMGUI_DISABLE_FILE_FUNCTIONS #ifdef IMGUI_DISABLE_FILE_FUNCTIONS

View File

@@ -3917,9 +3917,6 @@ bool ImGui::InputDouble(const char* label, double* v, double step, double step_f
// - InputText() // - InputText()
// - InputTextWithHint() // - InputTextWithHint()
// - InputTextMultiline() // - InputTextMultiline()
// - InputTextGetCharInfo() [Internal]
// - InputTextReindexLines() [Internal]
// - InputTextReindexLinesRange() [Internal]
// - InputTextEx() [Internal] // - InputTextEx() [Internal]
// - DebugNodeInputTextState() [Internal] // - DebugNodeInputTextState() [Internal]
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
@@ -4531,7 +4528,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
IM_ASSERT(buf != NULL && buf_size >= 0); IM_ASSERT(buf != NULL && buf_size >= 0);
IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackHistory) && (flags & ImGuiInputTextFlags_Multiline))); // Can't use both together (they both use up/down keys) IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackHistory) && (flags & ImGuiInputTextFlags_Multiline))); // Can't use both together (they both use up/down keys)
IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackCompletion) && (flags & ImGuiInputTextFlags_AllowTabInput))); // Can't use both together (they both use tab key) IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackCompletion) && (flags & ImGuiInputTextFlags_AllowTabInput))); // Can't use both together (they both use tab key)
IM_ASSERT(!((flags & ImGuiInputTextFlags_ElideLeft) && (flags & ImGuiInputTextFlags_Multiline))); // Multiline will not work with left-trimming IM_ASSERT(!((flags & ImGuiInputTextFlags_ElideLeft) && (flags & ImGuiInputTextFlags_Multiline))); // Multiline does not not work with left-trimming
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
ImGuiIO& io = g.IO; ImGuiIO& io = g.IO;