AvaloniaUI/Avalonia · error · ArgumentOutOfRangeException

The parameter value cannot be greater than '{MaxFontEmSize}'

Error message

The parameter value cannot be greater than '{MaxFontEmSize}'

What it means

ValidateFontSize enforces an upper bound MaxFontEmSize to prevent overflow in the layout pipeline (metrics scale with em size, and an enormous value produces out-of-range pixel rectangles or allocation failures). This guard is the second of three in the validator.

Source

Thrown at src/Avalonia.Base/Media/FormattedText.cs:109

                TextWrapping.WrapWithOverflow,
                0, // line height not specified
                0, // indentation not specified
                0
            );

            InvalidateMetrics();
        }

        private static void ValidateFontSize(double emSize)
        {
            if (emSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(emSize), "The parameter value must be greater than zero.");
            }

            if (emSize > MaxFontEmSize)
            {
                throw new ArgumentOutOfRangeException(nameof(emSize), $"The parameter value cannot be greater than '{MaxFontEmSize}'");
            }

            if (double.IsNaN(emSize))
            {
                throw new ArgumentOutOfRangeException(nameof(emSize), "The parameter value must be a number.");
            }
        }

        private static void ValidateFlowDirection(FlowDirection flowDirection, string parameterName)
        {
            if ((int)flowDirection < 0 || (int)flowDirection > (int)FlowDirection.RightToLeft)
            {
                throw new InvalidEnumArgumentException(parameterName, (int)flowDirection, typeof(FlowDirection));
            }
        }

        private int ValidateRange(int startIndex, int count)
        {

View on GitHub (pinned to 11c5427268)

Solutions

  1. Clamp emSize to a sane maximum (e.g. Math.Min(emSize, FormattedText.MaxFontEmSize)) before construction.
  2. Constrain the input source (slider Maximum, config schema range, numeric up-down upper bound).
  3. Verify the unit; if you meant points, ensure the conversion is correct and not amplified twice.
  4. Unit-test the binding with extreme values (0, negative, MaxValue, NaN, Infinity).

Example fix

// before
var size = baseSize * zoomFactor; // zoomFactor unbounded
var ft = new FormattedText(text, font, size, ...);

// after
const double maxSize = 35791; // FormattedText.MaxFontEmSize reference
var size = Math.Clamp(baseSize * zoomFactor, 1.0, maxSize);
var ft = new FormattedText(text, font, size, ...);
Defensive patterns

Strategy: validation

Validate before calling

static double ClampEmSize(double v)
{
    const double MaxEm = 35791;
    if (!double.IsFinite(v)) return 12.0;
    return Math.Clamp(v, 1.0, MaxEm);
}

Prevention

When it happens

Trigger: Binding FontSize to a high-value slider without an upper clamp; passing a value computed by a misconfigured zoom factor (e.g. *1000); unit confusion where points/pixels/ems are mixed; an animation overshooting past MaxFontEmSize.

Common situations: A 'zoom' feature multiplying the base font size by an unbounded scale; XAML where FontSize is set in code-behind from a config value with no ceiling; binding to a TextBox input parsed as double without clamping.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/d72c2cd48a031f1e. Report an issue: GitHub.