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:net.sf.jasperreports.engine.export.JRHtmlExporter.java

/**
 *
 *///  w w  w .  jav a2 s . c  o m
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.JRPdfExporter.java

protected boolean canUseGlyphRendering(JRPrintText text, JRStyledText styledText) {
    Locale locale = getTextLocale(text);
    AttributedCharacterIterator attributesIterator = styledText.getAttributedString().getIterator();
    int index = 0;
    while (index < styledText.length()) {
        FontKey fontKey = extractFontKey(attributesIterator.getAttributes(), locale);
        if (!fontKey.fontAttribute.hasAttribute()) {
            return false;
        }//from  w ww.  j  ava  2s  . c  o m

        Boolean canUse = glyphRendererFonts.get(fontKey);
        if (canUse == null) {
            canUse = canUseGlyphRendering(fontKey);
            glyphRendererFonts.put(fontKey, canUse);
        }

        if (!canUse) {
            return false;
        }

        index = attributesIterator.getRunLimit();
        attributesIterator.setIndex(index);
    }
    return true;
}

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 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>");
    }
}

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// w ww  .j av a  2s.  co 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("");
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * This routine goes through the attributes and sets the font before calling the actual string drawing routine
 *
 * @param iter// w w  w.ja  va 2s  .com
 */
private void doAttributes(final AttributedCharacterIterator iter) {
    underline = false;
    final Set set = iter.getAttributes().keySet();
    for (Iterator iterator = set.iterator(); iterator.hasNext();) {
        final AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute) iterator
                .next();
        if (!(attribute instanceof TextAttribute)) {
            continue;
        }
        final TextAttribute textattribute = (TextAttribute) attribute;
        if (textattribute.equals(TextAttribute.FONT)) {
            final Font font = (Font) iter.getAttributes().get(textattribute);
            setFont(font);
        } else if (textattribute.equals(TextAttribute.UNDERLINE)) {
            if (iter.getAttributes().get(textattribute) == TextAttribute.UNDERLINE_ON) {
                underline = true;
            }
        } else if (textattribute.equals(TextAttribute.SIZE)) {
            final Object obj = iter.getAttributes().get(textattribute);
            if (obj instanceof Integer) {
                final int i = ((Integer) obj).intValue();
                setFont(getFont().deriveFont(getFont().getStyle(), i));
            } else if (obj instanceof Float) {
                final float f = ((Float) obj).floatValue();
                setFont(getFont().deriveFont(getFont().getStyle(), f));
            }
        } else if (textattribute.equals(TextAttribute.FOREGROUND)) {
            setColor((Color) iter.getAttributes().get(textattribute));
        } else if (textattribute.equals(TextAttribute.FAMILY)) {
            final Font font = getFont();
            final Map fontAttributes = font.getAttributes();
            fontAttributes.put(TextAttribute.FAMILY, iter.getAttributes().get(textattribute));
            setFont(font.deriveFont(fontAttributes));
        } else if (textattribute.equals(TextAttribute.POSTURE)) {
            final Font font = getFont();
            final Map fontAttributes = font.getAttributes();
            fontAttributes.put(TextAttribute.POSTURE, iter.getAttributes().get(textattribute));
            setFont(font.deriveFont(fontAttributes));
        } else if (textattribute.equals(TextAttribute.WEIGHT)) {
            final Font font = getFont();
            final Map fontAttributes = font.getAttributes();
            fontAttributes.put(TextAttribute.WEIGHT, iter.getAttributes().get(textattribute));
            setFont(font.deriveFont(fontAttributes));
        }
    }
}