Example usage for java.awt.font LineBreakMeasurer nextOffset

List of usage examples for java.awt.font LineBreakMeasurer nextOffset

Introduction

In this page you can find the example usage for java.awt.font LineBreakMeasurer nextOffset.

Prototype

public int nextOffset(float wrappingWidth, int offsetLimit, boolean requireNextWord) 

Source Link

Document

Returns the position at the end of the next layout.

Usage

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;//w w  w.  j  a  v a  2s  .  c o m
    if (logTrace) {
        log.trace("exact line break index measured at " + (paragraphOffset + breakIndex));
    }

    return breakIndex;
}