@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
GraphicText graphicText = (GraphicText) component;
// default style properties
StyleObjectModel styleModel = graphicText.getStyleObjectModel();
StyleFontModel font = styleModel.getFont();
String fontName = font.getName();
int fontSize = font.getSize();
int fontStyle = font.getStyle();
String text = Rendering.convertToString(context, graphicText, graphicText.getValue());
int direction = graphicText.getDirection();
AffineTransform transform = new AffineTransform();
float translationIndex = (float) 1.2;
float increaseIndex = (float) 2.4;
if (text.length() <= 3) {
translationIndex = 5;
increaseIndex = 10;
}
transform.rotate(Math.toRadians(-direction));
FontRenderContext frc = new FontRenderContext(transform, true, true);
TextLayout textLayout = new TextLayout(text, new Font(fontName, fontStyle, fontSize), frc);
Rectangle bounds = textLayout.getOutline(transform).getBounds();
// calculate transformed text width and height
double textHeight = bounds.getSize().getHeight();
double textWidth = bounds.getSize().getWidth();
BufferedImage image = new BufferedImage(
(int) Math.round(textWidth * increaseIndex),
(int) Math.round(textHeight * increaseIndex),
BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(styleModel.getColor());
graphics.translate(translationIndex * textWidth, translationIndex * textHeight);
graphics.transform(transform);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);