Example usage for java.text AttributedCharacterIterator getAttributes

List of usage examples for java.text AttributedCharacterIterator getAttributes

Introduction

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

Prototype

public Map<Attribute, Object> getAttributes();

Source Link

Document

Returns a map with the attributes defined on the current character.

Usage

From source file:AttributedStringUtilities.java

/**
 * Tests two attributed strings for equality.
 * //from  w ww  .  jav  a  2 s. c om
 * @param s1
 *          string 1 (<code>null</code> permitted).
 * @param s2
 *          string 2 (<code>null</code> permitted).
 * 
 * @return <code>true</code> if <code>s1</code> and <code>s2</code> are
 *         equal or both <code>null</code>, and <code>false</code>
 *         otherwise.
 */
public static boolean equal(AttributedString s1, AttributedString s2) {
    if (s1 == null) {
        return (s2 == null);
    }
    if (s2 == null) {
        return false;
    }
    AttributedCharacterIterator it1 = s1.getIterator();
    AttributedCharacterIterator it2 = s2.getIterator();
    char c1 = it1.first();
    char c2 = it2.first();
    int start = 0;
    while (c1 != CharacterIterator.DONE) {
        int limit1 = it1.getRunLimit();
        int limit2 = it2.getRunLimit();
        if (limit1 != limit2) {
            return false;
        }
        // if maps aren't equivalent, return false
        Map m1 = it1.getAttributes();
        Map m2 = it2.getAttributes();
        if (!m1.equals(m2)) {
            return false;
        }
        // now check characters in the run are the same
        for (int i = start; i < limit1; i++) {
            if (c1 != c2) {
                return false;
            }
            c1 = it1.next();
            c2 = it2.next();
        }
        start = limit1;
    }
    return c2 == CharacterIterator.DONE;
}

From source file:Main.java

/**
 * Serialises an <code>AttributedString</code> object.
 *
 * @param as  the attributed string object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *//* w w  w. ja  va 2  s  .  c o m*/
public static void writeAttributedString(AttributedString as, ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (as != null) {
        stream.writeBoolean(false);
        AttributedCharacterIterator aci = as.getIterator();
        // build a plain string from aci
        // then write the string
        StringBuffer plainStr = new StringBuffer();
        char current = aci.first();
        while (current != CharacterIterator.DONE) {
            plainStr = plainStr.append(current);
            current = aci.next();
        }
        stream.writeObject(plainStr.toString());

        // then write the attributes and limits for each run
        current = aci.first();
        int begin = aci.getBeginIndex();
        while (current != CharacterIterator.DONE) {
            // write the current character - when the reader sees that this
            // is not CharacterIterator.DONE, it will know to read the
            // run limits and attributes
            stream.writeChar(current);

            // now write the limit, adjusted as if beginIndex is zero
            int limit = aci.getRunLimit();
            stream.writeInt(limit - begin);

            // now write the attribute set
            Map atts = new HashMap(aci.getAttributes());
            stream.writeObject(atts);
            current = aci.setIndex(limit);
        }
        // write a character that signals to the reader that all runs
        // are done...
        stream.writeChar(CharacterIterator.DONE);
    } else {
        // write a flag that indicates a null
        stream.writeBoolean(true);
    }

}

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  w  ww .j ava  2  s. c  om
 * 
 * @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:com.siteview.ecc.report.xls.JRXlsExporter.java

protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont,
        Locale locale) {//from  w w w  .  j  av a 2s .com
    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

/**
 *
 *///from w ww .jav a2s  .  co 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.JRXlsxExporter.java

/**
 *
 *//*from  ww w  . j a  va2 s.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:net.sf.jasperreports.engine.export.JRRtfExporter.java

/**
 * Draw a text box// ww w  .j ava  2 s.  com
 * @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.ooxml.JRPptxExporter.java

/**
 *
 *///from  w w  w.j  ava  2 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:net.sf.jasperreports.engine.export.JRXlsExporter.java

protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont,
        Locale locale) {// w w  w  .  j a v a 2 s .c  om
    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:com.dlya.facturews.DlyaPdfExporter2.java

/**
 *
 *//*from w ww  .j av  a  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;
}