| 5644 | } |
| 5645 | |
| 5646 | ImVec2 ImFontCalcTextSizeEx(ImFont* font, float size, float max_width, float wrap_width, const char* text_begin, const char* text_end_display, const char* text_end, const char** out_remaining, ImVec2* out_offset, ImDrawTextFlags flags) |
| 5647 | { |
| 5648 | if (!text_end) |
| 5649 | text_end = text_begin + ImStrlen(text_begin); // FIXME-OPT: Need to avoid this. |
| 5650 | if (!text_end_display) |
| 5651 | text_end_display = text_end; |
| 5652 | |
| 5653 | ImFontBaked* baked = font->GetFontBaked(size); |
| 5654 | const float line_height = ImCeil(size * font->LineHeight); |
| 5655 | const float scale = size / baked->Size; |
| 5656 | |
| 5657 | ImVec2 text_size = ImVec2(0, 0); |
| 5658 | float line_width = 0.0f; |
| 5659 | |
| 5660 | const bool word_wrap_enabled = (wrap_width > 0.0f); |
| 5661 | const char* word_wrap_eol = NULL; |
| 5662 | |
| 5663 | const char* s = text_begin; |
| 5664 | while (s < text_end_display) |
| 5665 | { |
| 5666 | // Word-wrapping |
| 5667 | if (word_wrap_enabled) |
| 5668 | { |
| 5669 | // 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. |
| 5670 | if (!word_wrap_eol) |
| 5671 | word_wrap_eol = ImFontCalcWordWrapPositionEx(font, size, s, text_end, wrap_width - line_width, flags); |
| 5672 | |
| 5673 | if (s >= word_wrap_eol) |
| 5674 | { |
| 5675 | if (text_size.x < line_width) |
| 5676 | text_size.x = line_width; |
| 5677 | text_size.y += line_height; |
| 5678 | line_width = 0.0f; |
| 5679 | s = ImTextCalcWordWrapNextLineStart(s, text_end, flags); // Wrapping skips upcoming blanks |
| 5680 | if (flags & ImDrawTextFlags_StopOnNewLine) |
| 5681 | break; |
| 5682 | word_wrap_eol = NULL; |
| 5683 | continue; |
| 5684 | } |
| 5685 | } |
| 5686 | |
| 5687 | // Decode and advance source |
| 5688 | const char* prev_s = s; |
| 5689 | unsigned int c = (unsigned int)*s; |
| 5690 | if (c < 0x80) |
| 5691 | s += 1; |
| 5692 | else |
| 5693 | s += ImTextCharFromUtf8(&c, s, text_end); |
| 5694 | |
| 5695 | if (c == '\n') |
| 5696 | { |
| 5697 | text_size.x = ImMax(text_size.x, line_width); |
| 5698 | text_size.y += line_height; |
| 5699 | line_width = 0.0f; |
| 5700 | if (flags & ImDrawTextFlags_StopOnNewLine) |
| 5701 | break; |
| 5702 | continue; |
| 5703 | } |
no test coverage detected