Example usage for java.awt.font TextAttribute FONT

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

Introduction

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

Prototype

TextAttribute FONT

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

Click Source Link

Document

Attribute key used to provide the font to use to render text.

Usage

From source file:lucee.runtime.img.Image.java

public void drawString(String text, int x, int y, Struct attr) throws PageException {

    if (attr != null && attr.size() > 0) {

        // font/*from  w ww. j a v  a2s  . co  m*/
        String font = StringUtil.toLowerCase(Caster.toString(attr.get("font", ""))).trim();
        if (!StringUtil.isEmpty(font)) {
            font = FontUtil.getFont(font).getFontName();
        } else
            font = "Serif";

        // alpha
        //float alpha=Caster.toFloatValue(attr.get("alpha",null),1F);

        // size
        int size = Caster.toIntValue(attr.get("size", Constants.INTEGER_10));

        // style
        int style = Font.PLAIN;
        String strStyle = StringUtil.toLowerCase(Caster.toString(attr.get("style", "")));
        strStyle = StringUtil.removeWhiteSpace(strStyle);
        if (!StringUtil.isEmpty(strStyle)) {
            if ("plain".equals(strStyle))
                style = Font.PLAIN;
            else if ("bold".equals(strStyle))
                style = Font.BOLD;
            else if ("italic".equals(strStyle))
                style = Font.ITALIC;
            else if ("bolditalic".equals(strStyle))
                style = Font.BOLD + Font.ITALIC;
            else if ("bold,italic".equals(strStyle))
                style = Font.BOLD + Font.ITALIC;
            else if ("italicbold".equals(strStyle))
                style = Font.BOLD + Font.ITALIC;
            else if ("italic,bold".equals(strStyle))
                style = Font.BOLD + Font.ITALIC;
            else
                throw new ExpressionException("key style of argument attributeCollection has an invalid value ["
                        + strStyle + "], valid values are [plain,bold,italic,bolditalic]");
        }

        // strikethrough
        boolean strikethrough = Caster.toBooleanValue(attr.get("strikethrough", Boolean.FALSE));

        // underline
        boolean underline = Caster.toBooleanValue(attr.get("underline", Boolean.FALSE));

        AttributedString as = new AttributedString(text);
        as.addAttribute(TextAttribute.FONT, new Font(font, style, size));
        if (strikethrough)
            as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
        if (underline)
            as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
        Graphics2D g = getGraphics();
        //if(alpha!=1D) setAlpha(g,alpha);

        g.drawString(as.getIterator(), x, y);
    } else
        getGraphics().drawString(text, x, y);

}

From source file:com.raddle.tools.MergeMain.java

private void setCellRenderer(JList list) {
    list.setCellRenderer(new DefaultListCellRenderer() {

        private static final long serialVersionUID = 1L;

        @Override/*www . j av a  2 s . c  o  m*/
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value != null) {
                PropertyLine line = (PropertyLine) value;
                boolean isInSelected = false;
                for (int selectedIndex : list.getSelectedIndices()) {
                    if (index == selectedIndex) {
                        isInSelected = true;
                    }
                }
                if (!isInSelected) {
                    if (CompareResult.extra == line.getCompareResult()) {
                        c.setBackground(new Color(0xFFC48E));
                    } else if (CompareResult.different == line.getCompareResult()) {
                        c.setBackground(new Color(0xBBBBFF));
                    }
                }
                if (line.getState() != null) {
                    if (LineState.added == line.getState()) {
                        c.setForeground(new Color(0xCC0033));
                    } else if (LineState.updated == line.getState()) {
                        c.setForeground(new Color(0x0066CC));
                    } else if (LineState.deleted == line.getState()) {
                        if (line.getOriginalValue() == null) {
                            c.setForeground(new Color(0xAAAAAA));
                        } else {
                            c.setForeground(new Color(0x666666));
                        }
                        Map<Attribute, Object> map = new HashMap<Attribute, Object>();
                        map.put(TextAttribute.FONT, c.getFont());
                        map.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
                        c.setFont(Font.getFont(map));
                    }
                }
            }
            return c;
        }

    });
}

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 ww.  j  a  v a 2s. co m*/
 */
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));
        }
    }
}

From source file:ru.runa.wfe.graph.image.figure.AbstractFigure.java

private int drawText(Graphics2D graphics, String text, int hOffset) {
    Rectangle r = getTextBoundsRectangle();
    Rectangle2D textBounds = graphics.getFontMetrics().getStringBounds(text, graphics);
    if (textBounds.getWidth() > r.getWidth() - 4) {
        int y = coords[1] + hOffset;
        AttributedString attributedString = new AttributedString(text);
        attributedString.addAttribute(TextAttribute.FONT, graphics.getFont());
        AttributedCharacterIterator characterIterator = attributedString.getIterator();
        LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, graphics.getFontRenderContext());
        while (measurer.getPosition() < characterIterator.getEndIndex()) {
            TextLayout textLayout = measurer.nextLayout((float) r.getWidth() - 4);
            y += textLayout.getAscent();
            float x = (float) (r.getCenterX() + 2 - textLayout.getBounds().getCenterX());
            textLayout.draw(graphics, x, y);
            y += textLayout.getDescent() + textLayout.getLeading();
        }/*from   www .  j a v  a  2s.c  o  m*/
        return y - coords[1];
    } else {
        graphics.drawString(text, (float) (r.getCenterX() + 2 - textBounds.getCenterX()),
                (float) (coords[1] + textBounds.getHeight() + hOffset));
        return (int) (textBounds.getHeight() + hOffset + 3);
    }
}