AvaloniaUI/Avalonia · error · ArgumentOutOfRangeException
Invalid {nameof(TextRunProperties.FontRenderingEmSize)}
Error message
Invalid {nameof(TextRunProperties.FontRenderingEmSize)} What it means
Thrown by the TextCharacters constructor when the supplied TextRunProperties has a FontRenderingEmSize that is zero or negative. FontRenderingEmSize represents the font size in EM units used to render the text run; it must be a strictly positive number. Avalonia rejects non-positive sizes early because the text layout engine cannot shape or measure glyphs without a valid em size.
Source
Thrown at src/Avalonia.Base/Media/TextFormatting/TextCharacters.cs:35
private const char WordJoiner = '\u2060';
private static readonly string s_wordJoinerRun = new string(WordJoiner, 8);
/// <summary>
/// Constructs a run for text content from a string.
/// </summary>
public TextCharacters(string text, TextRunProperties textRunProperties)
: this(text.AsMemory(), textRunProperties)
{
}
/// <summary>
/// Constructs a run for text content from a memory region.
/// </summary>
public TextCharacters(ReadOnlyMemory<char> text, TextRunProperties textRunProperties)
{
if (textRunProperties.FontRenderingEmSize <= 0)
{
throw new ArgumentOutOfRangeException(nameof(textRunProperties), textRunProperties.FontRenderingEmSize,
$"Invalid {nameof(TextRunProperties.FontRenderingEmSize)}");
}
Text = text;
Properties = textRunProperties;
}
/// <inheritdoc />
public override int Length
=> Text.Length;
/// <inheritdoc />
public override ReadOnlyMemory<char> Text { get; }
/// <inheritdoc />
public override TextRunProperties Properties { get; }
/// <summary>View on GitHub (pinned to 11c5427268)
Solutions
- Ensure the TextRunProperties.FontRenderingEmSize is set to a positive value (e.g. 12) before constructing TextCharacters.
- If building GenericTextRunProperties programmatically, pass a valid typeface with the desired font size in the constructor or set the property explicitly.
- Validate the font size at the call site with a guard: if (props.FontRenderingEmSize <= 0) throw or assign a sane default.
- Check upstream style/template binding to confirm a font size is actually resolved at runtime.
Example fix
// before
var props = new GenericTextRunProperties(typeface, 0); // FontRenderingEmSize = 0
var run = new TextCharacters("hello", props); // throws
// after
var props = new GenericTextRunProperties(typeface, 14);
var run = new TextCharacters("hello", props); Defensive patterns
Strategy: validation
Validate before calling
static TextCharacters SafeCreate(string text, TextRunProperties props)
{
if (props.FontRenderingEmSize <= 0)
throw new ArgumentException("FontRenderingEmSize must be > 0.", nameof(props));
return new TextCharacters(text, props);
} Type guard
static bool IsValidTextRunProperties(TextRunProperties p)
=> p != null && p.FontRenderingEmSize > 0; Prevention
- Always set FontRenderingEmSize explicitly when constructing TextRunProperties.
- Add an assertion or guard at the boundary where properties are built.
- Avoid custom TextRunProperties subclasses that default FontRenderingEmSize to 0.
When it happens
Trigger: Constructing a new TextCharacters(string, TextRunProperties) or TextCharacters(ReadOnlyMemory<char>, TextRunProperties) where the GenericTextRunProperties.FontRenderingEmSize (or any TextRunProperties implementation) is set to 0, a negative value, or left at a default of 0 on a custom properties object.
Common situations: Creating a custom TextRunProperties implementation whose FontRenderingEmSize defaults to 0; passing a properties object that was only partially initialized; copying properties from a style or template that did not set a font size; unit confusion where a size in points or pixels was never assigned.
Related errors
- {nameof(textLength)} ('0') must be a non-zero value.
- The parameter value must be greater than zero.
- The parameter value cannot be greater than '{MaxFontEmSize}'
- The parameter value must be a number.
- Font weight must be > 0.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/999f7ca24064a897.
Report an issue: GitHub.