| 2843 | } |
| 2844 | |
| 2845 | ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining) const |
| 2846 | { |
| 2847 | if (!text_end) |
| 2848 | text_end = text_begin + strlen(text_begin); // FIXME-OPT: Need to avoid this. |
| 2849 | |
| 2850 | const float line_height = size; |
| 2851 | const float scale = size / FontSize; |
| 2852 | |
| 2853 | ImVec2 text_size = ImVec2(0,0); |
| 2854 | float line_width = 0.0f; |
| 2855 | |
| 2856 | const bool word_wrap_enabled = (wrap_width > 0.0f); |
| 2857 | const char* word_wrap_eol = NULL; |
| 2858 | |
| 2859 | const char* s = text_begin; |
| 2860 | while (s < text_end) |
| 2861 | { |
| 2862 | if (word_wrap_enabled) |
| 2863 | { |
| 2864 | // 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. |
| 2865 | if (!word_wrap_eol) |
| 2866 | { |
| 2867 | word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - line_width); |
| 2868 | if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity. |
| 2869 | 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 |
| 2870 | } |
| 2871 | |
| 2872 | if (s >= word_wrap_eol) |
| 2873 | { |
| 2874 | if (text_size.x < line_width) |
| 2875 | text_size.x = line_width; |
| 2876 | text_size.y += line_height; |
| 2877 | line_width = 0.0f; |
| 2878 | word_wrap_eol = NULL; |
| 2879 | |
| 2880 | // Wrapping skips upcoming blanks |
| 2881 | while (s < text_end) |
| 2882 | { |
| 2883 | const char c = *s; |
| 2884 | if (ImCharIsBlankA(c)) { s++; } else if (c == '\n') { s++; break; } else { break; } |
| 2885 | } |
| 2886 | continue; |
| 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | // Decode and advance source |
| 2891 | const char* prev_s = s; |
| 2892 | unsigned int c = (unsigned int)*s; |
| 2893 | if (c < 0x80) |
| 2894 | { |
| 2895 | s += 1; |
| 2896 | } |
| 2897 | else |
| 2898 | { |
| 2899 | s += ImTextCharFromUtf8(&c, s, text_end); |
| 2900 | if (c == 0) // Malformed UTF-8? |
| 2901 | break; |
| 2902 | } |
no test coverage detected