0
\$\begingroup\$

I use 3 sizes for fonts in my game: small, medium, and large. I take the base font size and attempt to scale the font based on screen size and density, but this is proving to be pretty hard.

Does anyone have a proven formula for scaling fonts? Right now I'm simply using the pixel width of the screen to put the device into 1x, 2x, and 3x font scale bucket sizes, but I feel like I should be using the screen density in the calculation as well.

\$\endgroup\$
  • \$\begingroup\$ You should definitely consider using screen density in your calculation as well. You can get it easily by using Gdx.graphics.getDensity() \$\endgroup\$ – nhydock Mar 6 '14 at 22:29
2
\$\begingroup\$

There are a few methods I used which worked in the past, each has their own pros and cons.

The first was to generate about 10 different sizes of fonts in Heiro as part of a single atlas. I then import each of them in a static Assets class which I use to access assets throughout my game. When I want to draw text of a certain size, I call a static method of my Assets class which takes the size I want to draw and gives me to closest BitmapFont from the atlas to the size that I want to draw.

Pros: Text looks pretty good, as long as you generate close to the sizes you need. You don't need to deal with gdx-freetype. Loading is fast, since it is just a single atlas (though it could be large). Cons: You the atlas can be very large. It takes some time to tweak the sizes you need in the atlas. When I add sizes to the atlas, I need to manually add them to the Assets class so they can be loaded (though you could possibly do something more clever to look through the region names and deduce what sizes are included).

The second is to use gdx-freetype to generate fonts as needed. In my Assets class I have a method which generates fonts of the size that is needed from a font file. I have a cache which I use to share previously-generated fonts for that size.

Pros: Text looks perfect on every size screen. Cons: Loading takes forever, which is especially bad if you use a font size for the first time in the middle of a game (huge lag). I generated font sizes at startup to hide this latency, but then I have a long loading screen to generate a bunch of fonts.

The third method I use is to use gdx-freetype to generate a small/medium/large font size scaled to the current display. I can then set scaling to get these the right size as-needed, and I can set texture filtering to make this not look bad. Since the fonts are scaled to the current screen, it looks pretty good across screen sizes.

Pros: Text looks pretty good. Doesn't take that long to load (only 2 or 3 fonts generated). Text scales with screen sizes gracefully. Cons: Text might not look perfect. Takes longer to load than a single atlas.

\$\endgroup\$
  • \$\begingroup\$ Third method is what I use in LibGDX. As long as you standardize your UI, it's no problem to conform everything to 2-4 font sizes for every on-screen element. \$\endgroup\$ – user39686 Mar 14 '14 at 20:18
  • \$\begingroup\$ Thanks for your response, but I think you missed the point of my question. :) Im looking for the formula you use to scale the fonts, based on the screen size. \$\endgroup\$ – you786 Mar 14 '14 at 21:49

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.