dotnet/wpf · error · ArgumentException

SR.CharacterMetrics_MissingRequiredField

Error message

SR.CharacterMetrics_MissingRequiredField

What it means

ParseMetrics requires a fixed number of fields (NumRequiredFields). If the split yields an empty field at a required position, the constructor throws ArgumentException with SR.CharacterMetrics_MissingRequiredField because optional fields may be empty but required ones may not.

Solutions

  1. Supply all required fields in order (semicolon-separated) in the metrics string.
  2. Append empty segments only after the required fields when omitting optional values.
  3. Regenerate the metrics entry from the font metadata rather than editing it by hand.

Example fix

// before
new CharacterMetrics("0,0,0;0,0;0"); // missing required fields

// after
new CharacterMetrics("0,0,0;0,0;0;0,0;0,0;0,0;0,0;0,0;0;0;0;0"); // all required fields present
Defensive patterns

Strategy: validation

Validate before calling

static bool HasRequiredFieldCount(string metrics, int required) =>
    metrics.Split(';').Length > required;

Try / catch

try { var m = new CharacterMetrics(str); }
catch (ArgumentException ex) when (ex.Message.Contains("required")) { Log("Missing required metrics field"); m = null; }

Prevention

When it happens

Trigger: Metrics strings with too few semicolon-separated fields, trailing/empty segments in required positions, or malformed files where fields were deleted.

Common situations: Truncated composite-font metric entries; generators emitting fewer fields than required; manual edits removing a field without removing its separator correctly.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/d5f7aa132f799984. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetrics.cs:171

                if (k > i)
                {
                    // Non-empty field; convert it to double.
                    ReadOnlySpan<char> field = s.AsSpan(i, k - i);
                    if (!double.TryParse(
                        field,
                        NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
                        System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS,
                        out metrics[fieldIndex]
                        ))
                    {
                        throw new ArgumentException(SR.Format(SR.CannotConvertStringToType, field.ToString(), "double"));
                    }
                }
                else if (fieldIndex < NumRequiredFields)
                {
                    // Empty field; make sure it's an optional one.
                    throw new ArgumentException(SR.CharacterMetrics_MissingRequiredField);
                }

                ++fieldIndex;

                if (j < s.Length)
                {
                    // There's a comma so check if we've exceeded the number of fields.
                    if (fieldIndex == NumFields)
                        throw new ArgumentException(SR.CharacterMetrics_TooManyFields);

                    // Initialize character index for next iteration.
                    i = j + 1;
                }
                else
                {
                    // No more fields; check if we have all required fields.
                    if (fieldIndex < NumRequiredFields)
                    {

View on GitHub (pinned to 81131a70a4)