Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound.
| 3539 | |
| 3540 | // Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound. |
| 3541 | void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip) const |
| 3542 | { |
| 3543 | if (!text_end) |
| 3544 | text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls. |
| 3545 | |
| 3546 | // Align to be pixel perfect |
| 3547 | pos.x = IM_FLOOR(pos.x); |
| 3548 | pos.y = IM_FLOOR(pos.y); |
| 3549 | float x = pos.x; |
| 3550 | float y = pos.y; |
| 3551 | if (y > clip_rect.w) |
| 3552 | return; |
| 3553 | |
| 3554 | const float scale = size / FontSize; |
| 3555 | const float line_height = FontSize * scale; |
| 3556 | const bool word_wrap_enabled = (wrap_width > 0.0f); |
| 3557 | const char* word_wrap_eol = NULL; |
| 3558 | |
| 3559 | // Fast-forward to first visible line |
| 3560 | const char* s = text_begin; |
| 3561 | if (y + line_height < clip_rect.y && !word_wrap_enabled) |
| 3562 | while (y + line_height < clip_rect.y && s < text_end) |
| 3563 | { |
| 3564 | s = (const char*)memchr(s, '\n', text_end - s); |
| 3565 | s = s ? s + 1 : text_end; |
| 3566 | y += line_height; |
| 3567 | } |
| 3568 | |
| 3569 | // For large text, scan for the last visible line in order to avoid over-reserving in the call to PrimReserve() |
| 3570 | // 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) |
| 3571 | if (text_end - s > 10000 && !word_wrap_enabled) |
| 3572 | { |
| 3573 | const char* s_end = s; |
| 3574 | float y_end = y; |
| 3575 | while (y_end < clip_rect.w && s_end < text_end) |
| 3576 | { |
| 3577 | s_end = (const char*)memchr(s_end, '\n', text_end - s_end); |
| 3578 | s_end = s_end ? s_end + 1 : text_end; |
| 3579 | y_end += line_height; |
| 3580 | } |
| 3581 | text_end = s_end; |
| 3582 | } |
| 3583 | if (s == text_end) |
| 3584 | return; |
| 3585 | |
| 3586 | // Reserve vertices for remaining worse case (over-reserving is useful and easily amortized) |
| 3587 | const int vtx_count_max = (int)(text_end - s) * 4; |
| 3588 | const int idx_count_max = (int)(text_end - s) * 6; |
| 3589 | const int idx_expected_size = draw_list->IdxBuffer.Size + idx_count_max; |
| 3590 | draw_list->PrimReserve(idx_count_max, vtx_count_max); |
| 3591 | |
| 3592 | ImDrawVert* vtx_write = draw_list->_VtxWritePtr; |
| 3593 | ImDrawIdx* idx_write = draw_list->_IdxWritePtr; |
| 3594 | unsigned int vtx_current_idx = draw_list->_VtxCurrentIdx; |
| 3595 | |
| 3596 | const ImU32 col_untinted = col | ~IM_COL32_A_MASK; |
| 3597 | |
| 3598 | while (s < text_end) |
no test coverage detected