libgdx-bitmap-font-text — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited libgdx-bitmap-font-text (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
Quick reference for BitmapFont text rendering, GlyphLayout measurement, BitmapFontCache, NinePatch, DistanceFieldFont, and color markup. Covers com.badlogic.gdx.graphics.g2d.BitmapFont, GlyphLayout, BitmapFontCache, NinePatch, DistanceFieldFont.
Pre-rendered font stored as texture(s) + .fnt descriptor. Created with Hiero, BMFont, or at runtime via FreeType (see libgdx-freetype skill).
BitmapFont font = new BitmapFont(); // Built-in 15px Liberation SansUseful for prototyping only. The default font is bundled in the libGDX JAR.
new BitmapFont() // default 15px Liberation Sans
new BitmapFont(boolean flip) // default font, flipped
new BitmapFont(FileHandle fontFile) // load from .fnt
new BitmapFont(FileHandle fontFile, boolean flip)
new BitmapFont(FileHandle fontFile, FileHandle imageFile, boolean flip)
new BitmapFont(FileHandle fontFile, FileHandle imageFile, boolean flip, boolean integer)
new BitmapFont(FileHandle fontFile, TextureRegion region) // custom texture
new BitmapFont(FileHandle fontFile, TextureRegion region, boolean flip)
new BitmapFont(BitmapFontData data, TextureRegion region, boolean integer)
new BitmapFont(BitmapFontData data, Array<TextureRegion> pageRegions, boolean integer)true, Y=0 is top (top-down coordinates). Default false (Y-up, libGDX standard). Scene2D uses unflipped fonts.true, positions are rounded to integers to reduce filtering artifacts. Default true.The `y` parameter is the top of capital letters (cap height), NOT the baseline, NOT the bottom of text.
// All return GlyphLayout except the GlyphLayout overload which returns void
GlyphLayout draw(Batch batch, CharSequence str, float x, float y)
GlyphLayout draw(Batch batch, CharSequence str, float x, float y,
float targetWidth, int halign, boolean wrap)
GlyphLayout draw(Batch batch, CharSequence str, float x, float y,
int start, int end, float targetWidth, int halign, boolean wrap)
GlyphLayout draw(Batch batch, CharSequence str, float x, float y,
int start, int end, float targetWidth, int halign, boolean wrap, String truncate)
void draw(Batch batch, GlyphLayout layout, float x, float y) // pre-computed layoutx — left edge of text.y — top of most capital letters (cap height position). The GlyphLayout height is the distance from y down to the baseline.targetWidth — max width for wrapping/alignment/truncation.halign — Align.left, Align.center, or Align.right (lowercase! NOT Align.CENTER).wrap — word wrap within targetWidth.truncate — if non-null, truncate with this suffix (e.g., "...") instead of wrapping.Gotchas:
draw() must be called between batch.begin() / batch.end().draw() calls clear the internal cache. For static text drawn every frame, use BitmapFontCache instead.GlyphLayout is owned by the font's internal cache and will be overwritten on the next draw() call. Copy metrics immediately if needed.font.setColor(Color.RED); // affects subsequent draws
font.setColor(1, 0, 0, 1); // r, g, b, a
Color c = font.getColor(); // returns mutable Color — modify in place
font.getData().setScale(2f); // uniform scale (on BitmapFontData, NOT BitmapFont)
font.getData().setScale(2f, 3f); // scaleX, scaleY (throws if 0)
float sx = font.getScaleX(); // convenience getters on BitmapFont
float sy = font.getScaleY();DO NOT use `font.setScale()` — no such method exists on BitmapFont. Use font.getData().setScale().
DO NOT use `font.setSize()` — no such method exists. Scale is the only option, and it causes blurriness. For clean multi-size text, generate separate BitmapFonts at each size (or use FreeType at runtime).
All metric methods are on BitmapFont (convenience delegates to BitmapFontData):
font.getLineHeight() // distance between baselines of consecutive lines
font.getCapHeight() // top of capitals to baseline
font.getXHeight() // top of lowercase to baseline
font.getAscent() // cap height to top of tallest glyph
font.getDescent() // baseline to bottom of lowest descender (NEGATIVE value)
font.getSpaceXadvance() // width of space characterfont.getData() // returns BitmapFontData
font.getData().markupEnabled // public boolean field, default false
font.getRegions() // Array<TextureRegion> — texture pages
font.getRegion() // first TextureRegion (convenience)
font.getCache() // internal BitmapFontCache (expert use)
font.newFontCache() // creates new independent BitmapFontCache
font.setFixedWidthGlyphs("0123456789") // make digits fixed-width for score display
font.ownsTexture() // true if font created the texture (dispose will free it)
font.setOwnsTexture(boolean) // control texture ownership
font.dispose() // disposes texture(s) IF font owns themDO NOT use `font.getBounds()` — removed in libGDX 1.5.6. Use GlyphLayout for text measurement.
Computes text metrics without drawing. Implements Pool.Poolable.
new GlyphLayout()
new GlyphLayout(BitmapFont font, CharSequence str)
new GlyphLayout(BitmapFont font, CharSequence str, Color color,
float targetWidth, int halign, boolean wrap)
new GlyphLayout(BitmapFont font, CharSequence str, int start, int end, Color color,
float targetWidth, int halign, boolean wrap, String truncate)layout.setText(font, str)
layout.setText(font, str, color, targetWidth, halign, wrap)
layout.setText(font, str, start, end, color, targetWidth, halign, wrap, truncate)layout.width // actual rendered width (float)
layout.height // actual rendered height (float) — distance from y to baseline
layout.runs // Array<GlyphRun> — one per line segment
layout.glyphCount // total glyphsGlyphLayout layout = new GlyphLayout(font, "Hello");
float x = (screenWidth - layout.width) / 2;
float y = (screenHeight + layout.height) / 2; // y is TOP of text, so add height
font.draw(batch, layout, x, y);GlyphLayout layout = Pools.obtain(GlyphLayout.class);
layout.setText(font, "text");
// ... use layout.width, layout.height ...
Pools.free(layout); // calls reset(), returns to poolGotchas:
width and height are public fields, not methods. layout.width not layout.getWidth().setText() to avoid per-frame allocation.Pre-computes vertex data for static text. Avoids recomputing glyph positions every frame.
BitmapFontCache cache = new BitmapFontCache(font);
BitmapFontCache cache = new BitmapFontCache(font, boolean integer);
// Or get the font's internal cache:
BitmapFontCache cache = font.getCache();// setText clears then adds (returns GlyphLayout)
GlyphLayout setText(str, x, y)
GlyphLayout setText(str, x, y, targetWidth, halign, wrap)
GlyphLayout setText(str, x, y, start, end, targetWidth, halign, wrap)
GlyphLayout setText(str, x, y, start, end, targetWidth, halign, wrap, truncate)
void setText(GlyphLayout layout, x, y) // from pre-computed layout, returns VOID
// addText appends without clearing (returns GlyphLayout)
GlyphLayout addText(str, x, y)
GlyphLayout addText(str, x, y, targetWidth, halign, wrap)
GlyphLayout addText(str, x, y, start, end, targetWidth, halign, wrap)
GlyphLayout addText(str, x, y, start, end, targetWidth, halign, wrap, truncate)
void addText(GlyphLayout layout, x, y) // returns VOID
// Drawing
void draw(Batch batch) // Batch interface, not SpriteBatch
void draw(Batch batch, int start, int end) // character range
void draw(Batch batch, float alphaModulation) // alpha multiplier
// Positioning (without recomputing glyphs)
void setPosition(float x, float y)
void translate(float xAmount, float yAmount)
float getX()
float getY()
// Color
void setColor(Color color) // for FUTURE text
void setColor(float r, float g, float b, float a)
Color getColor() // color for future text
void setColors(Color tint) // overwrites ALL cached text to flat color
void setColors(float r, float g, float b, float a)
void tint(Color tint) // multiplies tint with each glyph's existing color (preserves markup colors)
void setAlphas(float alpha) // changes only alpha, preserves RGB
void clear()Key distinction: setColor()/getColor() affect subsequently added text. setColors() and tint() affect already cached text. tint() preserves per-glyph color variation from markup; setColors() overwrites to flat color.
Inline color tags in text strings. Color only — no size, bold, or italic markup.
font.getData().markupEnabled = true; // default is falseOr in Skin JSON:
{ "com.badlogic.gdx.graphics.g2d.BitmapFont": {
"default-font": { "file": "myfont.fnt", "markupEnabled": true }
}}| Tag | Effect |
|---|---|
[RED] | Named color (case-sensitive, ALL_CAPS for built-in) |
[#FF0000] | Hex RRGGBB (alpha defaults to FF) |
[#FF0000AA] | Hex RRGGBBAA with explicit alpha |
[] | Pop to previous color (stack-based) |
[[ | Escaped literal [ character |
font.draw(batch, "[RED]Warning:[] normal text", x, y);
font.draw(batch, "[#00FF00]green [#FF000080]translucent red", x, y);
font.draw(batch, "Array index [[0]", x, y); // displays: Array index [0]Colors class)CLEAR, CLEAR_WHITE, BLACK, WHITE, LIGHT_GRAY, GRAY, DARK_GRAY, BLUE, NAVY, ROYAL, SLATE, SKY, CYAN, TEAL, GREEN, CHARTREUSE, LIME, FOREST, OLIVE, YELLOW, GOLD, GOLDENROD, ORANGE, BROWN, TAN, FIREBRICK, RED, SCARLET, CORAL, SALMON, PINK, MAGENTA, PURPLE, VIOLET, MAROON
Colors.put("PERU", Color.valueOf("CD853F"));
// Then use: font.draw(batch, "[PERU]custom color[]", x, y);Gotchas:
fontColor from LabelStyle in skin JSON — it overrides markup colors.[] is a pop operation, not "reset to default." Excess pops are silently ignored.Scalable image preserving corners and edges. Used for UI backgrounds (buttons, panels).
new NinePatch(TextureRegion region, int left, int right, int top, int bottom) // border widths in px
new NinePatch(Texture texture, int left, int right, int top, int bottom)
new NinePatch(TextureRegion... patches) // 9 explicit regions (TOP_LEFT..BOTTOM_RIGHT)
new NinePatch(TextureRegion region) // degenerate: center-only, stretches uniformly
new NinePatch(Texture texture) // degenerate: center-only
new NinePatch(NinePatch ninePatch) // copy
new NinePatch(NinePatch ninePatch, Color color)Constructor border parameters are `int`, not float. Order is left, right, top, bottom.
ninePatch.draw(Batch batch, float x, float y, float width, float height)
ninePatch.draw(Batch batch, float x, float y, float originX, float originY,
float width, float height, float scaleX, float scaleY, float rotation)Parameter type is Batch (interface), not SpriteBatch.
ninePatch.getTotalWidth() // leftWidth + middleWidth + rightWidth (minimum width)
ninePatch.getTotalHeight() // topHeight + middleHeight + bottomHeight (minimum height)
ninePatch.getLeftWidth() // all six patch dimensions have getters AND setters
ninePatch.getRightWidth()
ninePatch.getTopHeight()
ninePatch.getBottomHeight()
ninePatch.getMiddleWidth()
ninePatch.getMiddleHeight()ninePatch.getPadLeft() // returns padLeft if set, else getLeftWidth()
ninePatch.getPadRight() // same pattern for right/top/bottom
ninePatch.setPadding(left, right, top, bottom)ninePatch.setColor(Color color) // tint (blended with Batch color at draw time)
ninePatch.getColor() // returns mutable Color, default WHITENinePatch patch = atlas.createPatch("panel"); // reads split/pad data from .atlas fileReturns null if region not found. Throws IllegalArgumentException if region has no split data. Cache the result — this method does string lookup + new allocation each call.
NinePatchDrawable drawable = new NinePatchDrawable(ninePatch);
drawable.setPatch(ninePatch); // also sets minWidth/minHeight/padding from patch
NinePatchDrawable tinted = drawable.tint(Color.GRAY); // returns NEW drawable with tinted copyResolution-independent text using signed distance field textures. Extends BitmapFont.
DistanceFieldFont font = new DistanceFieldFont(Gdx.files.internal("myfont.fnt"));Has 8 constructors mirroring most BitmapFont overloads (missing: no-arg default font and flip-only). Automatically sets TextureFilter.Linear on load.
ShaderProgram shader = DistanceFieldFont.createDistanceFieldShader(); // static method
batch.setShader(shader);
// ... draw distance field font ...
batch.setShader(null); // restore default shader for non-SDF renderingThe shader uses a u_smoothing uniform. The font's internal DistanceFieldFontCache sets this uniform automatically before/after each draw, flushing the batch as needed. This means you only need to set the shader on the batch — smoothing is handled internally.
font.setDistanceFieldSmoothing(float smoothing) // on DistanceFieldFont, NOT BitmapFont
font.getDistanceFieldSmoothing()Smoothing is automatically scaled by getScaleX() internally.
Use Hiero with the "Distance field" effect, or external tools like msdf-atlas-gen. The .fnt file format is standard BMFont; only the texture is different (stores distance values in alpha channel).
All lowercase. com.badlogic.gdx.utils.Align:
Align.center // 1 DO NOT use Align.CENTER (doesn't exist)
Align.top // 2
Align.bottom // 4
Align.left // 8
Align.right // 16
Align.topLeft // top | left
Align.topRight // top | right
Align.bottomLeft // bottom | left
Align.bottomRight // bottom | rightlibgdx-freetype skill): Generates BitmapFont at runtime from .ttf/.otf. The result IS a BitmapFont — all APIs here apply.libgdx-scene2d-ui skill): Label wraps BitmapFont. Skin loads BitmapFont from .fnt files and supports markupEnabled in JSON.gdx-assetmanager skill): Loads BitmapFont asynchronously. Do NOT manually dispose AssetManager-loaded fonts.y in draw() is the top of capital letters (cap height), not the baseline or bottom. To position text at a bottom y, use y + layout.height.GlyphLayout to measure text.font.getData().setScale(). Scaling causes blurriness — generate fonts at the needed size instead.Align.center, Align.left, Align.right.setText() or use BitmapFontCache for static text. GlyphLayout implements Poolable for pool-based reuse.[RED] if markup is not enabled.new BitmapFont(...).left, right, top, bottom (NOT left, top, right, bottom).void. The other draw() overloads return GlyphLayout.~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.