Example usage for java.awt.font TextAttribute POSTURE_REGULAR

List of usage examples for java.awt.font TextAttribute POSTURE_REGULAR

Introduction

In this page you can find the example usage for java.awt.font TextAttribute POSTURE_REGULAR.

Prototype

Float POSTURE_REGULAR

To view the source code for java.awt.font TextAttribute POSTURE_REGULAR.

Click Source Link

Document

The standard posture, upright.

Usage

From source file:net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.java

@Override
public boolean start(JRStyledText styledText) {
    if (!measureSimpleTexts) {
        return false;
    }/*from   w w  w.jav  a2  s.co  m*/

    List<Run> runs = styledText.getRuns();
    if (runs.size() != 1) {
        // multiple styles
        return false;
    }

    wholeText = styledText.getText();
    if (wholeText.indexOf('\t') >= 0) {
        // supporting tabs is more difficult because we'd need
        // measureParagraphFragment to include the white space advance.
        return false;
    }

    Run run = styledText.getRuns().get(0);
    if (run.attributes.get(TextAttribute.SUPERSCRIPT) != null) {
        // not handling this case, see JRStyledText.getAwtAttributedString
        return false;
    }

    AwtFontAttribute fontAttribute = AwtFontAttribute.fromAttributes(run.attributes);
    Number size = (Number) run.attributes.get(TextAttribute.SIZE);
    if (!fontAttribute.hasAttribute() || size == null) {
        // this should not happen, but still checking
        return false;
    }

    int style = 0;
    Number posture = (Number) run.attributes.get(TextAttribute.POSTURE);
    if (posture != null && !TextAttribute.POSTURE_REGULAR.equals(posture)) {
        if (TextAttribute.POSTURE_OBLIQUE.equals(posture)) {
            style |= Font.ITALIC;
        } else {
            // non standard posture
            return false;
        }
    }

    Number weight = (Number) run.attributes.get(TextAttribute.WEIGHT);
    if (weight != null && !TextAttribute.WEIGHT_REGULAR.equals(weight)) {
        if (TextAttribute.WEIGHT_BOLD.equals(weight)) {
            style |= Font.BOLD;
        } else {
            // non standard weight
            return false;
        }
    }

    fontKey = new FontKey(fontAttribute, size.floatValue(), style, styledText.getLocale());
    createFontInfo(run.attributes);

    return true;
}

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

@Override
protected Map<Attribute, Object> getAttributes(AttributeSet attrSet) {
    Map<Attribute, Object> attrMap = new HashMap<Attribute, Object>();
    if (attrSet.isDefined(StyleConstants.FontFamily)) {
        attrMap.put(TextAttribute.FAMILY, StyleConstants.getFontFamily(attrSet));
    }/*from w ww .  j  a v a  2s .  com*/

    if (attrSet.isDefined(StyleConstants.Bold)) {
        attrMap.put(TextAttribute.WEIGHT,
                StyleConstants.isBold(attrSet) ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR);
    }

    if (attrSet.isDefined(StyleConstants.Italic)) {
        attrMap.put(TextAttribute.POSTURE, StyleConstants.isItalic(attrSet) ? TextAttribute.POSTURE_OBLIQUE
                : TextAttribute.POSTURE_REGULAR);
    }

    if (attrSet.isDefined(StyleConstants.Underline)) {
        attrMap.put(TextAttribute.UNDERLINE,
                StyleConstants.isUnderline(attrSet) ? TextAttribute.UNDERLINE_ON : null);
    }

    if (attrSet.isDefined(StyleConstants.StrikeThrough)) {
        attrMap.put(TextAttribute.STRIKETHROUGH,
                StyleConstants.isStrikeThrough(attrSet) ? TextAttribute.STRIKETHROUGH_ON : null);
    }

    if (attrSet.isDefined(StyleConstants.FontSize)) {
        attrMap.put(TextAttribute.SIZE, StyleConstants.getFontSize(attrSet));
    }

    if (attrSet.isDefined(StyleConstants.Foreground)) {
        attrMap.put(TextAttribute.FOREGROUND, StyleConstants.getForeground(attrSet));
    }

    if (attrSet.isDefined(StyleConstants.Background)) {
        attrMap.put(TextAttribute.BACKGROUND, StyleConstants.getBackground(attrSet));
    }

    //FIXME: why StyleConstants.isSuperscript(attrSet) does return false
    if (attrSet.isDefined(StyleConstants.Superscript) && !StyleConstants.isSubscript(attrSet)) {
        attrMap.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);
    }

    if (attrSet.isDefined(StyleConstants.Subscript) && StyleConstants.isSubscript(attrSet)) {
        attrMap.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);
    }

    return attrMap;
}

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

/**
 *
 *///from   w  ww.  j av  a  2 s  .  c om
private void parseStyle(JRStyledText styledText, Node parentNode) throws SAXException {
    NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            styledText.append(node.getNodeValue());
        } else if (node.getNodeType() == Node.ELEMENT_NODE && NODE_style.equals(node.getNodeName())) {
            NamedNodeMap nodeAttrs = node.getAttributes();

            Map<Attribute, Object> styleAttrs = new HashMap<Attribute, Object>();

            if (nodeAttrs.getNamedItem(ATTRIBUTE_fontName) != null) {
                styleAttrs.put(TextAttribute.FAMILY, nodeAttrs.getNamedItem(ATTRIBUTE_fontName).getNodeValue());
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_isBold) != null) {
                styleAttrs.put(TextAttribute.WEIGHT,
                        Boolean.valueOf(nodeAttrs.getNamedItem(ATTRIBUTE_isBold).getNodeValue())
                                ? TextAttribute.WEIGHT_BOLD
                                : TextAttribute.WEIGHT_REGULAR);
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_isItalic) != null) {
                styleAttrs.put(TextAttribute.POSTURE,
                        Boolean.valueOf(nodeAttrs.getNamedItem(ATTRIBUTE_isItalic).getNodeValue())
                                ? TextAttribute.POSTURE_OBLIQUE
                                : TextAttribute.POSTURE_REGULAR);
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_isUnderline) != null) {
                styleAttrs.put(TextAttribute.UNDERLINE,
                        Boolean.valueOf(nodeAttrs.getNamedItem(ATTRIBUTE_isUnderline).getNodeValue())
                                ? TextAttribute.UNDERLINE_ON
                                : null);
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_isStrikeThrough) != null) {
                styleAttrs.put(TextAttribute.STRIKETHROUGH,
                        Boolean.valueOf(nodeAttrs.getNamedItem(ATTRIBUTE_isStrikeThrough).getNodeValue())
                                ? TextAttribute.STRIKETHROUGH_ON
                                : null);
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_size) != null) {
                styleAttrs.put(TextAttribute.SIZE,
                        Float.valueOf(nodeAttrs.getNamedItem(ATTRIBUTE_size).getNodeValue()));
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_pdfFontName) != null) {
                styleAttrs.put(JRTextAttribute.PDF_FONT_NAME,
                        nodeAttrs.getNamedItem(ATTRIBUTE_pdfFontName).getNodeValue());
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_pdfEncoding) != null) {
                styleAttrs.put(JRTextAttribute.PDF_ENCODING,
                        nodeAttrs.getNamedItem(ATTRIBUTE_pdfEncoding).getNodeValue());
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_isPdfEmbedded) != null) {
                styleAttrs.put(JRTextAttribute.IS_PDF_EMBEDDED,
                        Boolean.valueOf(nodeAttrs.getNamedItem(ATTRIBUTE_isPdfEmbedded).getNodeValue()));
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_forecolor) != null) {
                Color color = JRColorUtil.getColor(nodeAttrs.getNamedItem(ATTRIBUTE_forecolor).getNodeValue(),
                        Color.black);
                styleAttrs.put(TextAttribute.FOREGROUND, color);
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_backcolor) != null) {
                Color color = JRColorUtil.getColor(nodeAttrs.getNamedItem(ATTRIBUTE_backcolor).getNodeValue(),
                        Color.black);
                styleAttrs.put(TextAttribute.BACKGROUND, color);
            }

            int startIndex = styledText.length();

            parseStyle(styledText, node);

            styledText.addRun(new JRStyledText.Run(styleAttrs, startIndex, styledText.length()));
        } else if (node.getNodeType() == Node.ELEMENT_NODE && NODE_bold.equalsIgnoreCase(node.getNodeName())) {
            Map<Attribute, Object> styleAttrs = new HashMap<Attribute, Object>();
            styleAttrs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);

            int startIndex = styledText.length();

            parseStyle(styledText, node);

            styledText.addRun(new JRStyledText.Run(styleAttrs, startIndex, styledText.length()));
        } else if (node.getNodeType() == Node.ELEMENT_NODE
                && NODE_italic.equalsIgnoreCase(node.getNodeName())) {
            Map<Attribute, Object> styleAttrs = new HashMap<Attribute, Object>();
            styleAttrs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);

            int startIndex = styledText.length();

            parseStyle(styledText, node);

            styledText.addRun(new JRStyledText.Run(styleAttrs, startIndex, styledText.length()));
        } else if (node.getNodeType() == Node.ELEMENT_NODE
                && NODE_underline.equalsIgnoreCase(node.getNodeName())) {
            Map<Attribute, Object> styleAttrs = new HashMap<Attribute, Object>();
            styleAttrs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);

            int startIndex = styledText.length();

            parseStyle(styledText, node);

            styledText.addRun(new JRStyledText.Run(styleAttrs, startIndex, styledText.length()));
        } else if (node.getNodeType() == Node.ELEMENT_NODE && NODE_sup.equalsIgnoreCase(node.getNodeName())) {
            Map<Attribute, Object> styleAttrs = new HashMap<Attribute, Object>();
            styleAttrs.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);

            int startIndex = styledText.length();

            parseStyle(styledText, node);

            styledText.addRun(new JRStyledText.Run(styleAttrs, startIndex, styledText.length()));
        } else if (node.getNodeType() == Node.ELEMENT_NODE && NODE_sub.equalsIgnoreCase(node.getNodeName())) {
            Map<Attribute, Object> styleAttrs = new HashMap<Attribute, Object>();
            styleAttrs.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);

            int startIndex = styledText.length();

            parseStyle(styledText, node);

            styledText.addRun(new JRStyledText.Run(styleAttrs, startIndex, styledText.length()));
        } else if (node.getNodeType() == Node.ELEMENT_NODE && NODE_font.equalsIgnoreCase(node.getNodeName())) {
            NamedNodeMap nodeAttrs = node.getAttributes();

            Map<Attribute, Object> styleAttrs = new HashMap<Attribute, Object>();

            if (nodeAttrs.getNamedItem(ATTRIBUTE_size) != null) {
                styleAttrs.put(TextAttribute.SIZE,
                        Float.valueOf(nodeAttrs.getNamedItem(ATTRIBUTE_size).getNodeValue()));
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_color) != null) {
                Color color = JRColorUtil.getColor(nodeAttrs.getNamedItem(ATTRIBUTE_color).getNodeValue(),
                        Color.black);
                styleAttrs.put(TextAttribute.FOREGROUND, color);
            }

            if (nodeAttrs.getNamedItem(ATTRIBUTE_fontFace) != null) {
                String fontFaces = nodeAttrs.getNamedItem(ATTRIBUTE_fontFace).getNodeValue();

                StringTokenizer t = new StringTokenizer(fontFaces, ",");
                while (t.hasMoreTokens()) {
                    String face = t.nextToken().trim();
                    if (AVAILABLE_FONT_FACE_NAMES.contains(face)) {
                        styleAttrs.put(TextAttribute.FAMILY, face);
                        break;
                    }
                }
            }

            int startIndex = styledText.length();

            parseStyle(styledText, node);

            styledText.addRun(new JRStyledText.Run(styleAttrs, startIndex, styledText.length()));

        } else if (node.getNodeType() == Node.ELEMENT_NODE && NODE_br.equalsIgnoreCase(node.getNodeName())) {
            styledText.append("\n");

            int startIndex = styledText.length();
            resizeRuns(styledText.getRuns(), startIndex, 1);

            parseStyle(styledText, node);
            styledText.addRun(
                    new JRStyledText.Run(new HashMap<Attribute, Object>(), startIndex, styledText.length()));

            if (startIndex < styledText.length()) {
                styledText.append("\n");
                resizeRuns(styledText.getRuns(), startIndex, 1);
            }
        } else if (node.getNodeType() == Node.ELEMENT_NODE && NODE_li.equalsIgnoreCase(node.getNodeName())) {
            String tmpText = styledText.getText();
            if (tmpText.length() > 0 && !tmpText.endsWith("\n")) {
                styledText.append("\n");
            }
            styledText.append(" \u2022 ");

            int startIndex = styledText.length();
            resizeRuns(styledText.getRuns(), startIndex, 1);
            parseStyle(styledText, node);
            styledText.addRun(
                    new JRStyledText.Run(new HashMap<Attribute, Object>(), startIndex, styledText.length()));

            // if the text in the next node does not start with a '\n', or 
            // if the next node is not a <li /> one, we have to append a new line
            Node nextNode = node.getNextSibling();
            String textContent = getFirstTextOccurence(nextNode);
            if (nextNode != null && !((nextNode.getNodeType() == Node.ELEMENT_NODE
                    && NODE_li.equalsIgnoreCase(nextNode.getNodeName())
                    || (textContent != null && textContent.startsWith("\n"))))) {
                styledText.append("\n");
                resizeRuns(styledText.getRuns(), startIndex, 1);
            }
        } else if (node.getNodeType() == Node.ELEMENT_NODE && NODE_a.equalsIgnoreCase(node.getNodeName())) {
            if (hyperlink == null) {
                NamedNodeMap nodeAttrs = node.getAttributes();

                Map<Attribute, Object> styleAttrs = new HashMap<Attribute, Object>();

                hyperlink = new JRBasePrintHyperlink();
                hyperlink.setHyperlinkType(HyperlinkTypeEnum.REFERENCE);
                styleAttrs.put(JRTextAttribute.HYPERLINK, hyperlink);

                if (nodeAttrs.getNamedItem(ATTRIBUTE_href) != null) {
                    hyperlink.setHyperlinkReference(nodeAttrs.getNamedItem(ATTRIBUTE_href).getNodeValue());
                }

                if (nodeAttrs.getNamedItem(ATTRIBUTE_type) != null) {
                    hyperlink.setLinkType(nodeAttrs.getNamedItem(ATTRIBUTE_type).getNodeValue());
                }

                if (nodeAttrs.getNamedItem(ATTRIBUTE_target) != null) {
                    hyperlink.setLinkTarget(nodeAttrs.getNamedItem(ATTRIBUTE_target).getNodeValue());
                }

                int startIndex = styledText.length();

                parseStyle(styledText, node);

                styledText.addRun(new JRStyledText.Run(styleAttrs, startIndex, styledText.length()));

                hyperlink = null;
            } else {
                throw new SAXException("Hyperlink <a> tags cannot be nested.");
            }
        } else if (node.getNodeType() == Node.ELEMENT_NODE && NODE_param.equalsIgnoreCase(node.getNodeName())) {
            if (hyperlink == null) {
                throw new SAXException("Hyperlink <param> tags must appear inside an <a> tag only.");
            } else {
                NamedNodeMap nodeAttrs = node.getAttributes();

                JRPrintHyperlinkParameter parameter = new JRPrintHyperlinkParameter();

                if (nodeAttrs.getNamedItem(ATTRIBUTE_name) != null) {
                    parameter.setName(nodeAttrs.getNamedItem(ATTRIBUTE_name).getNodeValue());
                }

                if (nodeAttrs.getNamedItem(ATTRIBUTE_valueClass) != null) {
                    parameter.setValueClass(nodeAttrs.getNamedItem(ATTRIBUTE_valueClass).getNodeValue());
                }

                String strValue = node.getTextContent();
                if (strValue != null) {
                    Object value = JRValueStringUtils.deserialize(parameter.getValueClass(), strValue);
                    parameter.setValue(value);
                }

                hyperlink.addHyperlinkParameter(parameter);
            }
        } else if (node.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = "<" + node.getNodeName() + ">";
            throw new SAXException("Tag " + nodeName + " is not a valid styled text tag.");
        }
    }
}