dotnet/wpf · error · ArgumentException

SR.GlyphsAdvanceWidthCannotBeNegative

Error message

SR.GlyphsAdvanceWidthCannotBeNegative

What it means

Thrown by Glyphs.ParseGlyphsProperty when a glyph's advance-width component in the GlyphIndices string (the first comma-separated value after the index) parses to a negative double. Advance widths measure horizontal distance and must be zero or positive, so the parser rejects negatives with ArgumentException.

Solutions

  1. Change the advance-width value to a non-negative number (use 0 for zero-advance glyphs).
  2. Clamp computed widths with Math.Max(0, width) before formatting them into GlyphIndices.
  3. If a negative-looking value is really an offset, move it to the offset X/Y fields (3rd/4th values) instead.

Example fix

// before
glyphs.GlyphIndices = ";41,-12.5";
// after
glyphs.GlyphIndices = ";41,12.5"; // or 0 if the advance is intentionally empty
Defensive patterns

Strategy: validation

Validate before calling

static string FormatGlyphEntry(int index, double advanceWidth, double offsetX = 0, double offsetY = 0)
    => $";{index},{Math.Max(0, advanceWidth)},{offsetX},{offsetY}"; // clamp negative widths

Type guard

static bool HasNonNegativeAdvance(string entry) => !double.TryParse(entry.Split(',')[1], out double w) || w >= 0;

Try / catch

try { glyphs.GlyphIndices = built; }
catch (ArgumentException ex) when (ex.Message.Contains("advance")) { built = built.Replace(",-", ",0"); glyphs.GlyphIndices = built; }

Prevention

When it happens

Trigger: A GlyphIndices entry with a negative second field, e.g. ";41,-5" or ";(2:1),-1.5"; also computed widths from code that produced NaN/negative values from failed measurements.

Common situations: Programmatic width computation returning negative values for invisible/zero-width characters; typo'd minus sign in hand-written XAML; exporting glyph metrics from another tool with sign conventions differing from WPF.

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/531d3e364015c1b6. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Glyphs.cs:598

                                        {
                                            SetClusterMapEntry(clusterMap, ch, (ushort)parsedGlyphCount);
                                        }
                                    }
                                    else
                                    {
                                        SetClusterMapEntry(clusterMap, parsedCharacterCount, (ushort)parsedGlyphCount);
                                    }
                                }
                                parsedGlyphData.advanceWidth = GetAdvanceWidth(fontFace, parsedGlyphData.glyphIndex, sideways);
                                break;

                            case 1:
                                // interpret glyph advance spec
                                if (!IsEmpty(valueSpec))
                                {
                                    parsedGlyphData.advanceWidth = double.Parse(valueSpec, provider: CultureInfo.InvariantCulture);
                                    if (parsedGlyphData.advanceWidth < 0)
                                        throw new ArgumentException(SR.GlyphsAdvanceWidthCannotBeNegative);
                                }
                                break;

                            case 2:
                                // interpret glyph offset X
                                if (!IsEmpty(valueSpec))
                                    parsedGlyphData.offsetX = double.Parse(valueSpec, provider: CultureInfo.InvariantCulture);
                                break;

                            case 3:
                                // interpret glyph offset Y
                                if (!IsEmpty(valueSpec))
                                    parsedGlyphData.offsetY = double.Parse(valueSpec, provider: CultureInfo.InvariantCulture);
                                break;

                            default:
                                // too many commas; can't interpret
                                throw new ArgumentException(SR.GlyphsTooManyCommas);

View on GitHub (pinned to 81131a70a4)