MCPcopy Create free account
hub / github.com/VictorGordan/opengl-tutorials / RenderText

Method RenderText

ImGUI GLFW Tutorial/imgui/imgui_draw.cpp:3510–3699  ·  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

3508
3509// Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound.
3510void 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
3511{
3512 if (!text_end)
3513 text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls.
3514
3515 // Align to be pixel perfect
3516 pos.x = IM_FLOOR(pos.x);
3517 pos.y = IM_FLOOR(pos.y);
3518 float x = pos.x;
3519 float y = pos.y;
3520 if (y > clip_rect.w)
3521 return;
3522
3523 const float scale = size / FontSize;
3524 const float line_height = FontSize * scale;
3525 const bool word_wrap_enabled = (wrap_width > 0.0f);
3526 const char* word_wrap_eol = NULL;
3527
3528 // Fast-forward to first visible line
3529 const char* s = text_begin;
3530 if (y + line_height < clip_rect.y && !word_wrap_enabled)
3531 while (y + line_height < clip_rect.y && s < text_end)
3532 {
3533 s = (const char*)memchr(s, '\n', text_end - s);
3534 s = s ? s + 1 : text_end;
3535 y += line_height;
3536 }
3537
3538 // For large text, scan for the last visible line in order to avoid over-reserving in the call to PrimReserve()
3539 // 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)
3540 if (text_end - s > 10000 && !word_wrap_enabled)
3541 {
3542 const char* s_end = s;
3543 float y_end = y;
3544 while (y_end < clip_rect.w && s_end < text_end)
3545 {
3546 s_end = (const char*)memchr(s_end, '\n', text_end - s_end);
3547 s_end = s_end ? s_end + 1 : text_end;
3548 y_end += line_height;
3549 }
3550 text_end = s_end;
3551 }
3552 if (s == text_end)
3553 return;
3554
3555 // Reserve vertices for remaining worse case (over-reserving is useful and easily amortized)
3556 const int vtx_count_max = (int)(text_end - s) * 4;
3557 const int idx_count_max = (int)(text_end - s) * 6;
3558 const int idx_expected_size = draw_list->IdxBuffer.Size + idx_count_max;
3559 draw_list->PrimReserve(idx_count_max, vtx_count_max);
3560
3561 ImDrawVert* vtx_write = draw_list->_VtxWritePtr;
3562 ImDrawIdx* idx_write = draw_list->_IdxWritePtr;
3563 unsigned int vtx_current_idx = draw_list->_VtxCurrentIdx;
3564
3565 const ImU32 col_untinted = col | ~IM_COL32_A_MASK;
3566
3567 while (s < text_end)

Callers 1

AddTextMethod · 0.45

Calls 3

ImCharIsBlankAFunction · 0.70
ImTextCharFromUtf8Function · 0.70
PrimReserveMethod · 0.45

Tested by

no test coverage detected