Glyph Layouts

  Edit on GitHub

These docs are for 0.13.0-SNAPSHOT, an unreleased SNAPSHOT. See the latest release.

Glyph Layout

Internally, a FontCache uses a GlyphLayout. The GlyphLayout handles are the measuring of the text as well as wrapping the text. We can create our own GlyphLayout to measure text if needed. The lines of text are then stored in a GlyphRun which holds the glyphs of the specified font for that line of text. This does not handle any sort of drawing or caching of data.

val font: BitmapFont = resourcesVfs["arial_32.fnt"].readBitmapFont()
val layout = GlyphLayout()
layout.setText(font, "My text", color = Color.WHITE, width = 0f, scaleX = 1f, scaleY = 1f, align = HAlign.LEFT, wrap = false)

println("width: ${layout.width} - height: ${layout.height}")

// iterate through the runs of the layout and print out each character
layout.runs.forEach { run ->
    val x = run.x
    val y = run.y
    run.glyphs.forEeach { glyph ->
        print(glyph.code.toChar())
    }
    println()
}

Spacing and Justify

letterSpacing and lineSpacing add extra pixels between glyphs and lines. They aren’t scaled by scaleX/scaleY. justify stretches wrapped lines to width by widening their spaces.

layout.setText(font, "My long text", width = 200f, wrap = true, letterSpacing = 1f, lineSpacing = 4f, justify = true)

The same parameters are on FontCache/TextureFontCache setText and addText, and BitmapFont.draw.