MCPcopy Create free account
hub / github.com/RenderKit/embree / RenderText

Method RenderText

tutorials/common/imgui/imgui_draw.cpp:3547–3736  ·  view source on GitHub ↗

Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound.

Source from the content-addressed store, hash-verified

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

Callers 1

AddTextMethod · 0.45

Calls 3

ImTextCharFromUtf8Function · 0.85
PrimReserveMethod · 0.80

Tested by

no test coverage detected