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 enum
Specifies horizontal alignment for text.static enum
Specifies vertical alignment for text. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateLabel
(String content) Creates a newBitmapText
instance initialized with this font.float
getCharacterAdvance
(char curChar, char nextChar, float size) Retrieves the character set associated with this font.float
getLineWidth
(CharSequence text) Calculates the width of the given text in font units.getPage
(int index) Retrieves a specific font page material by its index.int
Returns the total number of font pages (materials) associated with this font.float
Returns the preferred size of the font, which is typically its rendered size.boolean
Checks if this font is configured for right-to-left (RTL) text rendering.void
merge
(BitmapFont newFont) Merges anotherBitmapFont
into this one.void
read
(JmeImporter im) void
setCharSet
(BitmapCharacterSet charSet) Sets the character set for this font.void
setGlyphParser
(GlyphParser glyphParser) For cursive fonts a GlyphParser needs to be specified which is used to determine glyph shape by the adjacent glyphs.void
Sets the array of materials (font pages) for this font.void
setRightToLeft
(boolean rightToLeft) Specifies whether this font should be rendered as right-to-left (RTL).void
setStyle
(int style) Sets the style for the font's character set.void
write
(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 newBitmapText
instance 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
BitmapText
instance.
-
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
- TheBitmapCharacterSet
to 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 ofMaterial
objects 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
Material
for 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
BitmapCharacterSet
of 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 size
is 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 anotherBitmapFont
into 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
- TheBitmapFont
to 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:
write
in interfaceSavable
- Throws:
IOException
-
read
- Specified by:
read
in interfaceSavable
- Throws:
IOException
-