Hiero or BMFont are nice tools when it comes to generating bitmap fonts offline, adding fancy effects like dropshadows and similar things. However, this also means that you have to generate various versions of your fonts, for different screen sizes and resolutions, a tedious process, that is not exact.
For the Reddit app i’m working on i needed something more flexibel. Thus i wrapped FreeType, the goto FOSS solution when it comes to font rendering. The code can be found in the gdx-freetype project in SVN. It allows you to generate BitmapFontData and BitmapFont instances on the fly from TrueType font files. Here’s how you use it:
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
BitmapFont font15 = generator.generateFont(15);
BitmapFont font22 = generator.generateFont(22);
Remember to dispose the generator once you are done generating fonts for a specific TrueType file. The generateData method has overloads that allow you to specify more things, such as the characters you want the BitmapFont to contain and so on.
To use gdx-freetype, grab the latest nightlies, link gdx-freetype.jar and gdx-freetype-natives.jar to your desktop project, link gdx-freetype.jar to your Android project, and copy the armeabi/libgdx-freetype.so and armeabi-v7a/libgdx-freetype.so files to your Android project’s libs/ folder, just like with the libgdx.so files.
- If you use to big of a size, things might explode. BitmapFonts still only support a single atlas page at the moment, something i’ll try to fix in the next couple of weeks.
- Asian scripts “might” work, see caveat above though. They contain just to many glyphs. I’m thinking about ways to fix this.
- Right-to-left scripts like arabic are a no-go. The layouting “algorithms” in BitmapFont and BitmapFontCache have no idea how to handle that.
- Throwing just any font at FreeType is not a super awesome idea. Some fonts in the wild are just terrible, with bad or no hinting information and will look like poopoo.
That being said, i’ll try to improve support of more complex scripts, arabic would be nice 🙂 But i guess there’s a reason for this bug on the Android tracker.