MCPcopy Create free account
hub / github.com/RenderKit/embree / CalcWordWrapPositionA

Method CalcWordWrapPositionA

tutorials/common/imgui/imgui_draw.cpp:3348–3445  ·  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

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

Callers

nothing calls this directly

Calls 2

ImTextCharFromUtf8Function · 0.85
ImCharIsBlankWFunction · 0.85

Tested by

no test coverage detected