line = text.substring (lineStart, wordStart + 1); lineWidth = textWidth (line, fontList); while (wordStart < wordEnd && lineWidth < width) {
while (wordStart < wordEnd) { line = text.substring (lineStart, wordStart + 1); lineWidth = textWidth (line, font); if (lineWidth >= width) break;
String wrapText (String text, int fontList, int width) {	String Lf = "\n";	text = convertToLf (text);	int length = text.length ();	if (width <= 0 || length == 0 || length == 1) return text;	StringBuffer result = new StringBuffer ();	int lineStart = 0, lineEnd = 0;	while (lineStart < length) {		lineEnd = text.indexOf (Lf, lineStart);		boolean noLf = lineEnd == -1;		if (noLf) lineEnd = length;		int nextStart = lineEnd + Lf.length ();		while (lineEnd > lineStart + 1 && Character.isWhitespace (text.charAt (lineEnd - 1))) {			lineEnd--;		}		int wordStart = lineStart, wordEnd = lineStart;		int i = lineStart;		while (i < lineEnd) {			int lastStart = wordStart, lastEnd = wordEnd;			wordStart = i;			while (i < lineEnd && !Character.isWhitespace (text.charAt (i))) {				i++;			}			wordEnd = i - 1;			String line = text.substring (lineStart, wordEnd + 1);			int lineWidth = textWidth (line, fontList);			while (i < lineEnd && Character.isWhitespace (text.charAt (i))) {				i++;			}			if (lineWidth > width) {				if (lastStart == wordStart) {					line = text.substring (lineStart, wordStart + 1);					lineWidth = textWidth (line, fontList);					while (wordStart < wordEnd && lineWidth < width) {						wordStart++;					}					if (wordStart == lastStart) wordStart++;					lastEnd = wordStart - 1;				}				line = text.substring (lineStart, lastEnd + 1);				result.append (line); result.append (Lf);				i = wordStart; lineStart = wordStart; wordEnd = wordStart;			}		}		if (lineStart < lineEnd) {			result.append (text.substring (lineStart, lineEnd));		}		if (!noLf) {			result.append (Lf);		}		lineStart = nextStart;	}	return result.toString ();}