List of usage examples for java.text BreakIterator preceding
public int preceding(int offset)
From source file:org.jivesoftware.community.util.StringUtils.java
public static String wordWrap(String input, int width, Locale locale) { if (input == null) return ""; if (width < 5) return input; if (width >= input.length()) return input; if (locale == null) locale = JiveGlobals.getLocale(); StringBuffer buf = new StringBuffer(input); boolean endOfLine = false; int lineStart = 0; for (int i = 0; i < buf.length(); i++) { if (buf.charAt(i) == '\n') { lineStart = i + 1;/*from w w w . j a v a 2s. c o m*/ endOfLine = true; } if (i <= (lineStart + width) - 1) continue; if (!endOfLine) { int limit = i - lineStart - 1; BreakIterator breaks = BreakIterator.getLineInstance(locale); breaks.setText(buf.substring(lineStart, i)); int end = breaks.last(); if (end == limit + 1 && !Character.isWhitespace(buf.charAt(lineStart + end))) end = breaks.preceding(end - 1); if (end != -1 && end == limit + 1) { buf.replace(lineStart + end, lineStart + end + 1, "\n"); lineStart += end; continue; } if (end != -1 && end != 0) { buf.insert(lineStart + end, '\n'); lineStart = lineStart + end + 1; } else { buf.insert(i, '\n'); lineStart = i + 1; } } else { buf.insert(i, '\n'); lineStart = i + 1; endOfLine = false; } } return buf.toString(); }
From source file:net.sf.jasperreports.engine.fill.TextMeasurer.java
protected void appendTruncateSuffix(TextLineWrapper lineWrapper) { String truncateSuffx = getTruncateSuffix(); if (truncateSuffx == null) { return;/*from w w w . j a v a 2 s . com*/ } int lineStart = prevMeasuredState.textOffset; //advance from the line start until the next line start or the first newline String lineText = lineWrapper.getLineText(lineStart, measuredState.textOffset); int linePosition = lineText.length(); //iterate to the beginning of the line boolean done = false; do { measuredState = prevMeasuredState.cloneState(); String text = lineText.substring(0, linePosition) + truncateSuffx; boolean truncateAtChar = isToTruncateAtChar(); TextLineWrapper lastLineWrapper = lineWrapper.lastLineWrapper(text, measuredState.textOffset, linePosition, truncateAtChar); BreakIterator breakIterator = truncateAtChar ? BreakIterator.getCharacterInstance() : BreakIterator.getLineInstance(); breakIterator.setText(text); if (renderNextLine(lastLineWrapper, null, new int[] { 0 }, new TabStop[] { null }, new boolean[] { false })) { int lastPos = lastLineWrapper.paragraphPosition(); //test if the entire suffix fit if (lastPos == linePosition + truncateSuffx.length()) { //subtract the suffix from the offset measuredState.textOffset -= truncateSuffx.length(); measuredState.textSuffix = truncateSuffx; done = true; } else { linePosition = breakIterator.preceding(linePosition); if (linePosition == BreakIterator.DONE) { //if the text suffix did not fit the line, only the part of it that fits will show //truncate the suffix String actualSuffix = truncateSuffx.substring(0, measuredState.textOffset - prevMeasuredState.textOffset); //if the last text char is not a new line if (prevMeasuredState.textOffset > 0 && lineWrapper.charAt(prevMeasuredState.textOffset - 1) != '\n') { //force a new line so that the suffix is displayed on the last line actualSuffix = '\n' + actualSuffix; } measuredState.textSuffix = actualSuffix; //restore the next to last line offset measuredState.textOffset = prevMeasuredState.textOffset; done = true; } } } else { //if the line did not fit, leave it empty done = true; } } while (!done); }