Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
src
README.md
build.gradle
gradle.properties

README.md

KTX: FreeType font utilities

A tiny modules that makes it easier to use gdx-freetype library.

Why?

gdx-freetype requires quite a bit of setup before it can be fully integrated with LibGDX AssetManager. This module aims to limit the boilerplate necessary to load FreeType fonts in LibGDX applications.

Guide

This module consists of the following functions:

  • Extension method AssetManager.registerFreeTypeFontLoaders allows to register all loaders required to load FreeType font assets. It should be called right after constructing a AssetManager instance and before loading any assets.
  • Extension method AssetManager.loadFreeTypeFont allows to easily configure loaded BitmapFont instances with Kotlin DSL.
  • freeTypeFontParameters function is a Kotlin DSL for customizing font loading parameters.
  • FreeTypeFontGenerator.generateFont extension function allows to generate BitmapFont instances using a FreeTypeFontGenerator with Kotlin DSL.

Usage examples

Creating AssetManager with registered FreeType font loaders:

import com.badlogic.gdx.assets.AssetManager
import ktx.freetype.*

fun initiateAssetManager(): AssetManager {
  val assetManager = AssetManager()
  // Calling registerFreeTypeFontLoaders is necessary in order to load TTF/OTF files.
  assetManager.registerFreeTypeFontLoaders()
  return assetManager
}

Registering BitmapFont loaders for custom file extensions:

import ktx.freetype.*

assetManager.registerFreeTypeFontLoaders(fileExtensions = arrayOf(".custom"))

Replacing default BitmapFont loader with FreeType font loader:

import ktx.freetype.*

assetManager.registerFreeTypeFontLoaders(replaceDefaultBitmapFontLoader = true)

Loading a FreeType font using AssetManager:

import ktx.freetype.*

assetManager.loadFreeTypeFont("font.ttf")

Loading a FreeType font with custom parameters using AssetManager:

import ktx.freetype.*

assetManager.loadFreeTypeFont("font.ttf") {
  size = 14
  borderWidth = 1.5f
  color = Color.ORANGE
  borderColor = Color.BLUE
}

Accessing a fully loaded font (note: AssetManager must finish loading the asset first):

import com.badlogic.gdx.graphics.g2d.BitmapFont

val font = assetManager.get<BitmapFont>("font.ttf")

Using delegation to schedule loading of a FreeType font:

import ktx.assets.getValue
import ktx.freetype.loadFreeTypeFont

val font: BitmapFont by assetManager.loadFreeTypeFont("font.ttf")

// `font` variable can be accessed once the asset is fully loaded. See ktx-assets README.

Loading FreeTypeFontGenerator with ktx-assets:

import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator
import ktx.assets.load

assetManager.load<FreeTypeFontGenerator>("font.tff")

Creating a FreeTypeFontLoaderParameter with customized font parameters:

import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader.FreeTypeFontLoaderParameter
import ktx.freetype.*

fun getFontParameters(): FreeTypeFontLoaderParameter = freeTypeFontParameters("font.ttf") {
  size = 14
  borderWidth = 1.5f
  color = Color.ORANGE
  borderColor = Color.BLUE
}

Generating a new BitmapFont using LibGDX FreeTypeFontGenerator:

import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator
import ktx.freetype.*

val generator: FreeTypeFontGenerator = getGenerator()
// Default parameters:
val fontA = generator.generateFont()
// Customized:
val fontB = generator.generateFont {
  size = 42
}

Alternatives

FreeType font loaders can be registered manually. See this article.

Additional documentation