public class BitmapFont extends Object implements Disposable
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 BitmapFontCache
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! :)
Modifier and Type | Class and Description |
---|---|
static class |
BitmapFont.BitmapFontData
Backing data for a
BitmapFont . |
static class |
BitmapFont.Glyph
Represents a single character in a font page.
|
Constructor and Description |
---|
BitmapFont()
Creates a BitmapFont using the default 15pt Arial font included in the libgdx JAR file.
|
BitmapFont(BitmapFont.BitmapFontData data,
Array<TextureRegion> pageRegions,
boolean integer)
Constructs a new BitmapFont from the given
BitmapFont.BitmapFontData and array of TextureRegion . |
BitmapFont(BitmapFont.BitmapFontData data,
TextureRegion region,
boolean integer)
Constructs a new BitmapFont from the given
BitmapFont.BitmapFontData and TextureRegion . |
BitmapFont(boolean flip)
Creates a BitmapFont using the default 15pt Arial font included in the libgdx JAR file.
|
BitmapFont(FileHandle fontFile)
Creates a BitmapFont from a BMFont file.
|
BitmapFont(FileHandle fontFile,
boolean flip)
Creates a BitmapFont from a BMFont file.
|
BitmapFont(FileHandle fontFile,
FileHandle imageFile,
boolean flip)
Creates a BitmapFont from a BMFont file, using the specified image for glyphs.
|
BitmapFont(FileHandle fontFile,
FileHandle imageFile,
boolean flip,
boolean integer)
Creates a BitmapFont from a BMFont file, using the specified image for glyphs.
|
BitmapFont(FileHandle fontFile,
TextureRegion region)
Creates a BitmapFont with the glyphs relative to the specified region.
|
BitmapFont(FileHandle fontFile,
TextureRegion region,
boolean flip)
Creates a BitmapFont with the glyphs relative to the specified region.
|
Modifier and Type | Method and Description |
---|---|
void |
dispose()
Disposes the texture used by this BitmapFont's region IF this BitmapFont created the texture.
|
GlyphLayout |
draw(Batch batch,
CharSequence str,
float x,
float y)
Draws text at the specified position.
|
GlyphLayout |
draw(Batch batch,
CharSequence str,
float x,
float y,
float targetWidth,
int halign,
boolean wrap)
Draws text at the specified position.
|
GlyphLayout |
draw(Batch batch,
CharSequence str,
float x,
float y,
int start,
int end,
float targetWidth,
int halign,
boolean wrap)
Draws text at the specified position.
|
GlyphLayout |
draw(Batch batch,
CharSequence str,
float x,
float y,
int start,
int end,
float targetWidth,
int halign,
boolean wrap,
String truncate)
Draws text at the specified position.
|
void |
draw(Batch batch,
GlyphLayout layout,
float x,
float y)
Draws text at the specified position.
|
float |
getAscent()
Returns the ascent, which is the distance from the cap height to the top of the tallest glyph.
|
BitmapFontCache |
getCache()
For expert usage -- returns the BitmapFontCache used by this font, for rendering to a sprite batch.
|
float |
getCapHeight()
Returns the cap height, which is the distance from the top of most uppercase characters to the baseline.
|
Color |
getColor()
Returns the color of text drawn with this font.
|
BitmapFont.BitmapFontData |
getData()
Gets the underlying
BitmapFont.BitmapFontData for this BitmapFont. |
float |
getDescent()
Returns the descent, which is the distance from the bottom of the glyph that extends the lowest to the baseline.
|
float |
getLineHeight()
Returns the line height, which is the distance from one line of text to the next.
|
TextureRegion |
getRegion()
Returns the first texture region.
|
TextureRegion |
getRegion(int index)
Returns the texture page at the given index.
|
Array<TextureRegion> |
getRegions()
Returns the array of TextureRegions that represents each texture page of glyphs.
|
float |
getScaleX() |
float |
getScaleY() |
float |
getSpaceWidth()
Returns the width of the space character.
|
float |
getXHeight()
Returns the x-height, which is the distance from the top of most lowercase characters to the baseline.
|
boolean |
isFlipped()
Returns true if this BitmapFont has been flipped for use with a y-down coordinate system.
|
BitmapFontCache |
newFontCache()
Creates a new BitmapFontCache for this font.
|
boolean |
ownsTexture() |
void |
setColor(Color color)
A convenience method for setting the font color.
|
void |
setColor(float r,
float g,
float b,
float a)
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.
|
String |
toString() |
boolean |
usesIntegerPositions()
Checks whether this font uses integer positions for drawing.
|
public BitmapFont()
public BitmapFont(boolean flip)
flip
- If true, the glyphs will be flipped for use with a perspective where 0,0 is the upper left corner.public BitmapFont(FileHandle fontFile, TextureRegion region)
dispose()
method will not dispose the region's texture in this
case!
The font data is not flipped.
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.public BitmapFont(FileHandle fontFile, TextureRegion region, boolean flip)
dispose()
method will not dispose the region's texture in this
case!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.public BitmapFont(FileHandle fontFile)
public BitmapFont(FileHandle fontFile, boolean flip)
flip
- If true, the glyphs will be flipped for use with a perspective where 0,0 is the upper left corner.public BitmapFont(FileHandle fontFile, FileHandle imageFile, boolean flip)
flip
- If true, the glyphs will be flipped for use with a perspective where 0,0 is the upper left corner.public BitmapFont(FileHandle fontFile, FileHandle imageFile, boolean flip, boolean integer)
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.public BitmapFont(BitmapFont.BitmapFontData data, TextureRegion region, boolean integer)
BitmapFont.BitmapFontData
and TextureRegion
. 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.
integer
- If true, rendering positions will be at integer values to avoid filtering artifacts.public BitmapFont(BitmapFont.BitmapFontData data, Array<TextureRegion> pageRegions, boolean integer)
BitmapFont.BitmapFontData
and array of TextureRegion
. 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.integer
- If true, rendering positions will be at integer values to avoid filtering artifacts.public GlyphLayout draw(Batch batch, CharSequence str, float x, float y)
public GlyphLayout draw(Batch batch, CharSequence str, float x, float y, float targetWidth, int halign, boolean wrap)
public GlyphLayout draw(Batch batch, CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap)
public GlyphLayout draw(Batch batch, CharSequence str, float x, float y, int start, int end, float targetWidth, int halign, boolean wrap, String truncate)
public void draw(Batch batch, GlyphLayout layout, float x, float y)
public Color getColor()
public void setColor(Color color)
getColor()
.public void setColor(float r, float g, float b, float a)
getColor()
.public float getScaleX()
public float getScaleY()
public TextureRegion getRegion()
getRegions()
.public Array<TextureRegion> getRegions()
public TextureRegion getRegion(int index)
public float getLineHeight()
public float getSpaceWidth()
public float getXHeight()
public float getCapHeight()
public float getAscent()
public float getDescent()
public boolean isFlipped()
public void dispose()
dispose
in interface Disposable
public void setFixedWidthGlyphs(CharSequence glyphs)
public void setUseIntegerPositions(boolean integer)
public boolean usesIntegerPositions()
public BitmapFontCache getCache()
public BitmapFont.BitmapFontData getData()
BitmapFont.BitmapFontData
for this BitmapFont.public boolean ownsTexture()
public void setOwnsTexture(boolean ownsTexture)
dispose()
is called. Use with care!ownsTexture
- whether the font owns the texturepublic BitmapFontCache newFontCache()
Note this method is called by the BitmapFont constructors. If a subclass overrides this method, it will be called before the subclass constructors.
Copyright © 2017. All rights reserved.