Search in sources :

Example 51 with AttributedCharacterIterator

use of java.text.AttributedCharacterIterator in project poi by apache.

the class DrawTextParagraph method breakText.

/**
     * break text into lines, each representing a line of text that fits in the wrapping width
     *
     * @param graphics The drawing context for computing text-lengths.
     */
protected void breakText(Graphics2D graphics) {
    lines.clear();
    DrawFactory fact = DrawFactory.getInstance(graphics);
    StringBuilder text = new StringBuilder();
    AttributedString at = getAttributedString(graphics, text);
    boolean emptyParagraph = ("".equals(text.toString().trim()));
    AttributedCharacterIterator it = at.getIterator();
    LineBreakMeasurer measurer = new LineBreakMeasurer(it, graphics.getFontRenderContext());
    for (; ; ) {
        int startIndex = measurer.getPosition();
        // add a pixel to compensate rounding errors
        double wrappingWidth = getWrappingWidth(lines.size() == 0, graphics) + 1;
        // shape width can be smaller that the sum of insets (this was proved by a test file)
        if (wrappingWidth < 0) {
            wrappingWidth = 1;
        }
        int nextBreak = text.indexOf("\n", startIndex + 1);
        if (nextBreak == -1) {
            nextBreak = it.getEndIndex();
        }
        TextLayout layout = measurer.nextLayout((float) wrappingWidth, nextBreak, true);
        if (layout == null) {
            // layout can be null if the entire word at the current position
            // does not fit within the wrapping width. Try with requireNextWord=false.
            layout = measurer.nextLayout((float) wrappingWidth, nextBreak, false);
        }
        if (layout == null) {
            // exit if can't break any more
            break;
        }
        int endIndex = measurer.getPosition();
        // skip over new line breaks (we paint 'clear' text runs not starting or ending with \n)
        if (endIndex < it.getEndIndex() && text.charAt(endIndex) == '\n') {
            measurer.setPosition(endIndex + 1);
        }
        TextAlign hAlign = paragraph.getTextAlign();
        if (hAlign == TextAlign.JUSTIFY || hAlign == TextAlign.JUSTIFY_LOW) {
            layout = layout.getJustifiedLayout((float) wrappingWidth);
        }
        AttributedString str = (emptyParagraph) ? // we will not paint empty paragraphs
        null : new AttributedString(it, startIndex, endIndex);
        DrawTextFragment line = fact.getTextFragment(layout, str);
        lines.add(line);
        maxLineHeight = Math.max(maxLineHeight, line.getHeight());
        if (endIndex == it.getEndIndex()) {
            break;
        }
    }
    rawText = text.toString();
}
Also used : AttributedString(java.text.AttributedString) TextAlign(org.apache.poi.sl.usermodel.TextParagraph.TextAlign) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) Paint(java.awt.Paint) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 52 with AttributedCharacterIterator

use of java.text.AttributedCharacterIterator in project chipKIT32-MAX by chipKIT32.

the class InputMethodSupport method inputMethodTextChanged.

/**
   * Handles events from InputMethod.
   * This method judges whether beginning of input or 
   * progress of input or end and call related method.
   * 
   * @param event event from Input Method.
   */
public void inputMethodTextChanged(InputMethodEvent event) {
    AttributedCharacterIterator text = event.getText();
    committed_count = event.getCommittedCharacterCount();
    if (isBeginInputProcess(text, textManager)) {
        textManager.beginCompositionText(text, committed_count);
        caretPositionChanged(event);
        return;
    }
    if (isInputProcess(text)) {
        textManager.processCompositionText(text, committed_count);
        caretPositionChanged(event);
        return;
    }
    textManager.endCompositionText(text, committed_count);
    caretPositionChanged(event);
}
Also used : AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Aggregations

AttributedCharacterIterator (java.text.AttributedCharacterIterator)52 AttributedString (java.text.AttributedString)35 TextLayout (java.awt.font.TextLayout)17 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)15 Point (java.awt.Point)8 Font (java.awt.Font)7 Paint (java.awt.Paint)7 FontRenderContext (java.awt.font.FontRenderContext)7 Graphics2D (java.awt.Graphics2D)6 Color (java.awt.Color)5 ArrayList (java.util.ArrayList)5 WeakHashMap (java.util.WeakHashMap)5 Rectangle (java.awt.Rectangle)4 Attribute (java.text.AttributedCharacterIterator.Attribute)4 DecimalFormat (java.text.DecimalFormat)4 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 HashSet (java.util.HashSet)3 Dimension (java.awt.Dimension)2 Image (java.awt.Image)2