{"record":{"id":"ffc0a2aa222cf045","repo":"dotnet/wpf","slug":"sr-format-sr-collectionnumberofelementsshouldbeequalto","errorCode":null,"errorMessage":"SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, characters.Count)","messagePattern":"SR\\.Format\\(SR\\.CollectionNumberOfElementsShouldBeEqualTo, characters\\.Count\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs","lineNumber":398,"sourceCode":"                                    }\n                                    else\n                                    {\n                                        if (clusterMap[i] < clusterMap[i - 1])\n                                            throw new ArgumentException(SR.ClusterMapEntriesShouldNotDecrease, nameof(clusterMap));\n\n                                        if (clusterMap[i] >= GlyphCount)\n                                            throw new ArgumentException(SR.ClusterMapEntryShouldPointWithinGlyphIndices, nameof(clusterMap));\n                                    }\n                                }\n                            }\n                            else\n                            {\n                                throw new ArgumentException(SR.ClusterMapFirstEntryMustBeZero, nameof(clusterMap));\n                            }\n                        }\n                        else\n                        {\n                            throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, characters.Count), nameof(clusterMap));\n                        }\n                    }\n                    else\n                    {\n                        if (GlyphCount != characters.Count)\n                            throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, GlyphCount), nameof(clusterMap));\n                    }\n                }\n\n                if (caretStops != null && caretStops.Count != 0)\n                {\n                    if (caretStops.Count != CodepointCount + 1)\n                        throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, CodepointCount + 1), nameof(caretStops));\n                }\n\n                if (isSideways && (bidiLevel & 1) != 0)\n                    throw new ArgumentException(SR.SidewaysRTLTextIsNotSupported);\n","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs#L380-L416","documentation":"If the clusterMap is null or empty, GlyphRun.Initialize falls back to a 1:1 mapping assumption and requires clusterMap-length consistency with characters: in this branch (characters.Count == GlyphCount failing earlier checks) it demands the character count match. The message states the collection must have exactly characters.Count elements, thrown as ArgumentException on clusterMap.","triggerScenarios":"Constructing a GlyphRun via constructor, TryCreate, or EndInit where characters.Count != GlyphCount and no valid clusterMap is supplied, so the implicit mapping cannot be established.","commonSituations":"Passing pre-shaped glyph indices whose count differs from the number of Unicode characters (e.g. ligatures, surrogate pairs, combining marks) while leaving clusterMap null.","solutions":["Supply an explicit clusterMap that maps each character to glyph positions when counts differ","Ensure characters.Count == glyphIndices.Count for simple 1:1 runs","Use a text shaper (TextFormatter/ShapeableTextCharacters) to produce matching collections"],"exampleFix":"// before: 'ffi' ligature -> 1 glyph but 3 characters, no clusterMap\nnew GlyphRun(typeface, bidiLevel, isSideways, 12.0, glyphs, \"ffi\".ToCharArray(), null, ...);\n// after: provide clusterMap {0,0,0}\nnew GlyphRun(typeface, bidiLevel, isSideways, 12.0, glyphs, \"ffi\".ToCharArray(), new int[]{0,0,0}, ...);","handlingStrategy":"validation","validationCode":"if (clusterMap == null || clusterMap.Length == 0)\n{\n    if (characters.Count != glyphIndices.Count)\n        throw new InvalidOperationException(\"Provide a clusterMap when character and glyph counts differ\");\n}","typeGuard":"static bool CanUseImplicitMap(char[] chars, ushort[] glyphs) => chars.Length == glyphs.Length;","tryCatchPattern":"try { var run = new GlyphRun(...); }\ncatch (ArgumentException ex) when (ex.ParamName == \"clusterMap\") { /* supply explicit clusterMap */ }","preventionTips":["Always supply a clusterMap for shaped text with ligatures or surrogates","Shape text via TextFormatter rather than mapping chars to glyphs manually","Assert characters.Count == glyphIndices.Count for simple runs"],"tags":["wpf","glyphrun","argument-validation"],"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-22T01:17:13.364Z"}