Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound.
| 3554 | |
| 3555 | // Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound. |
| 3556 | 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 |
| 3557 | { |
| 3558 | if (!text_end) |
| 3559 | text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls. |
| 3560 | |
| 3561 | // Align to be pixel perfect |
| 3562 | float x = IM_FLOOR(pos.x); |
| 3563 | float y = IM_FLOOR(pos.y); |
| 3564 | if (y > clip_rect.w) |
| 3565 | return; |
| 3566 | |
| 3567 | const float start_x = x; |
| 3568 | const float scale = size / FontSize; |
| 3569 | const float line_height = FontSize * scale; |
| 3570 | const bool word_wrap_enabled = (wrap_width > 0.0f); |
| 3571 | |
| 3572 | // Fast-forward to first visible line |
| 3573 | const char* s = text_begin; |
| 3574 | if (y + line_height < clip_rect.y) |
| 3575 | while (y + line_height < clip_rect.y && s < text_end) |
| 3576 | { |
| 3577 | const char* line_end = (const char*)memchr(s, '\n', text_end - s); |
| 3578 | const char* line_next = line_end ? line_end + 1 : text_end; |
| 3579 | if (word_wrap_enabled) |
| 3580 | { |
| 3581 | // FIXME-OPT: This is not optimal as do first do a search for \n before calling CalcWordWrapPositionA(). |
| 3582 | // If the specs for CalcWordWrapPositionA() were reworked to optionally return on \n we could combine both. |
| 3583 | // However it is still better than nothing performing the fast-forward! |
| 3584 | s = CalcWordWrapPositionA(scale, s, line_next, wrap_width); |
| 3585 | s = CalcWordWrapNextLineStartA(s, text_end); |
| 3586 | } |
| 3587 | else |
| 3588 | { |
| 3589 | s = line_next; |
| 3590 | } |
| 3591 | y += line_height; |
| 3592 | } |
| 3593 | |
| 3594 | // For large text, scan for the last visible line in order to avoid over-reserving in the call to PrimReserve() |
| 3595 | // 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) |
| 3596 | if (text_end - s > 10000 && !word_wrap_enabled) |
| 3597 | { |
| 3598 | const char* s_end = s; |
| 3599 | float y_end = y; |
| 3600 | while (y_end < clip_rect.w && s_end < text_end) |
| 3601 | { |
| 3602 | s_end = (const char*)memchr(s_end, '\n', text_end - s_end); |
| 3603 | s_end = s_end ? s_end + 1 : text_end; |
| 3604 | y_end += line_height; |
| 3605 | } |
| 3606 | text_end = s_end; |
| 3607 | } |
| 3608 | if (s == text_end) |
| 3609 | return; |
| 3610 | |
| 3611 | // Reserve vertices for remaining worse case (over-reserving is useful and easily amortized) |
| 3612 | const int vtx_count_max = (int)(text_end - s) * 4; |
| 3613 | const int idx_count_max = (int)(text_end - s) * 6; |
no test coverage detected