List of usage examples for java.text BreakIterator getCharacterInstance
public static BreakIterator getCharacterInstance()
BreakIterator
instance for character breaks for the Locale#getDefault() default locale . From source file:com.amazon.android.ui.widget.EllipsizedTextView.java
/** * Sets ellipsis properties.//w w w. ja va 2 s . c o m * * @param widthMeasureSpec Ellipsis width. * @param heightMeasureSpec Ellipsis height. * @param layout Layout for ellipsis. * @param lastLine Last line length for ellipsis. * @param maxLines Max lines in ellipsis. */ private void setEllipsis(int widthMeasureSpec, int heightMeasureSpec, Layout layout, int lastLine, int maxLines) { mIsEllipsized = true; setFocusable(true); setClickable(true); final SpannableString ss = new SpannableString(mCharSequence); String visibleText = mCharSequence.toString(); mEllipsisImage = new StateImageSpan(getContext(), mGuillemetDrawableId, ImageSpan.ALIGN_BASELINE); final SpannableStringBuilder spannedText = new SpannableStringBuilder(); int ellipsisIndex = layout.getLineStart(Math.min(lastLine + 1, maxLines)); // Keep chopping words off until the ellipsis is on a visible line or there is only one // line left. do { // Only truncate the last line for long description. if (lastLine >= maxLines) { // Getting the first word break index before the last index of maxline. int safeBreakIndex = breakBefore(visibleText, ellipsisIndex, BreakIterator.getWordInstance()); final int maxLineStart = layout.getLineStart(maxLines - 1); // If this check pass, it means we just truncated a word that is longer than a line. if (safeBreakIndex < maxLineStart) { // Need to check character by character and break in the middle now. Checking // word by word should cover most cases, only do this if a word is longer than // line width. safeBreakIndex = breakBefore(visibleText, ellipsisIndex, BreakIterator.getCharacterInstance()); } ellipsisIndex = safeBreakIndex; } visibleText = visibleText.substring(0, ellipsisIndex); final CharSequence charOutput = ss.subSequence(0, ellipsisIndex); // Re-add ellipsis and convert to image spannedText.replace(0, spannedText.length(), charOutput); spannedText.append(ELLIPSIS); spannedText.setSpan(mEllipsisImage, ellipsisIndex, ellipsisIndex + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE); // Reset text and re-measure. super.setText(spannedText, BufferType.SPANNABLE); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } while (getLineCount() > getMaxLines() && getLineCount() > 1); requestFocus(); }
From source file:net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.java
protected void startParagraph(String text, int start, boolean truncateAtChar) { paragraphText = text;//from w w w .ja v a 2 s .co m paragraphTruncateAtChar = truncateAtChar; char[] textChars = text.toCharArray(); // direction is per paragraph paragraphLeftToRight = isLeftToRight(textChars); paragraphMeasureExact = isParagraphMeasureExact(textChars); if (logTrace) { log.trace("paragraph start at " + start + ", truncate at char " + truncateAtChar + ", LTR " + paragraphLeftToRight + ", exact measure " + paragraphMeasureExact); } paragraphOffset = start; paragraphPosition = 0; paragraphBreakIterator = truncateAtChar ? BreakIterator.getCharacterInstance() : BreakIterator.getLineInstance(); paragraphBreakIterator.setText(paragraphText); }
From source file:net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.java
protected int measureExactLineBreakIndex(float width, int endLimit, boolean requireWord) { //FIXME would it be faster to create and cache a LineBreakMeasurer for the whole paragraph? Map<Attribute, Object> attributes = new HashMap<Attribute, Object>(); // we only need the font as it includes the size and style attributes.put(TextAttribute.FONT, fontInfo.fontInfo.font); String textLine = paragraphText.substring(paragraphPosition, endLimit); AttributedString attributedLine = new AttributedString(textLine, attributes); // we need a fresh iterator for the line BreakIterator breakIterator = paragraphTruncateAtChar ? BreakIterator.getCharacterInstance() : BreakIterator.getLineInstance(); LineBreakMeasurer breakMeasurer = new LineBreakMeasurer(attributedLine.getIterator(), breakIterator, context.getFontRenderContext()); int breakIndex = breakMeasurer.nextOffset(width, endLimit - paragraphPosition, requireWord) + paragraphPosition;//from w w w . ja va 2s .com if (logTrace) { log.trace("exact line break index measured at " + (paragraphOffset + breakIndex)); } return breakIndex; }
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 . jav a 2 s .co m } 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); }