How to get the natural fontsize of bitmapfont?

943 Views Asked by At

If I have a bitmapfont such as;

fonttexture = new Texture(Gdx.files.internal("data/fonttest.png"), true);
font= new BitmapFont(Gdx.files.internal("data/fonttest.fnt"), 
                     new TextureRegion(fonttexture ), true);

Is it possible to retrieve or deduce the natural fontsize used on the associated png texture file? The *.fnt file itself specifies this on the first line;

info face="Calibri Light" size=32 bold=0 italic=0 charset=""

Its that 32 I am after in code. I couldn't see a method on BitmapFonts class to directly get it, but there was various other height related attributes possible to get.

I tried recreating the size from those but didn't have much luck.

float baseToAssent = font.getCapHeight()+font.getAscent();
float baseToDecent = -font.getDescent();Decent);            
float fontHeight = baseToAssent+baseToDecent;  //too small, like 11 not 32

Not being a fonttographer? fontophile?...umm...graphic designer, I am probably putting the pieces together wrong, or maybe just lacking one.

In either case, any way to work out the font size used in the fonttest.png would be helpful.

Thanks

Usecase;

I am manually using that texture with a distance field shader. Knowing the textures natural font size is useful in order to accurately scale the text and lineheight to any requested font size without ever resizing a image and thus losing quality.

1

There are 1 best solutions below

2
AudioBubble On

Not sure how to get the font size specific, however you can easily get the String width and height of a font using a GlyphLayout, like this:

    BitmapFont font = new BitmapFont(Gdx.files.internal("data/fonttest.fnt", false);

    GlyphLayout glyph = new GlyphLayout(font, "TextMessage");
    float width = glyph.width;
    float height = glyph.height;

Read this topic if you want to learn more about GlyphLayout and BitmapFont. Experimented quite a bit with a 32px font, like yours. The String "W" seemed to be 29px wide, which was the widest I could find, so it's definately not precise if what you want to archive is the size exactly.

You could do a little hacky method, reading the .fnt file as a text file and looking for the Integer after "size=", pretty much like a properties file work, it will be less efficient but will return exact value.