dotnet/wpf · error · ArgumentException
SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo…
Error message
SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, GlyphCount)
What it means
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.
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
Example fix
// before: glyphs.Count==1 for 'é' composed as 2 chars, no clusterMap
// after
new GlyphRun(tf, 0, false, 12, glyphs, chars, new int[]{0,0}, null, null, null, false); // clusterMap given Defensive patterns
Strategy: validation
Validate before calling
if ((clusterMap == null || clusterMap.Length == 0) && glyphIndices.Count != characters.Count)
throw new InvalidOperationException("1:1 runs require equal counts; otherwise pass a clusterMap"); Type guard
static bool NeedsClusterMap(char[] chars, ushort[] glyphs) => chars.Length != glyphs.Length;
Try / catch
try { var run = new GlyphRun(...); }
catch (ArgumentException ex) when (ex.ParamName == "clusterMap") { /* build clusterMap */ } Prevention
- Count codepoints, not chars, for surrogate-heavy text
- Provide clusterMap whenever a shaper produces ligatures
- Test with emoji/combining-mark samples
When it happens
Trigger: Calling the GlyphRun constructor, TryCreate, or EndInit with clusterMap null/empty and glyphIndices.Count != characters.Count — common with ligatures, surrogate pairs, or combining sequences.
Common situations: Mapping Unicode text directly to glyphs without shaping; text containing emoji or CJK surrogate pairs where character count exceeds glyph count assumptions.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- SR.ClusterMapEntriesShouldNotDecrease
- SR.ClusterMapEntryShouldPointWithinGlyphIndices
- SR.ClusterMapFirstEntryMustBeZero
- SR.CollectionNumberOfElementsMustBeGreaterThanZero
- SR.Format(SR.CollectionNumberOfElementsMustBeLessOrEqualTo…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/8341b81a5197d973.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs:404
if (clusterMap[i] >= GlyphCount)
throw new ArgumentException(SR.ClusterMapEntryShouldPointWithinGlyphIndices, nameof(clusterMap));
}
}
}
else
{
throw new ArgumentException(SR.ClusterMapFirstEntryMustBeZero, nameof(clusterMap));
}
}
else
{
throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, characters.Count), nameof(clusterMap));
}
}
else
{
if (GlyphCount != characters.Count)
throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, GlyphCount), nameof(clusterMap));
}
}
if (caretStops != null && caretStops.Count != 0)
{
if (caretStops.Count != CodepointCount + 1)
throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, CodepointCount + 1), nameof(caretStops));
}
if (isSideways && (bidiLevel & 1) != 0)
throw new ArgumentException(SR.SidewaysRTLTextIsNotSupported);
// NOTE: In previous versions this function would estimate the size
// of this glyph run's bitmaps and compare it against the theoretical
// maximum size allowed before rendering falls back to using geometry.
// This was done in order to produce a managed exception where we might
// hit overflow or memory allocation issues in native code.
// We no longer own the code the produces these bitmaps so we can't reliablyView on GitHub (pinned to 81131a70a4)