| 152 | } |
| 153 | |
| 154 | std::list<UString> BitmapFont::wordWrapText(const UString &Text, int MaxWidth) |
| 155 | { |
| 156 | int txtwidth; |
| 157 | auto lines = split(Text, "\n"); |
| 158 | std::list<UString> wrappedLines; |
| 159 | |
| 160 | for (const UString &str : lines) |
| 161 | { |
| 162 | txtwidth = getFontWidth(str); |
| 163 | |
| 164 | if (txtwidth > MaxWidth) |
| 165 | { |
| 166 | auto remainingChunksVector = split(str, " "); |
| 167 | auto remainingChunks = |
| 168 | std::list<UString>(remainingChunksVector.begin(), remainingChunksVector.end()); |
| 169 | UString currentLine; |
| 170 | |
| 171 | while (!remainingChunks.empty()) |
| 172 | { |
| 173 | UString currentTestLine; |
| 174 | if (currentLine != "") |
| 175 | currentTestLine = currentLine + " "; |
| 176 | |
| 177 | auto ¤tChunk = remainingChunks.front(); |
| 178 | currentTestLine += currentChunk; |
| 179 | |
| 180 | auto estimatedLength = getFontWidth(currentTestLine); |
| 181 | |
| 182 | if (estimatedLength < MaxWidth) |
| 183 | { |
| 184 | currentLine = currentTestLine; |
| 185 | remainingChunks.pop_front(); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | if (currentLine == "") |
| 190 | { |
| 191 | LogWarning("No break in line \"%s\" found - this will probably overflow " |
| 192 | "the control", |
| 193 | currentTestLine); |
| 194 | currentLine = currentTestLine; |
| 195 | remainingChunks.pop_front(); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | wrappedLines.push_back(currentLine); |
| 200 | currentLine = ""; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | if (currentLine != "") |
| 205 | wrappedLines.push_back(currentLine); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | wrappedLines.push_back(str); |
| 210 | } |
| 211 | } |