MCPcopy Create free account
hub / github.com/Aegel5/SimpleSwitcher / RenderText

Method RenderText

imgui_tiny_app/imgui/imgui_draw.cpp:5752–5955  ·  view source on GitHub ↗

Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound. DO NOT CALL DIRECTLY THIS WILL CHANGE WILDLY IN 2026. Use ImDrawList::AddText().

Source from the content-addressed store, hash-verified

5750// Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound.
5751// DO NOT CALL DIRECTLY THIS WILL CHANGE WILDLY IN 2026. Use ImDrawList::AddText().
5752void 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, ImDrawTextFlags flags)
5753{
5754 // Align to be pixel perfect
5755begin:
5756 float x = IM_TRUNC(pos.x);
5757 float y = IM_TRUNC(pos.y);
5758 if (y > clip_rect.w)
5759 return;
5760
5761 if (!text_end)
5762 text_end = text_begin + ImStrlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls.
5763
5764 const float line_height = size;
5765 ImFontBaked* baked = GetFontBaked(size);
5766
5767 const float scale = size / baked->Size;
5768 const float origin_x = x;
5769 const bool word_wrap_enabled = (wrap_width > 0.0f);
5770
5771 // Fast-forward to first visible line
5772 const char* s = text_begin;
5773 if (y + line_height < clip_rect.y)
5774 while (y + line_height < clip_rect.y && s < text_end)
5775 {
5776 const char* line_end = (const char*)ImMemchr(s, '\n', text_end - s);
5777 if (word_wrap_enabled)
5778 {
5779 // FIXME-OPT: This is not optimal as do first do a search for \n before calling CalcWordWrapPosition().
5780 // If the specs for CalcWordWrapPosition() were reworked to optionally return on \n we could combine both.
5781 // However it is still better than nothing performing the fast-forward!
5782 s = ImFontCalcWordWrapPositionEx(this, size, s, line_end ? line_end : text_end, wrap_width, flags);
5783 s = ImTextCalcWordWrapNextLineStart(s, text_end, flags);
5784 }
5785 else
5786 {
5787 s = line_end ? line_end + 1 : text_end;
5788 }
5789 y += line_height;
5790 }
5791
5792 // For large text, scan for the last visible line in order to avoid over-reserving in the call to PrimReserve()
5793 // 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)
5794 if (text_end - s > 10000 && !word_wrap_enabled)
5795 {
5796 const char* s_end = s;
5797 float y_end = y;
5798 while (y_end < clip_rect.w && s_end < text_end)
5799 {
5800 s_end = (const char*)ImMemchr(s_end, '\n', text_end - s_end);
5801 s_end = s_end ? s_end + 1 : text_end;
5802 y_end += line_height;
5803 }
5804 text_end = s_end;
5805 }
5806 if (s == text_end)
5807 return;
5808
5809 // Reserve vertices for remaining worse case (over-reserving is useful and easily amortized)

Callers 1

AddTextMethod · 0.45

Calls 7

ImTextCharFromUtf8Function · 0.85
PrimReserveMethod · 0.80
FindGlyphMethod · 0.80
PrimUnreserveMethod · 0.80
AddDrawCmdMethod · 0.80

Tested by

no test coverage detected