4

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 : enter image description here

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 !

Community
  • 1
  • 1
vdlmrc
  • 737
  • 5
  • 17
  • Is your OpenGL viewport exactly the same as the screen size? (It could be the fonts are fine and there is scaling happening elsewhere?) – P.T. Feb 26 '15 at 06:55
  • I didn't touch the viewport, so by default it should the same size as the screen size, I guess. – vdlmrc Feb 26 '15 at 13:01
  • Maybe I have the same problem due to viewport, I am using FitViewport, and the font is not smooth at all. How can I set the viewport so that the font is smooth ? – JavaDev Jun 19 '16 at 12:31

2 Answers2

9

OK, so finally here is a solution : I simply changed the TextureFilter from MipMapLinearNearest to Linear, and I obtain a smooth texture : enter image description here

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);
vdlmrc
  • 737
  • 5
  • 17
1

To get an even better quality, I think you will have to hack into the shader. And it's not that difficult because of libgdx.

Distance field fonts should serve you best according to me. The wiki provides a nice tutorial about using them. They can actually give a significant boost to the quality of the font.

Hope this helps.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
  • Thank you for the answer, but I didn't want to use the shader as it requires to use a spritebatch. In my app I'm using a stage and use the fonts in several actors. The shader won't fit in my code. I'm pretty sure that there is only one little something missing in my code to use the TextureFilters, but I'm not able to find out what it is. – vdlmrc Feb 26 '15 at 03:57