Package com.jme3.font
Class BitmapFont
java.lang.Object
com.jme3.font.BitmapFont
- All Implemented Interfaces:
Savable
Represents a font loaded from a bitmap font definition
(e.g., generated by AngelCode Bitmap Font Generator).
It manages character sets, font pages (textures), and provides utilities for text measurement and rendering.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumSpecifies horizontal alignment for text.static enumSpecifies vertical alignment for text. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateLabel(String content) Creates a newBitmapTextinstance initialized with this font.floatgetCharacterAdvance(char curChar, char nextChar, float size) Retrieves the character set associated with this font.floatgetLineWidth(CharSequence text) Calculates the width of the given text in font units.getPage(int index) Retrieves a specific font page material by its index.intReturns the total number of font pages (materials) associated with this font.floatReturns the preferred size of the font, which is typically its rendered size.booleanChecks if this font is configured for right-to-left (RTL) text rendering.voidmerge(BitmapFont newFont) Merges anotherBitmapFontinto this one.voidread(JmeImporter im) voidsetCharSet(BitmapCharacterSet charSet) Sets the character set for this font.voidsetGlyphParser(GlyphParser glyphParser) For cursive fonts a GlyphParser needs to be specified which is used to determine glyph shape by the adjacent glyphs.voidSets the array of materials (font pages) for this font.voidsetRightToLeft(boolean rightToLeft) Specifies whether this font should be rendered as right-to-left (RTL).voidsetStyle(int style) Sets the style for the font's character set.voidwrite(JmeExporter ex)
-
Constructor Details
-
BitmapFont
public BitmapFont()Creates a new instance of `BitmapFont`. This constructor is primarily used for deserialization.
-
-
Method Details
-
createLabel
Creates a newBitmapTextinstance initialized with this font. The label's size will be set to the font's rendered size, and its text content will be set to the provided string.- Parameters:
content- The initial text content for the label.- Returns:
- A new
BitmapTextinstance.
-
isRightToLeft
public boolean isRightToLeft()Checks if this font is configured for right-to-left (RTL) text rendering.- Returns:
- true if this is a right-to-left font, otherwise false (default is left-to-right).
-
setRightToLeft
public void setRightToLeft(boolean rightToLeft) Specifies whether this font should be rendered as right-to-left (RTL). By default, it is set to false (left-to-right).- Parameters:
rightToLeft- true to enable right-to-left rendering; false for left-to-right.
-
getPreferredSize
public float getPreferredSize()Returns the preferred size of the font, which is typically its rendered size.- Returns:
- The preferred size of the font in font units.
-
setCharSet
Sets the character set for this font. The character set contains information about individual glyphs, their positions, and kerning data.- Parameters:
charSet- TheBitmapCharacterSetto associate with this font.
-
setPages
Sets the array of materials (font pages) for this font. Each material corresponds to a texture page containing character bitmaps. The character set's page size is also updated based on the number of pages.- Parameters:
pages- An array ofMaterialobjects representing the font pages.
-
getPage
Retrieves a specific font page material by its index.- Parameters:
index- The index of the font page to retrieve.- Returns:
- The
Materialfor the specified font page. - Throws:
IndexOutOfBoundsException- if the index is out of bounds.
-
getPageSize
public int getPageSize()Returns the total number of font pages (materials) associated with this font.- Returns:
- The number of font pages.
-
getCharSet
Retrieves the character set associated with this font.- Returns:
- The
BitmapCharacterSetof this font.
-
setGlyphParser
For cursive fonts a GlyphParser needs to be specified which is used to determine glyph shape by the adjacent glyphs. If nothing is set, all glyphs will be rendered isolated.- Parameters:
glyphParser- the desired parser (alias created) or null for none (default=null)
-
getGlyphParser
- Returns:
- The GlyphParser set on the font, or null if it has no glyph parser.
-
getCharacterAdvance
public float getCharacterAdvance(char curChar, char nextChar, float size) -
getLineWidth
Calculates the width of the given text in font units. This method accounts for character advances, kerning, and line breaks. It also attempts to skip custom color tags (e.g., "\#RRGGBB#" or "\#RRGGBBAA#") based on a specific format.Note: This method calculates width in "font units" where the font's
rendered sizeis the base. Actual pixel scaling for display is typically handled byBitmapText.- Parameters:
text- The text to measure.- Returns:
- The maximum line width of the text in font units.
-
merge
Merges anotherBitmapFontinto this one. This operation combines the character sets and font pages. If both fonts contain the same style, the merge will fail and throw a RuntimeException.- Parameters:
newFont- TheBitmapFontto merge into this one. It must have a style assigned.
-
setStyle
public void setStyle(int style) Sets the style for the font's character set. This method is typically used when a font file contains only one style but needs to be assigned a specific style identifier for merging with other multi-style fonts.- Parameters:
style- The integer style identifier to set.
-
write
- Specified by:
writein interfaceSavable- Throws:
IOException
-
read
- Specified by:
readin interfaceSavable- Throws:
IOException
-