dotnet/wpf · error · ArgumentException

SR.Format(SR.CodePointOutOfRange, value)

Error message

SR.Format(SR.CodePointOutOfRange, value)

What it means

CharacterMetricsDictionary keys are Unicode code points and must lie in the range 0..FontFamilyMap.LastUnicodeScalar (the maximum valid UTF-16/Unicode scalar value, 0x10FFFF). ConvertKey throws this ArgumentOutOfRangeException-style ArgumentException after a key has been successfully parsed to an int but is negative or exceeds that maximum.

Solutions

  1. Ensure the key is a valid Unicode scalar: 0 <= key <= 0x10FFFF, excluding surrogates if you need a real character
  2. Split surrogate pairs into individual scalar values and add one entry per scalar
  3. Validate the key with char.ConvertToUtf32 before inserting

Example fix

// before
 dictionary.Add(0x1F600 + 0x100000000, metrics); // out of range
// after
 dictionary.Add(char.ConvertToUtf32("\uD83D\uDE00", 0), metrics); // 0x1F600
Defensive patterns

Strategy: validation

Validate before calling

bool IsValidCodePoint(int v) => v >= 0 && v <= 0x10FFFF && !char.IsSurrogate((char)v);

Try / catch

try { dict.Add(codePoint, metrics); } catch (ArgumentException ex) when (ex.Message.Contains("code point")) { /* clamp or split value */ }

Prevention

When it happens

Trigger: Calling Add or the indexer with an int key < 0 or > 0x10FFFF (e.g. a surrogate-pair packed value, a negative sentinel, or a hash-like number), including string keys whose hex value exceeds the maximum.

Common situations: Encoding surrogate pairs as a single 32-bit number and using it as a key; using negative values as 'not set' sentinels; keys taken from full character codes including non-character planes above the scalar limit.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/1336be4fd7b0e86a. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs:504

            string s = key as string;
            if (s != null)
            {
                int i = 0;
                if (!FontFamilyMap.ParseHexNumber(s, ref i, out value) || i < s.Length)
                    throw new ArgumentException(SR.Format(SR.CannotConvertStringToType, s, "int"), nameof(key));
            }
            else if (key is int)
            {
                value = (int)key;
            }
            else
            {
                throw new ArgumentException(SR.Format(SR.CannotConvertType, key.GetType(), "int"), nameof(key));
            }

            if (value < 0 || value > FontFamilyMap.LastUnicodeScalar)
                throw new ArgumentException(SR.Format(SR.CodePointOutOfRange, value), nameof(key));

            return value;
        }

        private CharacterMetrics ConvertValue(object value)
        {
            CharacterMetrics metrics = value as CharacterMetrics;
            if (metrics != null)
                return metrics;

            ArgumentNullException.ThrowIfNull(value);

            throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(CharacterMetrics), value.GetType()));
        }

        private struct Enumerator : SC.IDictionaryEnumerator, IEnumerator<KeyValuePair<int, CharacterMetrics>>
        {
            private CharacterMetricsDictionary _dictionary;

View on GitHub (pinned to 81131a70a4)