{"record":{"id":"d5f7aa132f799984","repo":"dotnet/wpf","slug":"sr-charactermetrics-missingrequiredfield","errorCode":null,"errorMessage":"SR.CharacterMetrics_MissingRequiredField","messagePattern":"SR\\.CharacterMetrics_MissingRequiredField","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetrics.cs","lineNumber":171,"sourceCode":"\n                if (k > i)\n                {\n                    // Non-empty field; convert it to double.\n                    ReadOnlySpan<char> field = s.AsSpan(i, k - i);\n                    if (!double.TryParse(\n                        field,\n                        NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,\n                        System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS,\n                        out metrics[fieldIndex]\n                        ))\n                    {\n                        throw new ArgumentException(SR.Format(SR.CannotConvertStringToType, field.ToString(), \"double\"));\n                    }\n                }\n                else if (fieldIndex < NumRequiredFields)\n                {\n                    // Empty field; make sure it's an optional one.\n                    throw new ArgumentException(SR.CharacterMetrics_MissingRequiredField);\n                }\n\n                ++fieldIndex;\n\n                if (j < s.Length)\n                {\n                    // There's a comma so check if we've exceeded the number of fields.\n                    if (fieldIndex == NumFields)\n                        throw new ArgumentException(SR.CharacterMetrics_TooManyFields);\n\n                    // Initialize character index for next iteration.\n                    i = j + 1;\n                }\n                else\n                {\n                    // No more fields; check if we have all required fields.\n                    if (fieldIndex < NumRequiredFields)\n                    {","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetrics.cs#L153-L189","documentation":"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.","triggerScenarios":"Metrics strings with too few semicolon-separated fields, trailing/empty segments in required positions, or malformed files where fields were deleted.","commonSituations":"Truncated composite-font metric entries; generators emitting fewer fields than required; manual edits removing a field without removing its separator correctly.","solutions":["Supply all required fields in order (semicolon-separated) in the metrics string.","Append empty segments only after the required fields when omitting optional values.","Regenerate the metrics entry from the font metadata rather than editing it by hand."],"exampleFix":"// before\nnew CharacterMetrics(\"0,0,0;0,0;0\"); // missing required fields\n\n// after\nnew 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","handlingStrategy":"validation","validationCode":"static bool HasRequiredFieldCount(string metrics, int required) =>\n    metrics.Split(';').Length > required;","typeGuard":null,"tryCatchPattern":"try { var m = new CharacterMetrics(str); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"required\")) { Log(\"Missing required metrics field\"); m = null; }","preventionTips":["Ensure metrics strings contain all required fields in order before the optional trailing ones.","Validate field count on import of composite-font metric files.","Generate metrics entries from tooling that always emits the full required field set."],"tags":["wpf","parsing","typography","missing-field"],"backgroundTag":"empty-required-field","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}