Skip to content

Commit b11d44d

Browse files
authored
Remove dead code in FontSourceCollection/FontSource (+ hollow CAS remnants) (#9838)
* Remove deadcode as _isWindowsFonts is always false * Remove _isWindowsFonts variable and conditionals * Remove constructors and callers with isWindowsFonts * Remove _skipDemand from FontSource as it is unused after CAS removal * Remove skipDemand from constructors and callers of those ctors * Remove unused constants
1 parent 5c7de0f commit b11d44d

File tree

4 files changed

+39
-104
lines changed

4 files changed

+39
-104
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FamilyCollection.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private IList<CompositeFontFamily> UserCompositeFonts
7979
{
8080
if (_userCompositeFonts == null)
8181
{
82-
_userCompositeFonts = GetCompositeFontList(new FontSourceCollection(_folderUri, false, true));
82+
_userCompositeFonts = GetCompositeFontList(new FontSourceCollection(_folderUri, true));
8383
}
8484
return _userCompositeFonts;
8585
}
@@ -240,7 +240,6 @@ internal static CompositeFontFamily GetCompositeFontFamilyAtIndex(int index)
240240
if (_systemCompositeFonts[index] == null)
241241
{
242242
FontSource fontSource = new FontSource(new Uri(Path.Combine(FamilyCollection.SxSFontsResourcePrefix, _systemCompositeFontsFileNames[index] + Util.CompositeFontExtension), UriKind.RelativeOrAbsolute),
243-
skipDemand:true,
244243
isComposite:true,
245244
isInternalCompositeFont:true);
246245

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontSource.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public FontSourceFactory() { }
2525

2626
public IFontSource Create(string uriString)
2727
{
28-
return new FontSource(new Uri(uriString), false);
28+
return new FontSource(new Uri(uriString));
2929
}
3030
}
3131

@@ -43,31 +43,29 @@ internal class FontSource : IFontSource
4343

4444
#region Constructors
4545

46-
public FontSource(Uri fontUri, bool skipDemand)
46+
public FontSource(Uri fontUri)
4747
{
48-
Initialize(fontUri, skipDemand, false, isInternalCompositeFont: false);
48+
Initialize(fontUri, false, isInternalCompositeFont: false);
4949
}
5050

51-
public FontSource(Uri fontUri, bool skipDemand, bool isComposite)
51+
public FontSource(Uri fontUri, bool isComposite)
5252
{
53-
Initialize(fontUri, skipDemand, isComposite, isInternalCompositeFont: false);
53+
Initialize(fontUri, isComposite, isInternalCompositeFont: false);
5454
}
5555

5656
/// <summary>
5757
/// Allows WPF to construct its internal CompositeFonts from resource URIs.
5858
/// </summary>
5959
/// <param name="fontUri"></param>
60-
/// <param name="skipDemand"></param>
6160
/// <param name="isComposite"></param>
62-
public FontSource(Uri fontUri, bool skipDemand, bool isComposite, bool isInternalCompositeFont)
61+
public FontSource(Uri fontUri, bool isComposite, bool isInternalCompositeFont)
6362
{
64-
Initialize(fontUri, skipDemand, isComposite, isInternalCompositeFont);
63+
Initialize(fontUri, isComposite, isInternalCompositeFont);
6564
}
6665

67-
private void Initialize(Uri fontUri, bool skipDemand, bool isComposite, bool isInternalCompositeFont)
66+
private void Initialize(Uri fontUri, bool isComposite, bool isInternalCompositeFont)
6867
{
6968
_fontUri = fontUri;
70-
_skipDemand = skipDemand;
7169
_isComposite = isComposite;
7270
_isInternalCompositeFont = isInternalCompositeFont;
7371
Invariant.Assert(_isInternalCompositeFont || _fontUri.IsAbsoluteUri);
@@ -87,7 +85,13 @@ private void Initialize(Uri fontUri, bool skipDemand, bool isComposite, bool isI
8785
/// <summary>
8886
/// Use this to ensure we don't call Uri.IsFile on a relative URI.
8987
/// </summary>
90-
public bool IsFile { get { return !_isInternalCompositeFont && _fontUri.IsFile; } }
88+
public bool IsFile
89+
{
90+
get
91+
{
92+
return !_isInternalCompositeFont && _fontUri.IsFile;
93+
}
94+
}
9195

9296
public bool IsComposite
9397
{
@@ -419,8 +423,6 @@ protected override void Dispose(bool disposing)
419423

420424
private Uri _fontUri;
421425

422-
private bool _skipDemand;
423-
424426
private static SizeLimitedCache<Uri, byte[]> _resourceCache = new SizeLimitedCache<Uri, byte[]>(MaximumCacheItems);
425427

426428
/// <summary>

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontSourceCollection.cs

Lines changed: 21 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public FontSourceCollectionFactory() { }
2121

2222
public IFontSourceCollection Create(string uriString)
2323
{
24-
return new FontSourceCollection(new Uri(uriString), false);
24+
return new FontSourceCollection(new Uri(uriString));
2525
}
2626
}
2727

@@ -30,20 +30,19 @@ public IFontSourceCollection Create(string uriString)
3030
/// </summary>
3131
internal class FontSourceCollection : IFontSourceCollection
3232
{
33-
public FontSourceCollection(Uri folderUri, bool isWindowsFonts)
33+
public FontSourceCollection(Uri folderUri)
3434
{
35-
Initialize(folderUri, isWindowsFonts, false);
35+
Initialize(folderUri, false);
3636
}
3737

38-
public FontSourceCollection(Uri folderUri, bool isWindowsFonts, bool tryGetCompositeFontsOnly)
38+
public FontSourceCollection(Uri folderUri, bool tryGetCompositeFontsOnly)
3939
{
40-
Initialize(folderUri, isWindowsFonts, tryGetCompositeFontsOnly);
40+
Initialize(folderUri, tryGetCompositeFontsOnly);
4141
}
4242

43-
private void Initialize(Uri folderUri, bool isWindowsFonts, bool tryGetCompositeFontsOnly)
43+
private void Initialize(Uri folderUri, bool tryGetCompositeFontsOnly)
4444
{
4545
_uri = folderUri;
46-
_isWindowsFonts = isWindowsFonts;
4746
_tryGetCompositeFontsOnly = tryGetCompositeFontsOnly;
4847

4948
bool isComposite = false;
@@ -55,7 +54,7 @@ private void Initialize(Uri folderUri, bool isWindowsFonts, bool tryGetComposite
5554
if (isSingleSupportedFile || !Util.IsEnumerableFontUriScheme(_uri))
5655
{
5756
_fontSources = new List<Text.TextInterface.IFontSource>(1);
58-
_fontSources.Add(new FontSource(_uri, false, isComposite));
57+
_fontSources.Add(new FontSource(_uri, isComposite));
5958
}
6059
else
6160
{
@@ -69,27 +68,11 @@ private void InitializeDirectoryProperties()
6968

7069
if (_uri.IsFile)
7170
{
72-
if (_isWindowsFonts)
73-
{
74-
if (object.ReferenceEquals(_uri, Util.WindowsFontsUriObject))
75-
{
76-
// We know the local path and that it's a folder
77-
_isFileSystemFolder = true;
78-
}
79-
else
80-
{
81-
// It's a file within the Windows Fonts folder
82-
_isFileSystemFolder = false;
83-
}
84-
}
85-
else
86-
{
87-
// Get the local path
88-
string localPath = _uri.LocalPath;
71+
// Get the local path
72+
string localPath = _uri.LocalPath;
8973

90-
// Decide if it's a file or folder based on syntax, not contents of file system
91-
_isFileSystemFolder = localPath[localPath.Length - 1] == Path.DirectorySeparatorChar;
92-
}
74+
// Decide if it's a file or folder based on syntax, not contents of file system
75+
_isFileSystemFolder = localPath[localPath.Length - 1] == Path.DirectorySeparatorChar;
9376
}
9477
}
9578

@@ -107,55 +90,14 @@ private void SetFontSources()
10790
bool isOnlyCompositeFontFiles = false;
10891
if (_isFileSystemFolder)
10992
{
110-
if (_isWindowsFonts)
93+
if (_tryGetCompositeFontsOnly)
11194
{
112-
if (_tryGetCompositeFontsOnly)
113-
{
114-
files = Directory.GetFiles(_uri.LocalPath, "*" + Util.CompositeFontExtension);
115-
isOnlyCompositeFontFiles = true;
116-
}
117-
else
118-
{
119-
// fontPaths accumulates font file paths obtained from the registry and the file system
120-
// This collection is a set, i.e. only keys matter, not values.
121-
HashSet<string> fontPaths = new HashSet<string>(512, StringComparer.OrdinalIgnoreCase);
122-
123-
using (RegistryKey fontsKey = Registry.LocalMachine.OpenSubKey(InstalledWindowsFontsRegistryKey))
124-
{
125-
// The registry key should be present on a valid Windows installation.
126-
Invariant.Assert(fontsKey != null);
127-
128-
foreach (string fontValue in fontsKey.GetValueNames())
129-
{
130-
string fileName = fontsKey.GetValue(fontValue) as string;
131-
if (fileName != null)
132-
{
133-
// See if the path doesn't contain any directory information.
134-
// Shell uses the same method to determine whether to prepend the path with %windir%\fonts.
135-
if (Path.GetFileName(fileName) == fileName)
136-
fileName = Path.Combine(Util.WindowsFontsLocalPath, fileName);
137-
138-
fontPaths.Add(fileName);
139-
}
140-
}
141-
}
142-
143-
fontPaths.UnionWith(Directory.EnumerateFiles(_uri.LocalPath));
144-
145-
files = fontPaths;
146-
}
147-
}
95+
files = Directory.GetFiles(_uri.LocalPath, "*" + Util.CompositeFontExtension);
96+
isOnlyCompositeFontFiles = true;
97+
}
14898
else
14999
{
150-
if (_tryGetCompositeFontsOnly)
151-
{
152-
files = Directory.GetFiles(_uri.LocalPath, "*" + Util.CompositeFontExtension);
153-
isOnlyCompositeFontFiles = true;
154-
}
155-
else
156-
{
157-
files = Directory.GetFiles(_uri.LocalPath);
158-
}
100+
files = Directory.GetFiles(_uri.LocalPath);
159101
}
160102
}
161103
else
@@ -168,7 +110,7 @@ private void SetFontSources()
168110
{
169111
foreach (string file in files)
170112
{
171-
fontSources.Add(new FontSource(new Uri(file, UriKind.Absolute), _isWindowsFonts, true));
113+
fontSources.Add(new FontSource(new Uri(file, UriKind.Absolute), true));
172114
}
173115
}
174116
else
@@ -177,7 +119,7 @@ private void SetFontSources()
177119
foreach (string file in files)
178120
{
179121
if (Util.IsSupportedFontExtension(Path.GetExtension(file), out isComposite))
180-
fontSources.Add(new FontSource(new Uri(file, UriKind.Absolute), _isWindowsFonts, isComposite));
122+
fontSources.Add(new FontSource(new Uri(file, UriKind.Absolute), isComposite));
181123
}
182124
}
183125
}
@@ -201,12 +143,12 @@ private void SetFontSources()
201143
if (String.IsNullOrEmpty(resourceName))
202144
{
203145
isComposite = Util.IsCompositeFont(Path.GetExtension(_uri.AbsoluteUri));
204-
fontSources.Add(new FontSource(_uri, _isWindowsFonts, isComposite));
146+
fontSources.Add(new FontSource(_uri, isComposite));
205147
}
206148
else
207149
{
208150
isComposite = Util.IsCompositeFont(Path.GetExtension(resourceName));
209-
fontSources.Add(new FontSource(new Uri(_uri, resourceName), _isWindowsFonts, isComposite));
151+
fontSources.Add(new FontSource(new Uri(_uri, resourceName), isComposite));
210152
}
211153
}
212154
}
@@ -240,17 +182,11 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
240182

241183
private Uri _uri;
242184

243-
private bool _isWindowsFonts;
244-
245185
// _isFileSystemFolder flag makes sense only when _uri.IsFile is set to true.
246186
private bool _isFileSystemFolder;
247187
private volatile IList<Text.TextInterface.IFontSource> _fontSources;
248188

249189
// Flag to indicate that only composite fonts in the provided URI location should be retrieved.
250190
private bool _tryGetCompositeFontsOnly;
251-
252-
private const string InstalledWindowsFontsRegistryKey = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts";
253-
private const string InstalledWindowsFontsRegistryKeyFullPath = @"HKEY_LOCAL_MACHINE\" + InstalledWindowsFontsRegistryKey;
254-
}
191+
}
255192
}
256-

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ internal GlyphTypeface(MS.Internal.Text.TextInterface.Font font)
9797
Uri typefaceSource = new Uri(uriPath);
9898

9999
_fontFace = new FontFaceLayoutInfo(font);
100-
// We skip permission demands for FontSource because the above line already demands them for the right callers.
101-
_fontSource = new FontSource(typefaceSource, true);
100+
_fontSource = new FontSource(typefaceSource);
102101

103102
Invariant.Assert( styleSimulations == StyleSimulations.None
104103
|| styleSimulations == StyleSimulations.ItalicSimulation
@@ -151,8 +150,7 @@ private void Initialize(Uri typefaceSource, StyleSimulations styleSimulations)
151150

152151
_fontFace = new FontFaceLayoutInfo(_font);
153152

154-
// We skip permission demands for FontSource because the above line already demands them for the right callers.
155-
_fontSource = new FontSource(fontSourceUri, true);
153+
_fontSource = new FontSource(fontSourceUri);
156154

157155

158156
_initializationState = InitializationState.IsInitialized; // fully initialized

0 commit comments

Comments
 (0)