dotnet/wpf · error · FormatException

SR.CompositeFontInvalidUnicodeRange

Error message

SR.CompositeFontInvalidUnicodeRange

What it means

When a composite font (CompiledFontFamily/FontFamily XAML) specifies a FamilyMap whose UnicodeRanges attribute cannot be parsed, FamilyMap.ParseUnicodeRanges calls ThrowInvalidUnicodeRange, raising FormatException with CompositeFontInvalidUnicodeRange. Each range must be of the form U+XXXX or U+XXXX-U+YYYY, comma/whitespace separated, with valid hex values.

Solutions

  1. Fix the UnicodeRanges string in the composite font so every token matches U+XXXX[-U+XXXX] with valid hex and start <= end.
  2. Validate range strings before loading, e.g. with Regex ^U\+[0-9A-Fa-f]{1,6}(-U\+[0-9A-Fa-f]{1,6})?$ and numeric comparison.
  3. Load composite fonts from trusted, versioned resource files rather than user-supplied strings without validation.

Example fix

<!-- before -->
<FamilyMap Unicode="U+0600-U+05FF"/>
<!-- after -->
<FamilyMap Unicode="U+0590-U+05FF"/>
Defensive patterns

Strategy: validation

Validate before calling

static readonly Regex RangeRx = new Regex(@"^U\+[0-9A-Fa-f]{1,6}(-U\+[0-9A-Fa-f]{1,6})?$");
bool valid = unicodeRanges.Split(new[]{' ', ','}, StringSplitOptions.RemoveEmptyEntries).All(t => RangeRx.IsMatch(t));

Try / catch

try { ParseCompositeFont(xml); }
catch (FormatException ex) { log.LogError($"Invalid unicode range: {ex.Message}"); }

Prevention

When it happens

Trigger: Parsing a CompositeFont that contains a malformed unicodeRanges value: bad hex digits, reversed bounds (start > end), values above U+10FFFF, missing U+ prefix, or an empty range token.

Common situations: Hand-edited or third-party CompositeFont.xaml files, font fallback configurations copied with typos, or generated configs where hex values were formatted incorrectly.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMap.cs:221

                return true;
            }

            if (culture != null)
            {
                return familyMapLanguage.RangeIncludes(culture);
            }   

            return false;
        }

        internal Range[] Ranges
        {
            get { return _ranges; }
        }

        private static void ThrowInvalidUnicodeRange()
        {
            throw new FormatException(SR.CompositeFontInvalidUnicodeRange);
        }

        private static Range[] ParseUnicodeRanges(string unicodeRanges)
        {
            List<Range> ranges = new List<Range>(3);
            int index = 0;
            while (index < unicodeRanges.Length)
            {
                int firstNum;
                if (!ParseHexNumber(unicodeRanges, ref index, out firstNum))
                {
                    ThrowInvalidUnicodeRange();
                }

                int lastNum = firstNum;

                if (index < unicodeRanges.Length)
                {

View on GitHub (pinned to 81131a70a4)