To begin working with true type font files you'll first need to register the
TrueTypeLoader with jMonkeyEngine's
AssetManager system:
assetManager.registerLoader(TrueTypeLoader.class, "ttf");
Once this is complete you can begin loading ttf files. If you want to work with bitmap text, best for small fonts and UI text:
TrueTypeKeyBMP ttk = new TrueTypeKeyBMP("Interface/Fonts/myTypeface.ttf",
Style.Plain, 28);
TrueTypeFont ttf = (TrueTypeBMP)assetManager.loadAsset(ttk);
Conversely if you want to work with mesh text, best for large fonts and scene text:
TrueTypeKeyMesh ttk = new TrueTypeKeyMesh("Interface/Fonts/myTypeface.ttf",
Style.Plain, 28);
TrueTypeFont ttf = (TrueTypeMesh)assetManager.loadAsset(ttk);
The
TrueTypeKey takes in the path to your ttf file relative to your assets directory, a
Style and a point size. Additionally you can specify an outline size, for BMP text only, and the screen density in pixels per inch. When a screen density is specified the font will scale to match 1/72nd of an inch per point.
For bitmap texts it is often useful to scale the text down to create a nice crisp looking text. This is often not necessary on mobile devices as their dense screens tend to take care of this.
TrueTypeKeyBMP ttk = new TrueTypeKeyBMP("Interface/Fonts/myTypeface.ttf",
Style.Plain, 21);
TrueTypeFont ttf = (TrueTypeBMP)assetManager.loadAsset(ttk);
ttf.setScale(16/21f);
The above will provide a 21 point font, but all the text returned will be scaled down to 16 points. I recommend you experiement with different scales until you find a good quality for your font, however, a general rule I found that seems to work well is:
if (desiredPointSize < 32) {
actualSize = (int)Math.floor(desiredPointSize / 0.73f);
scale = desiredPointSize / (float)actualSize;
} else if (desiredPointSize < 53) {
actualSize = (int)Math.floor(desiredPointSize / 0.84f);
scale = desiredPointSize / (float)actualSize;
}
TrueTypeKeyBMP ttk = new TrueTypeKeyBMP("Interface/Fonts/myTypeface.ttf",
Style.Plain, actualSize);
TrueTypeFont ttf = (TrueTypeBMP)assetManager.loadAsset(ttk);
ttf.setScale(scale);