{"record":{"id":"5da3597096934d85","repo":"dotnet/wpf","slug":"stylesimulations","errorCode":null,"errorMessage":"styleSimulations","messagePattern":"styleSimulations","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs","lineNumber":131,"sourceCode":"            ArgumentNullException.ThrowIfNull(typefaceSource);\n\n            if (!typefaceSource.IsAbsoluteUri)\n                throw new ArgumentException(SR.UriNotAbsolute, nameof(typefaceSource));\n\n            // remember the original Uri that contains face index\n            _originalUri = typefaceSource;\n\n            // split the Uri into the font source Uri and face index\n            Uri fontSourceUri;\n            int faceIndex;\n            Util.SplitFontFaceIndex(typefaceSource, out fontSourceUri, out faceIndex);\n\n            if (   styleSimulations != StyleSimulations.None \n                && styleSimulations != StyleSimulations.ItalicSimulation \n                && styleSimulations != StyleSimulations.BoldSimulation\n                && styleSimulations != StyleSimulations.BoldItalicSimulation)\n            {\n                throw new InvalidEnumArgumentException(\"styleSimulations\", (int)styleSimulations, typeof(StyleSimulations));\n            }\n\n            _styleSimulations = styleSimulations;\n\n            MS.Internal.Text.TextInterface.FontCollection fontCollection = DWriteFactory.GetFontCollectionFromFile(fontSourceUri);\n            using (MS.Internal.Text.TextInterface.FontFace fontFaceDWrite = DWriteFactory.Instance.CreateFontFace(fontSourceUri,\n                                                                                                                  (uint)faceIndex,\n                                                                                                                  (MS.Internal.Text.TextInterface.FontSimulations)styleSimulations))\n            {\n                // This is the same behavior as 3.*. If we pass, for example, a path to a composite font file then a \n                // FileFormatException will be thrown!\n                if (fontFaceDWrite == null)\n                {\n                    throw new System.IO.FileFormatException(typefaceSource);             \n                }\n                _font = fontCollection.GetFontFromFontFace(fontFaceDWrite);\n            }\n","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs#L113-L149","documentation":"GlyphTypeface.Initialize validates that styleSimulations is one of the defined StyleSimulations values (None, ItalicSimulation, BoldSimulation, BoldItalicSimulation) and throws InvalidEnumArgumentException(\"styleSimulations\", ...) for any other integer. This protects DirectWrite from receiving an undefined simulation flag when the font face is created.","triggerScenarios":"new GlyphTypeface(uri, (StyleSimulations)99) or casting an arbitrary int/foreign enum into StyleSimulations; passing a Flags-combined value like ItalicSimulation | BoldSimulation (bitwise OR of enums is not a defined member here since BoldItalicSimulation is its own value); deserializing the value from XML/config as an unvalidated int.","commonSituations":"Reading StyleSimulations from configuration or command-line as a raw integer; mapping from another library's font-style enum with (StyleSimulations)(int)otherValue; math like value * 2 on the enum producing an undefined combination.","solutions":["Pass one of the exact StyleSimulations members: None, ItalicSimulation, BoldSimulation, or BoldItalicSimulation.","Validate the value with Enum.IsDefined(typeof(StyleSimulations), value) before casting, and default to StyleSimulations.None when undefined.","When mapping from another enum, translate explicitly (e.g. bold+italic -> StyleSimulations.BoldItalicSimulation) instead of casting the raw integer."],"exampleFix":"// before\nvar typeface = new GlyphTypeface(uri, (StyleSimulations)(styleFlag | 8)); // InvalidEnumArgumentException\n\n// after\nStyleSimulations sim = Enum.IsDefined(typeof(StyleSimulations), styleFlag)\n    ? (StyleSimulations)styleFlag\n    : StyleSimulations.None;\nvar typeface = new GlyphTypeface(uri, sim);","handlingStrategy":"validation","validationCode":"static StyleSimulations ToValidStyleSimulations(int raw) =>\n    Enum.IsDefined(typeof(StyleSimulations), raw)\n        ? (StyleSimulations)raw\n        : StyleSimulations.None;\n// or validate before use:\nif (!Enum.IsDefined(typeof(StyleSimulations), (int)sim))\n    throw new ArgumentOutOfRangeException(nameof(sim));","typeGuard":"static bool IsDefinedStyleSimulation(StyleSimulations sim) =>\n    sim is StyleSimulations.None\n       or StyleSimulations.ItalicSimulation\n       or StyleSimulations.BoldSimulation\n       or StyleSimulations.BoldItalicSimulation;","tryCatchPattern":"try { var typeface = new GlyphTypeface(uri, sim); }\ncatch (InvalidEnumArgumentException ex) when (ex.ParamName == \"styleSimulations\")\n{\n    var typeface = new GlyphTypeface(uri, StyleSimulations.None); // safe default\n}","preventionTips":["Never cast raw ints or foreign enums into StyleSimulations; validate with Enum.IsDefined first.","Do not bitwise-OR StyleSimulations values; use the dedicated BoldItalicSimulation member instead.","When the value comes from config/XML, parse it with Enum.TryParse and reject failures before use."],"tags":["wpf","glyphtypeface","enum","fonts","argument-validation"],"backgroundTag":"invalid-enum-argument","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"}