yesterday I started working om making custom sized fonts to my game, in game, using FreeTypeFaceGenerator
, and yesterday it worked fine, except an exception I was going to handle today. When I loaded it up today, and tried to run it, it keeps crashing in the part where I create the FTFGen object, I have no idea how to fix this.
How do I fix this, where do I even begin? I changed to font file and it gives me the same error it did yesterday, but I don't want the other file (Vivaldi), I need the one I'm testing (Viner Hand ITC).
How do I fix this? I'm using .TTF files, not making a bitmapfont file.
I don't want to provide my own source code, but I'll post the same way, just change a couple of things:
(I followed a guide located here )
The method recieves three parameters, A FileHandle
of the font file, a String
of the font name, and a float
array with all the sizes which will then turn to size of the font using
(int)(size*Gdx.graphics.getDensity())
This is the code:
FreeTypeFontGenerator ftfGen = new FreeTypeFontGenerator(handle);
for (float x : size) {
PixmapPacker packer = new PixmapPacker(512, 512, Format.RGBA8888,
2, false);
FreeTypeBitmapFontData fontData = ftfGen.generateData(
(int) (x * Gdx.graphics.getDensity()),
FreeTypeFontGenerator.DEFAULT_CHARS, false, packer);
Array<Page> pages = packer.getPages();
TextureRegion[] textureRegions = new TextureRegion[pages.size];
for (int i = 0; i < pages.size; i++) {
Page p = pages.get(i);
Texture texture = new Texture(new PixmapTextureData(
p.getPixmap(), p.getPixmap().getFormat(), false, false,
true)) {
@Override
public void dispose() {
super.dispose();
getTextureData().consumePixmap().dispose();
}
};
texture.setFilter(Texture.TextureFilter.Nearest,
Texture.TextureFilter.Nearest);
textureRegions[i] = new TextureRegion(texture);
BitmapFont font = new BitmapFont(fontData, textureRegions,
false);
FileHandle file = Gdx.files.local("Data\\VINERITC.TTF");
FileHandle pixmapDir = Gdx.files.local("Data\\VINERITC");
BitmapFontWriter.setOutputFormat(OutputFormat.Text);
String[] pageRefs = BitmapFontWriter.writePixmaps(
packer.getPages(), pixmapDir, fontName);
for (int j = 0; j < pageRefs.length; j++) {
pageRefs[j] = fontName + "_" + pageRefs[j];
}
BitmapFontWriter.writeFont(
font.getData(),
pageRefs,
file,
new FontInfo(fontName, (int) (x * Gdx.graphics
.getDensity())), 1, 1);
packer.dispose();
}
}
The error I am receiving is in this line of code:
FreeTypeFontGenerator ftfGen = new FreeTypeFontGenerator(handle);
Upon digging around in the code of LibGDX I found out that it is caused by this method, which uses a native method:
public static Face newMemoryFace(Library library, byte[] data, int dataSize, int faceIndex) {
ByteBuffer buffer = BufferUtils.newUnsafeByteBuffer(data.length);
BufferUtils.copy(data, 0, buffer, data.length);
long address = newMemoryFace(library.address, buffer, dataSize, faceIndex);
if(address == 0) {
BufferUtils.disposeUnsafeByteBuffer(buffer);
throw new GdxRuntimeException("Couldn't load font");
}
else {
library.fontData.put(address, buffer);
return new Face(address, library);
}
}
private static native long newMemoryFace(long library, ByteBuffer data, int dataSize, int faceIndex);
This part gives out the error:
long address = newMemoryFace(library.address, buffer, dataSize, faceIndex);
if(address == 0) {
BufferUtils.disposeUnsafeByteBuffer(buffer);
throw new GdxRuntimeException("Couldn't load font");
}
The newMemoryFace
is a native method.
I FOUND THE PROBLEM! This line is the problem:
FileHandle file = Gdx.files.local("Data\\VINERITC.TTF");
This line caused a creation of a new TTF file, and thus cause a problem with the loading system, make sure if you use this to write .fnt
and not TTF.
Couldn't load font
exception... \$\endgroup\$