*/
public class GraphicTextRenderer extends RendererBase {
@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);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
textLayout.draw(graphics, 0, 0);
int absciss = calculateAbsciss(image);
int ordinate = calculateOrdinate(image);
BufferedImage croppedImage = image.getSubimage(absciss - 1, ordinate - 1, (int) textWidth + 2, (int) textHeight + 2);
graphics.dispose();
ImageDataModel model = Rendering.getDataModel(croppedImage);
ResponseWriter writer = context.getResponseWriter();
int imageWidth = croppedImage.getWidth();
int imageHeight = croppedImage.getHeight();
Rendering.startWriteIMG(writer, context, graphicText, "png",
model, new int[]{imageWidth, imageHeight});
Rendering.writeComponentClassAttribute(writer, graphicText);
Rendering.writeStandardEvents(writer, graphicText);
writeAttribute(writer, "title ", graphicText.getTitle());
writeAttribute(writer, "lang ", graphicText.getLang());
writeAttribute(writer, "height", String.valueOf(imageHeight));
writeAttribute(writer, "width", String.valueOf(imageWidth));
Rendering.encodeInitComponentCall(context, graphicText, true);