java.text.BreakIterator#DONE源码实例Demo

下面列出了java.text.BreakIterator#DONE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: relex   文件: DocSplitterFallbackImpl.java
private void _process(String docText)
{
	lst = new ArrayList<TextInterval>();
	snl = new ArrayList<String>();
	if (docText == null) return;

	bdry.setText(docText);
	start = bdry.first();
	int end = bdry.next();
	while (BreakIterator.DONE != end)
	{
		lst.add(new TextInterval(start, end));
		snl.add(docText.substring(start, end));
		start = end;
		end = bdry.next();
	}
}
 
源代码2 项目: TencentKona-8   文件: BreakIteratorTest.java
private Vector testLastAndPrevious(BreakIterator bi, String text) {
    int p = bi.last();
    int lastP = p;
    Vector<String> result = new Vector<String>();

    if (p != text.length())
        errln("last() returned " + p + " instead of " + text.length());
    while (p != BreakIterator.DONE) {
        p = bi.previous();
        if (p != BreakIterator.DONE) {
            if (p >= lastP)
                errln("previous() failed to move backward: previous() on position "
                                + lastP + " yielded " + p);

            result.insertElementAt(text.substring(p, lastP), 0);
        }
        else {
            if (lastP != 0)
                errln("previous() returned DONE prematurely: offset was "
                                + lastP + " instead of 0");
        }
        lastP = p;
    }
    return result;
}
 
源代码3 项目: openjdk-8-source   文件: TextComponent.java
/**
 * Needed to unify forward and backward searching.
 * The method assumes that s is the text assigned to words.
 */
private int findWordLimit(int index, BreakIterator words, boolean direction,
                                 String s) {
    // Fix for 4256660 and 4256661.
    // Words iterator is different from character and sentence iterators
    // in that end of one word is not necessarily start of another word.
    // Please see java.text.BreakIterator JavaDoc. The code below is
    // based on nextWordStartAfter example from BreakIterator.java.
    int last = (direction == NEXT) ? words.following(index)
                                   : words.preceding(index);
    int current = (direction == NEXT) ? words.next()
                                      : words.previous();
    while (current != BreakIterator.DONE) {
        for (int p = Math.min(last, current); p < Math.max(last, current); p++) {
            if (Character.isLetter(s.charAt(p))) {
                return last;
            }
        }
        last = current;
        current = (direction == NEXT) ? words.next()
                                      : words.previous();
    }
    return BreakIterator.DONE;
}
 
源代码4 项目: HtmlUnit-Android   文件: V8BreakIterator.java
/**
 * Returns the type of the break.
 * @return {@code none}, {@code number}, {@code letter}, {@code kana}, {@code ideo} or {@code unknown}
 */
@JsxFunction
public String breakType() {
    if (!typeAlwaysNone_) {
        final int current = current();
        final int previous = breakIterator_.previous();
        if (previous == BreakIterator.DONE) {
            first();
        }
        else {
            next();
        }
        if (current != BreakIterator.DONE && previous != BreakIterator.DONE) {
            final String token = text_.substring(previous, current);
            if (token.matches(".*[a-zA-Z]+.*")) {
                return "letter";
            }
            if (token.matches("[0-9]+")) {
                return "number";
            }
        }
    }
    return "none";
}
 
源代码5 项目: jdk8u_jdk   文件: BreakIteratorTest.java
private void generalIteratorTest(BreakIterator bi, Vector expectedResult) {
    StringBuffer buffer = new StringBuffer();
    String text;
    for (int i = 0; i < expectedResult.size(); i++) {
        text = (String)expectedResult.elementAt(i);
        buffer.append(text);
    }
    text = buffer.toString();

    bi.setText(text);

    Vector nextResults = testFirstAndNext(bi, text);
    Vector previousResults = testLastAndPrevious(bi, text);

    logln("comparing forward and backward...");
    int errs = getErrorCount();
    compareFragmentLists("forward iteration", "backward iteration", nextResults,
                    previousResults);
    if (getErrorCount() == errs) {
        logln("comparing expected and actual...");
        compareFragmentLists("expected result", "actual result", expectedResult,
                        nextResults);
    }

    int[] boundaries = new int[expectedResult.size() + 3];
    boundaries[0] = BreakIterator.DONE;
    boundaries[1] = 0;
    for (int i = 0; i < expectedResult.size(); i++)
        boundaries[i + 2] = boundaries[i + 1] + ((String)expectedResult.elementAt(i)).
                        length();
    boundaries[boundaries.length - 1] = BreakIterator.DONE;

    testFollowing(bi, text, boundaries);
    testPreceding(bi, text, boundaries);
    testIsBoundary(bi, text, boundaries);

    doMultipleSelectionTest(bi, text);
}
 
源代码6 项目: TencentKona-8   文件: RuleBasedBreakIterator.java
/**
 * Sets the iterator to refer to the first boundary position following
 * the specified position.
 * @offset The position from which to begin searching for a break position.
 * @return The position of the first break after the current position.
 */
@Override
public int following(int offset) {

    CharacterIterator text = getText();
    checkOffset(offset, text);

    // Set our internal iteration position (temporarily)
    // to the position passed in.  If this is the _beginning_ position,
    // then we can just use next() to get our return value
    text.setIndex(offset);
    if (offset == text.getBeginIndex()) {
        cachedLastKnownBreak = handleNext();
        return cachedLastKnownBreak;
    }

    // otherwise, we have to sync up first.  Use handlePrevious() to back
    // us up to a known break position before the specified position (if
    // we can determine that the specified position is a break position,
    // we don't back up at all).  This may or may not be the last break
    // position at or before our starting position.  Advance forward
    // from here until we've passed the starting position.  The position
    // we stop on will be the first break position after the specified one.
    int result = cachedLastKnownBreak;
    if (result >= offset || result <= BreakIterator.DONE) {
        result = handlePrevious();
    } else {
        //it might be better to check if handlePrevious() give us closer
        //safe value but handlePrevious() is slow too
        //So, this has to be done carefully
        text.setIndex(result);
    }
    while (result != BreakIterator.DONE && result <= offset) {
        result = handleNext();
    }
    cachedLastKnownBreak = result;
    return result;
}
 
源代码7 项目: Java8CN   文件: GlyphView.java
/**
 * Determines the minimum span for this view along an axis.
 *
 * <p>This implementation returns the longest non-breakable area within
 * the view as a minimum span for {@code View.X_AXIS}.</p>
 *
 * @param axis  may be either {@code View.X_AXIS} or {@code View.Y_AXIS}
 * @return      the minimum span the view can be rendered into
 * @throws IllegalArgumentException if the {@code axis} parameter is invalid
 * @see         javax.swing.text.View#getMinimumSpan
 */
@Override
public float getMinimumSpan(int axis) {
    switch (axis) {
        case View.X_AXIS:
            if (minimumSpan < 0) {
                minimumSpan = 0;
                int p0 = getStartOffset();
                int p1 = getEndOffset();
                while (p1 > p0) {
                    int breakSpot = getBreakSpot(p0, p1);
                    if (breakSpot == BreakIterator.DONE) {
                        // the rest of the view is non-breakable
                        breakSpot = p0;
                    }
                    minimumSpan = Math.max(minimumSpan,
                            getPartialSpan(breakSpot, p1));
                    // Note: getBreakSpot returns the *last* breakspot
                    p1 = breakSpot - 1;
                }
            }
            return minimumSpan;
        case View.Y_AXIS:
            return super.getMinimumSpan(axis);
        default:
            throw new IllegalArgumentException("Invalid axis: " + axis);
    }
}
 
源代码8 项目: dragonwell8_jdk   文件: BreakIteratorTest.java
private void doMultipleSelectionTest(BreakIterator iterator, String testText)
{
    logln("Multiple selection test...");
    BreakIterator testIterator = (BreakIterator)iterator.clone();
    int offset = iterator.first();
    int testOffset;
    int count = 0;

    do {
        testOffset = testIterator.first();
        testOffset = testIterator.next(count);
        logln("next(" + count + ") -> " + testOffset);
        if (offset != testOffset)
            errln("next(n) and next() not returning consistent results: for step " + count + ", next(n) returned " + testOffset + " and next() had " + offset);

        if (offset != BreakIterator.DONE) {
            count++;
            offset = iterator.next();
        }
    } while (offset != BreakIterator.DONE);

    // now do it backwards...
    offset = iterator.last();
    count = 0;

    do {
        testOffset = testIterator.last();
        testOffset = testIterator.next(count);
        logln("next(" + count + ") -> " + testOffset);
        if (offset != testOffset)
            errln("next(n) and next() not returning consistent results: for step " + count + ", next(n) returned " + testOffset + " and next() had " + offset);

        if (offset != BreakIterator.DONE) {
            count--;
            offset = iterator.previous();
        }
    } while (offset != BreakIterator.DONE);
}
 
源代码9 项目: openjdk-jdk8u   文件: GlyphView.java
/**
 * Determines the minimum span for this view along an axis.
 *
 * <p>This implementation returns the longest non-breakable area within
 * the view as a minimum span for {@code View.X_AXIS}.</p>
 *
 * @param axis  may be either {@code View.X_AXIS} or {@code View.Y_AXIS}
 * @return      the minimum span the view can be rendered into
 * @throws IllegalArgumentException if the {@code axis} parameter is invalid
 * @see         javax.swing.text.View#getMinimumSpan
 */
@Override
public float getMinimumSpan(int axis) {
    switch (axis) {
        case View.X_AXIS:
            if (minimumSpan < 0) {
                minimumSpan = 0;
                int p0 = getStartOffset();
                int p1 = getEndOffset();
                while (p1 > p0) {
                    int breakSpot = getBreakSpot(p0, p1);
                    if (breakSpot == BreakIterator.DONE) {
                        // the rest of the view is non-breakable
                        breakSpot = p0;
                    }
                    minimumSpan = Math.max(minimumSpan,
                            getPartialSpan(breakSpot, p1));
                    // Note: getBreakSpot returns the *last* breakspot
                    p1 = breakSpot - 1;
                }
            }
            return minimumSpan;
        case View.Y_AXIS:
            return super.getMinimumSpan(axis);
        default:
            throw new IllegalArgumentException("Invalid axis: " + axis);
    }
}
 
源代码10 项目: inception   文件: MtasDocumentIndex.java
private String preprocessQuery(String aQuery)
{
    String result;

    if (!(aQuery.contains("\"") || aQuery.contains("[") || aQuery.contains("]")
            || aQuery.contains("{") || aQuery.contains("}") || aQuery.contains("<")
            || aQuery.contains(">"))) {
        // Convert raw words query to a Mtas CQP query

        result = "";
        BreakIterator words = BreakIterator.getWordInstance();
        words.setText(aQuery);

        int start = words.first();
        int end = words.next();
        while (end != BreakIterator.DONE) {
            String word = aQuery.substring(start, end);
            if (!word.trim().isEmpty()) {
                // Add the word to the query
                result += "\"" + word + "\"";
            }
            start = end;
            end = words.next();
            if (end != BreakIterator.DONE) {
                result += " ";
            }
        }
    }
    else {
        result = aQuery;
    }

    return result;
}
 
源代码11 项目: jdk8u-jdk   文件: RuleBasedBreakIterator.java
/**
 * Sets the iterator to refer to the first boundary position following
 * the specified position.
 * @offset The position from which to begin searching for a break position.
 * @return The position of the first break after the current position.
 */
@Override
public int following(int offset) {

    CharacterIterator text = getText();
    checkOffset(offset, text);

    // Set our internal iteration position (temporarily)
    // to the position passed in.  If this is the _beginning_ position,
    // then we can just use next() to get our return value
    text.setIndex(offset);
    if (offset == text.getBeginIndex()) {
        cachedLastKnownBreak = handleNext();
        return cachedLastKnownBreak;
    }

    // otherwise, we have to sync up first.  Use handlePrevious() to back
    // us up to a known break position before the specified position (if
    // we can determine that the specified position is a break position,
    // we don't back up at all).  This may or may not be the last break
    // position at or before our starting position.  Advance forward
    // from here until we've passed the starting position.  The position
    // we stop on will be the first break position after the specified one.
    int result = cachedLastKnownBreak;
    if (result >= offset || result <= BreakIterator.DONE) {
        result = handlePrevious();
    } else {
        //it might be better to check if handlePrevious() give us closer
        //safe value but handlePrevious() is slow too
        //So, this has to be done carefully
        text.setIndex(result);
    }
    while (result != BreakIterator.DONE && result <= offset) {
        result = handleNext();
    }
    cachedLastKnownBreak = result;
    return result;
}
 
源代码12 项目: TencentKona-8   文件: RuleBasedBreakIterator.java
/**
 * Advances the iterator backwards, to the last boundary preceding this one.
 * @return The position of the last boundary position preceding this one.
 */
@Override
public int previous() {
    // if we're already sitting at the beginning of the text, return DONE
    CharacterIterator text = getText();
    if (current() == text.getBeginIndex()) {
        return BreakIterator.DONE;
    }

    // set things up.  handlePrevious() will back us up to some valid
    // break position before the current position (we back our internal
    // iterator up one step to prevent handlePrevious() from returning
    // the current position), but not necessarily the last one before
    // where we started
    int start = current();
    int lastResult = cachedLastKnownBreak;
    if (lastResult >= start || lastResult <= BreakIterator.DONE) {
        getPrevious();
        lastResult = handlePrevious();
    } else {
        //it might be better to check if handlePrevious() give us closer
        //safe value but handlePrevious() is slow too
        //So, this has to be done carefully
        text.setIndex(lastResult);
    }
    int result = lastResult;

    // iterate forward from the known break position until we pass our
    // starting point.  The last break position before the starting
    // point is our return value
    while (result != BreakIterator.DONE && result < start) {
        lastResult = result;
        result = handleNext();
    }

    // set the current iteration position to be the last break position
    // before where we started, and then return that value
    text.setIndex(lastResult);
    cachedLastKnownBreak = lastResult;
    return lastResult;
}
 
源代码13 项目: xyTalk-pc   文件: StringUtils.java
/**
    * Reformats a string where lines that are longer than <tt>width</tt>
    * <p/>
    * are split apart at the earliest wordbreak or at maxLength, whichever is
    * <p/>
    * sooner. If the width specified is less than 5 or greater than the input
    * <p/>
    * Strings length the string will be returned as is.
    * <p/>
    * <p/>
    * <p/>
    * Please note that this method can be lossy - trailing spaces on wrapped
    * <p/>
    * lines may be trimmed.
    * 
    * @param input
    *            the String to reformat.
    * @param width
    *            the maximum length of any one line.
    * @param locale
    *            of the string to be wrapped.
    * @return a new String with reformatted as needed.
    */
   public static String wordWrap(String input, int width, Locale locale) {
// protect ourselves
if (input == null) {
    return "";
} else if (width < 5) {
    return input;
} else if (width >= input.length()) {
    return input;
}

StringBuilder buf = new StringBuilder(input);
boolean endOfLine = false;
int lineStart = 0;

for (int i = 0; i < buf.length(); i++) {
    if (buf.charAt(i) == '\n') {
	lineStart = i + 1;
	endOfLine = true;
    }
    // handle splitting at width character
    if (i > lineStart + width - 1) {
	if (!endOfLine) {
	    int limit = i - lineStart - 1;
	    BreakIterator breaks = BreakIterator
		    .getLineInstance(locale);
	    breaks.setText(buf.substring(lineStart, i));
	    int end = breaks.last();
	    // if the last character in the search string isn't a space,
	    // we can't split on it (looks bad). Search for a previous
	    // break character
	    if (end == limit + 1) {
		if (!Character
			.isWhitespace(buf.charAt(lineStart + end))) {
		    end = breaks.preceding(end - 1);
		}
	    }
	    // if the last character is a space, replace it with a \n
	    if (end != BreakIterator.DONE && end == limit + 1) {
		buf.replace(lineStart + end, lineStart + end + 1, "\n");
		lineStart = lineStart + end;
	    }
	    // otherwise, just insert a \n
	    else if (end != BreakIterator.DONE && 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();
   }
 
源代码14 项目: Java8CN   文件: GlyphView.java
/**
 * Returns a location to break at in the passed in region, or
 * BreakIterator.DONE if there isn't a good location to break at
 * in the specified region.
 */
private int getBreakSpot(int p0, int p1) {
    if (breakSpots == null) {
        // Re-calculate breakpoints for the whole view
        int start = getStartOffset();
        int end = getEndOffset();
        int[] bs = new int[end + 1 - start];
        int ix = 0;

        // Breaker should work on the parent element because there may be
        // a valid breakpoint at the end edge of the view (space, etc.)
        Element parent = getElement().getParentElement();
        int pstart = (parent == null ? start : parent.getStartOffset());
        int pend = (parent == null ? end : parent.getEndOffset());

        Segment s = getText(pstart, pend);
        s.first();
        BreakIterator breaker = getBreaker();
        breaker.setText(s);

        // Backward search should start from end+1 unless there's NO end+1
        int startFrom = end + (pend > end ? 1 : 0);
        for (;;) {
            startFrom = breaker.preceding(s.offset + (startFrom - pstart))
                      + (pstart - s.offset);
            if (startFrom > start) {
                // The break spot is within the view
                bs[ix++] = startFrom;
            } else {
                break;
            }
        }

        SegmentCache.releaseSharedSegment(s);
        breakSpots = new int[ix];
        System.arraycopy(bs, 0, breakSpots, 0, ix);
    }

    int breakSpot = BreakIterator.DONE;
    for (int i = 0; i < breakSpots.length; i++) {
        int bsp = breakSpots[i];
        if (bsp <= p1) {
            if (bsp > p0) {
                breakSpot = bsp;
            }
            break;
        }
    }
    return breakSpot;
}
 
源代码15 项目: openjdk-jdk8u   文件: RuleBasedBreakIterator.java
/**
 * Advances the iterator backwards, to the last boundary preceding this one.
 * @return The position of the last boundary position preceding this one.
 */
@Override
public int previous() {
    // if we're already sitting at the beginning of the text, return DONE
    CharacterIterator text = getText();
    if (current() == text.getBeginIndex()) {
        return BreakIterator.DONE;
    }

    // set things up.  handlePrevious() will back us up to some valid
    // break position before the current position (we back our internal
    // iterator up one step to prevent handlePrevious() from returning
    // the current position), but not necessarily the last one before
    // where we started
    int start = current();
    int lastResult = cachedLastKnownBreak;
    if (lastResult >= start || lastResult <= BreakIterator.DONE) {
        getPrevious();
        lastResult = handlePrevious();
    } else {
        //it might be better to check if handlePrevious() give us closer
        //safe value but handlePrevious() is slow too
        //So, this has to be done carefully
        text.setIndex(lastResult);
    }
    int result = lastResult;

    // iterate forward from the known break position until we pass our
    // starting point.  The last break position before the starting
    // point is our return value
    while (result != BreakIterator.DONE && result < start) {
        lastResult = result;
        result = handleNext();
    }

    // set the current iteration position to be the last break position
    // before where we started, and then return that value
    text.setIndex(lastResult);
    cachedLastKnownBreak = lastResult;
    return lastResult;
}
 
源代码16 项目: hottub   文件: GlyphView.java
/**
 * Returns a location to break at in the passed in region, or
 * BreakIterator.DONE if there isn't a good location to break at
 * in the specified region.
 */
private int getBreakSpot(int p0, int p1) {
    if (breakSpots == null) {
        // Re-calculate breakpoints for the whole view
        int start = getStartOffset();
        int end = getEndOffset();
        int[] bs = new int[end + 1 - start];
        int ix = 0;

        // Breaker should work on the parent element because there may be
        // a valid breakpoint at the end edge of the view (space, etc.)
        Element parent = getElement().getParentElement();
        int pstart = (parent == null ? start : parent.getStartOffset());
        int pend = (parent == null ? end : parent.getEndOffset());

        Segment s = getText(pstart, pend);
        s.first();
        BreakIterator breaker = getBreaker();
        breaker.setText(s);

        // Backward search should start from end+1 unless there's NO end+1
        int startFrom = end + (pend > end ? 1 : 0);
        for (;;) {
            startFrom = breaker.preceding(s.offset + (startFrom - pstart))
                      + (pstart - s.offset);
            if (startFrom > start) {
                // The break spot is within the view
                bs[ix++] = startFrom;
            } else {
                break;
            }
        }

        SegmentCache.releaseSharedSegment(s);
        breakSpots = new int[ix];
        System.arraycopy(bs, 0, breakSpots, 0, ix);
    }

    int breakSpot = BreakIterator.DONE;
    for (int i = 0; i < breakSpots.length; i++) {
        int bsp = breakSpots[i];
        if (bsp <= p1) {
            if (bsp > p0) {
                breakSpot = bsp;
            }
            break;
        }
    }
    return breakSpot;
}
 
源代码17 项目: moa   文件: Utils.java
/**
  * Breaks up the string, if wider than "columns" characters.
  *
  * @param s		the string to process
  * @param columns	the width in columns
  * @return		the processed string
  */
 public static String[] breakUp(String s, int columns) {
   Vector<String>	result;
   String		line;
   BreakIterator	boundary;
   int			boundaryStart;
   int			boundaryEnd;
   String		word;
   String		punctuation;
   int			i;
   String[]		lines;

   result      = new Vector<String>();
   punctuation = " .,;:!?'\"";
   lines       = s.split("\n");

   for (i = 0; i < lines.length; i++) {
     boundary      = BreakIterator.getWordInstance();
     boundary.setText(lines[i]);
     boundaryStart = boundary.first();
     boundaryEnd   = boundary.next();
     line          = "";

     while (boundaryEnd != BreakIterator.DONE) {
word = lines[i].substring(boundaryStart, boundaryEnd);
if (line.length() >= columns) {
  if (word.length() == 1) {
    if (punctuation.indexOf(word.charAt(0)) > -1) {
      line += word;
      word = "";
    }
  }
  result.add(line);
  line = "";
}
line          += word;
boundaryStart  = boundaryEnd;
boundaryEnd    = boundary.next();
     }
     if (line.length() > 0)
result.add(line);
   }

   return result.toArray(new String[result.size()]);
 }
 
源代码18 项目: gcs   文件: PlainText.java
/**
 * Break the paragraph into individual lines.
 * 
 * @param font the font used for rendering the text.
 * @param fontSize the fontSize used for rendering the text.
 * @param width the width of the box holding the content.
 * @return the individual lines.
 * @throws IOException
 */
List<Line> getLines(PDFont font, float fontSize, float width) throws IOException
{
    BreakIterator iterator = BreakIterator.getLineInstance();
    iterator.setText(textContent);
    
    final float scale = fontSize/FONTSCALE;
    
    int start = iterator.first();
    int end = iterator.next();
    float lineWidth = 0;
    
    List<Line> textLines = new ArrayList<Line>();
    Line textLine = new Line();

    while (end != BreakIterator.DONE)
    {
        String word = textContent.substring(start,end);
        float wordWidth = font.getStringWidth(word) * scale;
        
        lineWidth = lineWidth + wordWidth;

        // check if the last word would fit without the whitespace ending it
        if (lineWidth >= width && Character.isWhitespace(word.charAt(word.length()-1)))
        {
            float whitespaceWidth = font.getStringWidth(word.substring(word.length()-1)) * scale;
            lineWidth = lineWidth - whitespaceWidth;
        }
        
        if (lineWidth >= width)
        {
            textLine.setWidth(textLine.calculateWidth(font, fontSize));
            textLines.add(textLine);
            textLine = new Line();
            lineWidth = font.getStringWidth(word) * scale;
        }
        
        AttributedString as = new AttributedString(word);
        as.addAttribute(TextAttribute.WIDTH, wordWidth);
        Word wordInstance = new Word(word);
        wordInstance.setAttributes(as);
        textLine.addWord(wordInstance);
        start = end;
        end = iterator.next();
    }
    textLine.setWidth(textLine.calculateWidth(font, fontSize));
    textLines.add(textLine);
    return textLines;
}
 
源代码19 项目: jdk8u-jdk   文件: RuleBasedBreakIterator.java
/**
 * Advances the iterator backwards, to the last boundary preceding this one.
 * @return The position of the last boundary position preceding this one.
 */
@Override
public int previous() {
    // if we're already sitting at the beginning of the text, return DONE
    CharacterIterator text = getText();
    if (current() == text.getBeginIndex()) {
        return BreakIterator.DONE;
    }

    // set things up.  handlePrevious() will back us up to some valid
    // break position before the current position (we back our internal
    // iterator up one step to prevent handlePrevious() from returning
    // the current position), but not necessarily the last one before
    // where we started
    int start = current();
    int lastResult = cachedLastKnownBreak;
    if (lastResult >= start || lastResult <= BreakIterator.DONE) {
        getPrevious();
        lastResult = handlePrevious();
    } else {
        //it might be better to check if handlePrevious() give us closer
        //safe value but handlePrevious() is slow too
        //So, this has to be done carefully
        text.setIndex(lastResult);
    }
    int result = lastResult;

    // iterate forward from the known break position until we pass our
    // starting point.  The last break position before the starting
    // point is our return value
    while (result != BreakIterator.DONE && result < start) {
        lastResult = result;
        result = handleNext();
    }

    // set the current iteration position to be the last break position
    // before where we started, and then return that value
    text.setIndex(lastResult);
    cachedLastKnownBreak = lastResult;
    return lastResult;
}
 
源代码20 项目: dragonwell8_jdk   文件: GlyphView.java
/**
 * Returns a location to break at in the passed in region, or
 * BreakIterator.DONE if there isn't a good location to break at
 * in the specified region.
 */
private int getBreakSpot(int p0, int p1) {
    if (breakSpots == null) {
        // Re-calculate breakpoints for the whole view
        int start = getStartOffset();
        int end = getEndOffset();
        int[] bs = new int[end + 1 - start];
        int ix = 0;

        // Breaker should work on the parent element because there may be
        // a valid breakpoint at the end edge of the view (space, etc.)
        Element parent = getElement().getParentElement();
        int pstart = (parent == null ? start : parent.getStartOffset());
        int pend = (parent == null ? end : parent.getEndOffset());

        Segment s = getText(pstart, pend);
        s.first();
        BreakIterator breaker = getBreaker();
        breaker.setText(s);

        // Backward search should start from end+1 unless there's NO end+1
        int startFrom = end + (pend > end ? 1 : 0);
        for (;;) {
            startFrom = breaker.preceding(s.offset + (startFrom - pstart))
                      + (pstart - s.offset);
            if (startFrom > start) {
                // The break spot is within the view
                bs[ix++] = startFrom;
            } else {
                break;
            }
        }

        SegmentCache.releaseSharedSegment(s);
        breakSpots = new int[ix];
        System.arraycopy(bs, 0, breakSpots, 0, ix);
    }

    int breakSpot = BreakIterator.DONE;
    for (int i = 0; i < breakSpots.length; i++) {
        int bsp = breakSpots[i];
        if (bsp <= p1) {
            if (bsp > p0) {
                breakSpot = bsp;
            }
            break;
        }
    }
    return breakSpot;
}