GraphicsAPI reference
Class ShapedFont
Namespace: MicroEngine.Graphics
Assembly: MicroEngine.Text.Shaping.dll
Guide: Graphics
A TrueType or OpenType font that shapes its text: letters take the forms their neighbours call for (Arabic’s joined forms, Indic conjuncts, ligatures), marks sit where the font places them, and right-to-left scripts (Arabic, Hebrew) run right to left. HarfBuzz chooses the glyphs and their positions; each glyph is rasterized the first time it is drawn at a size and kept in glyph pages (textures), so later frames only draw quads.
public sealed class ShapedFont : IDisposableInheritance
Implements
Examples
var font = ShapedFont.FromBytes(Graphics, Assets.Bytes("fonts/Inter-Regular.ttf")) .AddFallback(Assets.Bytes("fonts/NotoSansArabic-Regular.ttf"));font.At(32).Draw(renderer, "مرحبا", new Vector2(24, 24), Color.White);Remarks
Use it in place of a [ScalableFont](/docs/api/microengine.graphics.scalablefont/) for text in a script that needs shaping; [At](/docs/api/microengine.graphics.shapedfont/#MicroEngine_Graphics_ShapedFont_At_System_Single_) gives an [IFont](/docs/api/microengine.graphics.ifont/), so widgets, layout and wrapping work with it unchanged. Faces added with [AddFallback](/docs/api/microengine.graphics.shapedfont/#MicroEngine_Graphics_ShapedFont_AddFallback_System_Byte___System_String_) supply the letters the main face lacks, a run of text at a time, so a Latin face with an Arabic and a Hebrew one after it draws all three. Letters no face has draw as the last face's missing-glyph box.
Lines that mix directions follow Unicode's bidirectional algorithm (UAX #9): a line reads in the direction of its first letter that has one ([IsRightToLeft](/docs/api/microengine.graphics.shapedfontsize/#MicroEngine_Graphics_ShapedFontSize_IsRightToLeft_System_String_)), and runs of the other direction inside it, a name or a number in an Arabic sentence, read in theirs. Explicit direction marks (embeddings, overrides, isolates) are ignored and brackets are not paired across directions.
Properties
Faces
How many faces the font has: the main one, then its fallbacks in the order they were added.
public int Faces { get; }Property Value
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 (Kerning does not apply: the font’s own positioning always does).
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 for the letters the faces before it lack (another script, symbols). Text already shaped is shaped again, so add fallbacks while loading.
public ShapedFont AddFallback(byte[] data, string? name = null)Parameters
data byte[]
name string?
Returns
Exceptions
The data is not a font the engine reads.
At(float)
The font at size (the same size as a ScalableFont’s), as an IFont; the same object for the same size.
public ShapedFontSize At(float size)Parameters
size float
Returns
Exceptions
size is not positive.
Dispose()
Releases the glyph pages and the faces. Text cannot be drawn afterwards.
public void Dispose()FromBytes(IGraphicsDevice, byte[], FontOptions?, string?)
Opens a font from the bytes of a TTF or OTF file.
public static ShapedFont 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 engine reads.
Load(IGraphicsDevice, string, FontOptions?)
Opens a TTF or OTF file.
public static ShapedFont 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 engine reads; the message names it.
Reset()
Drops every rasterized glyph and its pages (freeing their memory); glyphs drawn later are rasterized again.
public void Reset()