MCPcopy Create free account
hub / github.com/danoon2/Boxedwine / RenderTextEllipsis

Method RenderTextEllipsis

lib/imgui/imgui.cpp:2403–2478  ·  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

2401// 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.
2402// 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.
2403void 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)
2404{
2405 ImGuiContext& g = *GImGui;
2406 if (text_end_full == NULL)
2407 text_end_full = FindRenderedTextEnd(text);
2408 const ImVec2 text_size = text_size_if_known ? *text_size_if_known : CalcTextSize(text, text_end_full, false, 0.0f);
2409
2410 //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));
2411 //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));
2412 //draw_list->AddLine(ImVec2(clip_max_x, pos_min.y), ImVec2(clip_max_x, pos_max.y), IM_COL32(255, 0, 0, 255));
2413 // FIXME: We could technically remove (last_glyph->AdvanceX - last_glyph->X1) from text_size.x here and save a few pixels.
2414 if (text_size.x > pos_max.x - pos_min.x)
2415 {
2416 // Hello wo...
2417 // | | |
2418 // min max ellipsis_max
2419 // <-> this is generally some padding value
2420
2421 const ImFont* font = draw_list->_Data->Font;
2422 const float font_size = draw_list->_Data->FontSize;
2423 const char* text_end_ellipsis = NULL;
2424
2425 ImWchar ellipsis_char = font->EllipsisChar;
2426 int ellipsis_char_count = 1;
2427 if (ellipsis_char == (ImWchar)-1)
2428 {
2429 ellipsis_char = (ImWchar)'.';
2430 ellipsis_char_count = 3;
2431 }
2432 const ImFontGlyph* glyph = font->FindGlyph(ellipsis_char);
2433
2434 float ellipsis_glyph_width = glyph->X1; // Width of the glyph with no padding on either side
2435 float ellipsis_total_width = ellipsis_glyph_width; // Full width of entire ellipsis
2436
2437 if (ellipsis_char_count > 1)
2438 {
2439 // Full ellipsis size without free spacing after it.
2440 const float spacing_between_dots = 1.0f * (draw_list->_Data->FontSize / font->FontSize);
2441 ellipsis_glyph_width = glyph->X1 - glyph->X0 + spacing_between_dots;
2442 ellipsis_total_width = ellipsis_glyph_width * (float)ellipsis_char_count - spacing_between_dots;
2443 }
2444
2445 // We can now claim the space between pos_max.x and ellipsis_max.x
2446 const float text_avail_width = ImMax((ImMax(pos_max.x, ellipsis_max_x) - ellipsis_total_width) - pos_min.x, 1.0f);
2447 float text_size_clipped_x = font->CalcTextSizeA(font_size, text_avail_width, 0.0f, text, text_end_full, &text_end_ellipsis).x;
2448 if (text == text_end_ellipsis && text_end_ellipsis < text_end_full)
2449 {
2450 // Always display at least 1 character if there's no room for character + ellipsis
2451 text_end_ellipsis = text + ImTextCountUtf8BytesFromChar(text, text_end_full);
2452 text_size_clipped_x = font->CalcTextSizeA(font_size, FLT_MAX, 0.0f, text, text_end_ellipsis).x;
2453 }
2454 while (text_end_ellipsis > text && ImCharIsBlankA(text_end_ellipsis[-1]))
2455 {
2456 // 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)
2457 text_end_ellipsis--;
2458 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
2459 }
2460

Callers

nothing calls this directly

Calls 7

ImMaxFunction · 0.85
ImCharIsBlankAFunction · 0.85
ImVec2Class · 0.85
FindGlyphMethod · 0.80
CalcTextSizeAMethod · 0.80
RenderCharMethod · 0.80

Tested by

no test coverage detected