dotnet/wpf · error · ArgumentException
SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo…
Error message
SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, glyphIndices.Count)
What it means
If glyphOffsets is supplied and non-empty, GlyphRun.Initialize requires glyphOffsets.Count == glyphIndices.Count (one offset per glyph). Mismatched counts leave some glyphs without offsets, so it throws ArgumentException naming the glyphOffsets parameter.
Solutions
- Resize glyphOffsets to exactly glyphIndices.Count, filling with default Point(0,0) entries
- Pass null (or empty) glyphOffsets when no custom positioning is needed
- Recompute offsets whenever glyphIndices changes
Example fix
// before: offsets only for glyph 2
offsets = new Point[] { new Point(3,0) };
// after
offsets = glyphs.Select((g,i) => i == 2 ? new Point(3,0) : new Point(0,0)).ToArray(); Defensive patterns
Strategy: validation
Validate before calling
if (glyphOffsets != null && glyphOffsets.Count != 0 && glyphOffsets.Count != glyphIndices.Count)
throw new ArgumentException("glyphOffsets must match glyphIndices count");
// or normalize:
if (glyphOffsets != null && glyphOffsets.Count != glyphIndices.Count)
glyphOffsets = Enumerable.Range(0, glyphIndices.Count)
.Select(i => i < glyphOffsets.Count ? glyphOffsets[i] : new Point(0,0)).ToArray(); Type guard
static bool OffsetsMatchGlyphs(Point[] offsets, int glyphCount) => offsets == null || offsets.Length == 0 || offsets.Length == glyphCount;
Try / catch
try { var run = new GlyphRun(...); }
catch (ArgumentException ex) when (ex.ParamName == "glyphOffsets") { /* pad with Point(0,0) or pass null */ } Prevention
- Regenerate offsets whenever glyphIndices changes
- Use Point(0,0) placeholders for glyphs without custom offsets
- Pass null glyphOffsets when no positioning is required
When it happens
Trigger: Calling the GlyphRun constructor, TryCreate, or EndInit with a non-null, non-empty glyphOffsets whose length differs from glyphIndices.Count — e.g. providing offsets only for repositioned glyphs instead of all glyphs (use Point(0,0) for default).
Common situations: Custom glyph positioning (kerning adjustments, markup highlights) where offsets were built for a subset of glyphs; off-by-one after trimming glyphIndices.
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/e2a1a8eb0687dc14.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs:443
else
{
ArgumentOutOfRangeException.ThrowIfEqual(renderingEmSize, double.NaN);
ArgumentOutOfRangeException.ThrowIfNegative(renderingEmSize);
ArgumentNullException.ThrowIfNull(glyphTypeface);
ArgumentNullException.ThrowIfNull(glyphIndices);
if (glyphIndices.Count <= 0)
throw new ArgumentException(SR.CollectionNumberOfElementsMustBeGreaterThanZero, nameof(glyphIndices));
if (glyphIndices.Count > MaxGlyphCount)
{
throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsMustBeLessOrEqualTo, MaxGlyphCount), nameof(glyphIndices));
}
ArgumentNullException.ThrowIfNull(advanceWidths);
if (advanceWidths.Count != glyphIndices.Count)
throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, glyphIndices.Count), nameof(advanceWidths));
if (glyphOffsets != null && glyphOffsets.Count != 0 && glyphOffsets.Count != glyphIndices.Count)
throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, glyphIndices.Count), nameof(glyphOffsets));
// We should've caught all invalid cases above and thrown appropriate exceptions.
Invariant.Assert(false);
}
IsInitialized = true; // The glyphrun is completely initialized
}
#endregion Constructors
//------------------------------------------------------
//
// Public Methods
//
//------------------------------------------------------View on GitHub (pinned to 81131a70a4)