Package arc.freetype

Class FreeTypeFontGenerator

java.lang.Object
arc.freetype.FreeTypeFontGenerator
All Implemented Interfaces:
Disposable

public class FreeTypeFontGenerator extends Object implements Disposable
Generates Font and Font.FontData instances from TrueType, OTF, and other FreeType supported fonts.

Usage example:

 FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Core.files.internal("myfont.ttf"));
 BitmapFont font = gen.generateFont(16);
 gen.dispose(); // Don't dispose if doing incremental glyph generation.
 

The generator has to be disposed once it is no longer used. The returned Font instances are managed by the user and have to be disposed as usual.

  • Field Details

    • DEFAULT_CHARS

      public static final String DEFAULT_CHARS
      See Also:
    • NO_MAXIMUM

      public static final int NO_MAXIMUM
      A hint to scale the texture as needed, without capping it at any maximum size
      See Also:
  • Constructor Details

    • FreeTypeFontGenerator

      public FreeTypeFontGenerator(Fi fontFile)
    • FreeTypeFontGenerator

      public FreeTypeFontGenerator(Fi fontFile, int faceIndex)
      Creates a new generator from the given font file. Uses Fi.length() to determine the file size. If the file length could not be determined (it was 0), an extra copy of the font bytes is performed. Throws a ArcRuntimeException if loading did not succeed.
  • Method Details

    • getMaxTextureSize

      public static int getMaxTextureSize()
      Returns the maximum texture size that will be used by generateData() when creating a texture atlas for the glyphs.
      Returns:
      the power-of-two max texture size
    • setMaxTextureSize

      public static void setMaxTextureSize(int texSize)
      Sets the maximum size that will be used when generating texture atlases for glyphs with generateData(). The default is 1024. By specifying NO_MAXIMUM, the texture atlas will scale as needed.

      The power-of-two square texture size will be capped to the given texSize. It's recommended that a power-of-two value be used here.

      Multiple pages may be used to fit all the generated glyphs. You can query the resulting number of pages by calling bitmapFont.getRegions().length or freeTypeBitmapFontData.getTextureRegions().length.

      If PixmapPacker is specified when calling generateData, this parameter is ignored.

      Parameters:
      texSize - the maximum texture size for one page of glyphs
    • generateFont

      public Font generateFont(FreeTypeFontGenerator.FreeTypeFontParameter parameter)
    • generateFont

      Generates a new Font. The size is expressed in pixels. Throws a ArcRuntimeException if the font could not be generated. Using big sizes might cause such an exception.
      Parameters:
      parameter - configures how the font is generated
    • scaleForPixelHeight

      public int scaleForPixelHeight(int height)
      Uses ascender and descender of font to calculate real height that makes all glyphs to fit in given pixel size. Source: http://nothings.org/stb/stb_truetype.h / stbtt_ScaleForPixelHeight
    • scaleForPixelWidth

      public int scaleForPixelWidth(int width, int numChars)
      Uses max advance, ascender and descender of font to calculate real height that makes any n glyphs to fit in given pixel width.
      Parameters:
      width - the max width to fit (in pixels)
      numChars - max number of characters that to fill width
    • scaleToFitSquare

      public int scaleToFitSquare(int width, int height, int numChars)
      Uses max advance, ascender and descender of font to calculate real height that makes any n glyphs to fit in given pixel width and height.
      Parameters:
      width - the max width to fit (in pixels)
      height - the max height to fit (in pixels)
      numChars - max number of characters that to fill width
    • generateGlyphAndBitmap

      public FreeTypeFontGenerator.GlyphAndBitmap generateGlyphAndBitmap(int c, int size, boolean flip)
      Returns null if glyph was not found. If there is nothing to render, for example with various space characters, then bitmap is null.
    • generateData

      public FreeTypeFontGenerator.FreeTypeFontData generateData(int size)
      Generates a new Font.FontData instance, expert usage only. Throws a ArcRuntimeException if something went wrong.
      Parameters:
      size - the size in pixels
    • generateData

    • generateData

      Generates a new Font.FontData instance, expert usage only. Throws a ArcRuntimeException if something went wrong.
      Parameters:
      parameter - configures how the font is generated
    • dispose

      public void dispose()
      Cleans up all resources of the generator. Call this if you no longer use the generator.
      Specified by:
      dispose in interface Disposable