GraphicsAPI reference
Class BitmapFont
Namespace: MicroEngine.Graphics
Assembly: MicroEngine.Text.dll
Guide: Text
A proportional pixel font. The built-in face (“Micro 5x7”) is authored in code so every game has crisp, license-free text without loading files. Glyph widths are derived by trimming empty columns, so narrow letters like ‘i’ take less space.
public sealed class BitmapFont : IFont, IDisposableInheritance
Implements
Extension Methods
FontExtensions.DrawCentered(IFont, IRenderer, string, Vector2, Color), FontExtensions.DrawShadowed(IFont, IRenderer, string, Vector2, Color, Color, Vector2?), FontExtensions.MeasureWidth(IFont, string), FontExtensions.Wrap(IFont, string, float)
Remarks
Every game has the built-in face in Game.Font. It is an IFont (at scale 1), like a
ScalableFont at a size, so widgets and layout code take either. Text is drawn in white glyphs multiplied by the
colour given, so any colour works. Characters without a glyph are drawn as ‘?’; ‘\n’ starts a new line.
Properties
CapHeight
Height of capital letters above the baseline.
public int CapHeight { get; }Property Value
LineHeight
Distance between the tops of two lines, in pixels (10 for the built-in face).
public int LineHeight { get; }Property Value
Spacing
Pixels between two characters (1).
public int Spacing { get; }Property Value
Methods
Advance(char)
Horizontal distance from c to the next character, spacing included, in unscaled pixels.
public int Advance(char c)Parameters
c char
Returns
CreateDefault(IGraphicsDevice?)
Creates the built-in face. Pass null for a measurement-only font (tests, layout).
public static BitmapFont CreateDefault(IGraphicsDevice? device)Parameters
device IGraphicsDevice?
Returns
Dispose()
Releases the font’s texture. The engine disposes Game.Font itself.
public void Dispose()Draw(IRenderer, string, Vector2, Color, float)
Draws text with its top-left at position.
public void Draw(IRenderer renderer, string text, Vector2 position, Color color, float scale = 1)Parameters
renderer IRenderer
The renderer.
text string
The text; ‘\n’ starts a new line.
position Vector2
Top-left of the first line, in virtual (or camera world) pixels.
color Color
Text colour.
scale float
Size multiplier; whole numbers keep the pixels crisp.
Exceptions
The font was created without a graphics device.
DrawCentered(IRenderer, string, Vector2, Color, float, Color?)
Draws text centred on center, rounded to whole pixels, with an optional 1px drop shadow.
public void DrawCentered(IRenderer renderer, string text, Vector2 center, Color color, float scale = 1, Color? shadow = null)Parameters
renderer IRenderer
text string
center Vector2
color Color
scale float
shadow Color?
DrawOutlined(IRenderer, string, Vector2, Color, Color, float)
Draws text with a full 1px outline (8 directions).
public void DrawOutlined(IRenderer renderer, string text, Vector2 position, Color color, Color outline, float scale = 1)Parameters
renderer IRenderer
text string
position Vector2
color Color
outline Color
scale float
DrawShadowed(IRenderer, string, Vector2, Color, Color, float)
Draws text with a 1px drop shadow, the most legible style over busy pixel art.
public void DrawShadowed(IRenderer renderer, string text, Vector2 position, Color color, Color shadow, float scale = 1)Parameters
renderer IRenderer
text string
position Vector2
color Color
shadow Color
scale float
Measure(string, float)
Size of text in pixels at scale: the widest line by the height of its lines (8 pixels for one line at scale 1).
public Vector2 Measure(string text, float scale = 1)Parameters
text string
scale float
Returns
MeasureWidth(string)
Width in pixels of the widest line (unscaled).
public int MeasureWidth(string text)Parameters
text string
Returns
Wrap(string, int)
Greedy word wrap that respects explicit newlines. A word wider than maxWidth on its own (a long
name, a URL) is broken between characters, so no line is wider than maxWidth.
public IReadOnlyList<string> Wrap(string text, int maxWidth)Parameters
text string
maxWidth int