MCPcopy Create free account
hub / github.com/InteractiveComputerGraphics/SPlisHSPlasH / CalcTextSizeA

Method CalcTextSizeA

extern/imgui/imgui_draw.cpp:3456–3538  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3454}
3455
3456ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining) const
3457{
3458 if (!text_end)
3459 text_end = text_begin + strlen(text_begin); // FIXME-OPT: Need to avoid this.
3460
3461 const float line_height = size;
3462 const float scale = size / FontSize;
3463
3464 ImVec2 text_size = ImVec2(0, 0);
3465 float line_width = 0.0f;
3466
3467 const bool word_wrap_enabled = (wrap_width > 0.0f);
3468 const char* word_wrap_eol = NULL;
3469
3470 const char* s = text_begin;
3471 while (s < text_end)
3472 {
3473 if (word_wrap_enabled)
3474 {
3475 // Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature.
3476 if (!word_wrap_eol)
3477 word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - line_width);
3478
3479 if (s >= word_wrap_eol)
3480 {
3481 if (text_size.x < line_width)
3482 text_size.x = line_width;
3483 text_size.y += line_height;
3484 line_width = 0.0f;
3485 word_wrap_eol = NULL;
3486 s = CalcWordWrapNextLineStartA(s, text_end); // Wrapping skips upcoming blanks
3487 continue;
3488 }
3489 }
3490
3491 // Decode and advance source
3492 const char* prev_s = s;
3493 unsigned int c = (unsigned int)*s;
3494 if (c < 0x80)
3495 {
3496 s += 1;
3497 }
3498 else
3499 {
3500 s += ImTextCharFromUtf8(&c, s, text_end);
3501 if (c == 0) // Malformed UTF-8?
3502 break;
3503 }
3504
3505 if (c < 32)
3506 {
3507 if (c == '\n')
3508 {
3509 text_size.x = ImMax(text_size.x, line_width);
3510 text_size.y += line_height;
3511 line_width = 0.0f;
3512 continue;
3513 }

Callers 2

RenderTextEllipsisMethod · 0.80
CalcTextSizeMethod · 0.80

Calls 4

ImVec2Function · 0.85
ImTextCharFromUtf8Function · 0.85
ImMaxFunction · 0.85

Tested by

no test coverage detected