public static TypefaceCollectionEntry Get(FontFamily fontFamily, FontWeight fontWeight, FontStyle fontStyle) { if (fontFamily.Key != null) { return(SKTypefaceCollectionCache.GetOrAddTypefaceCollection(fontFamily) .Get(fontFamily.Name, fontWeight, fontStyle)); } var typefaceCollection = s_cache.GetOrAdd(fontFamily.Name, new ConcurrentDictionary <FontKey, TypefaceCollectionEntry>()); var key = new FontKey(fontWeight, fontStyle); if (typefaceCollection.TryGetValue(key, out var entry)) { return(entry); } var skTypeface = SKTypeface.FromFamilyName(fontFamily.Name, (SKFontStyleWeight)fontWeight, SKFontStyleWidth.Normal, (SKFontStyleSlant)fontStyle) ?? SKTypeface.Default; var typeface = new Typeface(fontFamily.Name, fontWeight, fontStyle); entry = new TypefaceCollectionEntry(typeface, skTypeface); typefaceCollection[key] = entry; return(entry); }
public void AddEntry(string familyName, FontKey key, TypefaceCollectionEntry entry) { if (!_fontFamilies.TryGetValue(familyName, out var fontFamily)) { fontFamily = new ConcurrentDictionary <FontKey, TypefaceCollectionEntry>(); _fontFamilies.TryAdd(familyName, fontFamily); } fontFamily.TryAdd(key, entry); }
/// <summary> /// Creates the custom font collection. /// </summary> /// <param name="fontFamily">The font family.</param> /// <returns></returns> private static SKTypefaceCollection CreateCustomFontCollection(FontFamily fontFamily) { var fontAssets = FontFamilyLoader.LoadFontAssets(fontFamily.Key); var typeFaceCollection = new SKTypefaceCollection(); var assetLoader = AvaloniaLocator.Current.GetService <IAssetLoader>(); foreach (var asset in fontAssets) { var assetStream = assetLoader.Open(asset); var skTypeface = SKTypeface.FromStream(assetStream); var typeface = new Typeface(fontFamily, (FontWeight)skTypeface.FontWeight, (FontStyle)skTypeface.FontSlant); var entry = new TypefaceCollectionEntry(typeface, skTypeface); typeFaceCollection.AddEntry(skTypeface.FamilyName, new FontKey(typeface.Weight, typeface.Style), entry); } return(typeFaceCollection); }