Class SpriteFont
Represents a base class for the sprite fonts. It supports sprite rendering and calculating text render bounds.
public abstract class SpriteFont : ISpanDrawableFont, IFont
- Inheritance
-
SpriteFont
- Implements
- Derived
- Inherited Members
- Extension Methods
Remarks
The SpriteFont class provides core functionality for rendering text using sprite-based fonts. It handles:
- Glyph positioning and metrics
- Baseline alignment
- Kerning and character spacing
- Multi-line text with proper line spacing
Constructors
SpriteFont(FontInfo, IFontAtlas, IFallbackFontResolver?)
Initializes a new instance of the SpriteFont class.
protected SpriteFont(FontInfo info, IFontAtlas atlas, IFallbackFontResolver? fontResolver)
Parameters
infoFontInfoInfo about the created font.
atlasIFontAtlasThe texture atlas containing the font's glyphs.
fontResolverIFallbackFontResolverAn instance of the service that helps with finding fallback fonts for cases when glyph is not found.
Properties
Atlas
Gets an instance of the IFontAtlas that stores all the font glyphs.
public IFontAtlas Atlas { get; }
Property Value
DefaultCodepoint
Gets or sets a codepoint of the default font character.
public int DefaultCodepoint { get; set; }
Property Value
Remarks
This character will be returned in case when requested character not found in the font.
FontResolver
Gets or sets an instance of the service to resolve fallback fonts when character is not found.
public IFallbackFontResolver? FontResolver { get; protected set; }
Property Value
Glyphs
Gets the info about loaded font glyphs.
public IReadOnlyDictionary<int, FontGlyph> Glyphs { get; }
Property Value
Info
Gets the info about this font.
public FontInfo Info { get; protected set; }
Property Value
Metrics
Gets the calculated font metrics.
public FontMetrics Metrics { get; protected set; }
Property Value
RenderSizeMultiplier
Gets or sets the multiplier for the font rendering size.
protected float RenderSizeMultiplier { get; set; }
Property Value
Methods
CalculateBounds(ReadOnlySpan<char>, in FontRenderingOptions)
Calculates estimated bounds for the string to render.
public Rectangle CalculateBounds(ReadOnlySpan<char> text, in FontRenderingOptions options)
Parameters
textReadOnlySpan<char>Text to calculate bounds.
optionsFontRenderingOptionsOptions for the calculations.
Returns
- Rectangle
Estimated bounds required for drawing.
CalculateBounds(string, in FontRenderingOptions)
Calculates estimated bounds for the string to render.
public Rectangle CalculateBounds(string text, in FontRenderingOptions options)
Parameters
textstringText to calculate bounds.
optionsFontRenderingOptionsOptions for the calculations.
Returns
- Rectangle
Estimated bounds required for drawing.
DrawString(IRenderContext, ReadOnlySpan<char>, in FontRenderingOptions)
Draws specified text using this font instance.
public void DrawString(IRenderContext context, ReadOnlySpan<char> text, in FontRenderingOptions options)
Parameters
contextIRenderContextAn instance of the render context to draw font.
textReadOnlySpan<char>Text to draw.
optionsFontRenderingOptionsOptions for the rendering.
DrawString(IRenderContext, string, in FontRenderingOptions)
Draws specified text using this font instance.
public void DrawString(IRenderContext context, string text, in FontRenderingOptions options)
Parameters
contextIRenderContextAn instance of the render context to draw font.
textstringText to draw.
optionsFontRenderingOptionsOptions for the rendering.
GetGlyph(int)
Gets the glyph for the specified character codepoint.
public virtual FontGlyph GetGlyph(int codepoint)
Parameters
codepointintCodepoint to get glyph for.
Returns
- FontGlyph
Glyph info to use with this font.
Remarks
If the requested codepoint is not found in the font, this method will attempt to return the glyph for DefaultCodepoint. If that also fails, it returns None.
GetGlyphTexture(FontGlyph)
Gets an image for the specified glyph inside this font.
protected ITexture GetGlyphTexture(FontGlyph glyph)
Parameters
glyphFontGlyphGlyph to get image for.
Returns
- ITexture
An instance of the texture atlas for the specified glyph.
GetKerning(FontGlyph, FontGlyph)
Gets the kerning between two glyphs.
protected abstract float GetKerning(FontGlyph current, FontGlyph previous)
Parameters
Returns
- float
Kerning value in pixels.
Remarks
Kerning adjusts the spacing between specific pairs of glyphs to improve visual appearance. For example, in "VA", the 'A' might be moved slightly left to tuck under the 'V'.
GetRenderGlyphs(ReadOnlySpan<char>, in FontRenderingOptions)
Enumerates glyphs to render for the specified text.
public List<RenderGlyph> GetRenderGlyphs(ReadOnlySpan<char> text, in FontRenderingOptions options)
Parameters
textReadOnlySpan<char>Text to enumerate glyphs for.
optionsFontRenderingOptionsRendering options to apply.
Returns
- List<RenderGlyph>
An enumeration for all the rendered glyphs.
GetRenderGlyphs(string, in FontRenderingOptions)
Enumerates glyphs to render for the specified text.
public List<RenderGlyph> GetRenderGlyphs(string text, in FontRenderingOptions options)
Parameters
textstringText to enumerate glyphs for.
optionsFontRenderingOptionsRendering options to apply.
Returns
- List<RenderGlyph>
An enumeration for all the rendered glyphs.
GetStyledGlyph(int)
Gets an instance of the StyledGlyphDefinition for the specified codepoint for this font instance.
protected StyledGlyphDefinition GetStyledGlyph(int codepoint)
Parameters
codepointintCharacter codepoint to get glyph info for.
Returns
- StyledGlyphDefinition
An instance of the StyledGlyphDefinition with info from this font and specified
codepoint.
MeasureAdvance(ReadOnlySpan<char>, in FontRenderingOptions)
Measures the raw cursor advance for the specified text - how far the text cursor (caret) moves after "typing" every character, including trailing whitespace that has no visible ink of its own.
public Vector2 MeasureAdvance(ReadOnlySpan<char> text, in FontRenderingOptions options)
Parameters
textReadOnlySpan<char>Text to measure.
optionsFontRenderingOptionsOptions for the measurement.
Returns
Remarks
Unlike MeasureString(string, in FontRenderingOptions)/CalculateBounds(string, in FontRenderingOptions) - which report the tightest box containing actual glyph ink, appropriate for auto-sizing content to what's visually drawn - this always reflects the true pen position, even past a glyph whose own ink extends beyond its own advance width. Caret placement and "reserve room for what was typed" sizing (e.g. TextBox) must use this, not the ink-based measurements: a glyph that overhangs its advance can otherwise make a trailing, inkless character (like a space) appear to not move the cursor at all, because the ink-based bounds were already wider than the true cursor position.
MeasureAdvance(string, in FontRenderingOptions)
Measures the raw cursor advance for the specified text - how far the text cursor (caret) moves after "typing" every character, including trailing whitespace that has no visible ink of its own.
public Vector2 MeasureAdvance(string text, in FontRenderingOptions options)
Parameters
textstringText to measure.
optionsFontRenderingOptionsOptions for the measurement.
Returns
Remarks
Unlike MeasureString(string, in FontRenderingOptions)/CalculateBounds(string, in FontRenderingOptions) - which report the tightest box containing actual glyph ink, appropriate for auto-sizing content to what's visually drawn - this always reflects the true pen position, even past a glyph whose own ink extends beyond its own advance width. Caret placement and "reserve room for what was typed" sizing (e.g. TextBox) must use this, not the ink-based measurements: a glyph that overhangs its advance can otherwise make a trailing, inkless character (like a space) appear to not move the cursor at all, because the ink-based bounds were already wider than the true cursor position.
MeasureString(ReadOnlySpan<char>, in FontRenderingOptions)
Gets the space required to draw string with this font.
public Vector2 MeasureString(ReadOnlySpan<char> text, in FontRenderingOptions options)
Parameters
textReadOnlySpan<char>The text to measure.
optionsFontRenderingOptionsOptions for the measure.
Returns
- Vector2
Maximal size in pixels to draw the string with current font parameters.
MeasureString(string, in FontRenderingOptions)
Gets the space required to draw string with this font.
public Vector2 MeasureString(string text, in FontRenderingOptions options)
Parameters
textstringThe text to measure.
optionsFontRenderingOptionsOptions for the measure.
Returns
- Vector2
Maximal size in pixels to draw the string with current font parameters.
Prepare(ReadOnlySpan<char>, in FontRenderingOptions, out int, out int)
Prepares for the rendering and calculates font parameters such as baseline and lineHeight.
protected virtual void Prepare(ReadOnlySpan<char> text, in FontRenderingOptions options, out int baseline, out int lineHeight)
Parameters
textReadOnlySpan<char>Text to prepare.
optionsFontRenderingOptionsOptions for the rendering to prepare.
baselineintThe baseline position for text rendering.
lineHeightintThe total height of a line of text.
Remarks
The baseline is the imaginary line upon which most characters sit. Some characters may descend below it (like 'g', 'j', 'p'). The line height determines the vertical space between lines of text, including ascenders and descenders.
SupportsCharacter(int)
Checks if a font supports specified codepoint instance.
public abstract bool SupportsCharacter(int codepoint)
Parameters
codepointintThe Unicode codepoint for the character to use.
Returns
TryGetDeviceGlyph(int, float, out FontGlyph)
Tries to get a glyph rasterized for the specified device scale, so text drawn under a scaling transform stays crisp.
protected virtual bool TryGetDeviceGlyph(int codepoint, float deviceScale, out FontGlyph glyph)
Parameters
codepointintThe codepoint to get the glyph for.
deviceScalefloatThe device scale, as returned by GetDeviceScale(in Transform2D).
glyphFontGlyphThe device-resolution glyph, with its size, bearing and texture region in device pixels.