Browse Source

Use ConcurrentDictionary

pull/10455/head
Benedikt Stebner 4 years ago
parent
commit
90ae432e63
  1. 24
      src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs

24
src/Avalonia.Base/Media/Fonts/EmbeddedFontCollection.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Globalization; using System.Globalization;
@ -9,8 +10,7 @@ namespace Avalonia.Media.Fonts
{ {
public class EmbeddedFontCollection : IFontCollection public class EmbeddedFontCollection : IFontCollection
{ {
private readonly Dictionary<string, Dictionary<FontCollectionKey, IGlyphTypeface>> _glyphTypefaceCache = private readonly ConcurrentDictionary<string, ConcurrentDictionary<FontCollectionKey, IGlyphTypeface>> _glyphTypefaceCache = new();
new Dictionary<string, Dictionary<FontCollectionKey, IGlyphTypeface>>();
private readonly List<FontFamily> _fontFamilies = new List<FontFamily>(1); private readonly List<FontFamily> _fontFamilies = new List<FontFamily>(1);
@ -45,11 +45,12 @@ namespace Avalonia.Media.Fonts
{ {
if (!_glyphTypefaceCache.TryGetValue(glyphTypeface.FamilyName, out var glyphTypefaces)) if (!_glyphTypefaceCache.TryGetValue(glyphTypeface.FamilyName, out var glyphTypefaces))
{ {
glyphTypefaces = new Dictionary<FontCollectionKey, IGlyphTypeface>(); glyphTypefaces = new ConcurrentDictionary<FontCollectionKey, IGlyphTypeface>();
_glyphTypefaceCache.Add(glyphTypeface.FamilyName, glyphTypefaces); if (_glyphTypefaceCache.TryAdd(glyphTypeface.FamilyName, glyphTypefaces))
{
_fontFamilies.Add(new FontFamily(_key, glyphTypeface.FamilyName)); _fontFamilies.Add(new FontFamily(_key, glyphTypeface.FamilyName));
}
} }
var key = new FontCollectionKey( var key = new FontCollectionKey(
@ -57,10 +58,7 @@ namespace Avalonia.Media.Fonts
glyphTypeface.Weight, glyphTypeface.Weight,
glyphTypeface.Stretch); glyphTypeface.Stretch);
if (!glyphTypefaces.ContainsKey(key)) glyphTypefaces.TryAdd(key, glyphTypeface);
{
glyphTypefaces.Add(key, glyphTypeface);
}
} }
} }
} }
@ -119,7 +117,7 @@ namespace Avalonia.Media.Fonts
} }
private static bool TryGetNearestMatch( private static bool TryGetNearestMatch(
Dictionary<FontCollectionKey, IGlyphTypeface> glyphTypefaces, ConcurrentDictionary<FontCollectionKey, IGlyphTypeface> glyphTypefaces,
FontCollectionKey key, FontCollectionKey key,
[NotNullWhen(true)] out IGlyphTypeface? glyphTypeface) [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface)
{ {
@ -173,7 +171,7 @@ namespace Avalonia.Media.Fonts
} }
private static bool TryFindStretchFallback( private static bool TryFindStretchFallback(
Dictionary<FontCollectionKey, IGlyphTypeface> glyphTypefaces, ConcurrentDictionary<FontCollectionKey, IGlyphTypeface> glyphTypefaces,
FontCollectionKey key, FontCollectionKey key,
[NotNullWhen(true)] out IGlyphTypeface? glyphTypeface) [NotNullWhen(true)] out IGlyphTypeface? glyphTypeface)
{ {
@ -206,7 +204,7 @@ namespace Avalonia.Media.Fonts
} }
private static bool TryFindWeightFallback( private static bool TryFindWeightFallback(
Dictionary<FontCollectionKey, IGlyphTypeface> glyphTypefaces, ConcurrentDictionary<FontCollectionKey, IGlyphTypeface> glyphTypefaces,
FontCollectionKey key, FontCollectionKey key,
[NotNullWhen(true)] out IGlyphTypeface? typeface) [NotNullWhen(true)] out IGlyphTypeface? typeface)
{ {

Loading…
Cancel
Save