Example usage for java.text AttributedCharacterIterator setIndex

List of usage examples for java.text AttributedCharacterIterator setIndex

Introduction

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

Prototype

public char setIndex(int position);

Source Link

Document

Sets the position to the specified position in the text and returns that character.

Usage

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

/**
 * Draw a text box/*  w  w w  . jav  a 2 s . c om*/
 * @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;/*  w w w .j av  a  2  s  . com*/
            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>");
    }
}

From source file:org.apache.fop.svg.ACIUtils.java

/**
 * Dumps the contents of an ACI to System.out. Used for debugging only.
 * @param aci the ACI to dump/*from   w w w.jav  a 2  s .  c  o  m*/
 */
public static void dumpAttrs(AttributedCharacterIterator aci) {
    aci.first();
    Set<Entry<Attribute, Object>> entries = aci.getAttributes().entrySet();
    for (Map.Entry<Attribute, Object> entry : entries) {
        if (entry.getValue() != null) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
    int start = aci.getBeginIndex();
    System.out.print("AttrRuns: ");
    while (aci.current() != CharacterIterator.DONE) {
        int end = aci.getRunLimit();
        System.out.print("" + (end - start) + ", ");
        aci.setIndex(end);
        if (start == end) {
            break;
        }
        start = end;
    }
    System.out.println("");
}