3
\$\begingroup\$

I've generated bitmap fonts for libgdx using Hiero. I've got several distance field fonts that require a lot of padding, unfortunately they don't seem to align correctly.

Below is a font with a bounding box drawn to the values returned from getBounds().

enter image description here

I've just changed to distance field fonts and all my logic that handles vertical positioning is broken because of this. If I use a font without any padding above or below then fonts are drawn aligned correctly.

My padding is 10 on all sides. Playing with a couple of characters, if I increase the y by 10 and decrease the height by 10 in the .fnt file I get the image below instead, which looks far more accurate.

enter image description here

If someone can tell me how I can resolve this issue, or a good way to work around it then that would be appreciated.

\$\endgroup\$
2
\$\begingroup\$

It has now been pointed out to me that on this page it says

Note: If you're replacing a "regular" font by a distance field font, be aware that the font metrics are not the same. In particular, the extra padding causes the baseline to shift downwards, so you'll need to compensate by drawing your text higher.

So this is a known problem using the libgdx BitmapFont with padding and they're suggesting just working around it. IMO it's a bug and should have been fixed, but that'll now be difficult if people are already compensating for it.

To fix this you should create a wrapper for BitmapFont and then whenever you're drawing text you should do the following to compensate for the padding.

y += padding * font.getScaleY();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.