MCPcopy Create free account
hub / github.com/KebsCS/KBotExt / RenderTextEllipsis

Method RenderTextEllipsis

KBotExt/imgui/imgui.cpp:3356–3410  ·  view source on GitHub ↗

Another overly complex function until we reorganize everything into a nice all-in-one helper. This is made more complex because we have dissociated the layout rectangle (pos_min..pos_max) which define _where_ the ellipsis is, from actual clipping of text and limit of the ellipsis display. This is because in the context of tabs we selectively hide part of the text when the Close Button appears, but

Source from the content-addressed store, hash-verified

3354// This is made more complex because we have dissociated the layout rectangle (pos_min..pos_max) which define _where_ the ellipsis is, from actual clipping of text and limit of the ellipsis display.
3355// This is because in the context of tabs we selectively hide part of the text when the Close Button appears, but we don't want the ellipsis to move.
3356void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, float clip_max_x, float ellipsis_max_x, const char* text, const char* text_end_full, const ImVec2* text_size_if_known)
3357{
3358 ImGuiContext& g = *GImGui;
3359 if (text_end_full == NULL)
3360 text_end_full = FindRenderedTextEnd(text);
3361 const ImVec2 text_size = text_size_if_known ? *text_size_if_known : CalcTextSize(text, text_end_full, false, 0.0f);
3362
3363 //draw_list->AddLine(ImVec2(pos_max.x, pos_min.y - 4), ImVec2(pos_max.x, pos_max.y + 4), IM_COL32(0, 0, 255, 255));
3364 //draw_list->AddLine(ImVec2(ellipsis_max_x, pos_min.y-2), ImVec2(ellipsis_max_x, pos_max.y+2), IM_COL32(0, 255, 0, 255));
3365 //draw_list->AddLine(ImVec2(clip_max_x, pos_min.y), ImVec2(clip_max_x, pos_max.y), IM_COL32(255, 0, 0, 255));
3366 // FIXME: We could technically remove (last_glyph->AdvanceX - last_glyph->X1) from text_size.x here and save a few pixels.
3367 if (text_size.x > pos_max.x - pos_min.x)
3368 {
3369 // Hello wo...
3370 // | | |
3371 // min max ellipsis_max
3372 // <-> this is generally some padding value
3373
3374 const ImFont* font = draw_list->_Data->Font;
3375 const float font_size = draw_list->_Data->FontSize;
3376 const float font_scale = font_size / font->FontSize;
3377 const char* text_end_ellipsis = NULL;
3378 const float ellipsis_width = font->EllipsisWidth * font_scale;
3379
3380 // We can now claim the space between pos_max.x and ellipsis_max.x
3381 const float text_avail_width = ImMax((ImMax(pos_max.x, ellipsis_max_x) - ellipsis_width) - pos_min.x, 1.0f);
3382 float text_size_clipped_x = font->CalcTextSizeA(font_size, text_avail_width, 0.0f, text, text_end_full, &text_end_ellipsis).x;
3383 if (text == text_end_ellipsis && text_end_ellipsis < text_end_full)
3384 {
3385 // Always display at least 1 character if there's no room for character + ellipsis
3386 text_end_ellipsis = text + ImTextCountUtf8BytesFromChar(text, text_end_full);
3387 text_size_clipped_x = font->CalcTextSizeA(font_size, FLT_MAX, 0.0f, text, text_end_ellipsis).x;
3388 }
3389 while (text_end_ellipsis > text && ImCharIsBlankA(text_end_ellipsis[-1]))
3390 {
3391 // Trim trailing space before ellipsis (FIXME: Supporting non-ascii blanks would be nice, for this we need a function to backtrack in UTF-8 text)
3392 text_end_ellipsis--;
3393 text_size_clipped_x -= font->CalcTextSizeA(font_size, FLT_MAX, 0.0f, text_end_ellipsis, text_end_ellipsis + 1).x; // Ascii blanks are always 1 byte
3394 }
3395
3396 // Render text, render ellipsis
3397 RenderTextClippedEx(draw_list, pos_min, ImVec2(clip_max_x, pos_max.y), text, text_end_ellipsis, &text_size, ImVec2(0.0f, 0.0f));
3398 ImVec2 ellipsis_pos = ImFloor(ImVec2(pos_min.x + text_size_clipped_x, pos_min.y));
3399 if (ellipsis_pos.x + ellipsis_width <= ellipsis_max_x)
3400 for (int i = 0; i < font->EllipsisCharCount; i++, ellipsis_pos.x += font->EllipsisCharStep * font_scale)
3401 font->RenderChar(draw_list, font_size, ellipsis_pos, GetColorU32(ImGuiCol_Text), font->EllipsisChar);
3402 }
3403 else
3404 {
3405 RenderTextClippedEx(draw_list, pos_min, ImVec2(clip_max_x, pos_max.y), text, text_end_full, &text_size, ImVec2(0.0f, 0.0f));
3406 }
3407
3408 if (g.LogEnabled)
3409 LogRenderedText(&pos_min, text, text_end_full);
3410}
3411
3412// Render a rectangle shaped with optional rounding and borders
3413void ImGui::RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, float rounding)

Callers

nothing calls this directly

Calls 7

ImMaxFunction · 0.85
ImCharIsBlankAFunction · 0.85
ImVec2Function · 0.85
ImFloorFunction · 0.85
CalcTextSizeAMethod · 0.80
RenderCharMethod · 0.80

Tested by

no test coverage detected