Example usage for java.text BreakIterator getLineInstance

List of usage examples for java.text BreakIterator getLineInstance

Introduction

In this page you can find the example usage for java.text BreakIterator getLineInstance.

Prototype

public static BreakIterator getLineInstance() 

Source Link

Document

Returns a new BreakIterator instance for line breaks for the Locale#getDefault() default locale .

Usage

From source file:com.conversantmedia.mapreduce.tool.RunJob.java

private static void splitLine(List<String> lines, String text, int maxLength) {
    BreakIterator boundary = BreakIterator.getLineInstance();
    boundary.setText(text);/* ww  w .  j  a  v  a2s. c o m*/
    int start = boundary.first();
    int end = boundary.next();
    int lineLength = 0;
    StringBuilder buffer = new StringBuilder();
    while (end != BreakIterator.DONE) {
        String word = text.substring(start, end);
        lineLength = lineLength + word.length();
        if (lineLength > maxLength) {
            lineLength = word.length();
            lines.add(buffer.toString());
            buffer.setLength(0);
        }
        buffer.append(word);
        start = end;
        end = boundary.next();
    }
    lines.add(buffer.toString());
}

From source file:net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.java

protected void startParagraph(String text, int start, boolean truncateAtChar) {
    paragraphText = text;/*  w  w w  .  j  a v  a  2  s. com*/
    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.j  a  v a2 s.  c o  m
    if (logTrace) {
        log.trace("exact line break index measured at " + (paragraphOffset + breakIndex));
    }

    return breakIndex;
}

From source file:com.redhat.rcm.version.Cli.java

private static void printTextLine(final String line, final String indent, final int max, final PrintWriter pw) {
    final String fmt = "%s%-" + max + "s\n";

    final List<String> lines = new ArrayList<String>();

    final BreakIterator iter = BreakIterator.getLineInstance();
    iter.setText(line);/*from  ww w  .j av a2 s .  co  m*/

    int start = iter.first();
    int end = BreakIterator.DONE;
    final StringBuilder currentLine = new StringBuilder();
    String seg;
    while (start != BreakIterator.DONE && (end = iter.next()) != BreakIterator.DONE) {
        seg = line.substring(start, end);
        if (currentLine.length() + seg.length() > max) {
            lines.add(currentLine.toString());
            currentLine.setLength(0);
        }

        currentLine.append(seg);
        start = end;
    }

    if (currentLine.length() > 0) {
        lines.add(currentLine.toString());
    }

    for (final String ln : lines) {
        pw.printf(fmt, indent, ln);
    }
}

From source file:net.sf.jasperreports.engine.fill.TextMeasurer.java

protected void appendTruncateSuffix(TextLineWrapper lineWrapper) {
    String truncateSuffx = getTruncateSuffix();
    if (truncateSuffx == null) {
        return;/*w w w.j a va 2 s . c  o  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);
}

From source file:com.redhat.rcm.version.Cli.java

private static void printKVLine(final String key, final String value, final String fmt, final int valMax,
        final PrintWriter pw) {
    final List<String> lines = new ArrayList<String>();

    final BreakIterator iter = BreakIterator.getLineInstance();
    iter.setText(value);//from w  ww  .  ja v  a2s .  c  om

    int start = iter.first();
    int end = BreakIterator.DONE;
    final StringBuilder currentLine = new StringBuilder();
    String seg;
    while (start != BreakIterator.DONE && (end = iter.next()) != BreakIterator.DONE) {
        seg = value.substring(start, end);
        if (currentLine.length() + seg.length() > valMax) {
            lines.add(currentLine.toString());
            currentLine.setLength(0);
        }

        currentLine.append(seg);
        start = end;
    }

    if (currentLine.length() > 0) {
        lines.add(currentLine.toString());
    }

    pw.printf(fmt, key, lines.isEmpty() ? "" : lines.get(0));
    if (lines.size() > 1) {
        for (int i = 1; i < lines.size(); i++) {
            // blank string to serve for indentation in format with two fields.
            pw.printf(fmt, "", lines.get(i));
        }
    }
}