{"record":{"id":"fe7a8174156803f0","repo":"dotnet/wpf","slug":"sr-compositefont-duplicatetypeface","errorCode":null,"errorMessage":"SR.CompositeFont_DuplicateTypeface","messagePattern":"SR\\.CompositeFont_DuplicateTypeface","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs","lineNumber":241,"sourceCode":"        }\n\n        #endregion\n\n        #region Internal implementation\n\n        private int InsertItem(int index, FamilyTypeface item)\n        {\n            ArgumentNullException.ThrowIfNull(item);\n\n            VerifyChangeable();\n\n            // Validate the index.\n            ArgumentOutOfRangeException.ThrowIfNegative(index);\n            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, Count);\n\n            // We can't have two items with same style, weight, stretch.\n            if (FindItem(item) >= 0)\n                throw new ArgumentException(SR.CompositeFont_DuplicateTypeface);\n\n            // Make room for the new item.\n            if (_items == null)\n            {\n                _items = new FamilyTypeface[InitialCapacity];\n            }\n            else if (_count == _items.Length)\n            {\n                FamilyTypeface[] items = new FamilyTypeface[_count * 2];\n                for (int i = 0; i < index; ++i)\n                    items[i] = _items[i];\n                for (int i = index; i < _count; ++i)\n                    items[i + 1] = _items[i];\n                _items = items;\n            }\n            else if (index < _count)\n            {\n                for (int i = _count - 1; i >= index; --i)","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs#L223-L259","documentation":"FamilyTypefaceCollection forbids duplicate typefaces: two entries with the same Style+Weight+Stretch combination. InsertItem runs FindItem(item) before inserting and throws ArgumentException with CompositeFont_DuplicateTypeface if an equivalent entry already exists.","triggerScenarios":"Calling Add or Insert with a FamilyTypeface whose (Style, Weight, Stretch) tuple matches an existing entry in the collection; also occurs during composite-font parsing when the ConfigFile/FontFamily section declares the same combination twice.","commonSituations":"Compositely defined fonts (FontFamily.CompositeFont) in XAML where two <FontFamilyMap>-level typeface entries repeat a style/weight/stretch triple, or programmatic addition without checking for an existing match.","solutions":["Check the collection first with FindName-equivalent search (or iterate) and only add when no matching Style/Weight/Stretch exists.","Replace the existing entry (Remove then Add) if you intend to override its metrics.","Fix duplicate typeface declarations in the composite font definition (XAML/ConfigFile)."],"exampleFix":"// before\ncollection.Add(new FamilyTypeface { Style = FontStyles.Normal, Weight = FontWeights.Regular, Stretch = FontStretches.Normal });\n// after\nbool exists = collection.Any(t => t.Style == FontStyles.Normal && t.Weight == FontWeights.Regular && t.Stretch == FontStretches.Normal);\nif (!exists) collection.Add(newFamilyTypeface);","handlingStrategy":"validation","validationCode":"bool duplicate = collection.Any(t => t.Style == item.Style && t.Weight == item.Weight && t.Stretch == item.Stretch);\nif (duplicate) return; // or replace the existing entry","typeGuard":null,"tryCatchPattern":"try { collection.Add(item); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"DuplicateTypeface\") || ex.Message.Contains(\"duplicate\")) { /* same style/weight/stretch exists */ }","preventionTips":["Deduplicate composite font declarations by Style/Weight/Stretch.","Use a keyed set (tuple of style/weight/stretch) while building the collection.","Validate XAML composite font sections for repeated triples."],"tags":["wpf","fonts","duplicate","invalid-argument"],"backgroundTag":"invalid-argument-value","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"}