dotnet/wpf · error · InvalidEnumArgumentException
styleSimulations
Error message
styleSimulations
What it means
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.
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.
Example fix
// before
var typeface = new GlyphTypeface(uri, (StyleSimulations)(styleFlag | 8)); // InvalidEnumArgumentException
// after
StyleSimulations sim = Enum.IsDefined(typeof(StyleSimulations), styleFlag)
? (StyleSimulations)styleFlag
: StyleSimulations.None;
var typeface = new GlyphTypeface(uri, sim); Defensive patterns
Strategy: validation
Validate before calling
static StyleSimulations ToValidStyleSimulations(int raw) =>
Enum.IsDefined(typeof(StyleSimulations), raw)
? (StyleSimulations)raw
: StyleSimulations.None;
// or validate before use:
if (!Enum.IsDefined(typeof(StyleSimulations), (int)sim))
throw new ArgumentOutOfRangeException(nameof(sim)); Type guard
static bool IsDefinedStyleSimulation(StyleSimulations sim) =>
sim is StyleSimulations.None
or StyleSimulations.ItalicSimulation
or StyleSimulations.BoldSimulation
or StyleSimulations.BoldItalicSimulation; Try / catch
try { var typeface = new GlyphTypeface(uri, sim); }
catch (InvalidEnumArgumentException ex) when (ex.ParamName == "styleSimulations")
{
var typeface = new GlyphTypeface(uri, StyleSimulations.None); // safe default
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "invalid argument", "unknown mode", "not supported": invalid enum-like argument errors explained — this error's family across 19 libraries.
Related errors
- SR.UriNotAbsolute
- ArgumentOutOfRangeException(authentication)
- ArgumentOutOfRangeException(userActivationMode)
- autoComplete
- captureMode
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/5da3597096934d85.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs:131
ArgumentNullException.ThrowIfNull(typefaceSource);
if (!typefaceSource.IsAbsoluteUri)
throw new ArgumentException(SR.UriNotAbsolute, nameof(typefaceSource));
// remember the original Uri that contains face index
_originalUri = typefaceSource;
// split the Uri into the font source Uri and face index
Uri fontSourceUri;
int faceIndex;
Util.SplitFontFaceIndex(typefaceSource, out fontSourceUri, out faceIndex);
if ( styleSimulations != StyleSimulations.None
&& styleSimulations != StyleSimulations.ItalicSimulation
&& styleSimulations != StyleSimulations.BoldSimulation
&& styleSimulations != StyleSimulations.BoldItalicSimulation)
{
throw new InvalidEnumArgumentException("styleSimulations", (int)styleSimulations, typeof(StyleSimulations));
}
_styleSimulations = styleSimulations;
MS.Internal.Text.TextInterface.FontCollection fontCollection = DWriteFactory.GetFontCollectionFromFile(fontSourceUri);
using (MS.Internal.Text.TextInterface.FontFace fontFaceDWrite = DWriteFactory.Instance.CreateFontFace(fontSourceUri,
(uint)faceIndex,
(MS.Internal.Text.TextInterface.FontSimulations)styleSimulations))
{
// This is the same behavior as 3.*. If we pass, for example, a path to a composite font file then a
// FileFormatException will be thrown!
if (fontFaceDWrite == null)
{
throw new System.IO.FileFormatException(typefaceSource);
}
_font = fontCollection.GetFontFromFontFace(fontFaceDWrite);
}
View on GitHub (pinned to 81131a70a4)