org.riotfamily.website.txt2img
Class TextRenderer

java.lang.Object
  extended by org.riotfamily.website.txt2img.TextRenderer
All Implemented Interfaces:
InitializingBean
Direct Known Subclasses:
ListItemRenderer

public class TextRenderer
extends Object
implements InitializingBean

Class to render text as image. Supports resampling to improve the kerning at small font sizes.

Since:
6.5
Author:
Felix Gnass [fgnass at neteye dot de]

Constructor Summary
TextRenderer()
           
 
Method Summary
 void afterPropertiesSet()
           
protected  int checkSize(int size)
           
protected  Graphics2D createGraphics(BufferedImage image)
           
protected  BufferedImage createImage(Dimension size)
           
protected  void drawText(String text, float maxWidth, String color, float fontSize, BufferedImage image)
           
 BufferedImage generate(String text, int maxWidth, String color)
           
protected  Font getFont(String text)
           
protected  Dimension getSize(String text, float fontSize, float maxWidth)
           
protected  Dimension layout(String text, float maxWidth, String color, float fontSize, BufferedImage image, boolean draw)
           
 void setAntiAlias(boolean antiAlias)
          Turns anti-aliasing off or on (default).
 void setColor(String color)
          Sets the text foreground color.
 void setFont(Resource res)
          Sets the font to use.
 void setInternalFontSize(int internalFontSize)
          Sets the font size at which the text is rendered internally when resampling is enabled.
 void setLineSpacing(int lineSpacing)
          Sets the interline spacing in pixels.
 void setMaxWidth(Integer maxWidth)
          Sets the maximum image width.
 void setPaddingBottom(int paddingBottom)
          Sets the padding at the bottom of the image in pixels.
 void setPaddingLeft(int paddingLeft)
          Sets the padding at the left side of the image in pixels.
 void setPaddingRight(int paddingRight)
          Sets the padding at the right side of the image in pixels.
 void setPaddingTop(int paddingTop)
          Sets the padding at the top of the image in pixels.
 void setResample(boolean resample)
          If set to true, the text will be rendered at a larger size internally and scaled down to the requested size.
 void setShrinkToFit(boolean shrinkToFit)
          Sets whether the font-size should be reduced if the text does not fit within the max-width.
 void setSize(float size)
          Sets the font size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TextRenderer

public TextRenderer()
Method Detail

setFont

public void setFont(Resource res)
             throws FontFormatException,
                    IOException
Sets the font to use. The resource must either point to a Type 1 or a TrueType font or a directory containing fonts. In case a directory is specified, the font that contains the most glyphs needed to render a given text is used. The font type is determined by the filename extension. Supported extensions are .pfa, .pfb and .ttf.

Throws:
FontFormatException
IOException

getFont

protected Font getFont(String text)

setColor

public void setColor(String color)
Sets the text foreground color. Examples of supported formats:
 #fff
 #fffff
 rgb(255,255,255)
 rgb(100%, 100%, 100%)
 


setSize

public void setSize(float size)
Sets the font size.


setAntiAlias

public void setAntiAlias(boolean antiAlias)
Turns anti-aliasing off or on (default).


setLineSpacing

public void setLineSpacing(int lineSpacing)
Sets the interline spacing in pixels. If not set, TextLayout.getLeading() is used.


setMaxWidth

public void setMaxWidth(Integer maxWidth)
Sets the maximum image width.


setShrinkToFit

public void setShrinkToFit(boolean shrinkToFit)
Sets whether the font-size should be reduced if the text does not fit within the max-width.


setPaddingTop

public void setPaddingTop(int paddingTop)
Sets the padding at the top of the image in pixels. The default value is 0.


setPaddingRight

public void setPaddingRight(int paddingRight)
Sets the padding at the right side of the image in pixels. The default value is 0.


setPaddingBottom

public void setPaddingBottom(int paddingBottom)
Sets the padding at the bottom of the image in pixels. The default value is 0.


setPaddingLeft

public void setPaddingLeft(int paddingLeft)
Sets the padding at the left side of the image in pixels. The default value is 0.


setResample

public void setResample(boolean resample)
If set to true, the text will be rendered at a larger size internally and scaled down to the requested size.


setInternalFontSize

public void setInternalFontSize(int internalFontSize)
Sets the font size at which the text is rendered internally when resampling is enabled.


afterPropertiesSet

public void afterPropertiesSet()
Specified by:
afterPropertiesSet in interface InitializingBean

generate

public BufferedImage generate(String text,
                              int maxWidth,
                              String color)

getSize

protected Dimension getSize(String text,
                            float fontSize,
                            float maxWidth)

drawText

protected void drawText(String text,
                        float maxWidth,
                        String color,
                        float fontSize,
                        BufferedImage image)

layout

protected Dimension layout(String text,
                           float maxWidth,
                           String color,
                           float fontSize,
                           BufferedImage image,
                           boolean draw)

checkSize

protected int checkSize(int size)

createGraphics

protected Graphics2D createGraphics(BufferedImage image)

createImage

protected BufferedImage createImage(Dimension size)