{"record":{"id":"67c9bc9a6339e27c","repo":"dotnet/wpf","slug":"notsupportedexception-glyphtypeface","errorCode":null,"errorMessage":"NotSupportedException","messagePattern":"NotSupportedException","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs","lineNumber":1737,"sourceCode":"        private delegate double GlyphAccessor(ushort glyphIndex, float pixelsPerDip, TextFormattingMode textFormattingMode, bool isSideways);\n\n        /// <summary>\n        /// This class is a helper to implement named indexers\n        /// for glyph metrics.\n        /// </summary>\n        private class GlyphIndexer : IDictionary<ushort, double>\n        {\n            internal GlyphIndexer(GlyphAccessor accessor, ushort numberOfGlyphs)\n            {\n                _accessor = accessor;\n                _numberOfGlyphs = numberOfGlyphs;\n            }\n\n            #region IDictionary<ushort,double> Members\n\n            public void Add(ushort key, double value)\n            {\n                throw new NotSupportedException();\n            }\n\n            public bool ContainsKey(ushort key)\n            {\n                return (key < _numberOfGlyphs);\n            }\n\n            public ICollection<ushort> Keys\n            {\n                get { return new SequentialUshortCollection(_numberOfGlyphs); }\n            }\n\n            public bool Remove(ushort key)\n            {\n                throw new NotSupportedException();\n            }\n\n            public bool TryGetValue(ushort key, out double value)","sourceCodeStart":1719,"sourceCodeEnd":1755,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs#L1719-L1755","documentation":"GlyphTypeface implements IDictionary<ushort,double> to expose glyph-to-advance metrics, but this dictionary is read-only: the Add(ushort,double) implementation is an explicit stub that always throws NotSupportedException. The library never supports mutating glyph metrics through this interface because the values come from the underlying font file's glyph data and cannot be modified.","triggerScenarios":"Casting a GlyphTypeface's glyph-metrics dictionary (e.g. its AdvanceHeights/IDictionary<ushort,double> view) to IDictionary<ushort,double> or IDictionary and calling Add(key, value) on it.","commonSituations":"Developers treating GlyphTypeface as a writable dictionary — e.g. copying metrics with collection-initializer syntax, writing custom glyph-override code, or generic code that populates an IDictionary<ushort,double> passed in from the caller.","solutions":["Do not mutate the dictionary; build a separate Dictionary<ushort,double> copy and modify that","Check IDictionary.IsReadOnly (or ICollection<KeyValuePair<ushort,double>>.IsReadOnly) before calling Add and skip or copy","If you need different advance widths, apply them at the text-formatting level (e.g. GlyphRun/Typeface APIs) rather than editing GlyphTypeface"],"exampleFix":"// before\nIDictionary<ushort,double> metrics = glyphTypeface.AdvanceHeights;\nmetrics.Add(42, 0.5); // NotSupportedException\n// after\nvar metrics = new Dictionary<ushort,double>(glyphTypeface.AdvanceHeights);\nmetrics[42] = 0.5; // modify your own copy","handlingStrategy":"try-catch","validationCode":"// check before mutating\nvar dict = (IDictionary<ushort,double>)glyphTypeface.AdvanceHeights;\nif (dict.IsReadOnly)\n    dict = new Dictionary<ushort,double>(dict); // editable copy","typeGuard":"bool IsMutable(IDictionary<ushort,double> d) => !d.IsReadOnly;","tryCatchPattern":"try {\n    metrics.Add(glyphIndex, value);\n} catch (NotSupportedException) {\n    var copy = new Dictionary<ushort,double>(metrics);\n    copy[glyphIndex] = value; // fall back to a copy\n}","preventionTips":["Treat GlyphTypeface's IDictionary members as read-only lookups only","Check IsReadOnly before any mutation on IDictionary you didn't create","Copy into your own Dictionary before customizing glyph metrics"],"tags":["wpf","csharp","read-only-collection","not-supported"],"backgroundTag":"unsupported-operation","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"}