Font Glyphs to Animationstrip, Need Constant Size


2004-04-23 09:25:41 PM
jbuilder9
I have a unique problem. I need to get images of font characters into an
animation strip. The animation strip is necessary for performance reasons.
Just printing chars as strings is way too slow. I am trying to use
GlyphVector to do this. The problem is in the SIZE of the individual
glyphs. There doesn't seem to be any consistency in the size of the
character glyphs and I need them all to be the same size to work in an
animation strip. I have tried to use the GlyphVector.getLogicalBounds(),
GlyphVector.getVisualBounds(), and the equivalent methods for the individual
glyphs, but those methods return illogical results. For example visual
bounds has a larger width but logical bounds has a larger height, aargh!
That make no sense at all and I can't find any documentation anywhere.
I am thinking of using a BufferedImageOp (RescaleOp) but I have no
consistent glyph size for the scale factor.
Does anybody know an easy way to do this?
Thanks.
Here is the code if you want to look at it:
// chars is an array of char
public void saveGlyphs( Graphics2D g2d )
{
FontRenderContext frc = g2d.getFontRenderContext();
GlyphVector gv = g2d.getFont().createGlyphVector( frc, chars );
Rectangle2D glyphLBounds = gv.getLogicalBounds();
int w = (int)Math.ceil( glyphLBounds.getWidth() );
int h = (int)Math.ceil( glyphLBounds.getHeight() );
if ( (h <= 0) || (w <= 0) )
{
g2d.drawString( "Font Size Bad: " + curFont.getFontName(), 20, 100);
return;
}
BufferedImage bi = new BufferedImage( w, h, BufferedImage.TYPE_INT_RGB );
Graphics2D big = bi.createGraphics();
big.setColor( textColor );
big.drawGlyphVector( gv, 0.0f, -(float)glyphLBounds.getY() );
*** BufferedImageOp bop = getScaledBOP( (float)glyphLBounds.getHeight() );
*** g2d.drawImage( bi, bop, 10, 100 );
big.dispose();
String filename = curFont.getFontName() + ".jpg";
try
{
javax.imageio.ImageIO.write( bi, "jpg", new File( "src/justatest/images",
filename ) );
}
catch (IOException ex)
{
ex.printStackTrace();
}
}