GraphicsAPI reference
Class ScalableFont
Namespace: MicroEngine.Graphics
Assembly: MicroEngine.Text.dll
Guide: Graphics
A TrueType or OpenType font (TTF, OTF with TrueType or CFF outlines) that draws text at any size. Glyphs are rasterized the first time they are drawn at a size and kept in shared glyph pages (textures), so later frames only draw quads; Prepare rasterizes ahead, so the first frames of play do no font work.
public sealed class ScalableFont : IDisposableInheritance
Implements
Examples
var font = ScalableFont.Load(Graphics, "Content/fonts/Inter-Regular.ttf");font.Draw(renderer, "Score 120", new Vector2(24, 24), 32, Color.White);var title = font.At(64); // an IFont for widgets and layouttitle.DrawCentered(renderer, "Paused", new Vector2(640, 360), Color.White);Remarks
Built on FontStashSharp (StbTrueTypeSharp rasterizer). One ScalableFont serves every size; extra faces
added with AddFallback supply characters the main face lacks (another script, symbols). Missing
characters draw as ‘?’. Usually loaded and owned by Game.Assets; a font made here is disposed by its creator.
Fields
Ascii
Printable ASCII: letters, digits and punctuation, a good default for Prepare.
public const string Ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"Field Value
Properties
Filter
How the glyph pages are sampled; applies to existing and future pages.
public TextureFilter Filter { get; set; }Property Value
GlyphsRasterized
Glyphs rasterized so far, across every size (a measure of font work done).
public int GlyphsRasterized { get; }Property Value
Name
The file name (or the name given), for messages.
public string Name { get; }Property Value
Options
The options the font was opened with.
public FontOptions Options { get; }Property Value
Pages
Glyph pages created so far (each PageSize squared, 4 bytes per pixel).
public int Pages { get; }Property Value
TextureBytes
Device memory of the glyph pages, in bytes.
public long TextureBytes { get; }Property Value
Methods
AddFallback(byte[], string?)
Adds another TTF/OTF face whose glyphs are used for characters this font lacks (accents, other scripts, icons). Faces are searched in the order they were added. Glyphs already rasterized are dropped (as by Reset), so add fallbacks while loading.
public ScalableFont AddFallback(byte[] data, string? name = null)Parameters
data byte[]
name string?
Returns
Exceptions
The data is not a font the rasterizer reads.
At(float)
The font at size (pixels per em), as an IFont; the same object for the same size.
public SizedFont At(float size)Parameters
size float
Returns
Exceptions
size is not positive.
Dispose()
Releases the glyph pages. Text cannot be drawn afterwards.
public void Dispose()Draw(IRenderer, string, Vector2, float, Color)
Draws text at size with its top-left at position.
public void Draw(IRenderer renderer, string text, Vector2 position, float size, Color color)Parameters
renderer IRenderer
text string
position Vector2
size float
color Color
FromBytes(IGraphicsDevice, byte[], FontOptions?, string?)
Opens a font from the bytes of a TTF or OTF file.
public static ScalableFont FromBytes(IGraphicsDevice device, byte[] data, FontOptions? options = null, string? name = null)Parameters
device IGraphicsDevice
Creates the glyph pages.
data byte[]
The whole font file.
options FontOptions?
Rasterization and sampling; null for Default.
name string?
A name for messages and diagnostics.
Returns
Exceptions
The data is not a font the rasterizer reads.
LineHeight(float)
Distance between the tops of two lines at size.
public float LineHeight(float size)Parameters
size float
Returns
Load(IGraphicsDevice, string, FontOptions?)
Opens a TTF or OTF file.
public static ScalableFont Load(IGraphicsDevice device, string path, FontOptions? options = null)Parameters
device IGraphicsDevice
path string
options FontOptions?
Returns
Exceptions
The file does not exist.
The file is not a font the rasterizer reads; the message names it.
Measure(string, float)
Size of text at size, in canvas pixels.
public Vector2 Measure(string text, float size)Parameters
text string
size float
Returns
Prepare(string, params float[])
Rasterizes characters at each of sizes now, so drawing them later costs no
font work. Call it while loading for the text a scene will show (digits for a score, the letters of a menu).
public void Prepare(string characters, params float[] sizes)Parameters
characters string
sizes float[]
Reset()
Drops every rasterized glyph and its pages (freeing their memory); glyphs drawn later are rasterized again.
public void Reset()