TrueType Fonts in libGDX
Asked Answered
K

5

43

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?

Kronfeld answered 28/2, 2012 at 17:9 Comment(5)
where can i get this library file? i've looked everywhere . the only places i've found (that didn't work ) are: code.google.com/p/libgdx/source/browse/trunk/extensions/… code.google.com/p/libgdx/source/browse/trunk/extensions/…Cule
Go here: libgdx.badlogicgames.com/nightlies and download the "libgdx-nightly-latest.zip" you will find the jars in the extensions directory.Kronfeld
which jars do i need to use ? i've tried to add "gdx-stb-truetype.jar" , but it keeps carshing and giving me exceptions , for example " javax.media.opengl.GLException: java.lang.NoClass DefFoundError: com/badlogic/gdx/utils/SharedLibraryLoader ..."Cule
You need gdx-stb-truetype-natives.jar and gdx-stb-truetype.jar. Add those both to your main project and make sure to add them to your build path.Kronfeld
still have the same error . i've even tried to use the other newest libraries that the desktop project wants but it didn't work(no error, but i can't see the text and some other things) : gdx-backend-jogl-natives.jar gdx-backend-jogl.jar gdx-natives.jar gdx-sources.jar gdx-stb-truetype-natives.jar gdx-stb-truetype.jar gdx.jarCule
V
42

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();
Vexillum answered 28/2, 2012 at 17:41 Comment(2)
How can I use this font with ui elements such as com.badlogic.gdx.scenes.scene2d.ui.Label? Or does it impossible?Condyloma
@Condyloma 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/…Ketene
K
28

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

Ketene answered 27/12, 2012 at 21:14 Comment(4)
Is it possible to set the color of a BitmapFont generated this way? I couldn't use .setColor(rgb) on it.Niobous
@Niobous Tried setColor(r, g, b, a)? libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/graphics/g2d/…Ketene
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.Kremenchug
Just to help clarify, those nightlies can be downloaded here: libgdx.badlogicgames.com/nightlies/dist/extensions/gdx-freetypeLacy
K
11

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.

Keldah answered 7/9, 2014 at 2:3 Comment(0)
T
3

This is working on an S4 clone phone: Pinewood is a downloaded font, in assets folder. See the folder structure below.

import android.util.Log;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;

public class Game implements ApplicationListener {
    private SpriteBatch mSpriteBatch;
    private Texture textureBackground;
    private BitmapFont mBitmapFont;

    public void create() {

        Gdx.graphics.setContinuousRendering(false);
        Gdx.graphics.requestRendering();

        mSpriteBatch = new SpriteBatch();

        Texture.setEnforcePotImages(false);
        textureBackground = new Texture(Gdx.files.internal("background.jpg"));

        FileHandle fontFile = Gdx.files.internal("Pinewood.ttf");
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);

        mBitmapFont = generator.generateFont(150);
        generator.dispose();

        mBitmapFont.setColor(0.9f, 0.5f, 0.5f, 1); 
        Log.e("Game", "mBitmapFont.getScaleX() : "+mBitmapFont.getScaleX() + ", mBitmapFont.getScaleY() "+mBitmapFont.getScaleY());

    }

    public void render() {
        Log.e("Game", "render()");

        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
        mSpriteBatch.begin();
        // Drawing goes here!
        mSpriteBatch.draw(textureBackground, 0, 0);
        mBitmapFont.draw(mSpriteBatch, "FPS:"+Gdx.graphics.getFramesPerSecond(), 110, 260);
        mSpriteBatch.end();     
    }

    public void resize(int width, int height) {
    }

    public void pause() {
    }

    public void resume() {
    }

    public void dispose() {
    }

}

enter image description here

Thrice answered 5/11, 2013 at 4:2 Comment(0)
C
0

you have to use FreeTypeFont library. github

1.as the github page say, for projects using gradle add this dependencies:

Core Dependency:

compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"

Desktop Dependency:

compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"

Android Dependency:

compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"

for more details and platforms see this page

if you don't use gradle see this

2.after library added to your project you can use it like this:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("myfont.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 12;
BitmapFont font12 = generator.generateFont(parameter); // font size 12 pixels
generator.dispose(); // don't forget to dispose to avoid memory leaks!

and put myfont.ttf in core assets folder

Converter answered 4/7, 2019 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.