APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.graphics.g2d

public Class GlyphLayout

extends Object
implements Poolable
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.utils.Pool.Poolable
Imports
com.badlogic.gdx.graphics.Color, .Colors, com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData, .BitmapFont.Glyph, com.badlogic.gdx.utils.Align, .Array, .DefaultPool, .FloatArray, .IntArray, .Justify, .Null, .Pool, .Pool.Poolable

Stores runs of glyphs for a piece of text. The text may contain newlines and color markup tags.

Where wrapping occurs is determined by BitmapFontData#getWrapIndex(Array, int). Additionally, when BitmapFontData#markupEnabled is true wrapping can occur at color start or end tags.

When wrapping occurs, whitespace is removed before and after the wrap position. Whitespace is determined by BitmapFontData#isWhitespace(char).

Glyphs positions are determined by BitmapFontData#getGlyphs(GlyphRun, CharSequence, int, int, Glyph).

This class is not thread safe, even if synchronized externally, and must only be used from the game thread.

Authors
Nathan Sweet, davebaol, Alexander Dorokhov, Thomas Creutzenberg

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
GlyphLayout.GlyphRun

Stores glyphs and positions for a line of text.

public static enum

Field Summary

Modifier and TypeField and Description
public final IntArray
colors

Determines the colors of the glpyhs in the runs.

private static final IntArray
private static final float
public int
glyphCount

Number of glyphs across all runs.

private static final Pool<GlyphLayout.GlyphRun>
public float
public final Array<GlyphLayout.GlyphRun>
runs

Each run has the glyphs for a line of text.

public float

Constructor Summary

AccessConstructor and Description
public
GlyphLayout()

Creates an empty GlyphLayout.

public
public
GlyphLayout(BitmapFont font, CharSequence str, Color color, float targetWidth, int halign, boolean wrap)

public
GlyphLayout(BitmapFont font, CharSequence str, int start, int end, Color color, float targetWidth, int halign, boolean wrap, String truncate)

Method Summary

Modifier and TypeMethod and Description
private void
alignRuns(float targetWidth, int halign)

Align runs to center or right of targetWidth.

private void
calculateWidthsAndJustify(BitmapFont.BitmapFontData fontData, float targetWidth, Justify justify)

Calculate run widths and the entire layout width.

private float
getGlyphWidth(BitmapFont.Glyph glyph, BitmapFont.BitmapFontData fontData)

Returns the distance from the glyph's drawing position to the right edge of the glyph.

private float
getLineOffset(Array<BitmapFont.Glyph> glyphs, BitmapFont.BitmapFontData fontData)

Returns an X offset for the first glyph so when drawn, none of it is left of the line's drawing position.

private void
justifyRun(GlyphLayout.GlyphRun run, float targetWidth, int gaps, Justify justify)

private int
parseColorMarkup(CharSequence str, int start, int end)

public void
reset()

Implements com.badlogic.gdx.utils.Pool.Poolable.reset.

Resets the object for reuse.

private void
setLastGlyphXAdvance(BitmapFont.BitmapFontData fontData, GlyphLayout.GlyphRun run)

Sets the xadvance of the last glyph to use its width instead of xadvance.

public void
setText(BitmapFont font, CharSequence str)

Calls setText with the whole string, the font's current color, and no alignment or wrapping.

public void
setText(BitmapFont font, CharSequence str, Color color, float targetWidth, int halign, boolean wrap)

Calls setText with the whole string and no truncation.

public void
setText(BitmapFont font, CharSequence str, int start, int end, Color
The default color to use for the text (the BitmapFont color is not used). If BitmapFontData#markupEnabled is true, color markup tags in the specified string may change the color for portions of the text.
color
,
float
The width used for alignment, line wrapping, and truncation. May be zero if those features are not used.
targetWidth
,
int
Horizontal alignment of the text, see Align.
halign
,
boolean wrap, String
If not null and the width of the glyphs exceed targetWidth, the glyphs are truncated and the glyphs for the specified truncate string are placed at the end. Empty string can be used to truncate without adding glyphs. Truncate should not be used with text that contains multiple lines. Wrap is ignored if truncate is not null.
truncate
)

public void
setText(BitmapFont font, CharSequence str, int start, int end, Color
The default color to use for the text (the BitmapFont color is not used). If BitmapFontData#markupEnabled is true, color markup tags in the specified string may change the color for portions of the text.
color
,
float
The width used for alignment, line wrapping, and truncation. May be zero if those features are not used.
targetWidth
,
int
Horizontal alignment of the text, see Align.
halign
,
boolean wrap, Justify
Justification of the text, see Justify.
justify
,
String
If not null and the width of the glyphs exceed targetWidth, the glyphs are truncated and the glyphs for the specified truncate string are placed at the end. Empty string can be used to truncate without adding glyphs. Truncate should not be used with text that contains multiple lines. Wrap is ignored if truncate is not null.
truncate
)

private static boolean
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

private void
truncate(BitmapFont.BitmapFontData fontData, GlyphLayout.GlyphRun run, float targetWidth, String
May be empty string.
truncate
)

private GlyphLayout.GlyphRun

Returns:

May be null if second run is all whitespace.
wrap
(BitmapFont.BitmapFontData fontData, GlyphLayout.GlyphRun first, int wrapIndex)

Breaks a run into two runs at the specified wrapIndex.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

colorsback to summary
public final IntArray colors

Determines the colors of the glpyhs in the runs. Entries are pairs where the first is the glyph index (across all runs) where the color starts and the second is the color encoded as ABGR8888.

For example: [0, WHITE, 4, GREEN, 5, WHITE]
Glpyhs 0 to 3 are WHITE, 4 is GREEN and 5 to the end are WHITE.

The array is empty if there are no runs, otherwise it has at least two entries: [0, startColor]

colorStackback to summary
private static final IntArray colorStack
epsilonback to summary
private static final float epsilon
glyphCountback to summary
public int glyphCount

Number of glyphs across all runs.

glyphRunPoolback to summary
private static final Pool<GlyphLayout.GlyphRun> glyphRunPool
heightback to summary
public float height
runsback to summary
public final Array<GlyphLayout.GlyphRun> runs

Each run has the glyphs for a line of text.

Runs are pooled, so references should not be kept past the next call to setText(BitmapFont, CharSequence, int, int, Color, float, int, boolean, String) or reset().

widthback to summary
public float width

Constructor Detail

GlyphLayoutback to summary
public GlyphLayout()

Creates an empty GlyphLayout.

GlyphLayoutback to summary
public GlyphLayout(BitmapFont font, CharSequence str)
See Also
setText(BitmapFont, CharSequence)
GlyphLayoutback to summary
public GlyphLayout(BitmapFont font, CharSequence str, Color color, float targetWidth, int halign, boolean wrap)
See Also
setText(BitmapFont, CharSequence)
GlyphLayoutback to summary
public GlyphLayout(BitmapFont font, CharSequence str, int start, int end, Color color, float targetWidth, int halign, boolean wrap, String truncate)
See Also
setText(BitmapFont, CharSequence)

Method Detail

alignRunsback to summary
private void alignRuns(float targetWidth, int halign)

Align runs to center or right of targetWidth. Requires run.width of runs to be already set

calculateWidthsAndJustifyback to summary
private void calculateWidthsAndJustify(BitmapFont.BitmapFontData fontData, float targetWidth, Justify justify)

Calculate run widths and the entire layout width.

getGlyphWidthback to summary
private float getGlyphWidth(BitmapFont.Glyph glyph, BitmapFont.BitmapFontData fontData)

Returns the distance from the glyph's drawing position to the right edge of the glyph.

getLineOffsetback to summary
private float getLineOffset(Array<BitmapFont.Glyph> glyphs, BitmapFont.BitmapFontData fontData)

Returns an X offset for the first glyph so when drawn, none of it is left of the line's drawing position.

justifyRunback to summary
private void justifyRun(GlyphLayout.GlyphRun run, float targetWidth, int gaps, Justify justify)
parseColorMarkupback to summary
private int parseColorMarkup(CharSequence str, int start, int end)
resetback to summary
public void reset()

Implements com.badlogic.gdx.utils.Pool.Poolable.reset.

Doc from com.badlogic.gdx.utils.Pool.Poolable.reset.

Resets the object for reuse. Object references should be nulled and fields may be set to default values.

setLastGlyphXAdvanceback to summary
private void setLastGlyphXAdvance(BitmapFont.BitmapFontData fontData, GlyphLayout.GlyphRun run)

Sets the xadvance of the last glyph to use its width instead of xadvance.

setTextback to summary
public void setText(BitmapFont font, CharSequence str)

Calls setText with the whole string, the font's current color, and no alignment or wrapping.

setTextback to summary
public void setText(BitmapFont font, CharSequence str, Color color, float targetWidth, int halign, boolean wrap)

Calls setText with the whole string and no truncation.

setTextback to summary
public void setText(BitmapFont font, CharSequence str, int start, int end, Color color, float targetWidth, int halign, boolean wrap, String truncate)
Parameters
color:Color

The default color to use for the text (the BitmapFont color is not used). If BitmapFontData#markupEnabled is true, color markup tags in the specified string may change the color for portions of the text.

targetWidth:float

The width used for alignment, line wrapping, and truncation. May be zero if those features are not used.

halign:int

Horizontal alignment of the text, see Align.

truncate:String

If not null and the width of the glyphs exceed targetWidth, the glyphs are truncated and the glyphs for the specified truncate string are placed at the end. Empty string can be used to truncate without adding glyphs. Truncate should not be used with text that contains multiple lines. Wrap is ignored if truncate is not null.

setTextback to summary
public void setText(BitmapFont font, CharSequence str, int start, int end, Color color, float targetWidth, int halign, boolean wrap, Justify justify, String truncate)
Parameters
color:Color

The default color to use for the text (the BitmapFont color is not used). If BitmapFontData#markupEnabled is true, color markup tags in the specified string may change the color for portions of the text.

targetWidth:float

The width used for alignment, line wrapping, and truncation. May be zero if those features are not used.

halign:int

Horizontal alignment of the text, see Align.

justify:Justify

Justification of the text, see Justify.

truncate:String

If not null and the width of the glyphs exceed targetWidth, the glyphs are truncated and the glyphs for the specified truncate string are placed at the end. Empty string can be used to truncate without adding glyphs. Truncate should not be used with text that contains multiple lines. Wrap is ignored if truncate is not null.

shouldJustifyback to summary
private static boolean shouldJustify(Justify justify, GlyphLayout.WrapState wrapState)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

truncateback to summary
private void truncate(BitmapFont.BitmapFontData fontData, GlyphLayout.GlyphRun run, float targetWidth, String truncate)
Parameters
truncate:String

May be empty string.

wrapback to summary
private GlyphLayout.GlyphRun wrap(BitmapFont.BitmapFontData fontData, GlyphLayout.GlyphRun first, int wrapIndex)

Breaks a run into two runs at the specified wrapIndex.

Returns:GlyphLayout.GlyphRun

May be null if second run is all whitespace.

com.badlogic.gdx.graphics.g2d back to summary

public Class GlyphLayout.GlyphRun

extends Object
implements Poolable
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.utils.Pool.Poolable

Stores glyphs and positions for a line of text.
Author
Nathan Sweet

Field Summary

Modifier and TypeField and Description
public Array<BitmapFont.Glyph>
public float
public GlyphLayout.WrapState
public float
x

public FloatArray
xAdvances

Contains glyphs.size+1 entries: The first entry is the X offset relative to the drawing position.

public float
y

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
pack-priv void
public void
reset()

Implements com.badlogic.gdx.utils.Pool.Poolable.reset.

Resets the object for reuse.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

glyphsback to summary
public Array<BitmapFont.Glyph> glyphs
widthback to summary
public float width
wrapStateback to summary
public GlyphLayout.WrapState wrapState
xback to summary
public float x
xAdvancesback to summary
public FloatArray xAdvances

Contains glyphs.size+1 entries:
The first entry is the X offset relative to the drawing position.
Subsequent entries are the X advance relative to previous glyph position.
The last entry is the width of the last glyph.

yback to summary
public float y

Constructor Detail

GlyphRunback to summary
public GlyphRun()

Method Detail

appendRunback to summary
pack-priv void appendRun(GlyphLayout.GlyphRun run)
resetback to summary
public void reset()

Implements com.badlogic.gdx.utils.Pool.Poolable.reset.

Doc from com.badlogic.gdx.utils.Pool.Poolable.reset.

Resets the object for reuse. Object references should be nulled and fields may be set to default values.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

com.badlogic.gdx.graphics.g2d back to summary

public final Enum GlyphLayout.WrapState

extends Enum<GlyphLayout.WrapState>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final GlyphLayout.WrapState
public static final GlyphLayout.WrapState
public static final GlyphLayout.WrapState

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static GlyphLayout.WrapState
public static GlyphLayout.WrapState[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

lastWrappedback to summary
public static final GlyphLayout.WrapState lastWrapped
notWrappedback to summary
public static final GlyphLayout.WrapState notWrapped
wrappedback to summary
public static final GlyphLayout.WrapState wrapped

Constructor Detail

WrapStateback to summary
private WrapState()

Method Detail

valueOfback to summary
public static GlyphLayout.WrapState valueOf(String name)
valuesback to summary
public static GlyphLayout.WrapState[] values()