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

Method CalcWordWrapPositionA

KBotExt/imgui/imgui_draw.cpp:3420–3516  ·  view source on GitHub ↗

Simple word-wrapping for English, not full-featured. Please submit failing cases! This will return the next location to wrap from. If no wrapping if necessary, this will fast-forward to e.g. text_end. FIXME: Much possible improvements (don't cut things like "word !", "word!!!" but cut within "word,,,,", more sensible support for punctuations, support for Unicode punctuations, etc.)

Source from the content-addressed store, hash-verified

3418// This will return the next location to wrap from. If no wrapping if necessary, this will fast-forward to e.g. text_end.
3419// FIXME: Much possible improvements (don't cut things like "word !", "word!!!" but cut within "word,,,,", more sensible support for punctuations, support for Unicode punctuations, etc.)
3420const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const
3421{
3422 // For references, possible wrap point marked with ^
3423 // "aaa bbb, ccc,ddd. eee fff. ggg!"
3424 // ^ ^ ^ ^ ^__ ^ ^
3425
3426 // List of hardcoded separators: .,;!?'"
3427
3428 // Skip extra blanks after a line returns (that includes not counting them in width computation)
3429 // e.g. "Hello world" --> "Hello" "World"
3430
3431 // Cut words that cannot possibly fit within one line.
3432 // e.g.: "The tropical fish" with ~5 characters worth of width --> "The tr" "opical" "fish"
3433 float line_width = 0.0f;
3434 float word_width = 0.0f;
3435 float blank_width = 0.0f;
3436 wrap_width /= scale; // We work with unscaled widths to avoid scaling every characters
3437
3438 const char* word_end = text;
3439 const char* prev_word_end = NULL;
3440 bool inside_word = true;
3441
3442 const char* s = text;
3443 IM_ASSERT(text_end != NULL);
3444 while (s < text_end)
3445 {
3446 unsigned int c = (unsigned int)*s;
3447 const char* next_s;
3448 if (c < 0x80)
3449 next_s = s + 1;
3450 else
3451 next_s = s + ImTextCharFromUtf8(&c, s, text_end);
3452
3453 if (c < 32)
3454 {
3455 if (c == '\n')
3456 {
3457 line_width = word_width = blank_width = 0.0f;
3458 inside_word = true;
3459 s = next_s;
3460 continue;
3461 }
3462 if (c == '\r')
3463 {
3464 s = next_s;
3465 continue;
3466 }
3467 }
3468
3469 const float char_width = ((int)c < IndexAdvanceX.Size ? IndexAdvanceX.Data[c] : FallbackAdvanceX);
3470 if (ImCharIsBlankW(c))
3471 {
3472 if (inside_word)
3473 {
3474 line_width += blank_width;
3475 blank_width = 0.0f;
3476 word_end = s;
3477 }

Callers

nothing calls this directly

Calls 2

ImTextCharFromUtf8Function · 0.85
ImCharIsBlankWFunction · 0.85

Tested by

no test coverage detected