Class SystemFonts
- Namespace
- Stride.CommunityToolkit.Rendering.Text
- Assembly
- Stride.CommunityToolkit.dll
Loads fonts that are installed on the machine, so text can be drawn in something other than Stride's default font without an asset pipeline or a font file shipped alongside the game.
public static class SystemFonts
- Inheritance
-
SystemFonts
Examples
var font = game.LoadSystemFont("Segoe UI", 48);
entity.Add(new WorldTextComponent { Text = "Docking clamp", Font = font, FontSize = 48 });
Remarks
A code-only game has no compiled font assets, so Stride's default font - bold, proportional, and the same in every such game - is the only one it gets for free. This finds a family's file in the operating system's font folders, registers it with Stride's font system and rasterises it at the size asked for, producing exactly what Font, Font and the debug overlay all accept.
Rasterised, not scaled: the font is sharp at whatever size it was loaded at, and a size well above what you draw at costs glyph cache space for nothing.
The result belongs to the caller. Loading the same family and size twice returns two separate fonts, so load each one once and keep it, and dispose it if the game outlives its use.
Properties
MonospaceCandidates
Monospace families to try, in order, on the current operating system. The same idea as SansSerifCandidates, for the fixed-pitch look of console and debug text.
public static IReadOnlyList<string> MonospaceCandidates { get; }
Property Value
SansSerifCandidates
Proportional families to try, in order, on the current operating system: the platform's usual screen font first, then the metric-compatible families the other platforms ship, so something is nearly always found without any font being bundled.
public static IReadOnlyList<string> SansSerifCandidates { get; }
Property Value
Methods
FindFile(string, FontStyle)
Looks for the file of a font family in the operating system's font folders, using the known file names of the common families and the usual naming conventions for the rest.
public static string? FindFile(string family, FontStyle style = FontStyle.Regular)
Parameters
Returns
Load(IServiceRegistry, string, float, FontStyle, string?)
Loads an installed font family, or returns null if it is not installed.
public static SpriteFont? Load(IServiceRegistry services, string family, float size, FontStyle style = FontStyle.Regular, string? fontFile = null)
Parameters
servicesIServiceRegistryThe game's services, which must include Stride's font system.
familystringThe family name as the system knows it, such as
"Segoe UI".sizefloatThe height the glyphs are rasterised at, in pixels.
styleFontStyleThe weight and slant wanted. Defaults to Regular.
fontFilestringThe TrueType file to register the family from, for fonts that are not in the system font folders. null, the default, searches those folders.
Returns
- SpriteFont
The font, or null if no file for the family was found.
LoadFirst(IServiceRegistry, IEnumerable<string>, float, FontStyle, string?)
Loads the first of several families that is installed - the way to ask for "a monospace font" rather than one specific one, since no single family is present on every machine.
public static SpriteFont? LoadFirst(IServiceRegistry services, IEnumerable<string> families, float size, FontStyle style = FontStyle.Regular, string? fontFile = null)
Parameters
servicesIServiceRegistryThe game's services, which must include Stride's font system.
familiesIEnumerable<string>Family names in order of preference, such as MonospaceCandidates.
sizefloatThe height the glyphs are rasterised at, in pixels.
styleFontStyleThe weight and slant wanted. Defaults to Regular.
fontFilestringA TrueType file to register the first family from, bypassing the search.
Returns
- SpriteFont
The font, or null if none of the families was found.