Table of Contents

Class SpriteFont

Namespace
Icy.Rendering.Fonts
Assembly
IcyUI.dll

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

info FontInfo

Info about the created font.

atlas IFontAtlas

The texture atlas containing the font's glyphs.

fontResolver IFallbackFontResolver

An 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

IFontAtlas

DefaultCodepoint

Gets or sets a codepoint of the default font character.

public int DefaultCodepoint { get; set; }

Property Value

int

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

IFallbackFontResolver

Glyphs

Gets the info about loaded font glyphs.

public IReadOnlyDictionary<int, FontGlyph> Glyphs { get; }

Property Value

IReadOnlyDictionary<int, FontGlyph>

Info

Gets the info about this font.

public FontInfo Info { get; protected set; }

Property Value

FontInfo

Metrics

Gets the calculated font metrics.

public FontMetrics Metrics { get; protected set; }

Property Value

FontMetrics

RenderSizeMultiplier

Gets or sets the multiplier for the font rendering size.

protected float RenderSizeMultiplier { get; set; }

Property Value

float

Methods

CalculateBounds(ReadOnlySpan<char>, in FontRenderingOptions)

Calculates estimated bounds for the string to render.

public Rectangle CalculateBounds(ReadOnlySpan<char> text, in FontRenderingOptions options)

Parameters

text ReadOnlySpan<char>

Text to calculate bounds.

options FontRenderingOptions

Options 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

text string

Text to calculate bounds.

options FontRenderingOptions

Options 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

context IRenderContext

An instance of the render context to draw font.

text ReadOnlySpan<char>

Text to draw.

options FontRenderingOptions

Options 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

context IRenderContext

An instance of the render context to draw font.

text string

Text to draw.

options FontRenderingOptions

Options for the rendering.

GetGlyph(int)

Gets the glyph for the specified character codepoint.

public virtual FontGlyph GetGlyph(int codepoint)

Parameters

codepoint int

Codepoint 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

glyph FontGlyph

Glyph 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

current FontGlyph

Current glyph info.

previous FontGlyph

Previous glyph info.

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

text ReadOnlySpan<char>

Text to enumerate glyphs for.

options FontRenderingOptions

Rendering 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

text string

Text to enumerate glyphs for.

options FontRenderingOptions

Rendering 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

codepoint int

Character 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

text ReadOnlySpan<char>

Text to measure.

options FontRenderingOptions

Options for the measurement.

Returns

Vector2

The cursor's total displacement from Position.

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

text string

Text to measure.

options FontRenderingOptions

Options for the measurement.

Returns

Vector2

The cursor's total displacement from Position.

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

text ReadOnlySpan<char>

The text to measure.

options FontRenderingOptions

Options 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

text string

The text to measure.

options FontRenderingOptions

Options 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

text ReadOnlySpan<char>

Text to prepare.

options FontRenderingOptions

Options for the rendering to prepare.

baseline int

The baseline position for text rendering.

lineHeight int

The 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

codepoint int

The Unicode codepoint for the character to use.

Returns

bool

true if this font instance can render specified character; otherwise false.

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

codepoint int

The codepoint to get the glyph for.

deviceScale float

The device scale, as returned by GetDeviceScale(in Transform2D).

glyph FontGlyph

The device-resolution glyph, with its size, bearing and texture region in device pixels.

Returns

bool

true if the font can rasterize at arbitrary sizes and produced a glyph; otherwise false. The base implementation returns false (bitmap fonts are simply scaled).