{"record":{"id":"ea946c68ab8005dc","repo":"dotnet/wpf","slug":"sr-compositefont-toomanyfamilymaps","errorCode":null,"errorMessage":"SR.CompositeFont_TooManyFamilyMaps","messagePattern":"SR\\.CompositeFont_TooManyFamilyMaps","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs","lineNumber":255,"sourceCode":"            }\n        }\n\n        #endregion\n\n        #region Internal implementation\n\n        private int InsertItem(int index, FontFamilyMap item)\n        {\n            ArgumentNullException.ThrowIfNull(item);\n\n            VerifyChangeable();\n\n            // Limit the number of family maps because we use ushort indexes in the skip lists.\n            // To exceed this limit a user would have to have a separate family maps for almost \n            // every Unicode value, in which case (since we search sequentially) performance\n            // would become a problem.\n            if (_count + 1 >= ushort.MaxValue)\n                throw new InvalidOperationException(SR.CompositeFont_TooManyFamilyMaps);\n\n            // Validate the index.\n            ArgumentOutOfRangeException.ThrowIfNegative(index);\n            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, Count);\n\n            // PrepareToAddFamilyMap validates the familyName and updates the internal state\n            // of the CompositeFontInfo object.\n            _fontInfo.PrepareToAddFamilyMap(item);\n\n            // Make room for the new item.\n            if (_items == null)\n            {\n                _items = new FontFamilyMap[InitialCapacity];\n            }\n            else if (_count == _items.Length)\n            {\n                FontFamilyMap[] items = new FontFamilyMap[_count * 2];\n                for (int i = 0; i < index; ++i)","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs#L237-L273","documentation":"FamilyMapCollection caps the number of FamilyMap entries at ushort.MaxValue because composite font matching stores family-map indexes in 16-bit skip-list structures. InsertItem (called by Add/Insert) throws InvalidOperationException with CompositeFont_TooManyFamilyMaps when adding one more would reach that limit.","triggerScenarios":"Programmatically adding/inserting FamilyMap entries into a FontFamily's FamilyMapCollection (or building a CompositeFont) until Count approaches 65535 — the check is _count + 1 >= ushort.MaxValue.","commonSituations":"Generating fallback font maps programmatically (e.g. one map per Unicode block or per character), loop-driven configuration builders without a count cap.","solutions":["Reduce the number of family maps by coalescing adjacent/overlapping Unicode ranges into fewer maps.","Cap the generation loop at ushort.MaxValue - 2 maps and log/merge the remainder.","Split the fallback strategy across multiple FontFamily definitions instead of one collection."],"exampleFix":"// before\nfor (int cp = 0; cp <= 0x10FFFF; cp++)\n    familyMaps.Add(new FamilyMap { Unicode = $\"U+{cp:X4}-U+{cp:X4}\", Target = \"Arial\" });\n// after\nconst int MaxMaps = ushort.MaxValue - 2;\nfor (int cp = 0; cp <= 0x10FFFF && familyMaps.Count < MaxMaps; cp++)\n    familyMaps.Add(new FamilyMap { Unicode = $\"U+{cp:X4}-U+{cp:X4}\", Target = \"Arial\" });","handlingStrategy":"validation","validationCode":"if (familyMaps.Count >= ushort.MaxValue - 1) throw new InvalidOperationException(\"Too many family maps; merge Unicode ranges.\");","typeGuard":null,"tryCatchPattern":"try { familyMaps.Add(map); }\ncatch (InvalidOperationException) { log.Error(\"Composite font map limit reached (ushort.MaxValue).\"); }","preventionTips":["Coalesce adjacent Unicode ranges to reduce map count","Enforce a generation-time cap below 65535 maps","Alert on composite fonts with thousands of maps as a design smell"],"tags":["wpf","fonts","collections","limit"],"backgroundTag":"value-out-of-range","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"}