Putting Freetypefont in libgdx Shell

In my uiskin.json I have this

com.badlogic.gdx.graphics.g2d.BitmapFont: { default-font: { file: text/default.fnt } }

This is normal when I have default.fnt in my text folder in the resource folder ...

However, I want to use Freetypefont.

How to create Freetypefont and load it into uiskin file?

+7
java libgdx freetype scene2d
source share
3 answers

This cannot be done in advance, since you are going to generate it at run time.

My workaround would be to keep the default font as it is, and replace it at runtime with code. You can use Skin.add(...) for this.

 BitmapFont newDefaultFont = FreeTypeFontGenerator...; skin.add("default-font", newDefaultFont, BitmapFont.class); 
+3
source share

Update

Thanks to a comment from Jos van Egmond telling how to do this and still downloading Skin from AssetManager , I decided to update the answer, the Old answer (and an alternative way to do it) is still untouched below.

The new procedure will look like this:

  • Download font files.
  • Create an ObjectMap<String, Object> and place your fonts on this map.
  • Create a SkinParameter and give it an ObjectMap as a parameter.
  • Download Skin through the AsserManager , as usual, using the created SkinParameter .

The code might look something like this:

 /* Load your fonts */ load font1... load font2... /* Create the ObjectMap and add the fonts to it */ ObjectMap<String, Object> fontMap = new ObjectMap<String, Object>(); fontMap.put("font1", font1); fontMap.put("font2", font2); /* Create the SkinParameter and supply the ObjectMap to it */ SkinParameter parameter = new SkinParameter(fontMap); /* Load the skin as usual */ assetManager.load("file/location/of/your/skin", Skin.class, parameter); 

Old

The current accepted answer is actually (view) incorrect. This blog post describes how to link to the generated BitmapFont in your Json file. The procedure looks something like this:

  • Create an empty file using the default constructor.
  • Add your fonts to your skin.
  • Add your atlas to your skin.
  • Upload your Json file to your skin.

The code might look something like this:

 // Create a new skin using the default constructor and // add your fonts to it. Skin skin = new Skin(); skin.add("myFont", myFont, BitmapFont.class); // Remember that this texture atlas is NOT automatically disposed by the skin, // so keep a reference and dispose of it yourself. skin.addRegion(new TextureAtlas("file/location/of/your/atlas"); skin.load(Gdx.files.internal("file/location/of/your/skin")); 

Now you can do something similar in your Json skin:

 ..., font: "myFont", ... 

Although this is a workaround, it also means that you cannot (at least I did not find a way) load your skin through AssetManager . You can still download TextureAtlas through the manager.

+5
source share

Go to the .json file and delete the following code:

 com.badlogic.gdx.graphics.g2d.BitmapFont: {default-font: { file: default.fnt }}, 

For me, everything was on the same line at the very top of json. Remember to also remove the comma at the end.

0
source share

All Articles