APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.graphics.g2d

public Class BitmapFontCache

extends Object
Class Inheritance
Known Direct Subclasses
com.badlogic.gdx.graphics.g2d.DistanceFieldFont.DistanceFieldFontCache
Imports
com.badlogic.gdx.graphics.Color, com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData, .BitmapFont.Glyph, .GlyphLayout.GlyphRun, com.badlogic.gdx.utils.Align, .Array, .FlushablePool, .IntArray, .NumberUtils, java.util.Arrays

Caches glyph geometry for a BitmapFont, providing a fast way to render static text. This saves needing to compute the glyph geometry each frame.
Authors
Nathan Sweet, davebaol, Alexander Dorokhov

Field Summary

Modifier and TypeField and Description
private final Color
private float
private final BitmapFont
private int
private int[]
idx

Number of vertex data entries per page.

private boolean
private final Array<GlyphLayout>
private IntArray[]
pageGlyphIndices

For each page, an array with a value for each glyph from that page, where the value is the index of the character in the full text being cached.

private float[][]
pageVertices

Vertex data per page.

private final FlushablePool<GlyphLayout>
private static final Color
private int[]
tempGlyphCount

Used internally to ensure a correct capacity for multi-page font vertex data.

private float
x

private float
y

Constructor Summary

AccessConstructor and Description
public
public
BitmapFontCache(BitmapFont font, boolean
If true, rendering positions will be at integer values to avoid filtering artifacts.
integer
)

Method Summary

Modifier and TypeMethod and Description
private void
addGlyph(BitmapFont.Glyph glyph, float x, float y, float color)

public GlyphLayout
addText(CharSequence str, float x, float y)

Adds glyphs for the specified text.

public GlyphLayout
addText(CharSequence str, float x, float y, float targetWidth, int halign, boolean wrap)

Adds glyphs for the specified text.

public GlyphLayout
addText(CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap)

Adds glyphs for the specified text.

public GlyphLayout

Returns:

The glyph layout for the cached string (the layout's height is the distance from y to the baseline).
addText
(CharSequence str, float
The x position for the left most character.
x
,
float
The y position for the top of most capital letters in the font (the cap height).
y
,
int
The first character of the string to draw.
start
,
int
The last character of the string to draw (exclusive).
end
,
float
The width of the area the text will be drawn, for wrapping or truncation.
targetWidth
,
int
Horizontal alignment of the text, see Align.
halign
,
boolean
If true, the text will be wrapped within targetWidth.
wrap
,
String
If not null, the text will be truncated within targetWidth with this string appended. May be an empty string.
truncate
)

Adds glyphs for the the specified text.

public void
addText(GlyphLayout
The cache keeps the layout until cleared or new text is set. The layout should not be modified before then.
layout
,
float x, float y)

Adds the specified glyphs.

private void
addToCache(GlyphLayout layout, float x, float y)

public void
clear()

Removes all glyphs in the cache.

public void
draw(Batch spriteBatch)

public void
draw(Batch spriteBatch, int start, int end)

public void
draw(Batch spriteBatch, float alphaModulation)

public Color
getColor()

Returns the color used for subsequently added text.

public BitmapFont
public Array<GlyphLayout>
public int
public int
getVertexCount(int page)

public float[]
public float[]
getVertices(int page)

public float
getX()

Returns the x position of the cached string, relative to the position when the string was cached.

public float
getY()

Returns the y position of the cached string, relative to the position when the string was cached.

private void
private void
requirePageGlyphs(int page, int glyphCount)

public void
setAlphas(float alpha)

Sets the alpha component of all text currently in the cache.

public void
setColor(Color color)

A convenience method for setting the cache color.

public void
setColor(float r, float g, float b, float a)

A convenience method for setting the cache color.

public void
setColors(float color)

Sets the color of all text currently in the cache.

public void
setColors(Color tint)

Sets the color of all text currently in the cache.

public void
setColors(float r, float g, float b, float a)

Sets the color of all text currently in the cache.

public void
setColors(Color tint, int start, int end)

Sets the color of the specified characters.

public void
setColors(float color, int start, int end)

Sets the color of the specified characters.

private void
setPageCount(int pageCount)

public void
setPosition(float
The x coordinate
x
,
float
The y coordinate
y
)

Sets the position of the text, relative to the position when the cached text was created.

public GlyphLayout
setText(CharSequence str, float x, float y)

Clears any cached glyphs and adds glyphs for the specified text.

public GlyphLayout
setText(CharSequence str, float x, float y, float targetWidth, int halign, boolean wrap)

Clears any cached glyphs and adds glyphs for the specified text.

public GlyphLayout
setText(CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap)

Clears any cached glyphs and adds glyphs for the specified text.

public GlyphLayout
setText(CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap, String truncate)

Clears any cached glyphs and adds glyphs for the specified text.

public void
setText(GlyphLayout layout, float x, float y)

Clears any cached glyphs and adds the specified glyphs.

public void
setUseIntegerPositions(boolean use)

Specifies whether to use integer positions or not.

public void
tint(Color tint)

Tints all text currently in the cache.

public void
translate(float
The amount in x to move the text
xAmount
,
float
The amount in y to move the text
yAmount
)

Sets the position of the text, relative to its current position.

public boolean
usesIntegerPositions()

Returns: whether this font uses integer positions for drawing.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

colorback to summary
private final Color color
currentTintback to summary
private float currentTint
fontback to summary
private final BitmapFont font
glyphCountback to summary
private int glyphCount
idxback to summary
private int[] idx

Number of vertex data entries per page.

integerback to summary
private boolean integer
layoutsback to summary
private final Array<GlyphLayout> layouts
pageGlyphIndicesback to summary
private IntArray[] pageGlyphIndices

For each page, an array with a value for each glyph from that page, where the value is the index of the character in the full text being cached.

pageVerticesback to summary
private float[][] pageVertices

Vertex data per page.

pooledLayoutsback to summary
private final FlushablePool<GlyphLayout> pooledLayouts
tempColorback to summary
private static final Color tempColor
tempGlyphCountback to summary
private int[] tempGlyphCount

Used internally to ensure a correct capacity for multi-page font vertex data.

xback to summary
private float x
yback to summary
private float y

Constructor Detail

BitmapFontCacheback to summary
public BitmapFontCache(BitmapFont font)
BitmapFontCacheback to summary
public BitmapFontCache(BitmapFont font, boolean integer)
Parameters
integer:boolean

If true, rendering positions will be at integer values to avoid filtering artifacts.

Method Detail

addGlyphback to summary
private void addGlyph(BitmapFont.Glyph glyph, float x, float y, float color)
addTextback to summary
public GlyphLayout addText(CharSequence str, float x, float y)

Adds glyphs for the specified text.

See Also
addText(CharSequence, float, float, int, int, float, int, boolean, String)
addTextback to summary
public GlyphLayout addText(CharSequence str, float x, float y, float targetWidth, int halign, boolean wrap)

Adds glyphs for the specified text.

See Also
addText(CharSequence, float, float, int, int, float, int, boolean, String)
addTextback to summary
public GlyphLayout addText(CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap)

Adds glyphs for the specified text.

See Also
addText(CharSequence, float, float, int, int, float, int, boolean, String)
addTextback to summary
public GlyphLayout addText(CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap, String truncate)

Adds glyphs for the the specified text.

Parameters
x:float

The x position for the left most character.

y:float

The y position for the top of most capital letters in the font (the cap height).

start:int

The first character of the string to draw.

end:int

The last character of the string to draw (exclusive).

targetWidth:float

The width of the area the text will be drawn, for wrapping or truncation.

halign:int

Horizontal alignment of the text, see Align.

wrap:boolean

If true, the text will be wrapped within targetWidth.

truncate:String

If not null, the text will be truncated within targetWidth with this string appended. May be an empty string.

Returns:GlyphLayout

The glyph layout for the cached string (the layout's height is the distance from y to the baseline).

addTextback to summary
public void addText(GlyphLayout layout, float x, float y)

Adds the specified glyphs.

Parameters
layout:GlyphLayout

The cache keeps the layout until cleared or new text is set. The layout should not be modified before then.

addToCacheback to summary
private void addToCache(GlyphLayout layout, float x, float y)
clearback to summary
public void clear()

Removes all glyphs in the cache.

drawback to summary
public void draw(Batch spriteBatch)
drawback to summary
public void draw(Batch spriteBatch, int start, int end)
drawback to summary
public void draw(Batch spriteBatch, float alphaModulation)
getColorback to summary
public Color getColor()

Returns the color used for subsequently added text. Modifying the color affects text subsequently added to the cache, but does not affect existing text currently in the cache.

getFontback to summary
public BitmapFont getFont()
getLayoutsback to summary
public Array<GlyphLayout> getLayouts()
getPageCountback to summary
public int getPageCount()
getVertexCountback to summary
public int getVertexCount(int page)
getVerticesback to summary
public float[] getVertices()
getVerticesback to summary
public float[] getVertices(int page)
getXback to summary
public float getX()

Returns the x position of the cached string, relative to the position when the string was cached.

getYback to summary
public float getY()

Returns the y position of the cached string, relative to the position when the string was cached.

requireGlyphsback to summary
private void requireGlyphs(GlyphLayout layout)
requirePageGlyphsback to summary
private void requirePageGlyphs(int page, int glyphCount)
setAlphasback to summary
public void setAlphas(float alpha)

Sets the alpha component of all text currently in the cache. Does not affect subsequently added text.

setColorback to summary
public void setColor(Color color)

A convenience method for setting the cache color. The color can also be set by modifying getColor().

setColorback to summary
public void setColor(float r, float g, float b, float a)

A convenience method for setting the cache color. The color can also be set by modifying getColor().

setColorsback to summary
public void setColors(float color)

Sets the color of all text currently in the cache. Does not affect subsequently added text.

setColorsback to summary
public void setColors(Color tint)

Sets the color of all text currently in the cache. Does not affect subsequently added text.

setColorsback to summary
public void setColors(float r, float g, float b, float a)

Sets the color of all text currently in the cache. Does not affect subsequently added text.

setColorsback to summary
public void setColors(Color tint, int start, int end)

Sets the color of the specified characters. This may only be called after setText(CharSequence, float, float) and is reset every time setText is called.

setColorsback to summary
public void setColors(float color, int start, int end)

Sets the color of the specified characters. This may only be called after setText(CharSequence, float, float) and is reset every time setText is called.

setPageCountback to summary
private void setPageCount(int pageCount)
setPositionback to summary
public void setPosition(float x, float y)

Sets the position of the text, relative to the position when the cached text was created.

Parameters
x:float

The x coordinate

y:float

The y coordinate

setTextback to summary
public GlyphLayout setText(CharSequence str, float x, float y)

Clears any cached glyphs and adds glyphs for the specified text.

See Also
addText(CharSequence, float, float, int, int, float, int, boolean, String)
setTextback to summary
public GlyphLayout setText(CharSequence str, float x, float y, float targetWidth, int halign, boolean wrap)

Clears any cached glyphs and adds glyphs for the specified text.

See Also
addText(CharSequence, float, float, int, int, float, int, boolean, String)
setTextback to summary
public GlyphLayout setText(CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap)

Clears any cached glyphs and adds glyphs for the specified text.

See Also
addText(CharSequence, float, float, int, int, float, int, boolean, String)
setTextback to summary
public GlyphLayout setText(CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap, String truncate)

Clears any cached glyphs and adds glyphs for the specified text.

See Also
addText(CharSequence, float, float, int, int, float, int, boolean, String)
setTextback to summary
public void setText(GlyphLayout layout, float x, float y)

Clears any cached glyphs and adds the specified glyphs.

See Also
addText(CharSequence, float, float, int, int, float, int, boolean, String)
setUseIntegerPositionsback to summary
public void setUseIntegerPositions(boolean use)

Specifies whether to use integer positions or not. Default is to use them so filtering doesn't kick in as badly.

tintback to summary
public void tint(Color tint)

Tints all text currently in the cache. Does not affect subsequently added text.

translateback to summary
public void translate(float xAmount, float yAmount)

Sets the position of the text, relative to its current position.

Parameters
xAmount:float

The amount in x to move the text

yAmount:float

The amount in y to move the text

usesIntegerPositionsback to summary
public boolean usesIntegerPositions()
Returns:boolean

whether this font uses integer positions for drawing.