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

Method CalcWordWrapPositionA

extern/imgui/imgui_draw.cpp:3357–3454  ·  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

3355// This will return the next location to wrap from. If no wrapping if necessary, this will fast-forward to e.g. text_end.
3356// 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.)
3357const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const
3358{
3359 // For references, possible wrap point marked with ^
3360 // "aaa bbb, ccc,ddd. eee fff. ggg!"
3361 // ^ ^ ^ ^ ^__ ^ ^
3362
3363 // List of hardcoded separators: .,;!?'"
3364
3365 // Skip extra blanks after a line returns (that includes not counting them in width computation)
3366 // e.g. "Hello world" --> "Hello" "World"
3367
3368 // Cut words that cannot possibly fit within one line.
3369 // e.g.: "The tropical fish" with ~5 characters worth of width --> "The tr" "opical" "fish"
3370 float line_width = 0.0f;
3371 float word_width = 0.0f;
3372 float blank_width = 0.0f;
3373 wrap_width /= scale; // We work with unscaled widths to avoid scaling every characters
3374
3375 const char* word_end = text;
3376 const char* prev_word_end = NULL;
3377 bool inside_word = true;
3378
3379 const char* s = text;
3380 while (s < text_end)
3381 {
3382 unsigned int c = (unsigned int)*s;
3383 const char* next_s;
3384 if (c < 0x80)
3385 next_s = s + 1;
3386 else
3387 next_s = s + ImTextCharFromUtf8(&c, s, text_end);
3388 if (c == 0)
3389 break;
3390
3391 if (c < 32)
3392 {
3393 if (c == '\n')
3394 {
3395 line_width = word_width = blank_width = 0.0f;
3396 inside_word = true;
3397 s = next_s;
3398 continue;
3399 }
3400 if (c == '\r')
3401 {
3402 s = next_s;
3403 continue;
3404 }
3405 }
3406
3407 const float char_width = ((int)c < IndexAdvanceX.Size ? IndexAdvanceX.Data[c] : FallbackAdvanceX);
3408 if (ImCharIsBlankW(c))
3409 {
3410 if (inside_word)
3411 {
3412 line_width += blank_width;
3413 blank_width = 0.0f;
3414 word_end = s;

Callers

nothing calls this directly

Calls 2

ImTextCharFromUtf8Function · 0.85
ImCharIsBlankWFunction · 0.85

Tested by

no test coverage detected