Class Font
- All Implemented Interfaces:
Disposable
- Direct Known Subclasses:
DistanceFieldFont
TextureRegion
containing the glyphs and a file in
the AngleCode BMFont text format that describes where each glyph is on the image.
Text is drawn using a Batch
. Text can be cached in a FontCache
for faster rendering of static text, which
saves needing to compute the location of each glyph each frame.
* The texture for a BitmapFont loaded from a file is managed. dispose()
must be called to free the texture when no
longer needed. A BitmapFont loaded using a TextureRegion
is managed if the region's texture is managed. Disposing the
BitmapFont disposes the region's texture, which may not be desirable if the texture is still being used elsewhere.
The code was originally based on Matthias Mann's TWL BitmapFont class. Thanks for sharing, Matthias! :)
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Backing data for aFont
.static class
Represents a single character in a font page. -
Constructor Summary
ConstructorDescriptionCreates a BitmapFont from a BMFont file.Creates a BitmapFont from a BMFont file.Creates a BitmapFont from a BMFont file, using the specified image for glyphs.Creates a BitmapFont from a BMFont file, using the specified image for glyphs.Font
(Fi fontFile, TextureRegion region) Creates a BitmapFont with the glyphs relative to the specified region.Font
(Fi fontFile, TextureRegion region, boolean flip) Creates a BitmapFont with the glyphs relative to the specified region.Font
(Font.FontData data, TextureRegion region, boolean integer) Constructs a new BitmapFont from the givenFont.FontData
andTextureRegion
.Font
(Font.FontData data, Seq<TextureRegion> pageRegions, boolean integer) Constructs a new BitmapFont from the givenFont.FontData
and array ofTextureRegion
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
dispose()
Disposes the texture used by this BitmapFont's region IF this BitmapFont created the texture.void
draw
(GlyphLayout layout, float x, float y) Draws text at the specified position.draw
(CharSequence str, float x, float y) Draws text at the specified position.draw
(CharSequence str, float x, float y, float targetWidth, int halign, boolean wrap) Draws text at the specified position.draw
(CharSequence str, float x, float y, int halign) draw
(CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap) Draws text at the specified position.draw
(CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap, String truncate) Draws text at the specified position.draw
(CharSequence str, float x, float y, Color color, float scale, boolean integer, int halign) float
Returns the ascent, which is the distance from the cap height to the top of the tallest glyph.getCache()
For expert usage -- returns the BitmapFontCache used by this font, for rendering to a sprite batch.float
Returns the cap height, which is the distance from the top of most uppercase characters to the baseline.getColor()
Returns the color of text drawn with this font.getData()
Gets the underlyingFont.FontData
for this BitmapFont.float
Returns the descent, which is the distance from the bottom of the glyph that extends the lowest to the baseline.float
Returns the line height, which is the distance from one line of text to the next.Returns the first texture region.getRegion
(int index) Returns the texture page at the given index.Returns the array of TextureRegions that represents each texture page of glyphs.float
float
float
Returns the x-advance of the space character.float
Returns the x-height, which is the distance from the top of most lowercase characters to the baseline.boolean
boolean
Returns true if this BitmapFont has been flipped for use with a y-down coordinate system.protected void
load
(Font.FontData data) Creates a new BitmapFontCache for this font.boolean
void
setColor
(float r, float g, float b, float a) A convenience method for setting the font color.void
A convenience method for setting the font color.void
setFixedWidthGlyphs
(CharSequence glyphs) Makes the specified glyphs fixed width.void
setOwnsTexture
(boolean ownsTexture) Sets whether the font owns the texture.void
setUseIntegerPositions
(boolean integer) Specifies whether to use integer positions.toString()
boolean
Checks whether this font uses integer positions for drawing.
-
Constructor Details
-
Font
Creates a BitmapFont with the glyphs relative to the specified region. If the region is null, the glyph textures are loaded from the image file given in the font file. Thedispose()
method will not dispose the region's texture in this case!The font data is not flipped.
- Parameters:
fontFile
- the font definition fileregion
- The texture region containing the glyphs. The glyphs must be relative to the lower left corner (ie, the region should not be flipped). If the region is null the glyph images are loaded from the image path in the font file.
-
Font
Creates a BitmapFont with the glyphs relative to the specified region. If the region is null, the glyph textures are loaded from the image file given in the font file. Thedispose()
method will not dispose the region's texture in this case!- Parameters:
region
- The texture region containing the glyphs. The glyphs must be relative to the lower left corner (ie, the region should not be flipped). If the region is null the glyph images are loaded from the image path in the font file.flip
- If true, the glyphs will be flipped for use with a perspective where 0,0 is the upper left corner.
-
Font
Creates a BitmapFont from a BMFont file. The image file name is read from the BMFont file and the image is loaded from the same directory. The font data is not flipped. -
Font
Creates a BitmapFont from a BMFont file. The image file name is read from the BMFont file and the image is loaded from the same directory.- Parameters:
flip
- If true, the glyphs will be flipped for use with a perspective where 0,0 is the upper left corner.
-
Font
Creates a BitmapFont from a BMFont file, using the specified image for glyphs. Any image specified in the BMFont file is ignored.- Parameters:
flip
- If true, the glyphs will be flipped for use with a perspective where 0,0 is the upper left corner.
-
Font
Creates a BitmapFont from a BMFont file, using the specified image for glyphs. Any image specified in the BMFont file is ignored.- Parameters:
flip
- If true, the glyphs will be flipped for use with a perspective where 0,0 is the upper left corner.integer
- If true, rendering positions will be at integer values to avoid filtering artifacts.
-
Font
Constructs a new BitmapFont from the givenFont.FontData
andTextureRegion
. If the TextureRegion is null, the image path(s) will be read from the BitmapFontData. The dispose() method will not dispose the texture of the region(s) if the region is != null.Passing a single TextureRegion assumes that your font only needs a single texture page. If you need to support multiple pages, either let the Font read the images themselves (by specifying null as the TextureRegion), or by specifying each page manually with the TextureRegion[] constructor.
- Parameters:
integer
- If true, rendering positions will be at integer values to avoid filtering artifacts.
-
Font
Constructs a new BitmapFont from the givenFont.FontData
and array ofTextureRegion
. If the TextureRegion is null or empty, the image path(s) will be read from the BitmapFontData. The dispose() method will not dispose the texture of the region(s) if the regions array is != null and not empty.- Parameters:
integer
- If true, rendering positions will be at integer values to avoid filtering artifacts.
-
-
Method Details
-
load
-
draw
Draws text at the specified position. -
draw
public GlyphLayout draw(CharSequence str, float x, float y, Color color, float scale, boolean integer, int halign) -
draw
-
draw
public GlyphLayout draw(CharSequence str, float x, float y, float targetWidth, int halign, boolean wrap) Draws text at the specified position. -
draw
public GlyphLayout draw(CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap) Draws text at the specified position. -
draw
public GlyphLayout draw(CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap, String truncate) Draws text at the specified position. -
draw
Draws text at the specified position. -
getColor
Returns the color of text drawn with this font. -
setColor
A convenience method for setting the font color. The color can also be set by modifyinggetColor()
. -
setColor
public void setColor(float r, float g, float b, float a) A convenience method for setting the font color. The color can also be set by modifyinggetColor()
. -
getScaleX
public float getScaleX() -
getScaleY
public float getScaleY() -
getRegion
Returns the first texture region. This is included for backwards compatibility, and for convenience since most fonts only use one texture page. For multi-page fonts, usegetRegions()
.- Returns:
- the first texture region
-
getRegions
Returns the array of TextureRegions that represents each texture page of glyphs.- Returns:
- the array of texture regions; modifying it may produce undesirable results
-
getRegion
Returns the texture page at the given index.- Returns:
- the texture page at the given index
-
getLineHeight
public float getLineHeight()Returns the line height, which is the distance from one line of text to the next. -
getSpaceXadvance
public float getSpaceXadvance()Returns the x-advance of the space character. -
getXHeight
public float getXHeight()Returns the x-height, which is the distance from the top of most lowercase characters to the baseline. -
getCapHeight
public float getCapHeight()Returns the cap height, which is the distance from the top of most uppercase characters to the baseline. Since the drawing position is the cap height of the first line, the cap height can be used to get the location of the baseline. -
getAscent
public float getAscent()Returns the ascent, which is the distance from the cap height to the top of the tallest glyph. -
getDescent
public float getDescent()Returns the descent, which is the distance from the bottom of the glyph that extends the lowest to the baseline. This number is negative. -
isFlipped
public boolean isFlipped()Returns true if this BitmapFont has been flipped for use with a y-down coordinate system. -
dispose
public void dispose()Disposes the texture used by this BitmapFont's region IF this BitmapFont created the texture.- Specified by:
dispose
in interfaceDisposable
-
isDisposed
public boolean isDisposed()- Specified by:
isDisposed
in interfaceDisposable
-
setFixedWidthGlyphs
Makes the specified glyphs fixed width. This can be useful to make the numbers in a font fixed width. Eg, when horizontally centering a score or loading percentage text, it will not jump around as different numbers are shown. -
setUseIntegerPositions
public void setUseIntegerPositions(boolean integer) Specifies whether to use integer positions. Default is to use them so filtering doesn't kick in as badly. -
usesIntegerPositions
public boolean usesIntegerPositions()Checks whether this font uses integer positions for drawing. -
getCache
For expert usage -- returns the BitmapFontCache used by this font, for rendering to a sprite batch. This can be used, for example, to manipulate glyph colors within a specific index.- Returns:
- the bitmap font cache used by this font
-
getData
Gets the underlyingFont.FontData
for this BitmapFont. -
ownsTexture
public boolean ownsTexture()- Returns:
- whether the texture is owned by the font, font disposes the texture itself if true
-
setOwnsTexture
public void setOwnsTexture(boolean ownsTexture) Sets whether the font owns the texture. In case it does, the font will also dispose of the texture whendispose()
is called. Use with care!- Parameters:
ownsTexture
- whether the font owns the texture
-
newFontCache
Creates a new BitmapFontCache for this font. Using this method allows the font to provide the BitmapFontCache implementation to customize rendering.Note this method is called by the BitmapFont constructors. If a subclass overrides this method, it will be called before the subclass constructors.
-
toString
-