Introduction
BitmapFont. However, there is a downside:
- BitmapFonts rely on an image, so you have to scale them if you want a different size, which may look ugly.
You could just save a BitmapFont of the biggest size needed in your game then and you never have to scale up, just down, right? Well, that’s true, but such a BitmapFont can easily take up two times as much space on your hard drive as the corresponding TrueType Font (.ttf). Now imagine you have to ship all your big BitmapFonts with your game and your game uses ten different fonts… on an Android device. Bye bye, free storage!
gdx-freetypeextension: - -
LibGDX.info
Details
Since this is an extension, it is not included in your libGDX project by default. How you add the extension differs based on the setup of your project.How to put gdx-freetype in your project
For Projects Using Gradle setup UI. Dependency management with Gradle. HTML5 https://github.com/intrigus/gdx-freetype-gwt You’re ready to go.How to use gdx-freetype in code
Using the gdx-freetype extension in your code is really simple.
A much simpler way to display your font is by placing your font file in project’s assets folder. You will have to modify the first line of the above code and mention just your font file name in the parameters. FreeTypeFontParameter:FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/myfont.ttf"));FreeTypeFontParameter parameter = new FreeTypeFontParameter();parameter.size = 12;BitmapFont font12 = generator.generateFont(parameter); // font size 12 pixelsgenerator.dispose(); // don't forget to dispose to avoid memory leaks!
/** The size in pixels */public int size = 16;/** Foreground color (required for non-black borders) */public Color color = Color.WHITE;/** Border width in pixels, 0 to disable */public float borderWidth = 0;/** Border color; only used if borderWidth > 0 */public Color borderColor = Color.BLACK;/** true for straight (mitered), false for rounded borders */public boolean borderStraight = false;/** Offset of text shadow on X axis in pixels, 0 to disable */public int shadowOffsetX = 0;/** Offset of text shadow on Y axis in pixels, 0 to disable */public int shadowOffsetY = 0;/** Shadow color; only used if shadowOffset > 0 */public Color shadowColor = new Color(0, 0, 0, 0.75f);/** The characters the font should contain */public String characters = DEFAULT_CHARS;/** Whether the font should include kerning */public boolean kerning = true;/** The optional PixmapPacker to use */public PixmapPacker packer = null;/** Whether to flip the font vertically */public boolean flip = false;/** Whether or not to generate mip maps for the resulting texture */public boolean genMipMaps = false;/** Minification filter */public TextureFilter minFilter = TextureFilter.Nearest;/** Magnification filter */public TextureFilter magFilter = TextureFilter.Nearest;
FreeTypeFontGenerator.setMaxTextureSizeto set the default page size. Examples:parameter.borderColor = Color.BLACK;parameter.borderWidth = 3;
parameter.shadowColor = Color.BLACK;parameter.shadowOffsetX = 3;parameter.shadowOffsetY = 3;
BitmapFonts generated via the FreeType extension using AssetManager. See FreeTypeFontLoaderTestlatest info about caveats
https://web.archive.org/web/20201128081723/https://www.badlogicgames.com/wordpress/?p=2300: - -
example
