Package arc.freetype
Class FreeTypeFontGenerator
java.lang.Object
arc.freetype.FreeTypeFontGenerator
- All Implemented Interfaces:
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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Font.FontData
used for fonts generated via theFreeTypeFontGenerator
.static class
Parameter container class that helps configure howFreeTypeFontGenerator.FreeTypeFontData
andFont
instances are generated.class
static enum
Font smoothing algorithm. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
static final int
A hint to scale the texture as needed, without capping it at any maximum size -
Constructor Summary
ConstructorDescriptionFreeTypeFontGenerator
(Fi fontFile) FreeTypeFontGenerator
(Fi fontFile, int faceIndex) Creates a new generator from the given font file. -
Method Summary
Modifier and TypeMethodDescriptionvoid
dispose()
Cleans up all resources of the generator.generateData
(int size) Generates a newFont.FontData
instance, expert usage only.generateData
(FreeTypeFontGenerator.FreeTypeFontParameter parameter, FreeTypeFontGenerator.FreeTypeFontData data) Generates a newFont.FontData
instance, expert usage only.generateFont
(FreeTypeFontGenerator.FreeTypeFontParameter parameter, FreeTypeFontGenerator.FreeTypeFontData data) Generates a newFont
.generateGlyphAndBitmap
(int c, int size, boolean flip) Returns null if glyph was not found.static int
Returns the maximum texture size that will be used by generateData() when creating a texture atlas for the glyphs.int
scaleForPixelHeight
(int height) Uses ascender and descender of font to calculate real height that makes all glyphs to fit in given pixel size.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.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.static void
setMaxTextureSize
(int texSize) Sets the maximum size that will be used when generating texture atlases for glyphs with generateData().Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface arc.util.Disposable
isDisposed
-
Field Details
-
DEFAULT_CHARS
- See Also:
-
NO_MAXIMUM
public static final int NO_MAXIMUMA hint to scale the texture as needed, without capping it at any maximum size- See Also:
-
-
Constructor Details
-
FreeTypeFontGenerator
-
FreeTypeFontGenerator
Creates a new generator from the given font file. UsesFi.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 aArcRuntimeException
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 specifyingNO_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
-
generateFont
public Font generateFont(FreeTypeFontGenerator.FreeTypeFontParameter parameter, FreeTypeFontGenerator.FreeTypeFontData data) Generates a newFont
. 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
Returns null if glyph was not found. If there is nothing to render, for example with various space characters, then bitmap is null. -
generateData
Generates a newFont.FontData
instance, expert usage only. Throws a ArcRuntimeException if something went wrong.- Parameters:
size
- the size in pixels
-
generateData
public FreeTypeFontGenerator.FreeTypeFontData generateData(FreeTypeFontGenerator.FreeTypeFontParameter parameter) -
generateData
public FreeTypeFontGenerator.FreeTypeFontData generateData(FreeTypeFontGenerator.FreeTypeFontParameter parameter, FreeTypeFontGenerator.FreeTypeFontData data) Generates a newFont.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 interfaceDisposable
-