I just had exactly the same problem and found a quite nice solution:

All the fonts I use are external Truetype Fonts and I load them from file by using

Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(fontFile));

I need font kerning but obviously Java2D still doesn't implement this in the latest 
version
(which is really a shame). So I had to look for a workaround and finally came up with 
this:

the iText library (a library for generating pdf files: http://www.lowagie.com/iText/) 
is able to
read TrueType font and it also reads the kerning tables. There's even a fuction
BaseFont.getKerning(char1, char2) defined. So I read the used fonts again with this 
library
and just use BaseFont as an information source for the kerning numbers. The font is 
read like
this:

BaseFont kernedFont = BaseFont.createFont(fontFile, BaseFont.WINANSI, false);

Now I can correct the glyph vectors with the right kerning numbers:

double charSpacing = 0;
GlyphVector glyphs = font.createGlyphVector(frc, str);
double fontSize = font.getSize2D();
double x = 0;
int num = glyphs.getNumGlyphs();
for (int i = 0; i < num; i++) {
 Point2D pos = glyphs.getGlyphPosition(i);
 GlyhpMetrics gm = glyphs.getGlyphMetrics(i);
 pos.x = x;
 glyphs.setGlyphPosition(i, pos);
 x += gm.getAdvance();
 if (i < num - 1) x += charSpacing * 0.001f * fontSize +
kernedFont.getKerning(str.charAt(i), str.charAt(i + 1)) *  0.001f * fontSize;
}

This works exactly as Illustrator. The spacing value (charSpacing) also handles 
letter-spacing
(or char-spacing) exactly as Illustrator does, values are given in 1/1000 em.

I hope this code helps those who need real high quality font rendering in java.

Best

J�rg Lehni

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to