Table of Contents

Class GenericFont

Namespace
MLEM.Font
Assembly
MLEM.dll

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

char

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

char

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

char

Properties

Bold

The bold version of this font.

public abstract GenericFont Bold { get; }

Property Value

GenericFont

Italic

The italic version of this font.

public abstract GenericFont Italic { get; }

Property Value

GenericFont

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

float

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

position Vector2

The position to draw at.

rotation float

The rotation to draw with.

origin Vector2

The origin to subtract from the position.

scale Vector2

The scale to draw with.

effects SpriteEffects

The flipping to draw with.

flipSize Vector2

The size of the string, which is only used when effects is 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

batch SpriteBatch

The sprite batch to draw with.

codePoint int

The code point which will be drawn.

character string

A string representation of the character which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

rotation float

A rotation of this character.

scale Vector2

A scaling of this character.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A 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

batch SpriteBatch
text string

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A 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

batch SpriteBatch
text string

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

rotation float

A rotation of this string.

origin Vector2

Center of the rotation. 0,0 by default.

scale Vector2

A scaling of this string.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A 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

batch SpriteBatch
text string

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

rotation float

A rotation of this string.

origin Vector2

Center of the rotation. 0,0 by default.

scale float

A scaling of this string.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A 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

batch SpriteBatch
text StringBuilder

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A 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

batch SpriteBatch
text StringBuilder

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

rotation float

A rotation of this string.

origin Vector2

Center of the rotation. 0,0 by default.

scale Vector2

A scaling of this string.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A 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

batch SpriteBatch
text StringBuilder

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

rotation float

A rotation of this string.

origin Vector2

Center of the rotation. 0,0 by default.

scale float

A scaling of this string.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A 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

codePoint int

The 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

text string

The text whose size to calculate

ignoreTrailingSpaces bool

Whether 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

text StringBuilder

The text whose size to calculate

ignoreTrailingSpaces bool

Whether 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

charPos Vector2

The position to move.

effects SpriteEffects

The flipping to move based on.

charSize Vector2

The size of the object to move, which is only used when effects is 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

text string

The text to split into multiple lines

width float

The maximum width that each line should have

scale float

The 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

text StringBuilder

The text to split into multiple lines

width float

The maximum width that each line should have

scale float

The 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

text string

The text to split into multiple lines

width float

The maximum width that each line should have

scale float

The 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

text StringBuilder

The text to split into multiple lines

width float

The maximum width that each line should have

scale float

The 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

stringPos Vector2

The position that the string is drawn at.

charPosOffset Vector2

The offset from the stringPos that the current character is drawn at.

rotation float

The rotation to draw with.

origin Vector2

The origin to subtract from the position.

scale Vector2

The scale to draw with.

effects SpriteEffects

The flipping to draw with.

stringSize Vector2

The size of the string, which is only used when effects is not Microsoft.Xna.Framework.Graphics.SpriteEffects.None.

charSize Vector2

The size of the current character, which is only used when effects is 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

text string

The text to truncate

width float

The maximum width, in display pixels based on the font and scale

scale float

The scale to use for width measurements

fromBack bool

If the string should be truncated from the back rather than the front

ellipsis string

The 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

text StringBuilder

The text to truncate

width float

The maximum width, in display pixels based on the font and scale

scale float

The scale to use for width measurements

fromBack bool

If the string should be truncated from the back rather than the front

ellipsis string

The 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