Search in sources :

Example 86 with AttributedString

use of java.text.AttributedString in project Lucee by lucee.

the class Image method drawString.

public void drawString(String text, int x, int y, Struct attr) throws PageException {
    if (attr != null && attr.size() > 0) {
        // font
        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(KeyConstants._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);
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) Point(java.awt.Point) ExpressionException(lucee.runtime.exp.ExpressionException) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Aggregations

AttributedString (java.text.AttributedString)86 AttributedCharacterIterator (java.text.AttributedCharacterIterator)37 TextLayout (java.awt.font.TextLayout)29 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)16 FontRenderContext (java.awt.font.FontRenderContext)13 Font (java.awt.Font)11 Point (java.awt.Point)11 Paint (java.awt.Paint)10 Graphics2D (java.awt.Graphics2D)9 WeakHashMap (java.util.WeakHashMap)8 Color (java.awt.Color)4 Bidi (java.text.Bidi)4 ArrayList (java.util.ArrayList)4 TreeSet (java.util.TreeSet)4 Rectangle (java.awt.Rectangle)3 HashSet (java.util.HashSet)3 Dimension (java.awt.Dimension)2 Insets (java.awt.Insets)2 AffineTransform (java.awt.geom.AffineTransform)2 Rectangle2D (java.awt.geom.Rectangle2D)2