Examples of BreakIterator


Examples of java.text.BreakIterator

      String text) {
        if("".equals(text)) {
          return 0;
        }
        // utility that helps us to break the lines
        final BreakIterator bi = BreakIterator.getLineInstance();
        bi.setText(text);
       
        int lineCount = 0;
        final int lineHeight = fm.getHeight();
       
        // offsets for String.substring(start, end);
        int startOffset = bi.first();
        int endOffset = bi.next();
        // we go over each possible line break that BreakIterator suggests.
        do {
          if(endOffset == text.length()) {
            // we are at the end. this would cause IllegalArgumentException
            // so we just subtract 1
            endOffset--;
          }
          // get the width of the current substring
          // and check if we are over the maximum width
          final String substring = text.substring(startOffset, endOffset);
          final int stringWidth = fm.stringWidth(substring);
          if(stringWidth > maxWidth) {
            // calculate how many lines we have to add.
            // If there is a very long string with no spaces
            // it could be that we have to add more than 1 line.
            int toAdd = (int) (Math.ceil((double) stringWidth / (double) maxWidth) - 1);
            lineCount+= toAdd;
            // we need to advance the startOffset so
            // we can start to search for a new line
            startOffset = bi.preceding(endOffset);
            bi.next();
          }
        } while((endOffset = bi.next()) != BreakIterator.DONE);
        // ensure that the rest of a line also gets a line
        lineCount++;
        return lineHeight * lineCount;
  }
 
View Full Code Here

Examples of java.text.BreakIterator

public class SentenceDetectionTest extends TamingTextTestJ4 {

  @Test
  public void testBreakIterator() {
    //<start id="sentDetect"/>
    BreakIterator sentIterator = BreakIterator.getSentenceInstance(Locale.US);
    String testString = "This is a sentence.  It has fruits, vegetables," +
            " etc. but does not have meat.  Mr. Smith went to Washington.";
    sentIterator.setText(testString);
    int start = sentIterator.first();
    int end = -1;
    List<String> sentences = new ArrayList<String>();
    while ((end = sentIterator.next()) != BreakIterator.DONE) {
      String sentence = testString.substring(start, end);
      start = end;
      sentences.add(sentence);
      System.out.println("Sentence: " + sentence);
    }
View Full Code Here

Examples of java.text.BreakIterator

      locale = country == null ? new Locale(language) : new Locale(language, country);
    }

    // construct BreakIterator
    String type = params.getFieldParam(fieldName, HighlightParams.BS_TYPE, "WORD").toLowerCase();
    BreakIterator bi = null;
    if(type.equals("character")){
      bi = locale == null ? BreakIterator.getCharacterInstance() : BreakIterator.getCharacterInstance(locale);
    }
    else if(type.equals("word")){
      bi = locale == null ? BreakIterator.getWordInstance() : BreakIterator.getWordInstance(locale);
View Full Code Here

Examples of java.text.BreakIterator

    public static TextBlock createTextBlock(final String text, final Font font,
            final Paint paint, final float maxWidth, final int maxLines,
            final TextMeasurer measurer) {

        final TextBlock result = new TextBlock();
        final BreakIterator iterator = BreakIterator.getLineInstance();
        iterator.setText(text);
        int current = 0;
        int lines = 0;
        final int length = text.length();
        while (current < length && lines < maxLines) {
            final int next = nextLineBreak(text, current, maxWidth, iterator,
View Full Code Here

Examples of java.text.BreakIterator

    String[] tokenize(
        final String aText,
        final Locale aLocale)
    {
      List<String> tokens = new ArrayList<String>();
      BreakIterator bi = BreakIterator.getWordInstance(aLocale);
      bi.setText(aText);
      int begin = 0;
      while (bi.next() != BreakIterator.DONE) {
        tokens.add(aText.substring(begin, bi.current()));
        begin = bi.current();
      }
      return tokens.toArray(new String[tokens.size()]);
    }
View Full Code Here

Examples of java.text.BreakIterator

              else
              {
                // Get a BreakIterator.  Note that this code doesn't
                // really work for multi-lingual pages (e.g., an English
                // page that contains some Japanese text).
                BreakIterator breaks = BreakIterator.getLineInstance(
                  context.getViewRoot().getLocale());
                breaks.setText(textString);

                _writeTextWithBreaks(context, breaks, textString, columns);
              }
            } // endif wrapping on
            else
View Full Code Here

Examples of java.text.BreakIterator

    public static TextBlock createTextBlock(final String text, final Font font,
            final Paint paint, final float maxWidth, final int maxLines,
            final TextMeasurer measurer) {
       
        final TextBlock result = new TextBlock();
        final BreakIterator iterator = BreakIterator.getLineInstance();
        iterator.setText(text);
        int current = 0;
        int lines = 0;
        final int length = text.length();
        while (current < length && lines < maxLines) {
            final int next = nextLineBreak(text, current, maxWidth, iterator,
View Full Code Here

Examples of java.text.BreakIterator

    int getBreakLocation(final Segment s, final FontMetrics fm, final int start, final int end,
            final TabExpander t, final int pos) {
        int offset = s.offset;
        int index = Utilities.getTabbedTextOffset(s, fm, start, end, t, pos, false);
        int fullIndex = offset + index;
        BreakIterator bi = BreakIterator.getWordInstance();
        bi.setText(s);
        if (bi.last() <= fullIndex) {
            return bi.last() - offset;
        }
        if (bi.isBoundary(fullIndex)) {
            return Character.isWhitespace(s.array[fullIndex]) ? index + 1 : index;
        }
        int prev = bi.preceding(fullIndex);
        if (prev == bi.first()) {
            return index;
        }
        return prev - offset;
    }
View Full Code Here

Examples of java.text.BreakIterator

        assertEquals(-1, Utilities.getPositionBelow(jta, 1, 0));
    }

    void getWordStartTest(final JTextComponent c) {
        AbstractDocument ad = (AbstractDocument) c.getDocument();
        BreakIterator bi = BreakIterator.getWordInstance();
        int length = ad.getLength();
        try {
            bi.setText(ad.getText(0, ad.getLength()));
        } catch (BadLocationException e) {
        }
        int iteratorWordStart = 0;
        bi.first();
        for (int i = 0; i < length; i++) {
            int utilitiesWordStart = 0;
            if (i < length - 1) {
                iteratorWordStart = bi.preceding(i + 1);
            } else {
                bi.last();
                iteratorWordStart = bi.previous();
            }
            try {
                utilitiesWordStart = Utilities.getWordStart(c, i);
            } catch (BadLocationException e) {
            }
View Full Code Here

Examples of java.text.BreakIterator

        getWordStartTest(jtf);
    }

    void getWordEndTest(final JTextComponent c) {
        AbstractDocument ad = (AbstractDocument) c.getDocument();
        BreakIterator bi = BreakIterator.getWordInstance();
        int length = ad.getLength();
        try {
            bi.setText(ad.getText(0, length));
        } catch (BadLocationException e) {
        }
        bi.first();
        for (int i = 0; i < length; i++) {
            int utilitiesWordEnd = 0;
            int iteratorWordEnd = bi.following(i);
            try {
                utilitiesWordEnd = Utilities.getWordEnd(c, i);
            } catch (BadLocationException e) {
            }
            assertEquals(iteratorWordEnd, utilitiesWordEnd);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.