I have problems to obtain smooth fonts with libGDX. I already search on this site, and on google, I tried the solutions on these questions here and here, but I always have poor rendering of my fonts.
Exemple :
I tried several methods, and always get the exact same result as described by the picture above.
One method I used to generate the font is :
public static BitmapFont generateFont(String fontPath, float size){
FileHandle fontFile = Gdx.files.internal(fontPath);
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
FreeTypeFontGenerator.FreeTypeFontParameter params = new FreeTypeFontGenerator.FreeTypeFontParameter();
params.genMipMaps = true;
params.magFilter = TextureFilter.MipMapLinearNearest;
params.minFilter = TextureFilter.MipMapLinearNearest;
params.size = (int)Math.ceil(size);
generator.scaleForPixelHeight((int)Math.ceil(size));
BitmapFont f = generator.generateFont(params);
return f;
}
Another strategy I tried was to load the fonts in an AssetManager :
FileHandleResolver resolver = new InternalFileHandleResolver();
assetsManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetsManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
FreeTypeFontLoaderParameter size4Params = new FreeTypeFontLoaderParameter();
sizeParams.fontFileName = "Fonts/GOTHIC.TTF";
sizeParams.fontParameters.size = (int)Math.ceil(2*Gdx.graphics.getWidth()/9);
sizeParams.fontParameters.genMipMaps = true;
sizeParams.fontParameters.minFilter = TextureFilter.MipMapLinearNearest;
sizeParams.fontParameters.magFilter = TextureFilter.MipMapLinearNearest;
assetsManager.load("font1.ttf", BitmapFont.class, sizeParams);
These 2 strategies give the same result, but what annoy me the most is that, even if I remove "genMipMaps = true", and the TextureFilters in these methods, I still have the same result. It's like the filters are useless for the fonts.
What am I missing ?
Thanks !
OK, so finally here is a solution : I simply changed the TextureFilter from MipMapLinearNearest to Linear, and I obtain a smooth texture :
At first I was very reluctant to use the linear filter, as I though it would decrease the frame rate, according to this post. But I didn't observe any fps drop with the FPSLogger, so it seems it's all good.
Here is my final code to generate the fonts with an asset manager :
FileHandleResolver resolver = new InternalFileHandleResolver();
assetsManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetsManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
FreeTypeFontLoaderParameter size4Params = new FreeTypeFontLoaderParameter();
sizeParams.fontFileName = "Fonts/GOTHIC.TTF";
sizeParams.fontParameters.size = (int)Math.ceil(2*Gdx.graphics.getWidth()/9);
sizeParams.fontParameters.minFilter = TextureFilter.Linear;
sizeParams.fontParameters.magFilter = TextureFilter.Linear;
assetsManager.load("font1.ttf", BitmapFont.class, sizeParams);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With