Package com.github.tommyettinger.textra
Class Layout
java.lang.Object
com.github.tommyettinger.textra.Layout
A replacement for libGDX's GlyphLayout, more or less; stores one or more (possibly empty) lines of text,
which can use color and style markup from
As of TextraTypist 2.6.0, Layout no longer uses Line objects at all. In all earlier versions, a Layout contained one or more Line objects, and each Line knew its own width, height, and glyph contents. Now, since Layout was already tracking advances, sizing, rotations, and so on independently of lines, it also tracks glyphs, the starting indices of lines, and the sizes of lines.
Font, and can be drawn with
Font.drawGlyphs(Batch, Layout, float, float, int). You can obtain a Layout with
new Layout() followed by setting the font, or just using a constructor.
As of TextraTypist 2.6.0, Layout no longer uses Line objects at all. In all earlier versions, a Layout contained one or more Line objects, and each Line knew its own width, height, and glyph contents. Now, since Layout was already tracking advances, sizing, rotations, and so on independently of lines, it also tracks glyphs, the starting indices of lines, and the sizes of lines.
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal com.badlogic.gdx.utils.FloatArrayContains one float per glyph; each is a multiplier to the x-advance of that glyph.protected booleanprotected floatprotected Stringprotected Fontfinal com.badlogic.gdx.utils.LongArrayContains one long per glyph; each long encodes a 16-bit char, 32-bit color, and 16 bits of style info.protected Justifyfinal com.badlogic.gdx.utils.FloatArrayContains two floats per line of text; even items are line widths (0-indexed), odd items are line heights.final com.badlogic.gdx.utils.IntArrayContains one int per line of text, with each int the index inglyphsof the first glyph on that line.protected intfinal com.badlogic.gdx.utils.FloatArrayContains two floats per glyph; even items are x offsets (0-indexed), odd items are y offsets.final com.badlogic.gdx.utils.FloatArrayContains one float per glyph; each is a rotation in degrees to apply to that glyph (around its center).final com.badlogic.gdx.utils.FloatArrayContains two floats per glyph, as size multipliers; even items (0-indexed) apply to x, odd items apply to y.protected float -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(long glyph) Adds alongglyph as processed byFontto store color and style info with the char.add(long glyph, float scale, float advance, float offsetX, float offsetY, float rotation) Adds alongglyph as processed byFontto store color and style info with the char.floataddToLastHeight(float additional) floataddToLastWidth(float additional) Primarily used bytoString(), but can be useful if you want to append many Layouts into a StringBuilder.Can be useful if you want to append many Layouts into a StringBuilder.appendSubstringInto(StringBuilder sb, int start, int end) Gets a String representation of part of this Layout, made of only the char portions of the glyphs from start (inclusive) to end (exclusive).clear()Clears the glyph contents of this Layout, but does not reset thetargetWidth,baseColor, orellipsis.intReturns how manylongglyphs are currently in this layout.intcountGlyphsBeforeLine(int lineIndex) Gets how manylongglyphs are currently in this layout before the start of the line with the givenlineIndex, and returns that count.One of the ways to set the font on a Layout; this one returns this Layout for chaining.floatGets the base color of the Layout, as the float bits of a Color.Gets the ellipsis, which may be null, or may be a String that can be placed at the end of the text if its max lines are exceeded.getFont()floatfloatfloatfloatgetLineHeight(int ln) intgetLineIndex(int index) Given an index into this Layout'sglyphs, this returns the index of the line in the Layout that holds the glyph at that index.intgetLineLength(int ln) floatgetLineWidth(int ln) intThe maximum number of lines this Layout can contain.floatfloatgetWidth()intlines()booleanAdds a new size-0 line if this hasn't reached its limit and returns true, or returns false if this has reached its limit ofgetMaxLines().voidreset()Resets the object for reuse.voidset(int index, long newGlyph) Ifindexis at least 0 and less thancountGlyphs(), this will set the glyph at that index to benewGlyph.voidset(int index, long newGlyph, float scale, float advance, float offsetX, float offsetY, float rotation) Ifindexis at least 0 and less thancountGlyphs(), this will set the glyph at that index to benewGlyph.setBaseColor(float baseColor) Sets the base color of the Layout; this is what font color will be used immediately after resetting formatting with[], as well as the initial color used by text that hasn't been formatted.setBaseColor(com.badlogic.gdx.graphics.Color baseColor) Sets the base color of the Layout; this is what font color will be used immediately after resetting formatting with[], as well as the initial color used by text that hasn't been formatted.setEllipsis(String ellipsis) Sets the ellipsis text, which replaces the last few glyphs if non-null and the text added would exceed thegetMaxLines()of this Layout.voidOne of the ways to set the font on a Layout; this is a traditional setter.setJustification(Justify justification) floatsetLastHeight(float newValue) floatsetLastWidth(float newValue) voidsetLineHeight(int ln, float height) voidsetLineWidth(int ln, float width) setMaxLines(int maxLines) Sets the maximum number of lines this Layout can contain; this is always at least 1.setTargetWidth(float targetWidth) toString()Simply delegates to callingappendInto(StringBuilder)with a new StringBuilder, calling toString(), and then returning.truncateExtra(int i)
-
Field Details
-
font
-
maxLines
protected int maxLines -
atLimit
protected boolean atLimit -
ellipsis
-
targetWidth
protected float targetWidth -
baseColor
protected float baseColor -
justification
-
glyphs
public final com.badlogic.gdx.utils.LongArray glyphsContains one long per glyph; each long encodes a 16-bit char, 32-bit color, and 16 bits of style info. If a glyph's upper 32 bits are all 0, it is fully transparent, which can be used as a sort of neutral value. -
lineStarts
public final com.badlogic.gdx.utils.IntArray lineStartsContains one int per line of text, with each int the index inglyphsof the first glyph on that line. This should always start with an item0, and the exclusive end index of the last line can be obtained withcountGlyphs(). -
lineSizes
public final com.badlogic.gdx.utils.FloatArray lineSizesContains two floats per line of text; even items are line widths (0-indexed), odd items are line heights. There is no neutral value for a line size, but 0 is reasonable for an empty line width and height. -
offsets
public final com.badlogic.gdx.utils.FloatArray offsetsContains two floats per glyph; even items are x offsets (0-indexed), odd items are y offsets. The neutral value for a glyph (the value that this defaults to, and means no change should be made) is 0.0f. -
sizing
public final com.badlogic.gdx.utils.FloatArray sizingContains two floats per glyph, as size multipliers; even items (0-indexed) apply to x, odd items apply to y. The neutral value for a glyph (the value that this defaults to, and means no change should be made) is 1.0f. -
rotations
public final com.badlogic.gdx.utils.FloatArray rotationsContains one float per glyph; each is a rotation in degrees to apply to that glyph (around its center). The neutral value for a glyph (the value that this defaults to, and means no change should be made) is 0.0f. -
advances
public final com.badlogic.gdx.utils.FloatArray advancesContains one float per glyph; each is a multiplier to the x-advance of that glyph. The neutral value for a glyph (the value that this defaults to, and means no change should be made) is 1.0f.
-
-
Constructor Details
-
Layout
public Layout() -
Layout
-
Layout
-
-
Method Details
-
getLineIndex
public int getLineIndex(int index) Given an index into this Layout'sglyphs, this returns the index of the line in the Layout that holds the glyph at that index. A line index can be used withsetLineHeight(int, float),getLineWidth(int)and similar methods.- Parameters:
index- an index into the given Layout- Returns:
- the index of the line in the Layout containing the given index; will be 0 if index is -1, or the last line index if index is -2 or out of bounds
-
font
One of the ways to set the font on a Layout; this one returns this Layout for chaining.- Parameters:
font- the font to use- Returns:
- this Layout, for chaining
-
getFont
-
setFont
One of the ways to set the font on a Layout; this is a traditional setter.- Parameters:
font- the font to use
-
add
Adds alongglyph as processed byFontto store color and style info with the char.- Parameters:
glyph- usually produced byFontto store color and style info with the char- Returns:
- this Layout, for chaining
-
add
public Layout add(long glyph, float scale, float advance, float offsetX, float offsetY, float rotation) Adds alongglyph as processed byFontto store color and style info with the char.- Parameters:
glyph- usually produced byFontto store color and style info with the charscale- 1.0f if unchanged; multiplies the size of the glyphadvance- 1.0f if unchanged; multiplies the x-advance of the glyph, and is usually related to scaleoffsetX- 0.0f if unchanged; added to the initial x-position of the glyphoffsetY- 0.0f if unchanged; added to the y-position of the glyph (with descenders lower than ascenders)rotation- 0.0f if unchanged; added to the rotation of the glyph, in degrees- Returns:
- this Layout, for chaining
-
set
public void set(int index, long newGlyph) Ifindexis at least 0 and less thancountGlyphs(), this will set the glyph at that index to benewGlyph. This does not change the sizing, advances, offsets, or rotations.- Parameters:
index- should be at least 0 and less thancountGlyphs(); the index to changenewGlyph- usually produced byFontto store color and style info with the char
-
set
public void set(int index, long newGlyph, float scale, float advance, float offsetX, float offsetY, float rotation) Ifindexis at least 0 and less thancountGlyphs(), this will set the glyph at that index to benewGlyph. This also sets the various FloatArray fields to have potentially-new values at that index.- Parameters:
index- should be at least 0 and less thancountGlyphs(); the index to changenewGlyph- usually produced byFontto store color and style info with the charscale- 1.0f if unchanged; multiplies the size of the glyphadvance- 1.0f if unchanged; multiplies the x-advance of the glyph, and is usually related to scaleoffsetX- 0.0f if unchanged; added to the initial x-position of the glyphoffsetY- 0.0f if unchanged; added to the y-position of the glyph (with descenders lower than ascenders)rotation- 0.0f if unchanged; added to the rotation of the glyph, in degrees
-
clear
Clears the glyph contents of this Layout, but does not reset thetargetWidth,baseColor, orellipsis. One empty line will be present in this Layout after it is cleared.- Returns:
- this Layout, after changes, for chaining
-
getWidth
public float getWidth() -
getHeight
public float getHeight() -
lines
public int lines() -
getLineLength
public int getLineLength(int ln) -
getLineWidth
public float getLineWidth(int ln) -
setLineWidth
public void setLineWidth(int ln, float width) -
getLineHeight
public float getLineHeight(int ln) -
setLineHeight
public void setLineHeight(int ln, float height) -
pushLineBare
public boolean pushLineBare()Adds a new size-0 line if this hasn't reached its limit and returns true, or returns false if this has reached its limit ofgetMaxLines().- Returns:
- true if a new line was added, or false if this couldn't add a line because it has reached its limit
-
getTargetWidth
public float getTargetWidth() -
setTargetWidth
-
getBaseColor
public float getBaseColor()Gets the base color of the Layout, as the float bits of a Color. The base color is what font color will be used immediately after resetting formatting with[], as well as the initial color used by text that hasn't been formatted. You can fill a Color object with this value usingColor.abgr8888ToColor(Color, float)(it modifies the Color you give it).- Returns:
- the base color of the Layout, as float bits
-
setBaseColor
Sets the base color of the Layout; this is what font color will be used immediately after resetting formatting with[], as well as the initial color used by text that hasn't been formatted. This takes the color as a primitive float, which you can get from a Color object withColor.toFloatBits(), or in some cases from existing data produced byFont.- Parameters:
baseColor- the float bits of a Color, as obtainable viaColor.toFloatBits()
-
setBaseColor
Sets the base color of the Layout; this is what font color will be used immediately after resetting formatting with[], as well as the initial color used by text that hasn't been formatted. If the given Color is null, this treats it as white.- Parameters:
baseColor- a Color to use for text that hasn't been formatted; if null, will be treated as white
-
getMaxLines
public int getMaxLines()The maximum number of lines this Layout can contain.- Returns:
- the maximum number of lines this Layout can contain
-
setMaxLines
Sets the maximum number of lines this Layout can contain; this is always at least 1. For effectively unlimited lines, passInteger.MAX_VALUEto this.- Parameters:
maxLines- the limit for how many lines this Layout can contain; always 1 or more
-
getEllipsis
Gets the ellipsis, which may be null, or may be a String that can be placed at the end of the text if its max lines are exceeded.- Returns:
- an ellipsis String or null
-
setEllipsis
Sets the ellipsis text, which replaces the last few glyphs if non-null and the text added would exceed thegetMaxLines()of this Layout. For the ellipsis to appear, this has to be called with a non-null String (often"...", or"…"if the font supports it), andsetMaxLines(int)needs to have been called with a small enough number, such as 1.- Parameters:
ellipsis- a String for a Layout to end with if its max lines are exceeded, or null to avoid such truncation
-
getJustification
-
setJustification
-
countGlyphs
public int countGlyphs()Returns how manylongglyphs are currently in this layout. This takes constant time.- Returns:
- how many
longglyphs are in this Layout
-
countGlyphsBeforeLine
public int countGlyphsBeforeLine(int lineIndex) Gets how manylongglyphs are currently in this layout before the start of the line with the givenlineIndex, and returns that count.
This takes constant time.- Returns:
- how many
longglyphs exist in this Layout before the start of the given line
-
reset
public void reset()Resets the object for reuse. The font is nullified, but the lines are cleared and then one blank line is re-added to lines so it can be used normally later. -
appendIntoDirect
Can be useful if you want to append many Layouts into a StringBuilder. This does not treat character u0002 any differently from other characters, which is where it differs fromappendInto(StringBuilder). This does not add or remove newlines from the Layout's contents, and can produce line breaks if they appear.- Parameters:
sb- a non-null StringBuilder from the JDK- Returns:
- sb, for chaining
-
appendSubstringInto
Gets a String representation of part of this Layout, made of only the char portions of the glyphs from start (inclusive) to end (exclusive). This can retrieve text from across multiple lines. If this encounters the special placeholder character u0002, it treats it as'[', like the rest of the library does. If this encounters emoji, icons, or other inline images assigned to the font withFont.addAtlas(TextureAtlas), then this will use a name that can be used to look up that inline image. For example, it will show an actual emoji like 🤖 instead of the gibberish character that[+robot]produces internally.- Parameters:
sb- a non-null StringBuilder from the JDK; will be modified if this Layout is non-emptystart- inclusive start index to begin taking chars fromend- exclusive end index to stop taking chars before- Returns:
- sb, for chaining
-
appendInto
Primarily used bytoString(), but can be useful if you want to append many Layouts into a StringBuilder. If this encounters the special placeholder character u0002, it treats it as'[', like the rest of the library does. If this encounters emoji, icons, or other inline images assigned to the font withFont.addAtlas(TextureAtlas), then this will use a name that can be used to look up that inline image. For example, it will show an actual emoji like 🤖 instead of the gibberish character that[+robot]produces internally. This does not add or remove newlines from the Layout's contents, and can produce line breaks if they appear.- Parameters:
sb- a non-null StringBuilder from the JDK; will be modified if this Layout is non-empty- Returns:
- sb, for chaining
-
toString
Simply delegates to callingappendInto(StringBuilder)with a new StringBuilder, calling toString(), and then returning. See the documentation for appendInto for more details. -
truncateExtra
Mostly intended for internal use; when lines have been removed but the other fields here have not been changed, this will trimadvances,rotations,sizing, andoffsetsto match the size of the total glyphs in all lines.- Parameters:
i- the length to truncate to; oftencountGlyphs()- Returns:
- this, for chaining
-
getLastWidth
public float getLastWidth() -
getLastHeight
public float getLastHeight() -
setLastWidth
public float setLastWidth(float newValue) -
setLastHeight
public float setLastHeight(float newValue) -
addToLastWidth
public float addToLastWidth(float additional) -
addToLastHeight
public float addToLastHeight(float additional)
-