From 911dfb5cd64cf273f0a435ef7a56b425da9c39ca Mon Sep 17 00:00:00 2001 From: Jan Karger Date: Fri, 27 Mar 2026 22:20:42 +0100 Subject: [PATCH] fix TypeInitializationException caused by duplicate font names Fonts.SystemFontFamilies can return multiple entries with the same name, causing ToDictionary to throw an ArgumentException. Use GroupBy to handle duplicates by keeping the first occurrence. --- Source/SVGImage/SVG/Utils/FontResolver.cs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Source/SVGImage/SVG/Utils/FontResolver.cs b/Source/SVGImage/SVG/Utils/FontResolver.cs index 7b63d91..373e527 100644 --- a/Source/SVGImage/SVG/Utils/FontResolver.cs +++ b/Source/SVGImage/SVG/Utils/FontResolver.cs @@ -27,15 +27,15 @@ public class FontResolver public FontResolver(int maxLevenshteinDistance = 0) { _availableFonts = Fonts.SystemFontFamilies - .Select(ff => new { NormalName = ff.Source, Family = ff }) - .ToDictionary(x => x.NormalName, x => x.Family, StringComparer.OrdinalIgnoreCase); + .GroupBy(ff => ff.Source, StringComparer.OrdinalIgnoreCase) + .ToDictionary(g => g.Key, g => g.First(), StringComparer.OrdinalIgnoreCase); _normalizedFontNameMap = new Dictionary(_availableFonts.Count); - foreach (var font in _availableFonts.Keys) - { - var name = Normalize(font); - if (!_normalizedFontNameMap.ContainsKey(name)) - _normalizedFontNameMap.Add(name, font); + foreach (var font in _availableFonts.Keys) + { + var name = Normalize(font); + if (!_normalizedFontNameMap.ContainsKey(name)) + _normalizedFontNameMap.Add(name, font); } MaxLevenshteinDistance = maxLevenshteinDistance; } @@ -157,7 +157,7 @@ private static string Normalize(string fontName) return string.Empty; } return _normalizationRegex.Replace(fontName, String.Empty).ToLowerInvariant(); - + } private static int[,] CreateDistanceMatrix(int length1, int length2) @@ -201,7 +201,7 @@ private static int Levenshtein(string string1, string string2) return string1.Length; } - + int[,] distanceMatrix = CreateDistanceMatrix(string1.Length, string2.Length); for (int i = 1; i <= string1.Length; i++) @@ -221,5 +221,5 @@ private static int Levenshtein(string string1, string string2) } } - + }