Accessing font kerning information in Java2D
Asked Answered
P

2

6

Little background..

I'm in the process of making an OpenGL game using Java and LWJGL. I've written a TextRenderer-class that renders text using cached pages of glyphs. The glyphs itself are rendered in Java2D to BufferedImages and packed into texture pages along with the glyph measurements. TextRenderer draws the characters as textured quads, using the cached information.

All this works well, except for one thing: missing kerning. Granted, it's not necessary to have as the text looks fine as it is, but it would improve the quality if I had access to the font kerning information.

And the question is..

Is it possible to obtain the kerning information using plain Java, in a way that would be portable across Windows, Linux and MacOS X? Back when I wrote the TextRenderer I briefly looked around but could not find such a way..

One possible solution

If there is no way of doing this in pure Java, I was thinking of writing a separate tool using Freetype. As listed in their features page:

FreeType 2 provides information that is often not available from other similar font engines, like kerning distances, glyph names, vertical metrics, etc.

The tool would store the kerning pairs for common characters into a file that my text renderer would load in and make use of. So this is probably what I will do if you guys don't come up with a better alternative. :)

Platina answered 15/6, 2009 at 15:11 Comment(0)
K
1

The only libraries I know of that read the kerning info "somwhat" correctly are iText and FOP from Apache.

http://www.1t3xt.info/api/com/lowagie/text/pdf/BaseFont.html http://svn.apache.org/viewvc/xmlgraphics/fop/tags/fop-0_95/src/java/org/apache/fop/fonts/ (a link to the svn as there seems to be no online api)

Kempf answered 15/6, 2009 at 15:31 Comment(1)
These both seem like overkill for getting kerning pairs, is there anything like freetype2 available for Java, or even a JNI wrapper for freetype2?Seka
M
2

Starting with Java SE 6, Java can provide kerning information when the font provides it. It is off by default and can be turned on like this:

Map<TextAttribute, Object> textAttributes = new HashMap<TextAttribute, Object>();  

textAttributes.put(TextAttribute.FAMILY, "Arial");  
textAttributes.put(TextAttribute.SIZE, 25f);  
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);  

Font font = Font.getFont(textAttributes);  

This forum thread contains a more detailed discussion on the topic:

http://forums.sun.com/thread.jspa?threadID=5359127

Mar answered 15/6, 2009 at 19:2 Comment(3)
This works for Java2D text rendering, but AFAIK it doesn't allow you to get the kerning pairs, which is what I need, as I'm rendering in OpenGL.Platina
While it takes a little work, I think that it would be possible to build a table of kerning pairs with the available information programmatically (e.g. in the application initialization, or you could calculate it offline in which case you won't need to do kerning in Java at all). I am considering using this approach for a project of mine.Mar
That is exactly what I am doing as well; an offline tool that stores the kerning pairs, probably using iText or Freetype in a C-program or perhaps a Python script. :)Platina
K
1

The only libraries I know of that read the kerning info "somwhat" correctly are iText and FOP from Apache.

http://www.1t3xt.info/api/com/lowagie/text/pdf/BaseFont.html http://svn.apache.org/viewvc/xmlgraphics/fop/tags/fop-0_95/src/java/org/apache/fop/fonts/ (a link to the svn as there seems to be no online api)

Kempf answered 15/6, 2009 at 15:31 Comment(1)
These both seem like overkill for getting kerning pairs, is there anything like freetype2 available for Java, or even a JNI wrapper for freetype2?Seka

© 2022 - 2024 — McMap. All rights reserved.