spectreconsole/spectre.console · error · InvalidOperationException

Invalid Figlet font

Error message

Invalid Figlet font

What it means

Thrown by ParseHeader when the header line is null or entirely whitespace. FIGlet fonts must begin with a header line containing the signature and metadata; a blank header is treated as an invalid font. This fires before the structural field-count check.

Source

Thrown at src/Spectre.Console/Widgets/Figlet/FigletFontParser.cs:101

    }

    private static bool TryParseIndex(string index, out int result)
    {
        var style = NumberStyles.Integer;
        if (index.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
        {
            index = index.ReplaceExact("0x", string.Empty);
            style = NumberStyles.HexNumber;
        }

        return int.TryParse(index, style, CultureInfo.InvariantCulture, out result);
    }

    private static FigletHeader ParseHeader(string text)
    {
        if (string.IsNullOrWhiteSpace(text))
        {
            throw new InvalidOperationException("Invalid Figlet font");
        }

        var parts = text.SplitWords(StringSplitOptions.RemoveEmptyEntries);
        if (parts.Length < 6)
        {
            throw new InvalidOperationException("Invalid Figlet font header");
        }

        if (!IsValidSignature(parts[0]))
        {
            throw new InvalidOperationException("Invalid Figlet font header signature");
        }

        int? fullLayout = null;
        if (parts.Length > 7 && int.TryParse(parts[7], NumberStyles.Integer, CultureInfo.InvariantCulture, out var fl))
        {
            fullLayout = fl;
        }

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Inspect the font source to confirm the first line is the flf2 header.
  2. Trim leading blank lines from the source before parsing.
  3. Replace the font with a known-good FIGlet font file.

Example fix

// before
var font = FigletFont.Load(sourceWithBlankFirstLine);

// after
var trimmed = sourceWithBlankFirstLine.TrimStart('\n', '\r');
var font = FigletFont.Load(trimmed);
Defensive patterns

Strategy: validation

Validate before calling

var firstLine = source.SplitLines().FirstOrDefault();
if (string.IsNullOrWhiteSpace(firstLine)) throw new ArgumentException("Figlet header is blank");

Type guard

static bool HasValidHeaderLine(string source) => !string.IsNullOrWhiteSpace(source.SplitLines().FirstOrDefault());

Prevention

When it happens

Trigger: Parsing a FIGlet font whose first line is blank or whitespace-only, or passing a source where leading whitespace produces an empty first token sequence.

Common situations: A font file with a leading blank line before the real header, copy-paste introducing stray whitespace, or a malformed/corrupted font downloaded from an unreliable source.

Related errors


AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13). Data as JSON: /api/errors/1c0b1c62d9eb4ca5. Report an issue: GitHub.