| 3398 | } |
| 3399 | |
| 3400 | ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining) const |
| 3401 | { |
| 3402 | if (!text_end) |
| 3403 | text_end = text_begin + strlen(text_begin); // FIXME-OPT: Need to avoid this. |
| 3404 | |
| 3405 | const float line_height = size; |
| 3406 | const float scale = size / FontSize; |
| 3407 | |
| 3408 | ImVec2 text_size = ImVec2(0, 0); |
| 3409 | float line_width = 0.0f; |
| 3410 | |
| 3411 | const bool word_wrap_enabled = (wrap_width > 0.0f); |
| 3412 | const char* word_wrap_eol = NULL; |
| 3413 | |
| 3414 | const char* s = text_begin; |
| 3415 | while (s < text_end) |
| 3416 | { |
| 3417 | if (word_wrap_enabled) |
| 3418 | { |
| 3419 | // 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. |
| 3420 | if (!word_wrap_eol) |
| 3421 | { |
| 3422 | word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - line_width); |
| 3423 | if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity. |
| 3424 | word_wrap_eol++; // +1 may not be a character start point in UTF-8 but it's ok because we use s >= word_wrap_eol below |
| 3425 | } |
| 3426 | |
| 3427 | if (s >= word_wrap_eol) |
| 3428 | { |
| 3429 | if (text_size.x < line_width) |
| 3430 | text_size.x = line_width; |
| 3431 | text_size.y += line_height; |
| 3432 | line_width = 0.0f; |
| 3433 | word_wrap_eol = NULL; |
| 3434 | |
| 3435 | // Wrapping skips upcoming blanks |
| 3436 | while (s < text_end) |
| 3437 | { |
| 3438 | const char c = *s; |
| 3439 | if (ImCharIsBlankA(c)) { s++; } else if (c == '\n') { s++; break; } else { break; } |
| 3440 | } |
| 3441 | continue; |
| 3442 | } |
| 3443 | } |
| 3444 | |
| 3445 | // Decode and advance source |
| 3446 | const char* prev_s = s; |
| 3447 | unsigned int c = (unsigned int)*s; |
| 3448 | if (c < 0x80) |
| 3449 | { |
| 3450 | s += 1; |
| 3451 | } |
| 3452 | else |
| 3453 | { |
| 3454 | s += ImTextCharFromUtf8(&c, s, text_end); |
| 3455 | if (c == 0) // Malformed UTF-8? |
| 3456 | break; |
| 3457 | } |
no test coverage detected