Repository: AvaloniaUI/Avalonia
File: src/Avalonia.Base/Media/Fonts/SystemFontCollection.cs (C#)

Code before:
        }

        public override bool TryMatchCharacter(int codepoint, FontStyle style, FontWeight weight, FontStretch stretch, string? familyName,
            CultureInfo? culture, out Typeface match)
        {
            match = default;
            
            if (_fontManager.PlatformImpl.TryMatchCharacter(codepoint, style, weight, stretch, culture, out var glyphTypeface))
            {                
                AddGlyphTypefaceByFamilyName(glyphTypeface.FamilyName, glyphTypeface);

                match = new Typeface(glyphTypeface.FamilyName, glyphTypeface.Style, glyphTypeface.Weight,
                    glyphTypeface.Stretch);

                return true;
            }

            return false;
        }
        
        private void AddGlyphTypefaceByFamilyName(string familyName, IGlyphTypeface glyphTypeface)
        {
            var typefaces = _glyphTypefaceCache.GetOrAdd(familyName,
                x =>
                {
                    _familyNames.Insert(0, familyName);

                    return new ConcurrentDictionary<FontCollectionKey, IGlyphTypeface?>();
                });

            typefaces.TryAdd(
                new FontCollectionKey(glyphTypeface.Style, glyphTypeface.Weight, glyphTypeface.Stretch),
                glyphTypeface);
        }
    }
}


Review comment (suggestion):
This was already the case before this PR, but the value factory [can be executed multiple times](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.getoradd?view=netstandard-2.0#system-collections-concurrent-concurrentdictionary-2-getoradd(-0-system-func((-0-1)))), effectively duplicating the family names.

>Since a key/value can be inserted by another thread while valueFactory is generating a value, you cannot trust that just because valueFactory executed, its produced value will be inserted into the dictionary and returned. If you call [GetOrAdd](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.getoradd?view=netstandard-2.0) simultaneously on different threads, valueFactory may be called multiple times, but only one key/value pair will be added to the dictionary.

This should be moved outside the delegate and synchronized.

(This can be done in another PR since this is an existing issue.)

Code after:
        }

        public override bool TryMatchCharacter(int codepoint, FontStyle style, FontWeight weight, FontStretch stretch, string? familyName,
            CultureInfo? culture, out Typeface match)
        {
            //TODO12: Think about removing familyName parameter
            match = default;

            if (_fontManager.PlatformImpl is IFontManagerImpl2 fontManagerImpl2)
            {
                if (fontManagerImpl2.TryMatchCharacter(codepoint, style, weight, stretch, culture, out var glyphTypeface))
                {
                    AddGlyphTypefaceByFamilyName(glyphTypeface.FamilyName, glyphTypeface);

                    match = new Typeface(glyphTypeface.FamilyName, glyphTypeface.Style, glyphTypeface.Weight,
                        glyphTypeface.Stretch);

                    return true;
                }

                return false;
            }
            else
            {
                return _fontManager.PlatformImpl.TryMatchCharacter(codepoint, style, weight, stretch, culture, out match);
            }
        }

        private void AddGlyphTypefaceByFamilyName(string familyName, IGlyphTypeface glyphTypeface)
        {
            // Add family name to the collection if not exists
            if (!_familyNames.Contains(familyName))
            {
                _familyNames.Add(familyName);
            }

            // Get or create the typefaces dictionary for the family name
            if (!_glyphTypefaceCache.TryGetValue(familyName, out var typefaces))
            {
                _glyphTypefaceCache[familyName] = typefaces = new ConcurrentDictionary<FontCollectionKey, IGlyphTypeface?>();
            }

            // Add the glyph typeface to the cache
            typefaces.TryAdd(new FontCollectionKey(glyphTypeface.Style, glyphTypeface.Weight, glyphTypeface.Stretch), glyphTypeface);
        }
    }
}