20

How can I scale a Bitmapfont object in LibGDX? It seems the method setScale is no longer available.

Pedro Romano Barbosa
  • 587
  • 3
  • 11
  • 29

4 Answers4

38

Use bitmapFont.getData().setScale(float x, float y).

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
14

It is not recommended to scale Bitmap font, because it looks pixlated when enlarged(which I believe is ugly)

It is recommended to use freetype generator for resizing your fonts

If you still want use bitmapFont Tenfour04 is correct

Fish
  • 1,689
  • 1
  • 18
  • 28
  • 1
    Can I use a large enough font so that I can resize it with getData().setScale(...,...) without looking ugly? – Pedro Romano Barbosa Apr 28 '15 at 12:48
  • Sure, just use a larger font and make sure you load its texture with mip-mapping turned on and use the filters (MipMapLinearLinear, Linear). – Tenfour04 Apr 28 '15 at 15:02
  • I am using Freetype generator, but it seems like it does not automatically scales down when running on a small resolution screen. How can i scale down Freetype font according to screen resolution? – Vipul Behl Aug 15 '16 at 10:23
  • I'm using freetype generator, but as it creates a whole new texture, I'm getting lots of flushes called because it constantly switches between my one big TextureAtlas with all my images, and the font Texture. Is there anyway to combine these into one TextureAtlas, at run time? – Russ Wheeler May 03 '17 at 00:08
4

As Fish mentioned, it is recommended to use freetype generator as following:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/timesBold.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();

Then change the font size in parameter and initialize BitmapFont:

fontParameter.size = 18;
BitmapFont font = fontGenerator.generateFont(fontParameter);

It is also needed to include libgdx-freetype.so in armeabi folder of your project.

Winter
  • 3,894
  • 7
  • 24
  • 56
Nasser Tahani
  • 725
  • 12
  • 33
  • could you please clarify how to determine what ttf fonts are actually available & where they are located on the device? Are these gdx fonts or android system fonts? thx – rockhammer Dec 13 '16 at 18:06
  • You can find .ttf files in the font folder of your windows OS. Then copy your desired font family file and locate it in the asset folder of your Libgdx project. – Nasser Tahani Dec 14 '16 at 11:32
0

You can assign Font Size to FreeTypeFontParameter while loading font

 FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("Lobster.ttf"));
    FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
    parameter.size = 50; // font size
     BitmapFont lobster= generator.generateFont(parameter);
    generator.dispose();
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154