Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound.
| 3610 | |
| 3611 | // Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound. |
| 3612 | void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip) const |
| 3613 | { |
| 3614 | if (!text_end) |
| 3615 | text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls. |
| 3616 | |
| 3617 | // Align to be pixel perfect |
| 3618 | float x = IM_FLOOR(pos.x); |
| 3619 | float y = IM_FLOOR(pos.y); |
| 3620 | if (y > clip_rect.w) |
| 3621 | return; |
| 3622 | |
| 3623 | const float start_x = x; |
| 3624 | const float scale = size / FontSize; |
| 3625 | const float line_height = FontSize * scale; |
| 3626 | const bool word_wrap_enabled = (wrap_width > 0.0f); |
| 3627 | |
| 3628 | // Fast-forward to first visible line |
| 3629 | const char* s = text_begin; |
| 3630 | if (y + line_height < clip_rect.y) |
| 3631 | while (y + line_height < clip_rect.y && s < text_end) |
| 3632 | { |
| 3633 | const char* line_end = (const char*)memchr(s, '\n', text_end - s); |
| 3634 | if (word_wrap_enabled) |
| 3635 | { |
| 3636 | // FIXME-OPT: This is not optimal as do first do a search for \n before calling CalcWordWrapPositionA(). |
| 3637 | // If the specs for CalcWordWrapPositionA() were reworked to optionally return on \n we could combine both. |
| 3638 | // However it is still better than nothing performing the fast-forward! |
| 3639 | s = CalcWordWrapPositionA(scale, s, line_end ? line_end : text_end, wrap_width); |
| 3640 | s = CalcWordWrapNextLineStartA(s, text_end); |
| 3641 | } |
| 3642 | else |
| 3643 | { |
| 3644 | s = line_end ? line_end + 1 : text_end; |
| 3645 | } |
| 3646 | y += line_height; |
| 3647 | } |
| 3648 | |
| 3649 | // For large text, scan for the last visible line in order to avoid over-reserving in the call to PrimReserve() |
| 3650 | // 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) |
| 3651 | if (text_end - s > 10000 && !word_wrap_enabled) |
| 3652 | { |
| 3653 | const char* s_end = s; |
| 3654 | float y_end = y; |
| 3655 | while (y_end < clip_rect.w && s_end < text_end) |
| 3656 | { |
| 3657 | s_end = (const char*)memchr(s_end, '\n', text_end - s_end); |
| 3658 | s_end = s_end ? s_end + 1 : text_end; |
| 3659 | y_end += line_height; |
| 3660 | } |
| 3661 | text_end = s_end; |
| 3662 | } |
| 3663 | if (s == text_end) |
| 3664 | return; |
| 3665 | |
| 3666 | // Reserve vertices for remaining worse case (over-reserving is useful and easily amortized) |
| 3667 | const int vtx_count_max = (int)(text_end - s) * 4; |
| 3668 | const int idx_count_max = (int)(text_end - s) * 6; |
| 3669 | const int idx_expected_size = draw_list->IdxBuffer.Size + idx_count_max; |
no test coverage detected