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