{"record":{"id":"3ceeb15c78172403","repo":"dotnet/wpf","slug":"sr-faceindexmustbepositiveorzero","errorCode":null,"errorMessage":"SR.FaceIndexMustBePositiveOrZero","messagePattern":"SR\\.FaceIndexMustBePositiveOrZero","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs","lineNumber":495,"sourceCode":"            // The only absolute URIs we allow in font family references are \"file:\" URIs.\n            return absoluteUri.IsFile;\n        }\n\n\n        internal static void SplitFontFaceIndex(Uri fontUri, out Uri fontSourceUri, out int faceIndex)\n        {\n            // extract face index\n            string fragment = fontUri.GetComponents(UriComponents.Fragment, UriFormat.SafeUnescaped);\n            if (!String.IsNullOrEmpty(fragment))\n            {\n                if (!int.TryParse(\n                        fragment,\n                        NumberStyles.None,\n                        CultureInfo.InvariantCulture,\n                        out faceIndex\n                    ))\n                {\n                    throw new ArgumentException(SR.FaceIndexMustBePositiveOrZero, nameof(fontUri));\n                }\n\n                // face index was specified in a fragment, we need to strip off fragment from the source Uri\n                fontSourceUri = new Uri(fontUri.GetComponents(Util.UriWithoutFragment, UriFormat.SafeUnescaped));\n            }\n            else\n            {\n                // simple case, no face index specified\n                faceIndex = 0;\n                fontSourceUri = fontUri;\n            }\n        }\n\n        internal static Uri CombineUriWithFaceIndex(string fontUri, int faceIndex)\n        {\n            if (faceIndex == 0)\n                return new Uri(fontUri);\n","sourceCodeStart":477,"sourceCodeEnd":513,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs#L477-L513","documentation":"Util.SplitFontFaceIndex parses a '#<index>' fragment appended to a font URI to extract a TrueType collection face index. If the fragment exists but is not a non-negative integer parseable with NumberStyles.None, it throws ArgumentException with SR.FaceIndexMustBePositiveOrZero.","triggerScenarios":"Passing a GlyphTypeface/font Uri whose fragment (text after '#') is not a valid non-negative integer, e.g. 'pack://...:,,,/fonts/MyFont.ttf#face1' or 'MyFont.ttf#-2'.","commonSituations":"Hand-written font URIs where the author intended a font family name in the fragment (family-name syntax like 'MyFont.ttf#My Family') but the API expects a numeric collection face index; URL-encoded or localized digits in the fragment.","solutions":["Use a plain non-negative integer in the URI fragment, e.g. 'MyFont.ttf#0' for the first face of a collection.","If you meant to select a family, use the GlyphTypeface family-name constructor instead of a fragment.","Strip or fix the fragment before constructing the Uri.","Parse/validate the fragment with int.TryParse(NumberStyles.None, InvariantCulture) before passing the Uri."],"exampleFix":"// before\nvar gt = new GlyphTypeface(new Uri(\"pack://application:,,,/Fonts/MyFont.ttf#My Family\"));\n// after\nvar gt = new GlyphTypeface(new Uri(\"pack://application:,,,/Fonts/MyFont.ttf#0\")); // face index 0","handlingStrategy":"validation","validationCode":"static bool HasValidFaceIndex(Uri fontUri, out int faceIndex)\n{\n    faceIndex = 0;\n    string frag = fontUri.Fragment;\n    if (string.IsNullOrEmpty(frag) || frag == \"#\") return true;\n    return int.TryParse(frag.Substring(1), NumberStyles.None,\n        CultureInfo.InvariantCulture, out faceIndex) && faceIndex >= 0;\n}","typeGuard":"static bool IsNumericFaceFragment(Uri fontUri) =>\n    fontUri.Fragment.Length > 1 &&\n    uint.TryParse(fontUri.Fragment.Substring(1), NumberStyles.None,\n        CultureInfo.InvariantCulture, out _);","tryCatchPattern":"try\n{\n    var gt = new GlyphTypeface(fontUri);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"FaceIndex\"))\n{\n    // fix: use numeric face index fragment like '#0', or family-name constructor\n}","preventionTips":["Always use a plain non-negative integer after '#' for collection face selection.","Never put family names in a font file Uri fragment.","Validate the fragment with int.TryParse(NumberStyles.None) before constructing the Uri."],"tags":["argument","font-uri","parsing","wpf"],"backgroundTag":"invalid-argument-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}