@@ -15,7 +15,7 @@ internal class SystemFontCollection : FontCollectionBase
1515 public SystemFontCollection ( FontManager fontManager )
1616 {
1717 _fontManager = fontManager ;
18- _familyNames = fontManager . PlatformImpl . GetInstalledFontFamilyNames ( ) . ToList ( ) ;
18+ _familyNames = fontManager . PlatformImpl . GetInstalledFontFamilyNames ( ) . Where ( x => ! string . IsNullOrEmpty ( x ) ) . ToList ( ) ;
1919 }
2020
2121 public override Uri Key => FontManager . SystemFontsKey ;
@@ -47,47 +47,83 @@ public override bool TryGetGlyphTypeface(string familyName, FontStyle style, Fon
4747
4848 var key = new FontCollectionKey ( style , weight , stretch ) ;
4949
50- var glyphTypefaces = _glyphTypefaceCache . GetOrAdd ( familyName ,
50+ if ( _glyphTypefaceCache . TryGetValue ( familyName , out var glyphTypefaces ) )
51+ {
52+ if ( glyphTypefaces . TryGetValue ( key , out glyphTypeface ) )
53+ {
54+ return glyphTypeface != null ;
55+ }
56+ }
57+
58+ glyphTypefaces ??= _glyphTypefaceCache . GetOrAdd ( familyName ,
5159 ( _ ) => new ConcurrentDictionary < FontCollectionKey , IGlyphTypeface ? > ( ) ) ;
5260
53- if ( glyphTypefaces . TryGetValue ( key , out glyphTypeface ) )
61+ //Try top create the font via system font manager
62+ if ( _fontManager . PlatformImpl . TryCreateGlyphTypeface ( familyName , style , weight , stretch , out glyphTypeface ) )
5463 {
64+ glyphTypefaces . TryAdd ( key , glyphTypeface ) ;
65+
66+ return true ;
67+ }
68+
69+ //Try to find nearest match if possible
70+ if ( ! TryGetNearestMatch ( glyphTypefaces , key , out glyphTypeface ) )
71+ {
72+ if ( TryGetGlyphTypeface ( _fontManager . DefaultFontFamily . Name , style , weight , stretch , out glyphTypeface ) )
73+ {
74+ glyphTypefaces . TryAdd ( key , glyphTypeface ) ;
75+ }
76+
5577 return glyphTypeface != null ;
5678 }
5779
58- if ( ! _fontManager . PlatformImpl . TryCreateGlyphTypeface ( familyName , style , weight , stretch , out glyphTypeface ) ||
59- ! glyphTypeface . FamilyName . Contains ( familyName ) )
80+ if ( TryCreateSyntheticGlyphTypeface ( glyphTypeface , style , weight , out var syntheticGlyphTypeface ) )
81+ {
82+ glyphTypefaces . TryAdd ( key , syntheticGlyphTypeface ) ;
83+
84+ glyphTypeface = syntheticGlyphTypeface ;
85+ }
86+ else
6087 {
61- //Try to find nearest match if possible
62- TryGetNearestMatch ( glyphTypefaces , key , out glyphTypeface ) ;
88+ glyphTypefaces . TryAdd ( key , glyphTypeface ) ;
6389 }
6490
65- if ( glyphTypeface is IGlyphTypeface2 glyphTypeface2 )
91+ return true ;
92+
93+ }
94+
95+ private bool TryCreateSyntheticGlyphTypeface ( IGlyphTypeface glyphTypeface , FontStyle style , FontWeight weight ,
96+ [ NotNullWhen ( true ) ] out IGlyphTypeface ? syntheticGlyphTypeface )
97+ {
98+ if ( glyphTypeface is IGlyphTypeface2 glyphTypeface2 )
6699 {
67100 var fontSimulations = FontSimulations . None ;
68101
69- if ( style != FontStyle . Normal && glyphTypeface2 . Style != style )
102+ if ( style != FontStyle . Normal && glyphTypeface2 . Style != style )
70103 {
71104 fontSimulations |= FontSimulations . Oblique ;
72105 }
73106
74- if ( ( int ) weight >= 600 && glyphTypeface2 . Weight != weight )
107+ if ( ( int ) weight >= 600 && glyphTypeface2 . Weight != weight )
75108 {
76109 fontSimulations |= FontSimulations . Bold ;
77110 }
78111
79- if ( fontSimulations != FontSimulations . None && glyphTypeface2 . TryGetStream ( out var stream ) )
112+ if ( fontSimulations != FontSimulations . None && glyphTypeface2 . TryGetStream ( out var stream ) )
80113 {
81114 using ( stream )
82115 {
83- _fontManager . PlatformImpl . TryCreateGlyphTypeface ( stream , fontSimulations , out glyphTypeface ) ;
116+ _fontManager . PlatformImpl . TryCreateGlyphTypeface ( stream , fontSimulations ,
117+ out syntheticGlyphTypeface ) ;
118+
119+ return syntheticGlyphTypeface != null ;
84120 }
85121 }
86122 }
87123
88- glyphTypefaces . TryAdd ( key , glyphTypeface ) ;
124+ syntheticGlyphTypeface = null ;
89125
90- return glyphTypeface != null ;
126+ return false ;
91127 }
92128
93129 public override void Initialize ( IFontManagerImpl fontManager )
0 commit comments