TrueType Fonts in libGDX

39,585

Solution 1

Yes you will definitely need to add the gdx-stb-truetype jars to your project as you stated in your edit. Here is how you will use it, pretty straighforward...

First you need to declare your BitmapFont and the characters you will use...

BitmapFont font;
public static final String FONT_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789][_!$%#@|\\/?-+=()*&.;,{}\"´`'<>";

Then you need to create the font...

font = TrueTypeFontFactory.createBitmapFont(Gdx.files.internal("font.ttf"), FONT_CHARACTERS, 12.5f, 7.5f, 1.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
font.setColor(1f, 0f, 0f, 1f);

You can play with the arguments you pass to createBitmapFont() and you will see what they do.

Then to render the font you would do it as you normally do...

batch.begin();
font.draw(batch, "This is some text", 10, 10);
batch.end();

Solution 2

Use the gdx-freetype extension:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
BitmapFont font15 = generator.generateData(15);
BitmapFont font22 = generator.generateData(22);
generator.dispose();

"To use gdx-freetype, grab the latest nightlies, link gdx-freetype.jar and gdx-freetype-natives.jar to your desktop project, link gdx-freetype.jar to your Android project, and copy the armeabi/libgdx-freetype.so and armeabi-v7a/libgdx-freetype.so files to your Android project’s libs/ folder, just like with the libgdx.so files."

Source: http://www.badlogicgames.com/wordpress/?p=2300

Solution 3

Did researched a lot and found this working method:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("myfont.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 12; // font size
BitmapFont font12 = generator.generateFont(parameter);
generator.dispose(); // avoid memory leaks, important

Use this if you failed tried answers above, libGDX Freetype Wiki for reference.

Share:
39,585
Alex_Hyzer_Kenoyer
Author by

Alex_Hyzer_Kenoyer

I enjoy Discgolf and a plump Lobster.

Updated on September 10, 2020

Comments

  • Alex_Hyzer_Kenoyer
    Alex_Hyzer_Kenoyer over 3 years

    Does anyone know how I can use a TTF font in libGDX? I have looked around and have seen things about StbTrueTypeFont but it doesn't seem to be in the latest release.

    EDIT: I found the StbTrueType font stuff, the jar file is located in the extensions directory. I've added it to my project. Now I just need to figure out how to use it. Any examples?

  • uni
    uni almost 12 years
    How can I use this font with ui elements such as com.badlogic.gdx.scenes.scene2d.ui.Label? Or does it impossible?
  • TalkLittle
    TalkLittle over 11 years
    @uni As with any BitmapFont, you can apply to a Label by passing a LabelStyle in the Label constructor. In the LabelStyle you can specify a font. libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx‌​/…
  • kelorek
    kelorek almost 11 years
    Is it possible to set the color of a BitmapFont generated this way? I couldn't use .setColor(rgb) on it.
  • TalkLittle
    TalkLittle almost 11 years
  • Ashraf Sayied-Ahmad
    Ashraf Sayied-Ahmad over 10 years
    I do not recommend use it since it's not managed, then when your game resume all the text drawn using "this on-the-fly font" will be white.
  • Richard
    Richard over 10 years
    Just to help clarify, those nightlies can be downloaded here: libgdx.badlogicgames.com/nightlies/dist/extensions/gdx-freet‌​ype