Skip to content

Commit

Permalink
Improve Word Wrapping to Handle Existing Line Breaks
Browse files Browse the repository at this point in the history
Updated the `wordWrap` method to properly process input lines that already contain `<br>` tags. This ensures existing line breaks are respected while fitting words within the maximum character limit per line.
  • Loading branch information
IllianiCBT committed Oct 19, 2024
1 parent 795f2c5 commit e4d092b
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions megamek/src/megamek/client/ui/WrapLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,28 @@ public static String wordWrap(String input) {
* @return The string with line breaks inserted.
*/
public static String wordWrap(String input, int maximumCharacters) {
StringTokenizer token = new StringTokenizer(input, " ");
String[] lines = input.split("<br>");

StringBuilder output = new StringBuilder(input.length());
output.append("<html>");

int lineLen = 0;
// Process each line
for (String line : lines) {
StringTokenizer token = new StringTokenizer(line, " ");

while (token.hasMoreTokens()) {
String word = token.nextToken();
int lineLen = 0;
while (token.hasMoreTokens()) {
String word = token.nextToken();

if (lineLen + word.length() > maximumCharacters) {
output.append("<br>");
lineLen = 0;
if (lineLen + word.length() > maximumCharacters) {
output.append("<br>");
lineLen = 0;
}
output.append(word).append(' ');
lineLen += word.length();
}
output.append(word).append(' ');
lineLen += word.length();
output.append("<br>");
}

output.append("</html>");
return output.toString();
}
Expand Down

0 comments on commit e4d092b

Please sign in to comment.