Package com.jme3.font

Class BitmapFont

java.lang.Object
com.jme3.font.BitmapFont
All Implemented Interfaces:
Savable

public class BitmapFont extends Object implements 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.
  • Constructor Details

    • BitmapFont

      public BitmapFont()
      Creates a new instance of `BitmapFont`. This constructor is primarily used for deserialization.
  • Method Details

    • createLabel

      public BitmapText createLabel(String content)
      Creates a new BitmapText 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

      public void setCharSet(BitmapCharacterSet charSet)
      Sets the character set for this font. The character set contains information about individual glyphs, their positions, and kerning data.
      Parameters:
      charSet - The BitmapCharacterSet to associate with this font.
    • setPages

      public void setPages(Material[] pages)
      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 of Material objects representing the font pages.
    • getPage

      public Material getPage(int index)
      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

      public BitmapCharacterSet getCharSet()
      Retrieves the character set associated with this font.
      Returns:
      The BitmapCharacterSet of this font.
    • setGlyphParser

      public void setGlyphParser(GlyphParser glyphParser)
      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

      public GlyphParser 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

      public float getLineWidth(CharSequence text)
      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 by BitmapText.

      Parameters:
      text - The text to measure.
      Returns:
      The maximum line width of the text in font units.
    • merge

      public void merge(BitmapFont newFont)
      Merges another BitmapFont 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 - The BitmapFont 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

      public void write(JmeExporter ex) throws IOException
      Specified by:
      write in interface Savable
      Throws:
      IOException
    • read

      public void read(JmeImporter im) throws IOException
      Specified by:
      read in interface Savable
      Throws:
      IOException