Example usage for java.text AttributedCharacterIterator getIndex

List of usage examples for java.text AttributedCharacterIterator getIndex

Introduction

In this page you can find the example usage for java.text AttributedCharacterIterator getIndex.

Prototype

public int getIndex();

Source Link

Document

Returns the current index.

Usage

From source file:net.sf.jasperreports.engine.util.JRStyledTextParser.java

/**
 * Outputs a styled text String given a set of element-level styled text
 * attributes and a styled text in the form of a String text and an iterator
 * of style attributes.//from   ww  w  . java2s  . c  o  m
 * 
 * @param parentAttrs the element-level styled text attributes
 * @param iterator iterator of styled text attributes
 * @param text the text
 * @return the String styled text representation
 */
public String write(Map<Attribute, Object> parentAttrs, AttributedCharacterIterator iterator, String text) {
    StringBuilder sb = new StringBuilder();

    int runLimit = 0;

    while (runLimit < iterator.getEndIndex() && (runLimit = iterator.getRunLimit()) <= iterator.getEndIndex()) {
        String chunk = text.substring(iterator.getIndex(), runLimit);
        Map<Attribute, Object> attrs = iterator.getAttributes();

        StringBuilder styleBuilder = writeStyleAttributes(parentAttrs, attrs);
        if (styleBuilder.length() > 0) {
            sb.append(LESS);
            sb.append(NODE_style);
            sb.append(styleBuilder.toString());
            sb.append(GREATER);
            writeChunk(sb, parentAttrs, attrs, chunk);
            sb.append(LESS_SLASH);
            sb.append(NODE_style);
            sb.append(GREATER);
        } else {
            writeChunk(sb, parentAttrs, attrs, chunk);
        }

        iterator.setIndex(runLimit);
    }

    return sb.toString();
}

From source file:net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter.java

/**
 *
 *///from  w  ww.j  a  v  a2s.com
protected void exportStyledText(JRStyle style, JRStyledText styledText, Locale locale, boolean isStyledText) {
    String text = styledText.getText();

    int runLimit = 0;

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        runHelper.export(style, iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit), locale,
                invalidCharReplacement, isStyledText);

        iterator.setIndex(runLimit);
    }
}

From source file:com.siteview.ecc.report.xls.JRXlsExporter.java

protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont,
        Locale locale) {/*from   w ww  .ja va2  s  .c o  m*/
    String text = styledText.getText();
    HSSFRichTextString richTextStr = new HSSFRichTextString(text);
    int runLimit = 0;
    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        Map attributes = iterator.getAttributes();
        JRFont runFont = attributes.isEmpty() ? defaultFont : new JRBaseFont(attributes);
        short runForecolor = attributes.get(TextAttribute.FOREGROUND) != null
                ? getNearestColor((Color) attributes.get(TextAttribute.FOREGROUND)).getIndex()
                : forecolor;
        HSSFFont font = getLoadedFont(runFont, runForecolor, attributes, locale);
        richTextStr.applyFont(iterator.getIndex(), runLimit, font);
        iterator.setIndex(runLimit);
    }
    return richTextStr;
}

From source file:net.sf.jasperreports.engine.export.ooxml.JRDocxExporter.java

/**
 *
 *//*  w w w. j  a va  2 s.c o m*/
protected void exportStyledText(JRStyle style, JRStyledText styledText, Locale locale, boolean hiddenText,
        boolean startedHyperlink, boolean isNewLineJustified) {
    Color elementBackcolor = null;
    Map<AttributedCharacterIterator.Attribute, Object> globalAttributes = styledText.getGlobalAttributes();
    if (globalAttributes != null) {
        elementBackcolor = (Color) styledText.getGlobalAttributes().get(TextAttribute.BACKGROUND);
    }

    String text = styledText.getText();

    int runLimit = 0;

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        Map<Attribute, Object> attributes = iterator.getAttributes();

        boolean localHyperlink = false;

        if (!startedHyperlink) {
            JRPrintHyperlink hyperlink = (JRPrintHyperlink) attributes.get(JRTextAttribute.HYPERLINK);
            if (hyperlink != null) {
                localHyperlink = startHyperlink(hyperlink, true);
            }
        }

        runHelper.export(style, iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit), locale,
                hiddenText, invalidCharReplacement, elementBackcolor, isNewLineJustified);

        if (localHyperlink) {
            endHyperlink(true);
        }

        iterator.setIndex(runLimit);
    }
}

From source file:net.sf.jasperreports.engine.export.ooxml.JRPptxExporter.java

/**
 *
 *///from w w  w . j a  v a2 s . c o  m
protected void exportStyledText(JRStyle style, JRStyledText styledText, Locale locale, String fieldType,
        String uuid) {
    String text = styledText.getText();

    int runLimit = 0;

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        if (fieldType != null) {
            runHelper.export(style, iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit),
                    locale, invalidCharReplacement, fieldType, uuid);
        } else {
            runHelper.export(style, iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit),
                    locale, invalidCharReplacement);

        }

        iterator.setIndex(runLimit);
    }
}

From source file:com.dlya.facturews.DlyaPdfExporter2.java

/**
 *
 *///from   w  w w.  java 2s  .c  o m
protected Phrase getPhrase(AttributedString as, String text, JRPrintText textElement) {
    Phrase phrase = new Phrase();
    int runLimit = 0;

    AttributedCharacterIterator iterator = as.getIterator();
    Locale locale = getTextLocale(textElement);

    boolean firstChunk = true;
    while (runLimit < text.length() && (runLimit = iterator.getRunLimit()) <= text.length()) {
        Map<Attribute, Object> attributes = iterator.getAttributes();
        Chunk chunk = getChunk(attributes, text.substring(iterator.getIndex(), runLimit), locale);

        if (firstChunk) {
            // only set anchor + bookmark for the first chunk in the text
            setAnchor(chunk, textElement, textElement);
        }

        JRPrintHyperlink hyperlink = textElement;
        if (hyperlink.getHyperlinkTypeValue() == HyperlinkTypeEnum.NONE) {
            hyperlink = (JRPrintHyperlink) attributes.get(JRTextAttribute.HYPERLINK);
        }

        setHyperlinkInfo(chunk, hyperlink);
        phrase.add(chunk);

        iterator.setIndex(runLimit);
        firstChunk = false;
    }

    return phrase;
}

From source file:net.sf.jasperreports.engine.export.JRXlsExporter.java

protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont,
        Locale locale) {//from   w ww.ja va  2  s . c  o  m
    String text = styledText.getText();
    HSSFRichTextString richTextStr = new HSSFRichTextString(text);
    int runLimit = 0;
    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        Map<Attribute, Object> attributes = iterator.getAttributes();
        JRFont runFont = attributes.isEmpty() ? defaultFont : new JRBaseFont(attributes);
        short runForecolor = attributes.get(TextAttribute.FOREGROUND) != null
                ? getWorkbookColor((Color) attributes.get(TextAttribute.FOREGROUND)).getIndex()
                : forecolor;
        HSSFFont font = getLoadedFont(runFont, runForecolor, attributes, locale);
        richTextStr.applyFont(iterator.getIndex(), runLimit, font);
        iterator.setIndex(runLimit);
    }
    return richTextStr;
}

From source file:net.sf.jasperreports.engine.export.JRHtmlExporter.java

/**
 *
 *///from   ww w. j  a  v a 2s  .  com
protected void exportStyledText(JRPrintText printText, JRStyledText styledText, String tooltip)
        throws IOException {
    Locale locale = getTextLocale(printText);
    LineSpacingEnum lineSpacing = printText.getParagraph().getLineSpacing();
    Float lineSpacingSize = printText.getParagraph().getLineSpacingSize();
    float lineSpacingFactor = printText.getLineSpacingFactor();
    Color backcolor = printText.getBackcolor();

    String text = styledText.getText();

    int runLimit = 0;

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    boolean first = true;
    boolean startedSpan = false;
    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        //if there are several text runs, write the tooltip into a parent <span>
        if (first && runLimit < styledText.length() && tooltip != null) {
            startedSpan = true;
            writer.write("<span title=\"");
            writer.write(JRStringUtil.xmlEncode(tooltip));
            writer.write("\">");
            //reset the tooltip so that inner <span>s to not use it
            tooltip = null;
        }
        first = false;

        exportStyledTextRun(iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit), tooltip,
                locale, lineSpacing, lineSpacingSize, lineSpacingFactor, backcolor);

        iterator.setIndex(runLimit);
    }

    if (startedSpan) {
        writer.write("</span>");
    }
}

From source file:net.sf.jasperreports.engine.export.JRRtfExporter.java

/**
 * Draw a text box/* ww w. j  av  a 2s .co m*/
 * @param text JasperReports text object (JRPrintText)
 * @throws JRException
 */
public void exportText(JRPrintText text) throws IOException, JRException {

    // use styled text
    JRStyledText styledText = getStyledText(text);
    if (styledText == null) {
        return;
    }

    int width = text.getWidth();
    int height = text.getHeight();

    int textHeight = (int) text.getTextHeight();

    if (textHeight <= 0) {
        if (height <= 0) {
            throw new JRException(EXCEPTION_MESSAGE_KEY_INVALID_TEXT_HEIGHT, (Object[]) null);
        }
        textHeight = height;
    }

    /*   */
    startElement(text);

    // padding for the text
    int topPadding = text.getLineBox().getTopPadding();
    int leftPadding = text.getLineBox().getLeftPadding();
    int bottomPadding = text.getLineBox().getBottomPadding();
    int rightPadding = text.getLineBox().getRightPadding();

    String rotation = null;

    switch (text.getRotationValue()) {
    case LEFT: {
        switch (text.getVerticalTextAlign()) {
        case BOTTOM: {
            leftPadding = Math.max(leftPadding, width - rightPadding - textHeight);
            break;
        }
        case MIDDLE: {
            leftPadding = Math.max(leftPadding, (width - rightPadding - textHeight) / 2);
            break;
        }
        case TOP:
        case JUSTIFIED:
        default: {
        }
        }
        rotation = "{\\sp{\\sn txflTextFlow}{\\sv 2}}";
        break;
    }
    case RIGHT: {
        switch (text.getVerticalTextAlign()) {
        case BOTTOM: {
            rightPadding = Math.max(rightPadding, width - leftPadding - textHeight);
            break;
        }
        case MIDDLE: {
            rightPadding = Math.max(rightPadding, (width - leftPadding - textHeight) / 2);
            break;
        }
        case TOP:
        case JUSTIFIED:
        default: {
        }
        }
        rotation = "{\\sp{\\sn txflTextFlow}{\\sv 3}}";
        break;
    }
    case UPSIDE_DOWN: {
        switch (text.getVerticalTextAlign()) {
        case TOP: {
            topPadding = Math.max(topPadding, height - bottomPadding - textHeight);
            break;
        }
        case MIDDLE: {
            topPadding = Math.max(topPadding, (height - bottomPadding - textHeight) / 2);
            break;
        }
        case BOTTOM:
        case JUSTIFIED:
        default: {
        }
        }
        rotation = "";
        break;
    }
    case NONE:
    default: {
        switch (text.getVerticalTextAlign()) {
        case BOTTOM: {
            topPadding = Math.max(topPadding, height - bottomPadding - textHeight);
            break;
        }
        case MIDDLE: {
            topPadding = Math.max(topPadding, (height - bottomPadding - textHeight) / 2);
            break;
        }
        case TOP:
        case JUSTIFIED:
        default: {
        }
        }
        rotation = "";
    }
    }

    contentWriter.write(rotation);
    contentWriter.write("{\\sp{\\sn dyTextTop}{\\sv ");
    contentWriter.write(String.valueOf(LengthUtil.emu(topPadding)));
    contentWriter.write("}}");
    contentWriter.write("{\\sp{\\sn dxTextLeft}{\\sv ");
    contentWriter.write(String.valueOf(LengthUtil.emu(leftPadding)));
    contentWriter.write("}}");
    contentWriter.write("{\\sp{\\sn dyTextBottom}{\\sv ");
    contentWriter.write(String.valueOf(LengthUtil.emu(bottomPadding)));
    contentWriter.write("}}");
    contentWriter.write("{\\sp{\\sn dxTextRight}{\\sv ");
    contentWriter.write(String.valueOf(LengthUtil.emu(rightPadding)));
    contentWriter.write("}}");
    contentWriter.write("{\\sp{\\sn fLine}{\\sv 0}}");
    contentWriter.write("{\\shptxt{\\pard ");

    contentWriter.write("\\fi" + LengthUtil.twip(text.getParagraph().getFirstLineIndent()) + " ");
    contentWriter.write("\\li" + LengthUtil.twip(text.getParagraph().getLeftIndent()) + " ");
    contentWriter.write("\\ri" + LengthUtil.twip(text.getParagraph().getRightIndent()) + " ");
    contentWriter.write("\\sb" + LengthUtil.twip(text.getParagraph().getSpacingBefore()) + " ");
    contentWriter.write("\\sa" + LengthUtil.twip(text.getParagraph().getSpacingAfter()) + " ");

    TabStop[] tabStops = text.getParagraph().getTabStops();
    if (tabStops != null && tabStops.length > 0) {
        for (int i = 0; i < tabStops.length; i++) {
            TabStop tabStop = tabStops[i];

            String tabStopAlign = "";

            switch (tabStop.getAlignment()) {
            case CENTER:
                tabStopAlign = "\\tqc";
                break;
            case RIGHT:
                tabStopAlign = "\\tqr";
                break;
            case LEFT:
            default:
                tabStopAlign = "";
                break;
            }

            contentWriter.write(tabStopAlign + "\\tx" + LengthUtil.twip(tabStop.getPosition()) + " ");
        }
    }

    //      JRFont font = text;
    if (text.getRunDirectionValue() == RunDirectionEnum.RTL) {
        contentWriter.write("\\rtlch");
    }
    //      writer.write("\\f");
    //      writer.write(String.valueOf(getFontIndex(font)));
    //      writer.write("\\cf");
    //      writer.write(String.valueOf(getColorIndex(text.getForecolor())));
    contentWriter.write("\\cb");
    contentWriter.write(String.valueOf(getColorIndex(text.getBackcolor())));
    contentWriter.write(" ");

    //      if (font.isBold())
    //         writer.write("\\b");
    //      if (font.isItalic())
    //         writer.write("\\i");
    //      if (font.isStrikeThrough())
    //         writer.write("\\strike");
    //      if (font.isUnderline())
    //         writer.write("\\ul");
    //      writer.write("\\fs");
    //      writer.write(String.valueOf(font.getFontSize() * 2));

    switch (text.getHorizontalTextAlign()) {
    case LEFT:
        contentWriter.write("\\ql");
        break;
    case CENTER:
        contentWriter.write("\\qc");
        break;
    case RIGHT:
        contentWriter.write("\\qr");
        break;
    case JUSTIFIED:
        contentWriter.write("\\qj");
        break;
    default:
        contentWriter.write("\\ql");
        break;
    }

    switch (text.getParagraph().getLineSpacing()) {
    case AT_LEAST: {
        contentWriter.write("\\sl" + LengthUtil.twip(text.getParagraph().getLineSpacingSize()));
        contentWriter.write(" \\slmult0 ");
        break;
    }
    case FIXED: {
        contentWriter.write("\\sl-" + LengthUtil.twip(text.getParagraph().getLineSpacingSize()));
        contentWriter.write(" \\slmult0 ");
        break;
    }
    case PROPORTIONAL: {
        contentWriter.write("\\sl" + (int) (text.getParagraph().getLineSpacingSize() * LINE_SPACING_FACTOR));
        contentWriter.write(" \\slmult1 ");
        break;
    }
    case DOUBLE: {
        contentWriter.write("\\sl" + (int) (2f * LINE_SPACING_FACTOR));
        contentWriter.write(" \\slmult1 ");
        break;
    }
    case ONE_AND_HALF: {
        contentWriter.write("\\sl" + (int) (1.5f * LINE_SPACING_FACTOR));
        contentWriter.write(" \\slmult1 ");
        break;
    }
    case SINGLE:
    default: {
        contentWriter.write("\\sl" + (int) (1f * LINE_SPACING_FACTOR));
        contentWriter.write(" \\slmult1 ");
        break;
    }
    }

    if (text.getAnchorName() != null) {
        writeAnchor(text.getAnchorName());
    }

    boolean startedHyperlink = exportHyperlink(text);

    // add parameters in case of styled text element
    String plainText = styledText.getText();
    int runLimit = 0;

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();
    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {

        Map<Attribute, Object> styledTextAttributes = iterator.getAttributes();
        JRFont styleFont = new JRBaseFont(styledTextAttributes);
        Color styleForeground = (Color) styledTextAttributes.get(TextAttribute.FOREGROUND);
        Color styleBackground = (Color) styledTextAttributes.get(TextAttribute.BACKGROUND);

        contentWriter.write("\\f");
        contentWriter.write(String.valueOf(getFontIndex(styleFont, getTextLocale(text))));
        contentWriter.write("\\fs");
        contentWriter.write(String.valueOf((int) (2 * styleFont.getFontsize())));

        if (styleFont.isBold()) {
            contentWriter.write("\\b");
        }
        if (styleFont.isItalic()) {
            contentWriter.write("\\i");
        }
        if (styleFont.isUnderline()) {
            contentWriter.write("\\ul");
        }
        if (styleFont.isStrikeThrough()) {
            contentWriter.write("\\strike");
        }

        if (TextAttribute.SUPERSCRIPT_SUPER.equals(styledTextAttributes.get(TextAttribute.SUPERSCRIPT))) {
            contentWriter.write("\\super");
        } else if (TextAttribute.SUPERSCRIPT_SUB.equals(styledTextAttributes.get(TextAttribute.SUPERSCRIPT))) {
            contentWriter.write("\\sub");
        }

        if (!(null == styleBackground || styleBackground.equals(text.getBackcolor()))) {
            contentWriter.write("\\highlight");
            contentWriter.write(String.valueOf(getColorIndex(styleBackground)));
        }
        contentWriter.write("\\cf");
        contentWriter.write(String.valueOf(getColorIndex(styleForeground)));
        contentWriter.write(" ");

        contentWriter.write(handleUnicodeText(plainText.substring(iterator.getIndex(), runLimit)));

        // reset all styles in the paragraph
        contentWriter.write("\\plain");

        iterator.setIndex(runLimit);
    }

    endHyperlink(startedHyperlink);

    contentWriter.write("\\par}}");

    /*   */
    finishElement();

    exportBox(text.getLineBox(), text.getX() + getOffsetX(), text.getY() + getOffsetY(), width, height);
}

From source file:net.sf.jasperreports.engine.export.HtmlExporter.java

protected void exportStyledText(JRPrintText printText, JRStyledText styledText, String tooltip,
        boolean hyperlinkStarted) throws IOException {
    Locale locale = getTextLocale(printText);
    LineSpacingEnum lineSpacing = printText.getParagraph().getLineSpacing();
    Float lineSpacingSize = printText.getParagraph().getLineSpacingSize();
    float lineSpacingFactor = printText.getLineSpacingFactor();
    Color backcolor = printText.getBackcolor();

    String text = styledText.getText();

    int runLimit = 0;

    addSearchAttributes(styledText, printText);

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    boolean first = true;
    boolean startedSpan = false;

    boolean highlightStarted = false;

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        //if there are several text runs, write the tooltip into a parent <span>
        if (first && runLimit < styledText.length() && tooltip != null) {
            startedSpan = true;/*from w  w w  . j a  va 2  s  . c  o  m*/
            writer.write("<span title=\"");
            writer.write(JRStringUtil.encodeXmlAttribute(tooltip));
            writer.write("\">");
            //reset the tooltip so that inner <span>s to not use it
            tooltip = null;
        }
        first = false;

        Map<Attribute, Object> attributes = iterator.getAttributes();
        Color highlightColor = (Color) attributes.get(JRTextAttribute.SEARCH_HIGHLIGHT);
        if (highlightColor != null && !highlightStarted) {
            highlightStarted = true;
            writer.write("<span class=\"jr_search_result\">");
        } else if (highlightColor == null && highlightStarted) {
            highlightStarted = false;
            writer.write("</span>");
        }

        exportStyledTextRun(attributes, text.substring(iterator.getIndex(), runLimit), tooltip, locale,
                lineSpacing, lineSpacingSize, lineSpacingFactor, backcolor, hyperlinkStarted);

        iterator.setIndex(runLimit);
    }

    if (highlightStarted) {
        writer.write("</span>");
    }

    if (startedSpan) {
        writer.write("</span>");
    }
}