AvaloniaUI/Avalonia · error · ArgumentException
Font weight must be > 0.
Error message
Font weight must be > 0.
What it means
Thrown by the Typeface constructor when the FontWeight argument is zero or negative. FontWeight must be a positive integer (typical values 100–900 in steps of 100). Avalonia validates this up front because the font matching and glyph pipeline require a meaningful weight.
Source
Thrown at src/Avalonia.Base/Media/Typeface.cs:28
/// </summary>
[DebuggerDisplay("Name = {FontFamily.Name}, Weight = {Weight}, Style = {Style}")]
public readonly struct Typeface : IEquatable<Typeface>
{
/// <summary>
/// Initializes a new instance of the <see cref="Typeface"/> class.
/// </summary>
/// <param name="fontFamily">The font family.</param>
/// <param name="style">The font style.</param>
/// <param name="weight">The font weight.</param>
/// <param name="stretch">The font stretch.</param>
public Typeface(FontFamily fontFamily,
FontStyle style = FontStyle.Normal,
FontWeight weight = FontWeight.Normal,
FontStretch stretch = FontStretch.Normal)
{
if (weight <= 0)
{
throw new ArgumentException("Font weight must be > 0.");
}
if ((int)stretch < 1)
{
throw new ArgumentException("Font stretch must be > 1.");
}
FontFamily = fontFamily ?? FontFamily.Default;
Style = style;
Weight = weight;
Stretch = stretch;
}
/// <summary>
/// Initializes a new instance of the <see cref="Typeface"/> class.
/// </summary>
/// <param name="fontFamilyName">The name of the font family.</param>
/// <param name="style">The font style.</param>View on GitHub (pinned to 11c5427268)
Solutions
- Pass a valid FontWeight such as FontWeight.Normal (400) or FontWeight.Bold (700).
- Validate any int-derived weight: if ((int)weight <= 0) weight = FontWeight.Normal.
- Ensure deserialization/config provides a weight in the 1–999 range.
Example fix
// before var typeface = new Typeface(family, weight: (FontWeight)0); // throws // after var typeface = new Typeface(family, weight: FontWeight.Normal);
Defensive patterns
Strategy: validation
Validate before calling
static Typeface SafeTypeface(FontFamily f, FontStyle st, FontWeight w, FontStretch sr)
{
if ((int)w <= 0) w = FontWeight.Normal;
return new Typeface(f, st, w, sr);
} Type guard
static bool IsValidWeight(FontWeight w) => (int)w > 0;
Prevention
- Never cast arbitrary non-positive ints to FontWeight.
- Default any unconfigured weight to FontWeight.Normal (400).
- Validate deserialized weight values before constructing a Typeface.
When it happens
Trigger: Constructing a Typeface with weight <= 0, e.g. new Typeface(family, weight: (FontWeight)0) or passing a default/uninitialized FontWeight enum that resolves to 0. Also triggered by casting an arbitrary int to FontWeight that is non-positive.
Common situations: Casting a config-driven integer to FontWeight where the value is 0 or negative; default(FontWeight) used before assignment (FontWeight.Normal is 400, but an explicit 0 cast bypasses it); deserialization of a weight field that was not populated.
Related errors
- Font stretch must be > 1.
- Could not create glyphTypeface. Font family: {FontFamily?.Na
- No cmap table found.
- Invalid {nameof(TextRunProperties.FontRenderingEmSize)}
- {nameof(textLength)} ('0') must be a non-zero value.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/7821c6b381c94aed.
Report an issue: GitHub.