public void addWord(String word) { int wordWidth = font.getWidth(word); if (wordWidth > maxWidth) { if (currentLineLength > 0) { endLine(); } for (char c : word.toCharArray()) { int charWidth = font.getWidth(c); if (currentLineLength + charWidth > maxWidth) { endLine(); } lineBuilder.append(c); currentLineLength += charWidth; lineHasWord = true; } } else { if (currentLineLength > 0 && currentLineLength + spaceWidth + wordWidth > maxWidth) { endLine(); } if (lineHasWord) { lineBuilder.append(' '); currentLineLength += spaceWidth; } lineBuilder.append(word); currentLineLength += wordWidth; lineHasWord = true; } }
public TextLineBuilder(Font font, int maxWidth) { this.font = font; this.spaceWidth = font.getWidth(' '); this.maxWidth = maxWidth; }