| 147 | } |
| 148 | |
| 149 | Vector2f Font::MeasureString(StringView text, float scale, float charSpacing, float lineSpacing) |
| 150 | { |
| 151 | std::size_t textLength = text.size(); |
| 152 | if (textLength == 0 || _charSize.Y <= 0) { |
| 153 | return Vector2f::Zero; |
| 154 | } |
| 155 | |
| 156 | float totalWidth = 0.0f, lastWidth = 0.0f, totalHeight = 0.0f; |
| 157 | float charSpacingPre = charSpacing; |
| 158 | float scalePre = scale; |
| 159 | |
| 160 | std::int32_t idx = 0; |
| 161 | do { |
| 162 | Pair<char32_t, std::size_t> cursor = Utf8::NextChar(text, idx); |
| 163 | |
| 164 | if (cursor.first() == '\n') { |
| 165 | // New line |
| 166 | if (totalWidth < lastWidth) { |
| 167 | totalWidth = lastWidth; |
| 168 | } |
| 169 | lastWidth = 0.0f; |
| 170 | totalHeight += (_charSize.Y * scale * lineSpacing); |
| 171 | } else if (cursor.first() == '\f') { |
| 172 | // Formatting |
| 173 | cursor = Utf8::NextChar(text, cursor.second()); |
| 174 | if (cursor.first() == '[') { |
| 175 | idx = std::int32_t(cursor.second()); |
| 176 | do { |
| 177 | cursor = Utf8::NextChar(text, idx); |
| 178 | if (cursor.first() == ']') { |
| 179 | break; |
| 180 | } |
| 181 | idx = std::int32_t(cursor.second()); |
| 182 | } while (idx < textLength); |
| 183 | } |
| 184 | } else { |
| 185 | Vector2f charSize = MeasureChar(cursor.first()); |
| 186 | if (charSize.X > 0 && charSize.Y > 0) { |
| 187 | lastWidth += (charSize.X + _baseSpacing) * charSpacingPre * scalePre; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | idx = std::int32_t(cursor.second()); |
| 192 | } while (idx < textLength); |
| 193 | |
| 194 | if (totalWidth < lastWidth) { |
| 195 | totalWidth = lastWidth; |
| 196 | } |
| 197 | totalHeight += (_charSize.Y * scale * lineSpacing); |
| 198 | |
| 199 | return Vector2f(ceilf(totalWidth), ceilf(totalHeight)); |
| 200 | } |
| 201 | |
| 202 | Vector2f Font::MeasureStringEx(StringView text, float scale, float charSpacing, float maxWidth, std::int32_t* charFit, float* charFitWidths) |
| 203 | { |
no test coverage detected