I want to render text to bitmap to use it later as a decal texture. I've read through libGDX fonts drawing code and came up with this:
private void writeTextToPixmap(Pixmap destPixmap, Pixmap fontPixmap, BitmapFont.BitmapFontData fontData, int startX, int startY, String text) {
int cursor = startX;
char[] chars = text.toCharArray();
for(int i = 0; i < chars.length; i++) {
BitmapFont.Glyph glyph = fontData.getGlyph(chars[i]);
destPixmap.drawPixmap(fontPixmap, glyph.srcX, glyph.srcY, glyph.width, glyph.height, cursor + glyph.xoffset, startY + glyph.yoffset, glyph.width, glyph.height);
cursor += glyph.xadvance;
}
}
The font was generated with Hiero using default settings.
But the result does not look as expected: https://i.sstatic.net/3zbAH.png
Usage:
Pixmap pixmap = new Pixmap(1024, 1024, Pixmap.Format.RGBA8888);
BitmapFont.BitmapFontData fontData = new BitmapFont.BitmapFontData(Gdx.files.internal("data/couriernew.fnt"), false);
Pixmap fontPixmap = new Pixmap(Gdx.files.internal("data/couriernew.png"));
writeTextToPixmap(pixmap, fontPixmap, fontData, 300, 400, "ga");