AvaloniaUI/Avalonia · error · InvalidDataException

Invalid fill rule.

Error message

Invalid fill rule.

What it means

Thrown by SetFillRule when the fill-rule argument following an 'F' command is missing or not exactly one character. The parser expects exactly one char ('0' for EvenOdd, '1' for NonZero) immediately after F. ReadArgument returns false (end of string / no number) or the slice length is not 1, so the token cannot be interpreted as a fill rule.

Source

Thrown at src/Avalonia.Base/Media/PathMarkupParser.cs:201

            _geometryContext.BeginFigure(_currentPoint);

            _beginFigurePoint = _currentPoint;

            _isOpen = true;
        }

        private void SetFillRule(
#if NET7SDK
            scoped
#endif
            ref ReadOnlySpan<char> span)
        {
            ThrowIfDisposed();

            if (!ReadArgument(ref span, out var fillRule) || fillRule.Length != 1)
            {
                throw new InvalidDataException("Invalid fill rule.");
            }

            FillRule rule;

            switch (fillRule[0])
            {
                case '0':
                    rule = FillRule.EvenOdd;
                    break;
                case '1':
                    rule = FillRule.NonZero;
                    break;
                default:
                    throw new InvalidDataException("Invalid fill rule");
            }

            _geometryContext.SetFillRule(rule);
        }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Use 'F0' for EvenOdd or 'F1' for NonZero, exactly one digit, no letters.
  2. Place the F command at the start of the path before any figure commands.
  3. Remove the F command entirely if you want the default fill rule (no fill-rule mutation).

Example fix

// before
geometry.Parse("FEvenOdd M0,0 L10,10 Z");

// after
geometry.Parse("F0 M0,0 L10,10 Z");  // 0 = EvenOdd, 1 = NonZero
Defensive patterns

Strategy: validation

Validate before calling

static bool HasValidFillRule(ReadOnlySpan<char> s)
{
    var i = 0;
    while (i < s.Length && char.IsWhiteSpace(s[i])) i++;
    return i < s.Length && (s[i] == '0' || s[i] == '1');
}
// before parsing, strip the F token and verify the next char is 0 or 1.

Type guard

static bool IsValidFillRuleChar(char c) => c is '0' or '1';

Try / catch

try { parser.Parse(data); }
catch (InvalidDataException ex) when (ex.Message.StartsWith("Invalid fill rule"))
{ /* report malformed F command to the user/data source */ }

Prevention

When it happens

Trigger: Path strings such as 'F' (no argument), 'F12' (two chars), or 'F ' followed by end of data. Also 'F0,1' where the parser reads a multi-char token.

Common situations: Hand-authoring SVG-style path data with an F command; copying a 'fill-rule' attribute value ('EvenOdd'/'NonZero') verbatim instead of the numeric 0/1 token this parser expects; truncating a path string mid-token.

Related errors


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