ImDrawList: Large text passed to AddText() are being scanned for their end in order to avoid pre-reserving too many vertices.

This commit is contained in:
omar
2018-08-21 23:00:40 +02:00
parent 975b5a7310
commit 68448c5faa
4 changed files with 23 additions and 6 deletions

View File

@ -2634,7 +2634,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col
// Fast-forward to first visible line
const char* s = text_begin;
if (!word_wrap_enabled && y + line_height < clip_rect.y)
if (y + line_height < clip_rect.y && !word_wrap_enabled)
while (y + line_height < clip_rect.y)
{
while (s < text_end)
@ -2643,6 +2643,22 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col
y += line_height;
}
// For large text, scan for the last visible line in order to avoid over-reserving in the call to PrimReserve()
// Note that very large horizontal line will still be affected by the issue (e.g. a one megabyte string buffer without a newline will likely crash atm)
if (text_end - s > 10000 && !word_wrap_enabled)
{
const char* s_end = s;
float y_end = y;
while (y_end < clip_rect.w)
{
while (s_end < text_end)
if (*s_end++ == '\n')
break;
y_end += line_height;
}
text_end = s_end;
}
// Reserve vertices for remaining worse case (over-reserving is useful and easily amortized)
const int vtx_count_max = (int)(text_end - s) * 4;
const int idx_count_max = (int)(text_end - s) * 6;