Class GenericFont
Represents a font with additional abilities. GenericSpriteFont
public abstract class GenericFont : GenericDataHolder, IGenericDataHolder
- Inheritance
-
GenericFont
- Implements
- Derived
- Inherited Members
Fields
Emsp
This field holds the unicode representation of a one em space. This is a character that isn't drawn, but has the same width as LineHeight. Whereas a regular Microsoft.Xna.Framework.Graphics.SpriteFont would have to explicitly support this character for width calculations, generic fonts implicitly support it in MeasureString(string, bool).
public const char Emsp = ' '
Field Value
Nbsp
This field holds the unicode representation of a non-breaking space. Whereas a regular Microsoft.Xna.Framework.Graphics.SpriteFont would have to explicitly support this character for width calculations, generic fonts implicitly support it in MeasureString(string, bool).
public const char Nbsp = ' '
Field Value
Zwsp
This field holds the unicode representation of a zero-width space. Whereas a regular Microsoft.Xna.Framework.Graphics.SpriteFont would have to explicitly support this character for width calculations and string splitting, generic fonts implicitly support it in MeasureString(string, bool) and SplitString(string, float, float).
public const char Zwsp = ''
Field Value
Properties
Bold
The bold version of this font.
public abstract GenericFont Bold { get; }
Property Value
Italic
The italic version of this font.
public abstract GenericFont Italic { get; }
Property Value
LineHeight
The height of each line of text of this font. This is the value that the text's draw position is offset by every time a newline character is reached.
public abstract float LineHeight { get; }
Property Value
Methods
CalculateStringTransform(Vector2, float, Vector2, Vector2, SpriteEffects, Vector2)
Calculates a transformation matrix for drawing a string with the given data.
public Matrix CalculateStringTransform(Vector2 position, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, Vector2 flipSize)
Parameters
positionVector2The position to draw at.
rotationfloatThe rotation to draw with.
originVector2The origin to subtract from the position.
scaleVector2The scale to draw with.
effectsSpriteEffectsThe flipping to draw with.
flipSizeVector2The size of the string, which is only used when
effectsis not Microsoft.Xna.Framework.Graphics.SpriteEffects.None.
Returns
- Matrix
A transformation matrix.
DrawCharacter(SpriteBatch, int, string, Vector2, Color, float, Vector2, SpriteEffects, float)
Draws the given code point with the given data for use in DrawString(SpriteBatch, StringBuilder, Vector2, Color, float, Vector2, Vector2, SpriteEffects, float). Note that this method should only be called internally for rendering of more complex strings, like in TextFormatter Code implementations.
public abstract void DrawCharacter(SpriteBatch batch, int codePoint, string character, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth)
Parameters
batchSpriteBatchThe sprite batch to draw with.
codePointintThe code point which will be drawn.
characterstringA string representation of the character which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
rotationfloatA rotation of this character.
scaleVector2A scaling of this character.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA depth of the layer of this character.
DrawString(SpriteBatch, string, Vector2, Color)
Submit a text string of sprites for drawing in the current batch.
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color)
Parameters
batchSpriteBatchtextstringThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
DrawString(SpriteBatch, string, Vector2, Color, float, Vector2, Vector2, SpriteEffects, float)
Submit a text string of sprites for drawing in the current batch.
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)
Parameters
batchSpriteBatchtextstringThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
rotationfloatA rotation of this string.
originVector2Center of the rotation. 0,0 by default.
scaleVector2A scaling of this string.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA depth of the layer of this string.
DrawString(SpriteBatch, string, Vector2, Color, float, Vector2, float, SpriteEffects, float)
Submit a text string of sprites for drawing in the current batch.
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)
Parameters
batchSpriteBatchtextstringThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
rotationfloatA rotation of this string.
originVector2Center of the rotation. 0,0 by default.
scalefloatA scaling of this string.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA depth of the layer of this string.
DrawString(SpriteBatch, StringBuilder, Vector2, Color)
Submit a text string of sprites for drawing in the current batch.
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color)
Parameters
batchSpriteBatchtextStringBuilderThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
DrawString(SpriteBatch, StringBuilder, Vector2, Color, float, Vector2, Vector2, SpriteEffects, float)
Submit a text string of sprites for drawing in the current batch.
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)
Parameters
batchSpriteBatchtextStringBuilderThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
rotationfloatA rotation of this string.
originVector2Center of the rotation. 0,0 by default.
scaleVector2A scaling of this string.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA depth of the layer of this string.
DrawString(SpriteBatch, StringBuilder, Vector2, Color, float, Vector2, float, SpriteEffects, float)
Submit a text string of sprites for drawing in the current batch.
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)
Parameters
batchSpriteBatchtextStringBuilderThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
rotationfloatA rotation of this string.
originVector2Center of the rotation. 0,0 by default.
scalefloatA scaling of this string.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA depth of the layer of this string.
MeasureCharacter(int)
Measures the width of the given code point with the default scale for use in MeasureString(string, bool). Note that this method does not support Nbsp, Zwsp and Emsp for most generic fonts, which is why MeasureString(string, bool) should be used even for single characters.
protected abstract float MeasureCharacter(int codePoint)
Parameters
codePointintThe code point whose width to calculate
Returns
- float
The width of the given character with the default scale
MeasureString(string, bool)
Measures the width of the given string when drawn with this font's underlying font. This method uses MeasureCharacter(int) internally to calculate the size of known characters and calculates additional characters like Nbsp, Zwsp and Emsp. If the text contains newline characters (\n), the size returned will represent a rectangle that encompasses the width of the longest line and the string's full height.
public Vector2 MeasureString(string text, bool ignoreTrailingSpaces = false)
Parameters
textstringThe text whose size to calculate
ignoreTrailingSpacesboolWhether trailing whitespace should be ignored in the returned size, causing the end of each line to be effectively trimmed
Returns
- Vector2
The size of the string when drawn with this font
MeasureString(StringBuilder, bool)
Measures the width of the given string when drawn with this font's underlying font. This method uses MeasureCharacter(int) internally to calculate the size of known characters and calculates additional characters like Nbsp, Zwsp and Emsp. If the text contains newline characters (\n), the size returned will represent a rectangle that encompasses the width of the longest line and the string's full height.
public Vector2 MeasureString(StringBuilder text, bool ignoreTrailingSpaces = false)
Parameters
textStringBuilderThe text whose size to calculate
ignoreTrailingSpacesboolWhether trailing whitespace should be ignored in the returned size, causing the end of each line to be effectively trimmed
Returns
- Vector2
The size of the string when drawn with this font
MoveFlipped(Vector2, SpriteEffects, Vector2)
Moves the passed charPos based on the given flipping data.
public Vector2 MoveFlipped(Vector2 charPos, SpriteEffects effects, Vector2 charSize)
Parameters
charPosVector2The position to move.
effectsSpriteEffectsThe flipping to move based on.
charSizeVector2The size of the object to move, which is only used when
effectsis not Microsoft.Xna.Framework.Graphics.SpriteEffects.None.
Returns
- Vector2
The moved position.
SplitString(string, float, float)
Splits a string to a given maximum width, adding newline characters between each line.
Also splits long words and supports zero-width spaces and takes into account existing newline characters in the passed text.
See SplitStringSeparate(string, float, float) for a method that differentiates between existing newline characters and splits due to maximum width.
public string SplitString(string text, float width, float scale)
Parameters
textstringThe text to split into multiple lines
widthfloatThe maximum width that each line should have
scalefloatThe scale to use for width measurements
Returns
- string
The split string, containing newline characters at each new line
SplitString(StringBuilder, float, float)
Splits a string to a given maximum width, adding newline characters between each line.
Also splits long words and supports zero-width spaces and takes into account existing newline characters in the passed text.
See SplitStringSeparate(string, float, float) for a method that differentiates between existing newline characters and splits due to maximum width.
public string SplitString(StringBuilder text, float width, float scale)
Parameters
textStringBuilderThe text to split into multiple lines
widthfloatThe maximum width that each line should have
scalefloatThe scale to use for width measurements
Returns
- string
The split string, containing newline characters at each new line
SplitStringSeparate(string, float, float)
Splits a string to a given maximum width and returns each split section as a separate string. Note that existing new lines are taken into account for line length, but not split in the resulting strings. This method differs from SplitString(string, float, float) in that it differentiates between pre-existing newline characters and splits due to maximum width.
public IEnumerable<string> SplitStringSeparate(string text, float width, float scale)
Parameters
textstringThe text to split into multiple lines
widthfloatThe maximum width that each line should have
scalefloatThe scale to use for width measurements
Returns
- IEnumerable<string>
The split string as an enumerable of split sections
SplitStringSeparate(StringBuilder, float, float)
Splits a string to a given maximum width and returns each split section as a separate string. Note that existing new lines are taken into account for line length, but not split in the resulting strings. This method differs from SplitString(string, float, float) in that it differentiates between pre-existing newline characters and splits due to maximum width.
public IEnumerable<string> SplitStringSeparate(StringBuilder text, float width, float scale)
Parameters
textStringBuilderThe text to split into multiple lines
widthfloatThe maximum width that each line should have
scalefloatThe scale to use for width measurements
Returns
- IEnumerable<string>
The split string as an enumerable of split sections
TransformSingleCharacter(Vector2, Vector2, float, Vector2, Vector2, SpriteEffects, Vector2, Vector2)
Transforms the position of a single character to draw. In general, it is efficient to calculate the transformation matrix once at the start (using CalculateStringTransform(Vector2, float, Vector2, Vector2, SpriteEffects, Vector2)) and to then apply flipping data for each character individually (using MoveFlipped(Vector2, SpriteEffects, Vector2)).
public Vector2 TransformSingleCharacter(Vector2 stringPos, Vector2 charPosOffset, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, Vector2 stringSize, Vector2 charSize)
Parameters
stringPosVector2The position that the string is drawn at.
charPosOffsetVector2The offset from the
stringPosthat the current character is drawn at.rotationfloatThe rotation to draw with.
originVector2The origin to subtract from the position.
scaleVector2The scale to draw with.
effectsSpriteEffectsThe flipping to draw with.
stringSizeVector2The size of the string, which is only used when
effectsis not Microsoft.Xna.Framework.Graphics.SpriteEffects.None.charSizeVector2The size of the current character, which is only used when
effectsis not Microsoft.Xna.Framework.Graphics.SpriteEffects.None.
Returns
- Vector2
The transformed final draw position.
TruncateString(string, float, float, bool, string)
Truncates a string to a given width. If the string's displayed area is larger than the maximum width, the string is cut off.
Optionally, the string can be cut off a bit sooner, adding the ellipsis at the end instead.
public string TruncateString(string text, float width, float scale, bool fromBack = false, string ellipsis = "")
Parameters
textstringThe text to truncate
widthfloatThe maximum width, in display pixels based on the font and scale
scalefloatThe scale to use for width measurements
fromBackboolIf the string should be truncated from the back rather than the front
ellipsisstringThe characters to add to the end of the string if it is too long
Returns
- string
The truncated string, or the same string if it is shorter than the maximum width
TruncateString(StringBuilder, float, float, bool, string)
Truncates a string to a given width. If the string's displayed area is larger than the maximum width, the string is cut off.
Optionally, the string can be cut off a bit sooner, adding the ellipsis at the end instead.
public StringBuilder TruncateString(StringBuilder text, float width, float scale, bool fromBack = false, string ellipsis = "")
Parameters
textStringBuilderThe text to truncate
widthfloatThe maximum width, in display pixels based on the font and scale
scalefloatThe scale to use for width measurements
fromBackboolIf the string should be truncated from the back rather than the front
ellipsisstringThe characters to add to the end of the string if it is too long
Returns
- StringBuilder
The truncated string, or the same string if it is shorter than the maximum width