iOfficeAI/OfficeCLI · error · ArgumentException

Invalid font size: '{value}'. Expected a finite number (e.g.

Error message

Invalid font size: '{value}'. Expected a finite number (e.g., '12', '10.5', '14pt').

What it means

Thrown by ParseFontSize when, after stripping an optional 'pt' suffix, the token is not a finite parseable number. This catches non-numeric sizes, NaN, and infinity. The 'pt' suffix is allowed but everything else must be a plain number.

Source

Thrown at src/officecli/Core/ParseHelpers.cs:367

    /// </summary>
    public static bool IsValidBooleanString(string? value) =>
        value != null && TrimInvisible(value).ToLowerInvariant() is "true" or "1" or "yes" or "on"
                                                                 or "false" or "0" or "no" or "off";

    /// <summary>
    /// Parse a font size string, stripping optional "pt" suffix.
    /// Supports integers and fractional values (e.g. "24", "10.5", "24pt").
    /// Returns double to preserve fractional sizes for correct unit conversion.
    /// </summary>
    public static double ParseFontSize(string value)
    {
        var trimmed = value.Trim();
        if (trimmed.EndsWith("pt", StringComparison.OrdinalIgnoreCase))
            trimmed = trimmed[..^2].Trim();
        if (trimmed.Contains(','))
            throw new ArgumentException($"Invalid font size: '{value}'. Comma is not allowed — use '.' as decimal separator (e.g., '10.5').");
        if (!double.TryParse(trimmed, CultureInfo.InvariantCulture, out var result) || double.IsNaN(result) || double.IsInfinity(result))
            throw new ArgumentException($"Invalid font size: '{value}'. Expected a finite number (e.g., '12', '10.5', '14pt').");
        if (result <= 0)
            throw new ArgumentException($"Invalid font size: '{value}'. Font size must be greater than 0.");
        // OOXML w:sz/w:szCs/w:fontSize are half-points and must be >= 1.
        // Anything below 0.5pt would round to val=0 on write, producing
        // schema-invalid OOXML. Reject up front with the same shape as
        // the "<= 0" guard above.
        if (result < 0.5)
            throw new ArgumentException($"Invalid font size: '{value}'. Minimum font size is 0.5pt (one half-point).");
        // OOXML caps user-entered font size at 1638pt (Word) and Office
        // renderers stop honoring values past ~4000pt anyway. Anything
        // larger silently overflows the int32 the writers cast to (PPTX
        // writes pt × 100, Word writes pt × 2 as half-points), producing
        // negative w:sz / a:rPr@sz values Word rejects on open. Reject
        // up front with the same shape as the lower-bound guards.
        if (result > 4000)
            throw new ArgumentException($"Invalid font size: '{value}'. Maximum font size is 4000pt (Office cap).");
        return result;
    }

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Supply a plain number or number+'pt' (e.g. "12" or "12pt").
  2. Convert other units to points before calling (1px ≈ 0.75pt at 96dpi).
  3. Validate the string is non-empty and numeric before parsing.

Example fix

// before
props["fontSize"] = "12px";
ParseFontSize(props["fontSize"]); // throws 293

// after
props["fontSize"] = "9"; // 12px ≈ 9pt
ParseFontSize(props["fontSize"]);
Defensive patterns

Strategy: validation

Validate before calling

if (!double.TryParse(size.TrimEnd("ptsPT".ToCharArray()), CultureInfo.InvariantCulture, out _))
    size = "12";

Type guard

static bool IsParsableSize(string s)
{ var t = s.Trim(); if (t.EndsWith("pt", StringComparison.OrdinalIgnoreCase)) t = t[..^2].Trim();
  return double.TryParse(t, CultureInfo.InvariantCulture, out var d) && !double.IsNaN(d) && !double.IsInfinity(d); }

Try / catch

try { pts = ParseFontSize(s); }
catch (ArgumentException ex) when (ex.Message.Contains("finite number"))
{ pts = 12; }

Prevention

When it happens

Trigger: Passing "abc", "", "12px" (px is not a recognized unit), or "1e999" (overflow to infinity). Also a size field that was never set and defaulted to an empty/placeholder string.

Common situations: Unit mismatch (px/em instead of pt or bare number); a JSON config with a null/empty size that got stringified; a template placeholder left in place.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/d94ed3dd8d012074. Report an issue: GitHub.