Text and fonts
Choose how game text is drawn (bitmap, distance field or multi-channel distance field), add outline, glow, shadow and gradient, and use images, waves and typewriter reveals in rich text.
Game text is drawn from a glyph atlas. Each text widget picks one of three rasters, and the raster decides what the text can do.
Rasters
| Raster | raster: value | What it is | Use it for |
|---|---|---|---|
| Bitmap | "bitmap" (default) | Coverage bitmaps at the device pixel size, with subpixel placement. | Body text and HUD labels at ordinary sizes. |
| Single-channel field | "sdf" | One signed distance per texel, shared by every size. | Large text that scales without re-rasterizing. |
| Multi-channel field | "msdf" | Three channels whose median is the distance, so corners stay sharp. | Titles, logos and any text with effects. |
The default stays bitmap everywhere, so a screen that never sets raster looks exactly as before. Only the field rasters carry effects.
Setting the raster in a .ui file
A Text node reads raster next to its other typography keys:
Text "Title" {
text: "Mowo"
font_size: 64.0
raster: "msdf"
}A game theme can name a raster for a font token the same way. Add raster to the Font entry of the .theme asset Engine/Source/Gameplay/MiriGraphics/Api/MiriGraphics/FMiriGraphicsTheme.h:
Fonts {
Font "title" {
family: "Geist"
size: 48.0
raster: "msdf"
}
}In C++, CMiriFontInfo::Raster holds the same choice Engine/Source/UI/Miri/Api/Miri/Text/CMiriFont.h.
The multi-channel atlas
The multi-channel atlas is created the first time a widget asks for it. A game that never draws msdf text never allocates it. It is one RGBA8 texture that starts at 1024 by 1024 texels and doubles up to the atlas limit.
Glyph fields are generated on a background thread. The first frame that draws a glyph shows a placeholder of the right shape; when the generated field is ready, the atlas uploads all the glyphs that finished together and the screen repaints once. Text never stalls a frame waiting for a glyph. A glyph that cannot be stored (a cell over 2048 texels, or a full atlas at its limit) is not drawn and logs one warning.
A glyph is generated at 48 em (96 em for text larger than that) and every smaller size reuses the same bitmap. The key also holds the spread, the distance range around the outline in texels. There are three buckets: 4, 8 and 16 texels. Plain text uses the smallest; effects that reach further from the outline (a wide glow) take a larger one automatically.
Effects
MTextBlock takes a CMiriTextEffects Engine/Source/UI/Miri/Api/Miri/Text/CMiriTextEffects.h through its Effects argument:
#include <Miri/Widgets/MTextBlock.h>
Miri::CMiriFontInfo font;
font.Size = 48.0f;
font.Raster = Miri::EMiriFontRaster::Msdf;
Miri::CMiriTextEffects effects;
effects.Outline.Width = 2.0f;
effects.Outline.Color = Miri::MiriColor(0.0f, 0.0f, 0.0f, 1.0f);
effects.Glow.Radius = 8.0f;
effects.Glow.Color = Miri::MiriColor(0.2f, 0.8f, 1.0f, 0.8f);
effects.Shadow.Enabled = true;
effects.Shadow.Offset = Miri::MiriVec2(2.0f, 3.0f);
effects.Shadow.Blur = 3.0f;
effects.Gradient.Enabled = true;
effects.Gradient.Top = Miri::MiriColor(1.0f, 1.0f, 0.6f, 1.0f);
effects.Gradient.Bottom = Miri::MiriColor(1.0f, 0.5f, 0.1f, 1.0f);
Miri::TSharedPtr<Miri::MTextBlock> title = MiriNew(Miri::MTextBlock).Font(font).Effects(effects);The block draws the passes in this order over the same glyph quads: shadow, glow, outline, then the fill, which carries the gradient from the top of the text to the bottom. Widths and radii are logical pixels and scale with the UI.
When the text colour is translucent, the passes draw into an offscreen layer at full strength and the layer fades once by the text alpha, so the outline, glow and shadow do not show through the fill. Where a layer is not available (a rotated or transformed ancestor) the passes blend one over another instead.
Glyph atlases come in four formats: bitmap (R8 coverage at a fixed size, with an RGBA8 companion for colour glyphs), SDF (R8 distance field, resolution independent), colour (RGBA8, straight alpha, emoji artwork only) and MSDF (RGBA8 multi-channel distance field, the median of RGB, generated off the main thread).
A bitmap text block ignores its effects and logs a warning once. Set the raster to sdf or msdf to use them. Text that holds colour glyphs (an emoji face) draws through the bitmap raster even in an sdf or msdf block, because the field shaders read shape and never colour; its effects are skipped and a warning is logged once.
Rich text
MRichTextBlock reads inline markup. Besides <b>, <i>, <u>, <color>, <size> and <link>, it understands three more tags.
| Tag | Effect |
|---|---|
<img id=heart size=18/> | An inline image, size logical pixels square, standing on the baseline. |
<wave amp=3 speed=6>…</wave> | Each glyph bobs on a sine wave of the clock. amp is in logical pixels, speed in radians a second. |
<type cps=20>…</type> | The text appears at cps characters a second. Several type runs reveal one after another. |
In a .ui file, a RichText node declares each image id as a property named img_<id> that holds an image path:
RichText "Pickup" {
text: "Found a <img id=heart size=18/> and <wave amp=2>luck</wave>"
img_heart: "Assets/UI/Icons/heart.png"
}From C++, call MRichTextBlock::SetImageResolver with a function that returns the texture for an id Engine/Source/UI/Miri/Api/Miri/Widgets/MRichTextBlock.h. An image that has not finished loading draws nothing until it does; the block asks again after 0.2 s, then backs off (0.4 s, 0.8 s and so on, up to 5 s) for as long as the resolver returns nothing. Link hit areas follow the text when a retained parent replays it somewhere else.
A block with an active wave or type run paints every frame while it runs, and the window it sits in keeps repainting. A type run stops costing anything once it has revealed all its text. Static rich text is retained like any other text.
Engine sources
This page describes behavior implemented in these files of the engine repository, checked against commit 23111c146.
- Engine/Source/UI/Miri/Api/Miri/Text/CMiriFont.h
- Engine/Source/UI/Miri/Api/Miri/Text/CMiriTextEffects.h
- Engine/Source/UI/Miri/Api/Miri/Widgets/MTextBlock.h
- Engine/Source/UI/Miri/Api/Miri/Widgets/MRichTextBlock.h
- Engine/Source/Gameplay/MiriGraphics/Api/MiriGraphics/FMiriGraphicsTheme.h