{"record":{"id":"e2a1a8eb0687dc14","repo":"dotnet/wpf","slug":"sr-format-sr-collectionnumberofelementsshouldbeequalto-e2a1a8","errorCode":null,"errorMessage":"SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, glyphIndices.Count)","messagePattern":"SR\\.Format\\(SR\\.CollectionNumberOfElementsShouldBeEqualTo, glyphIndices\\.Count\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs","lineNumber":443,"sourceCode":"            else\n            {\n                ArgumentOutOfRangeException.ThrowIfEqual(renderingEmSize, double.NaN);\n                ArgumentOutOfRangeException.ThrowIfNegative(renderingEmSize);\n                ArgumentNullException.ThrowIfNull(glyphTypeface);\n                ArgumentNullException.ThrowIfNull(glyphIndices);\n\n                if (glyphIndices.Count <= 0)\n                    throw new ArgumentException(SR.CollectionNumberOfElementsMustBeGreaterThanZero, nameof(glyphIndices));\n\n                if (glyphIndices.Count > MaxGlyphCount)\n                {\n                    throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsMustBeLessOrEqualTo, MaxGlyphCount), nameof(glyphIndices));\n                }\n\n                ArgumentNullException.ThrowIfNull(advanceWidths);\n\n                if (advanceWidths.Count != glyphIndices.Count)\n                    throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, glyphIndices.Count), nameof(advanceWidths));\n\n                if (glyphOffsets != null && glyphOffsets.Count != 0 && glyphOffsets.Count != glyphIndices.Count)\n                    throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, glyphIndices.Count), nameof(glyphOffsets));\n\n                // We should've caught all invalid cases above and thrown appropriate exceptions.\n                Invariant.Assert(false);\n            }\n\n            IsInitialized = true; // The glyphrun is completely initialized\n        }\n\n        #endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  Public Methods\n        //\n        //------------------------------------------------------","sourceCodeStart":425,"sourceCodeEnd":461,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs#L425-L461","documentation":"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.","triggerScenarios":"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).","commonSituations":"Custom glyph positioning (kerning adjustments, markup highlights) where offsets were built for a subset of glyphs; off-by-one after trimming glyphIndices.","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"],"exampleFix":"// before: offsets only for glyph 2\noffsets = new Point[] { new Point(3,0) };\n// after\noffsets = glyphs.Select((g,i) => i == 2 ? new Point(3,0) : new Point(0,0)).ToArray();","handlingStrategy":"validation","validationCode":"if (glyphOffsets != null && glyphOffsets.Count != 0 && glyphOffsets.Count != glyphIndices.Count)\n    throw new ArgumentException(\"glyphOffsets must match glyphIndices count\");\n// or normalize:\nif (glyphOffsets != null && glyphOffsets.Count != glyphIndices.Count)\n    glyphOffsets = Enumerable.Range(0, glyphIndices.Count)\n        .Select(i => i < glyphOffsets.Count ? glyphOffsets[i] : new Point(0,0)).ToArray();","typeGuard":"static bool OffsetsMatchGlyphs(Point[] offsets, int glyphCount) => offsets == null || offsets.Length == 0 || offsets.Length == glyphCount;","tryCatchPattern":"try { var run = new GlyphRun(...); }\ncatch (ArgumentException ex) when (ex.ParamName == \"glyphOffsets\") { /* pad with Point(0,0) or pass null */ }","preventionTips":["Regenerate offsets whenever glyphIndices changes","Use Point(0,0) placeholders for glyphs without custom offsets","Pass null glyphOffsets when no positioning is required"],"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"}