{"record":{"id":"8341b81a5197d973","repo":"dotnet/wpf","slug":"sr-format-sr-collectionnumberofelementsshouldbeequalto-8341b8","errorCode":null,"errorMessage":"SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, GlyphCount)","messagePattern":"SR\\.Format\\(SR\\.CollectionNumberOfElementsShouldBeEqualTo, GlyphCount\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs","lineNumber":404,"sourceCode":"                                        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\n                // NOTE:  In previous versions this function would estimate the size\n                // of this glyph run's bitmaps and compare it against the theoretical\n                // maximum size allowed before rendering falls back to using geometry.\n                // This was done in order to produce a managed exception where we might\n                // hit overflow or memory allocation issues in native code.\n                // We no longer own the code the produces these bitmaps so we can't reliably","sourceCodeStart":386,"sourceCodeEnd":422,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs#L386-L422","documentation":"When no clusterMap is provided, GlyphRun.Initialize requires an exact 1:1 correspondence: GlyphCount must equal characters.Count. The message states the collection must have exactly GlyphCount elements, thrown as ArgumentException on the clusterMap parameter.","triggerScenarios":"Calling the GlyphRun constructor, TryCreate, or EndInit with clusterMap null/empty and glyphIndices.Count != characters.Count — common with ligatures, surrogate pairs, or combining sequences.","commonSituations":"Mapping Unicode text directly to glyphs without shaping; text containing emoji or CJK surrogate pairs where character count exceeds glyph count assumptions.","solutions":["Provide a proper clusterMap instead of relying on the 1:1 assumption","Shape the text so glyph count matches character count for simple scripts","Filter/expand characters so counts align before constructing the run"],"exampleFix":"// before: glyphs.Count==1 for 'é' composed as 2 chars, no clusterMap\n// after\nnew GlyphRun(tf, 0, false, 12, glyphs, chars, new int[]{0,0}, null, null, null, false); // clusterMap given","handlingStrategy":"validation","validationCode":"if ((clusterMap == null || clusterMap.Length == 0) && glyphIndices.Count != characters.Count)\n    throw new InvalidOperationException(\"1:1 runs require equal counts; otherwise pass a clusterMap\");","typeGuard":"static bool NeedsClusterMap(char[] chars, ushort[] glyphs) => chars.Length != glyphs.Length;","tryCatchPattern":"try { var run = new GlyphRun(...); }\ncatch (ArgumentException ex) when (ex.ParamName == \"clusterMap\") { /* build clusterMap */ }","preventionTips":["Count codepoints, not chars, for surrogate-heavy text","Provide clusterMap whenever a shaper produces ligatures","Test with emoji/combining-mark samples"],"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-21T21:30:21.729Z"}