AvaloniaUI/Avalonia · error · ArgumentException
Font stretch must be > 1.
Error message
Font stretch must be > 1.
What it means
Thrown by the Typeface constructor when FontStretch, cast to int, is less than 1. FontStretch values are positive integers (e.g. FontStretch.Normal is 5). A stretch of 0 or negative is invalid because the font selection pipeline cannot match it.
Source
Thrown at src/Avalonia.Base/Media/Typeface.cs:33
/// 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>
/// <param name="weight">The font weight.</param>
/// <param name="stretch">The font stretch.</param>
public Typeface(string fontFamilyName,
FontStyle style = FontStyle.Normal,
FontWeight weight = FontWeight.Normal,View on GitHub (pinned to 11c5427268)
Solutions
- Use a valid FontStretch such as FontStretch.Normal (5) or FontStretch.Condensed (3).
- Validate: if ((int)stretch < 1) stretch = FontStretch.Normal.
- Ensure any deserialized stretch value falls in the valid positive range.
Example fix
// before var typeface = new Typeface(family, stretch: (FontStretch)0); // throws // after var typeface = new Typeface(family, stretch: FontStretch.Normal);
Defensive patterns
Strategy: validation
Validate before calling
static Typeface SafeTypefaceStretch(FontFamily f, FontStyle st, FontWeight w, FontStretch sr)
{
if ((int)sr < 1) sr = FontStretch.Normal;
return new Typeface(f, st, w, sr);
} Type guard
static bool IsValidStretch(FontStretch s) => (int)s >= 1;
Prevention
- Default any unconfigured stretch to FontStretch.Normal (5).
- Validate deserialized stretch values fall in the positive range.
- Avoid casting non-positive ints to FontStretch.
When it happens
Trigger: Constructing a Typeface with stretch cast to int < 1, e.g. new Typeface(family, stretch: (FontStretch)0). This happens when an uninitialized or out-of-range stretch value is supplied.
Common situations: Casting a numeric config value to FontStretch where it is 0 or negative; default(FontStretch) confusion (the Normal default is 5, not 0); deserialization of a stretch field left unset.
Related errors
- Font weight must be > 0.
- 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/658bbb32f7cd3b98.
Report an issue: GitHub.