Package com.github.tommyettinger.textra
Class Font
java.lang.Object
com.github.tommyettinger.textra.Font
- All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable
A replacement for libGDX's BitmapFont class, supporting additional markup to allow styling text with various effects.
This includes a "faux bold" and oblique mode using one font image; you don't need a bold and italic/oblique image
separate from the book face.
A Font represents either one size of a "standard" bitmap font (which can be drawn scaled up or down), or many sizes of a distance field font (using either the commonly-used SDF format or newer MSDF format). The same class is used for standard, SDF, and MSDF fonts, but you call
This interacts with the
A Font represents either one size of a "standard" bitmap font (which can be drawn scaled up or down), or many sizes of a distance field font (using either the commonly-used SDF format or newer MSDF format). The same class is used for standard, SDF, and MSDF fonts, but you call
enableShader(Batch) before rendering with SDF or MSDF fonts, and
can switch back to a normal SpriteBatch shader with batch.setShader(null);. You don't have to use SDF or MSDF
fonts, but they scale more cleanly. You can generate SDF fonts with
Hiero or [a related
tool](https://github.com/libgdx/libgdx/wiki/Distance-field-fonts#using-distance-fields-for-arbitrary-images) that is
part of libGDX; MSDF fonts are harder to generate, but possible using a tool like
Glamer.
This interacts with the
Layout class, with a Layout referencing a Font, and various methods in Font taking
a Layout. You usually want to have a Layout for any text you draw repeatedly, and draw that Layout each frame with
drawGlyphs(Batch, Layout, float, float, int) or a similar method.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDefines what types of distance field font this can use and render.static classDescribes the region of a glyph in a larger TextureRegion, carrying a little more info about the offsets that apply to where the glyph is rendered. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic longfloatRefers to the largest height of any glyph in the font, after scaling.floatOnly actually refers to a "cell" whenisMonois true; otherwise refers to the largest width of any glyph in the font, after scaling.Determines how colors are looked up by name; defaults to usingColors.floatWhendistanceFieldisFont.DistanceFieldType.SDForFont.DistanceFieldType.MSDF, this determines how much the edges of the glyphs should be aliased sharply (higher values) or anti-aliased softly (lower values).booleancom.badlogic.gdx.utils.IntIntMapcom.badlogic.gdx.utils.IntMap<Font.GlyphRegion>static longstatic StringFragment shader source meant for MSDF fonts.static longfloatRefers to the largest height of any glyph in the font, before any scaling.floatOnly actually refers to a "cell" whenisMonois true; otherwise refers to the largest width of any glyph in the font, before any scaling.com.badlogic.gdx.utils.Array<com.badlogic.gdx.graphics.g2d.TextureRegion>floatScale multiplier for width.floatScale multiplier for height.com.badlogic.gdx.graphics.glutils.ShaderProgramThe ShaderProgram used to render this font, as used byenableShader(Batch).static longstatic longstatic longstatic longstatic StringThe standard libGDX vertex shader source, which is also used by the MSDF shader. -
Constructor Summary
ConstructorsConstructorDescriptionFont(com.badlogic.gdx.graphics.g2d.BitmapFont bmFont, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a new Font from the existing BitmapFont, using its same Textures and TextureRegions for glyphs, and without a distance field effect.Font(com.badlogic.gdx.graphics.g2d.BitmapFont bmFont, Font.DistanceFieldType distanceField, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a new Font from the existing BitmapFont, using its same Textures and TextureRegions for glyphs, and with the specified distance field effect.Copy constructor; does not copy the font'sshaderorcolorLookup, if it has them (it uses the same reference for the new Font), but will fully copy everything else.Constructs a Font by reading in the given .fnt file and loading any images it specifies.Constructs a new Font by reading in a .fnt file with the given name (an internal handle is tried first, then a classpath handle) and loading any images specified in that file.Font(String fntName, com.badlogic.gdx.graphics.g2d.TextureRegion textureRegion, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a font using the given TextureRegion that holds all of its glyphs, with no distance field effect.Font(String fntName, com.badlogic.gdx.graphics.g2d.TextureRegion textureRegion, Font.DistanceFieldType distanceField, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a font based off of an AngelCode BMFont .fnt file and the given TextureRegion that holds all of its glyphs, with the specified distance field effect.Font(String fntName, com.badlogic.gdx.utils.Array<com.badlogic.gdx.graphics.g2d.TextureRegion> textureRegions, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a font based off of an AngelCode BMFont .fnt file and the given TextureRegion Array, with no distance field effect.Font(String fntName, com.badlogic.gdx.utils.Array<com.badlogic.gdx.graphics.g2d.TextureRegion> textureRegions, Font.DistanceFieldType distanceField, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a font based off of an AngelCode BMFont .fnt file, with the given TextureRegion Array and specified distance field effect.Font(String fntName, Font.DistanceFieldType distanceField)Constructs a Font by reading in the given .fnt file and loading any images it specifies.Font(String fntName, Font.DistanceFieldType distanceField, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a new Font by reading in a .fnt file with the given name (an internal handle is tried first, then a classpath handle) and loading any images specified in that file.Constructs a Font by reading in the given .fnt file and the given Texture by filename.Font(String fntName, String textureName, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a new Font by reading in a Texture from the given named path (internal is tried, then classpath), and no distance field effect.Font(String fntName, String textureName, Font.DistanceFieldType distanceField)Constructs a Font by reading in the given .fnt file and the given Texture by filename.Font(String fntName, String textureName, Font.DistanceFieldType distanceField, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a new Font by reading in a Texture from the given named path (internal is tried, then classpath), and the specified distance field effect. -
Method Summary
Modifier and TypeMethodDescriptionadjustLineHeight(float multiplier)Multiplies the line height bymultiplierwithout changing the size of any characters.voiddispose()Releases all resources of this object.voiddrawBlocks(com.badlogic.gdx.graphics.g2d.Batch batch, char blockChar, int[][] colors, float x, float y)Draws a grid made of rectangular blocks of int colors (typically RGBA) at the given x,y position in world space.voiddrawBlocks(com.badlogic.gdx.graphics.g2d.Batch batch, int[][] colors, float x, float y)Draws a grid made of rectangular blocks of int colors (typically RGBA) at the given x,y position in world space.floatdrawGlyph(com.badlogic.gdx.graphics.g2d.Batch batch, long glyph, float x, float y)Draws the specified glyph with a Batch at the given x, y position.floatdrawGlyph(com.badlogic.gdx.graphics.g2d.Batch batch, long glyph, float x, float y, float rotation)Draws the specified glyph with a Batch at the given x, y position and with the specified counterclockwise rotation, measured in degrees.floatdrawGlyphs(com.badlogic.gdx.graphics.g2d.Batch batch, Layout glyphs, float x, float y)Draws the specified Layout of glyphs with a Batch at a given x, y position, drawing the full layout.floatdrawGlyphs(com.badlogic.gdx.graphics.g2d.Batch batch, Layout glyphs, float x, float y, int align)Draws the specified Layout of glyphs with a Batch at a given x, y position, usingalignto determine how to position the text.floatdrawGlyphs(com.badlogic.gdx.graphics.g2d.Batch batch, Line glyphs, float x, float y)Draws the specified Line of glyphs with a Batch at a given x, y position, drawing the full Line using left alignment.floatdrawGlyphs(com.badlogic.gdx.graphics.g2d.Batch batch, Line glyphs, float x, float y, int align)Draws the specified Line of glyphs with a Batch at a given x, y position, usingalignto determine how to position the text.intdrawMarkupText(com.badlogic.gdx.graphics.g2d.Batch batch, String text, float x, float y)Draws the specified text at the given x,y position (in world space), parsing an extension of libGDX markup and using it to determine color, size, position, shape, strikethrough, underline, and case of the given CharSequence.voiddrawText(com.badlogic.gdx.graphics.g2d.Batch batch, CharSequence text, float x, float y)Draws the specified text at the given x,y position (in world space) with a white foreground.voiddrawText(com.badlogic.gdx.graphics.g2d.Batch batch, CharSequence text, float x, float y, int color)Draws the specified text at the given x,y position (in world space) with the given foreground color.voidenableShader(com.badlogic.gdx.graphics.g2d.Batch batch)Must be called before drawing anything with an SDF or MSDF font; does not need to be called for other fonts unless you are mixing them with SDF/MSDF fonts or other shaders.Gets the ColorLookup this uses to look up colors by name.static booleanisLowerCase(char c)Returns true ifcis a lower-case letter, or false otherwise.static booleanisUpperCase(char c)Returns true ifcis an upper-case letter, or false otherwise.intkerningPair(char first, char second)Assembles two chars into a kerning pair that can be looked up as a key inkerning.protected voidThe gritty parsing code that pulls relevant info from an AngelCode BMFont .fnt file and uses it to assemble the manyTextureRegions this has for each glyph.floatmeasureWidth(Line line)Measures the actual width that the given Line will use when drawn.regenerateLayout(Layout changing)scale(float horizontal, float vertical)Scales the font by the given horizontal and vertical multipliers.scaleTo(float width, float height)Scales the font so that it will have the given width and height.voidsetColorLookup(ColorLookup lookup)Unlikely to be used in most games, this allows changing how colors are looked up by name (or built) given aColorLookupinterface implementation.CallssetTextureFilter(Texture.TextureFilter, Texture.TextureFilter)withTexture.TextureFilter.Linearfor both min and mag filters.setTextureFilter(com.badlogic.gdx.graphics.Texture.TextureFilter minFilter, com.badlogic.gdx.graphics.Texture.TextureFilter magFilter)Sets the texture filters on each Texture that holds a TextureRegion used by the font to the givenminFilterandmagFilter.floatxAdvance(long glyph)Gets the distance to advance the cursor after drawingglyph, scaled byscaleXas if drawing.
-
Field Details
-
mapping
-
defaultValue
-
parents
public com.badlogic.gdx.utils.Array<com.badlogic.gdx.graphics.g2d.TextureRegion> parents -
distanceField
-
isMono
public boolean isMono -
kerning
public com.badlogic.gdx.utils.IntIntMap kerning -
distanceFieldCrispness
public float distanceFieldCrispnessWhendistanceFieldisFont.DistanceFieldType.SDForFont.DistanceFieldType.MSDF, this determines how much the edges of the glyphs should be aliased sharply (higher values) or anti-aliased softly (lower values). The default value is 1. -
cellWidth
public float cellWidthOnly actually refers to a "cell" whenisMonois true; otherwise refers to the largest width of any glyph in the font, after scaling. -
cellHeight
public float cellHeightRefers to the largest height of any glyph in the font, after scaling. -
originalCellWidth
public float originalCellWidthOnly actually refers to a "cell" whenisMonois true; otherwise refers to the largest width of any glyph in the font, before any scaling. -
originalCellHeight
public float originalCellHeightRefers to the largest height of any glyph in the font, before any scaling. -
scaleX
public float scaleXScale multiplier for width. -
scaleY
public float scaleYScale multiplier for height. -
colorLookup
Determines how colors are looked up by name; defaults to usingColors. -
BOLD
public static final long BOLD- See Also:
- Constant Field Values
-
OBLIQUE
public static final long OBLIQUE- See Also:
- Constant Field Values
-
UNDERLINE
public static final long UNDERLINE- See Also:
- Constant Field Values
-
STRIKETHROUGH
public static final long STRIKETHROUGH- See Also:
- Constant Field Values
-
SUBSCRIPT
public static final long SUBSCRIPT- See Also:
- Constant Field Values
-
MIDSCRIPT
public static final long MIDSCRIPT- See Also:
- Constant Field Values
-
SUPERSCRIPT
public static final long SUPERSCRIPT- See Also:
- Constant Field Values
-
vertexShader
The standard libGDX vertex shader source, which is also used by the MSDF shader.- See Also:
- Constant Field Values
-
msdfFragmentShader
Fragment shader source meant for MSDF fonts. This is automatically used whenenableShader(Batch)is called and thedistanceFieldisFont.DistanceFieldType.MSDF.- See Also:
- Constant Field Values
-
shader
public com.badlogic.gdx.graphics.glutils.ShaderProgram shaderThe ShaderProgram used to render this font, as used byenableShader(Batch). If this is null, the font will be rendered with the Batch's default shader. It may be set to a custom ShaderProgram ifdistanceFieldis set toFont.DistanceFieldType.MSDF, or to one created byDistanceFieldFont.createDistanceFieldShader()if distanceField is set toFont.DistanceFieldType.SDF. It can be set to a user-defined ShaderProgram; if it is meant to render MSDF or SDF fonts, then the ShaderProgram should have auniform float u_smoothing;that will be set byenableShader(Batch). Values passed to u_smoothing can vary a lot, depending on how the font was initially created, its current scale, and itsdistanceFieldCrispnessfield. You can also use a user-defined ShaderProgram with a font usingFont.DistanceFieldType.STANDARD, which may be easier and can use any uniforms you normally could with a ShaderProgram, since enableShader() won't change any of the uniforms.
-
-
Constructor Details
-
Font
Constructs a Font by reading in the given .fnt file and loading any images it specifies. Tries an internal handle first, then a local handle. Does not use a distance field effect.- Parameters:
fntName- the file path and name to a .fnt file this will load
-
Font
Constructs a Font by reading in the given .fnt file and loading any images it specifies. Tries an internal handle first, then a local handle. Uses the specified distance field effect.- Parameters:
fntName- the file path and name to a .fnt file this will loaddistanceField- determines how edges are drawn; if unsure, you should useFont.DistanceFieldType.STANDARD
-
Font
Constructs a Font by reading in the given .fnt file and the given Texture by filename. Tries an internal handle first, then a local handle. Does not use a distance field effect.- Parameters:
fntName- the file path and name to a .fnt file this will load
-
Font
Constructs a Font by reading in the given .fnt file and the given Texture by filename. Tries an internal handle first, then a local handle. Uses the specified distance field effect.- Parameters:
fntName- the file path and name to a .fnt file this will loaddistanceField- determines how edges are drawn; if unsure, you should useFont.DistanceFieldType.STANDARD
-
Font
Copy constructor; does not copy the font'sshaderorcolorLookup, if it has them (it uses the same reference for the new Font), but will fully copy everything else.- Parameters:
toCopy- another Font to copy
-
Font
Constructs a new Font by reading in a .fnt file with the given name (an internal handle is tried first, then a classpath handle) and loading any images specified in that file. No distance field effect is used.- Parameters:
fntName- the path and filename of a .fnt file this will load; may be internal or localxAdjust- how many pixels to offset each character's x-position by, moving to the rightyAdjust- how many pixels to offset each character's y-position by, moving upwidthAdjust- how many pixels to add to the used width of each character, using more to the rightheightAdjust- how many pixels to add to the used height of each character, using more above
-
Font
public Font(String fntName, Font.DistanceFieldType distanceField, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a new Font by reading in a .fnt file with the given name (an internal handle is tried first, then a classpath handle) and loading any images specified in that file. The specified distance field effect is used.- Parameters:
fntName- the path and filename of a .fnt file this will load; may be internal or localdistanceField- determines how edges are drawn; if unsure, you should useFont.DistanceFieldType.STANDARDxAdjust- how many pixels to offset each character's x-position by, moving to the rightyAdjust- how many pixels to offset each character's y-position by, moving upwidthAdjust- how many pixels to add to the used width of each character, using more to the rightheightAdjust- how many pixels to add to the used height of each character, using more above
-
Font
public Font(String fntName, String textureName, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a new Font by reading in a Texture from the given named path (internal is tried, then classpath), and no distance field effect.- Parameters:
fntName- the path and filename of a .fnt file this will load; may be internal or localtextureName- the path and filename of a texture file this will load; may be internal or localxAdjust- how many pixels to offset each character's x-position by, moving to the rightyAdjust- how many pixels to offset each character's y-position by, moving upwidthAdjust- how many pixels to add to the used width of each character, using more to the rightheightAdjust- how many pixels to add to the used height of each character, using more above
-
Font
public Font(String fntName, String textureName, Font.DistanceFieldType distanceField, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a new Font by reading in a Texture from the given named path (internal is tried, then classpath), and the specified distance field effect.- Parameters:
fntName- the path and filename of a .fnt file this will load; may be internal or localtextureName- the path and filename of a texture file this will load; may be internal or localdistanceField- determines how edges are drawn; if unsure, you should useFont.DistanceFieldType.STANDARDxAdjust- how many pixels to offset each character's x-position by, moving to the rightyAdjust- how many pixels to offset each character's y-position by, moving upwidthAdjust- how many pixels to add to the used width of each character, using more to the rightheightAdjust- how many pixels to add to the used height of each character, using more above
-
Font
public Font(String fntName, com.badlogic.gdx.graphics.g2d.TextureRegion textureRegion, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a font using the given TextureRegion that holds all of its glyphs, with no distance field effect.- Parameters:
fntName- the path and filename of a .fnt file this will load; may be internal or localtextureRegion- an existing TextureRegion, typically inside a larger TextureAtlasxAdjust- how many pixels to offset each character's x-position by, moving to the rightyAdjust- how many pixels to offset each character's y-position by, moving upwidthAdjust- how many pixels to add to the used width of each character, using more to the rightheightAdjust- how many pixels to add to the used height of each character, using more above
-
Font
public Font(String fntName, com.badlogic.gdx.graphics.g2d.TextureRegion textureRegion, Font.DistanceFieldType distanceField, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a font based off of an AngelCode BMFont .fnt file and the given TextureRegion that holds all of its glyphs, with the specified distance field effect.- Parameters:
fntName- the path and filename of a .fnt file this will load; may be internal or localtextureRegion- an existing TextureRegion, typically inside a larger TextureAtlasdistanceField- determines how edges are drawn; if unsure, you should useFont.DistanceFieldType.STANDARDxAdjust- how many pixels to offset each character's x-position by, moving to the rightyAdjust- how many pixels to offset each character's y-position by, moving upwidthAdjust- how many pixels to add to the used width of each character, using more to the rightheightAdjust- how many pixels to add to the used height of each character, using more above
-
Font
public Font(String fntName, com.badlogic.gdx.utils.Array<com.badlogic.gdx.graphics.g2d.TextureRegion> textureRegions, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a font based off of an AngelCode BMFont .fnt file and the given TextureRegion Array, with no distance field effect.- Parameters:
fntName- the path and filename of a .fnt file this will load; may be internal or localtextureRegions- an Array of TextureRegions that will be used in order as the .fnt file uses more pagesxAdjust- how many pixels to offset each character's x-position by, moving to the rightyAdjust- how many pixels to offset each character's y-position by, moving upwidthAdjust- how many pixels to add to the used width of each character, using more to the rightheightAdjust- how many pixels to add to the used height of each character, using more above
-
Font
public Font(String fntName, com.badlogic.gdx.utils.Array<com.badlogic.gdx.graphics.g2d.TextureRegion> textureRegions, Font.DistanceFieldType distanceField, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a font based off of an AngelCode BMFont .fnt file, with the given TextureRegion Array and specified distance field effect.- Parameters:
fntName- the path and filename of a .fnt file this will load; may be internal or localtextureRegions- an Array of TextureRegions that will be used in order as the .fnt file uses more pagesdistanceField- determines how edges are drawn; if unsure, you should useFont.DistanceFieldType.STANDARDxAdjust- how many pixels to offset each character's x-position by, moving to the rightyAdjust- how many pixels to offset each character's y-position by, moving upwidthAdjust- how many pixels to add to the used width of each character, using more to the rightheightAdjust- how many pixels to add to the used height of each character, using more above
-
Font
public Font(com.badlogic.gdx.graphics.g2d.BitmapFont bmFont, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a new Font from the existing BitmapFont, using its same Textures and TextureRegions for glyphs, and without a distance field effect.- Parameters:
bmFont- an existing BitmapFont that will be copied in almost every way this canxAdjust- how many pixels to offset each character's x-position by, moving to the rightyAdjust- how many pixels to offset each character's y-position by, moving upwidthAdjust- how many pixels to add to the used width of each character, using more to the rightheightAdjust- how many pixels to add to the used height of each character, using more above
-
Font
public Font(com.badlogic.gdx.graphics.g2d.BitmapFont bmFont, Font.DistanceFieldType distanceField, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)Constructs a new Font from the existing BitmapFont, using its same Textures and TextureRegions for glyphs, and with the specified distance field effect.- Parameters:
bmFont- an existing BitmapFont that will be copied in almost every way this candistanceField- determines how edges are drawn; if unsure, you should useFont.DistanceFieldType.STANDARDxAdjust- how many pixels to offset each character's x-position by, moving to the rightyAdjust- how many pixels to offset each character's y-position by, moving upwidthAdjust- how many pixels to add to the used width of each character, using more to the rightheightAdjust- how many pixels to add to the used height of each character, using more above
-
-
Method Details
-
getColorLookup
Gets the ColorLookup this uses to look up colors by name.- Returns:
- a ColorLookup implementation
-
setColorLookup
Unlikely to be used in most games, this allows changing how colors are looked up by name (or built) given aColorLookupinterface implementation.- Parameters:
lookup- a non-null ColorLookup
-
isLowerCase
public static boolean isLowerCase(char c)Returns true ifcis a lower-case letter, or false otherwise. Similar toCharacter.isLowerCase(char), but should actually work on GWT.- Parameters:
c- a char to check- Returns:
- true if c is a lower-case letter, or false otherwise.
-
isUpperCase
public static boolean isUpperCase(char c)Returns true ifcis an upper-case letter, or false otherwise. Similar toCharacter.isUpperCase(char), but should actually work on GWT.- Parameters:
c- a char to check- Returns:
- true if c is an upper-case letter, or false otherwise.
-
loadFNT
protected void loadFNT(String fntName, float xAdjust, float yAdjust, float widthAdjust, float heightAdjust)The gritty parsing code that pulls relevant info from an AngelCode BMFont .fnt file and uses it to assemble the manyTextureRegions this has for each glyph.- Parameters:
fntName- the file name of the .fnt file; can be internal or localxAdjust- added to the x-position for each glyph in the fontyAdjust- added to the y-position for each glyph in the fontwidthAdjust- added to the glyph width for each glyph in the fontheightAdjust- added to the glyph height for each glyph in the font
-
kerningPair
public int kerningPair(char first, char second)Assembles two chars into a kerning pair that can be looked up as a key inkerning. If you give such a pair tokerning'sIntIntMap.get(int, int)method, you'll get the amount of extra space (in the same unit the font uses) this will insert betweenfirstandsecond.- Parameters:
first- the first charsecond- the second char- Returns:
- a kerning pair that can be looked up in
kerning
-
scale
Scales the font by the given horizontal and vertical multipliers.- Parameters:
horizontal- how much to multiply the width of each glyph byvertical- how much to multiply the height of each glyph by- Returns:
- this Font, for chaining
-
scaleTo
Scales the font so that it will have the given width and height.- Parameters:
width- the target width of the font, in world unitsheight- the target height of the font, in world units- Returns:
- this Font, for chaining
-
adjustLineHeight
Multiplies the line height bymultiplierwithout changing the size of any characters. This can cut off the tops of letters if the multiplier is too small.- Parameters:
multiplier- will be applied tocellHeightandoriginalCellHeight- Returns:
- this Font, for chaining
-
setTextureFilter
CallssetTextureFilter(Texture.TextureFilter, Texture.TextureFilter)withTexture.TextureFilter.Linearfor both min and mag filters. This is the most common usage for setting the texture filters, and is appropriate when you have a large TextureRegion holding the font and you normally downscale it. This is automatically done forFont.DistanceFieldType.SDFandFont.DistanceFieldType.MSDFfonts, but you may also want to use it forFont.DistanceFieldType.STANDARDfonts when downscaling (they can look terrible if the defaultTexture.TextureFilter.Nearestfilter is used). Note that this sets the filter on every Texture that holds a TextureRegion used by the font, so it may affect the filter on other parts of an atlas.- Returns:
- this, for chaining
-
setTextureFilter
public Font setTextureFilter(com.badlogic.gdx.graphics.Texture.TextureFilter minFilter, com.badlogic.gdx.graphics.Texture.TextureFilter magFilter)Sets the texture filters on each Texture that holds a TextureRegion used by the font to the givenminFilterandmagFilter. You may want to use this to set a font usingFont.DistanceFieldType.STANDARDto use a better TextureFilter for smooth downscaling, likeTexture.TextureFilter.MipMapLinearLinearor justTexture.TextureFilter.Linear. You might, for some reason, want to set a font usingFont.DistanceFieldType.SDForFont.DistanceFieldType.MSDFto use TextureFilters other than its defaultTexture.TextureFilter.Linear. Note that this may affect the filter on other parts of an atlas.- Returns:
- this, for chaining
-
enableShader
public void enableShader(com.badlogic.gdx.graphics.g2d.Batch batch)Must be called before drawing anything with an SDF or MSDF font; does not need to be called for other fonts unless you are mixing them with SDF/MSDF fonts or other shaders. This also resets the Batch color to white, in case it had been left with a different setting before. If this Font is not an MSDF font, then this resets batch's shader to the default (usingbatch.setShader(null)).
This is called automatically forTextraLabelandTypingLabelif it hasn't been called already. You may still want to call this automatically for those cases if you have multiple such Labels that use the same Font; in that case, you can draw several Labels without ending the current batch. You do need to set the shader back to whatever you use for other items before you draw those, typically withbatch.setShader(null);.- Parameters:
batch- the Batch to instruct to use the appropriate shader for this font; should usually be a SpriteBatch
-
drawText
public void drawText(com.badlogic.gdx.graphics.g2d.Batch batch, CharSequence text, float x, float y)Draws the specified text at the given x,y position (in world space) with a white foreground.- Parameters:
batch- typically a SpriteBatchtext- typically a String, but this can also be a StringBuilder or some custom classx- the x position in world space to start drawing the text at (lower left corner)y- the y position in world space to start drawing the text at (lower left corner)
-
drawText
public void drawText(com.badlogic.gdx.graphics.g2d.Batch batch, CharSequence text, float x, float y, int color)Draws the specified text at the given x,y position (in world space) with the given foreground color.- Parameters:
batch- typically a SpriteBatchtext- typically a String, but this can also be a StringBuilder or some custom classx- the x position in world space to start drawing the text at (lower left corner)y- the y position in world space to start drawing the text at (lower left corner)color- an int color; typically this is RGBA, but custom shaders or Batches can use other kinds of color
-
drawBlocks
public void drawBlocks(com.badlogic.gdx.graphics.g2d.Batch batch, int[][] colors, float x, float y)Draws a grid made of rectangular blocks of int colors (typically RGBA) at the given x,y position in world space. This is only useful for monospace fonts. This assumes there is a full-block character at char u0000; Glamer produces fonts that have this already. Thecolorsparameter should be a rectangular 2D array, and because any colors that are the default int value0will be treated as transparent RGBA values, if a value is not assigned to a slot in the array then nothing will be drawn there. This is usually called before other methods that draw foreground text.
Internally, this usesBatch.draw(Texture, float[], int, int)to draw each rectangle with minimal overhead, and this also means it is unaffected by the batch color. If you want to alter the colors using a shader, the shader will receive each color incolorsas itsa_colorattribute, the same as if it was passed via the batch color.- Parameters:
batch- typically a SpriteBatchcolors- a 2D rectangular array of int colors (typically RGBA)x- the x position in world space to draw the text at (lower left corner)y- the y position in world space to draw the text at (lower left corner)
-
drawBlocks
public void drawBlocks(com.badlogic.gdx.graphics.g2d.Batch batch, char blockChar, int[][] colors, float x, float y)Draws a grid made of rectangular blocks of int colors (typically RGBA) at the given x,y position in world space. This is only useful for monospace fonts. TheblockCharshould visually be represented by a very large block, occupying all of a monospaced cell. Thecolorsparameter should be a rectangular 2D array, and because any colors that are the default int value0will be treated as transparent RGBA values, if a value is not assigned to a slot in the array then nothing will be drawn there. This is usually called before other methods that draw foreground text.
Internally, this usesBatch.draw(Texture, float[], int, int)to draw each rectangle with minimal overhead, and this also means it is unaffected by the batch color. If you want to alter the colors using a shader, the shader will receive each color incolorsas itsa_colorattribute, the same as if it was passed via the batch color.- Parameters:
batch- typically a SpriteBatchblockChar- a char that renders as a full block, occupying an entire monospaced cell with a colorcolors- a 2D rectangular array of int colors (typically RGBA)x- the x position in world space to draw the text at (lower left corner)y- the y position in world space to draw the text at (lower left corner)
-
drawMarkupText
public int drawMarkupText(com.badlogic.gdx.graphics.g2d.Batch batch, String text, float x, float y)Draws the specified text at the given x,y position (in world space), parsing an extension of libGDX markup and using it to determine color, size, position, shape, strikethrough, underline, and case of the given CharSequence. The text drawn will start as white, with the normal size as bycellWidthandcellHeight, normal case, and without bold, italic, superscript, subscript, strikethrough, or underline. Markup starts with[; the next non-letter character determines what that piece of markup toggles. Markup this knows:[[escapes a literal left bracket.[]clears all markup to the initial state without any applied.[*]toggles bold mode.[/]toggles italic (technically, oblique) mode.[^]toggles superscript mode (and turns off subscript or midscript mode).[=]toggles midscript mode (and turns off superscript or subscript mode).[.]toggles subscript mode (and turns off superscript or midscript mode).[_]toggles underline mode.[~]toggles strikethrough mode.[!]toggles all upper case mode.[,]toggles all lower case mode.[;]toggles capitalize each word mode.[#HHHHHHHH], where HHHHHHHH is a hex RGB888 or RGBA8888 int color, changes the color.[COLORNAME], where "COLORNAME" is a typically-upper-case color name that will be looked up withgetColorLookup(), changes the color. The name can optionally be preceded by|, which allows looking up colors with names that contain punctuation.
Parsing markup for a full screen every frame typically isn't necessary, and you may want to store the most recent glyphs by callingmarkup(String, Layout)and render its result withdrawGlyphs(Batch, Layout, float, float)every frame.- Parameters:
batch- typically a SpriteBatchtext- typically a String with markup, but this can also be a StringBuilder or some custom classx- the x position in world space to start drawing the text at (lower left corner)y- the y position in world space to start drawing the text at (lower left corner)- Returns:
- the number of glyphs drawn
-
drawGlyphs
public float drawGlyphs(com.badlogic.gdx.graphics.g2d.Batch batch, Layout glyphs, float x, float y)Draws the specified Layout of glyphs with a Batch at a given x, y position, drawing the full layout.- Parameters:
batch- typically a SpriteBatchglyphs- typically returned as part ofmarkup(String, Layout)x- the x position in world space to start drawing the glyph at (lower left corner)y- the y position in world space to start drawing the glyph at (lower left corner)- Returns:
- the number of glyphs drawn
-
drawGlyphs
public float drawGlyphs(com.badlogic.gdx.graphics.g2d.Batch batch, Layout glyphs, float x, float y, int align)Draws the specified Layout of glyphs with a Batch at a given x, y position, usingalignto determine how to position the text. Typically, align isAlign.left,Align.center, orAlign.right, which make the given x,y point refer to the lower-left corner, center-bottom edge point, or lower-right corner, respectively.- Parameters:
batch- typically a SpriteBatchglyphs- typically returned bymarkup(String, Layout)x- the x position in world space to start drawing the glyph at (where this is depends on align)y- the y position in world space to start drawing the glyph at (where this is depends on align)align- anAlignconstant; ifAlign.left, x and y refer to the lower left corner- Returns:
- the number of glyphs drawn
-
drawGlyphs
Draws the specified Line of glyphs with a Batch at a given x, y position, drawing the full Line using left alignment.- Parameters:
batch- typically a SpriteBatchglyphs- typically returned as part ofmarkup(String, Layout)x- the x position in world space to start drawing the glyph at (lower left corner)y- the y position in world space to start drawing the glyph at (lower left corner)- Returns:
- the number of glyphs drawn
-
drawGlyphs
public float drawGlyphs(com.badlogic.gdx.graphics.g2d.Batch batch, Line glyphs, float x, float y, int align)Draws the specified Line of glyphs with a Batch at a given x, y position, usingalignto determine how to position the text. Typically, align isAlign.left,Align.center, orAlign.right, which make the given x,y point refer to the lower-left corner, center-bottom edge point, or lower-right corner, respectively.- Parameters:
batch- typically a SpriteBatchglyphs- typically returned as part ofmarkup(String, Layout)x- the x position in world space to start drawing the glyph at (where this is depends on align)y- the y position in world space to start drawing the glyph at (where this is depends on align)align- anAlignconstant; ifAlign.left, x and y refer to the lower left corner- Returns:
- the number of glyphs drawn
-
xAdvance
public float xAdvance(long glyph)Gets the distance to advance the cursor after drawingglyph, scaled byscaleXas if drawing. This handles monospaced fonts correctly and ensures that for variable-width fonts, subscript, midscript, and superscript halve the advance amount. This does not consider kerning, if the font has it. If the glyph is fully transparent, this does not draw it at all, and treats its x advance as 0.- Parameters:
glyph- a long encoding the color, style information, and char of a glyph, as from aLine- Returns:
- the (possibly non-integer) amount to advance the cursor when you draw the given glyph, not counting kerning
-
measureWidth
Measures the actual width that the given Line will use when drawn.- Parameters:
line- a Line, as from inside a Layout- Returns:
- the width in world units
-
drawGlyph
public float drawGlyph(com.badlogic.gdx.graphics.g2d.Batch batch, long glyph, float x, float y)Draws the specified glyph with a Batch at the given x, y position. The glyph contains multiple types of data all packed into onelong: the bottom 16 bits store achar, the roughly 16 bits above that store formatting (bold, underline, superscript, etc.), and the remaining upper 32 bits store color as RGBA.- Parameters:
batch- typically a SpriteBatchglyph- a long storing a char, format, and color; typically part of a longer formatted text as a LongArrayx- the x position in world space to start drawing the glyph at (lower left corner)y- the y position in world space to start drawing the glyph at (lower left corner)- Returns:
- the distance in world units the drawn glyph uses up for width, as in a line of text
-
drawGlyph
public float drawGlyph(com.badlogic.gdx.graphics.g2d.Batch batch, long glyph, float x, float y, float rotation)Draws the specified glyph with a Batch at the given x, y position and with the specified counterclockwise rotation, measured in degrees. The glyph contains multiple types of data all packed into onelong: the bottom 16 bits store achar, the roughly 16 bits above that store formatting (bold, underline, superscript, etc.), and the remaining upper 32 bits store color as RGBA. Rotation is not stored in the long glyph; it may change frequently or as part of an animation.- Parameters:
batch- typically a SpriteBatchglyph- a long storing a char, format, and color; typically part of a longer formatted text as a LongListx- the x position in world space to start drawing the glyph at (lower left corner)y- the y position in world space to start drawing the glyph at (lower left corner)rotation- what angle to rotate the glyph, measured in degrees- Returns:
- the distance in world units the drawn glyph uses up for width, as in a line of text along the given rotation
-
markup
Reads markup from text, along with the chars to receive markup, processes it, and appends into appendTo, which is aLayoutholding one or moreLines. A common way of getting a Layout is withPools.obtain(Layout.class); you can free the Layout when you are done using it withPools.free(Object). This parses an extension of libGDX markup and uses it to determine color, size, position, shape, strikethrough, underline, and case of the given CharSequence. It also reads typing markup, for effects, but passes it through without changing it and without considering it for line wrapping or text position. The text drawn will start as white, with the normal size as determined by the font's metrics and scale (scaleXandscaleY), normal case, and without bold, italic, superscript, subscript, strikethrough, or underline. Markup starts with[; the next character determines what that piece of markup toggles. Markup this knows:[[escapes a literal left bracket.[]clears all markup to the initial state without any applied.[*]toggles bold mode.[/]toggles italic (technically, oblique) mode.[^]toggles superscript mode (and turns off subscript or midscript mode).[=]toggles midscript mode (and turns off superscript or subscript mode).[.]toggles subscript mode (and turns off superscript or midscript mode).[_]toggles underline mode.[~]toggles strikethrough mode.[!]toggles all upper case mode.[,]toggles all lower case mode.[;]toggles capitalize each word mode.[#HHHHHHHH], where HHHHHHHH is a hex RGB888 or RGBA8888 int color, changes the color.[COLORNAME], where "COLORNAME" is a typically-upper-case color name that will be looked up ingetColorLookup(), changes the color. The name can optionally be preceded by|, which allows looking up colors with names that contain punctuation.
appendTousingdrawGlyphs(Batch, Layout, float, float).- Parameters:
text- text with markupappendTo- a Layout that stores one or more Line objects, carrying color, style, chars, and size- Returns:
- appendTo, for chaining
-
regenerateLayout
-
dispose
public void dispose()Releases all resources of this object.- Specified by:
disposein interfacecom.badlogic.gdx.utils.Disposable
-