Skip to content

Commit

Permalink
Remove dead code in FontSourceCollection/FontSource (+ hollow CAS rem…
Browse files Browse the repository at this point in the history
…nants) (#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
  • Loading branch information
h3xds1nz authored Dec 18, 2024
1 parent 5c7de0f commit b11d44d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private IList<CompositeFontFamily> UserCompositeFonts
{
if (_userCompositeFonts == null)
{
_userCompositeFonts = GetCompositeFontList(new FontSourceCollection(_folderUri, false, true));
_userCompositeFonts = GetCompositeFontList(new FontSourceCollection(_folderUri, true));
}
return _userCompositeFonts;
}
Expand Down Expand Up @@ -240,7 +240,6 @@ internal static CompositeFontFamily GetCompositeFontFamilyAtIndex(int index)
if (_systemCompositeFonts[index] == null)
{
FontSource fontSource = new FontSource(new Uri(Path.Combine(FamilyCollection.SxSFontsResourcePrefix, _systemCompositeFontsFileNames[index] + Util.CompositeFontExtension), UriKind.RelativeOrAbsolute),
skipDemand:true,
isComposite:true,
isInternalCompositeFont:true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public FontSourceFactory() { }

public IFontSource Create(string uriString)
{
return new FontSource(new Uri(uriString), false);
return new FontSource(new Uri(uriString));
}
}

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

#region Constructors

public FontSource(Uri fontUri, bool skipDemand)
public FontSource(Uri fontUri)
{
Initialize(fontUri, skipDemand, false, isInternalCompositeFont: false);
Initialize(fontUri, false, isInternalCompositeFont: false);
}

public FontSource(Uri fontUri, bool skipDemand, bool isComposite)
public FontSource(Uri fontUri, bool isComposite)
{
Initialize(fontUri, skipDemand, isComposite, isInternalCompositeFont: false);
Initialize(fontUri, isComposite, isInternalCompositeFont: false);
}

/// <summary>
/// Allows WPF to construct its internal CompositeFonts from resource URIs.
/// </summary>
/// <param name="fontUri"></param>
/// <param name="skipDemand"></param>
/// <param name="isComposite"></param>
public FontSource(Uri fontUri, bool skipDemand, bool isComposite, bool isInternalCompositeFont)
public FontSource(Uri fontUri, bool isComposite, bool isInternalCompositeFont)
{
Initialize(fontUri, skipDemand, isComposite, isInternalCompositeFont);
Initialize(fontUri, isComposite, isInternalCompositeFont);
}

private void Initialize(Uri fontUri, bool skipDemand, bool isComposite, bool isInternalCompositeFont)
private void Initialize(Uri fontUri, bool isComposite, bool isInternalCompositeFont)
{
_fontUri = fontUri;
_skipDemand = skipDemand;
_isComposite = isComposite;
_isInternalCompositeFont = isInternalCompositeFont;
Invariant.Assert(_isInternalCompositeFont || _fontUri.IsAbsoluteUri);
Expand All @@ -87,7 +85,13 @@ private void Initialize(Uri fontUri, bool skipDemand, bool isComposite, bool isI
/// <summary>
/// Use this to ensure we don't call Uri.IsFile on a relative URI.
/// </summary>
public bool IsFile { get { return !_isInternalCompositeFont && _fontUri.IsFile; } }
public bool IsFile
{
get
{
return !_isInternalCompositeFont && _fontUri.IsFile;
}
}

public bool IsComposite
{
Expand Down Expand Up @@ -419,8 +423,6 @@ protected override void Dispose(bool disposing)

private Uri _fontUri;

private bool _skipDemand;

private static SizeLimitedCache<Uri, byte[]> _resourceCache = new SizeLimitedCache<Uri, byte[]>(MaximumCacheItems);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public FontSourceCollectionFactory() { }

public IFontSourceCollection Create(string uriString)
{
return new FontSourceCollection(new Uri(uriString), false);
return new FontSourceCollection(new Uri(uriString));
}
}

Expand All @@ -30,20 +30,19 @@ public IFontSourceCollection Create(string uriString)
/// </summary>
internal class FontSourceCollection : IFontSourceCollection
{
public FontSourceCollection(Uri folderUri, bool isWindowsFonts)
public FontSourceCollection(Uri folderUri)
{
Initialize(folderUri, isWindowsFonts, false);
Initialize(folderUri, false);
}

public FontSourceCollection(Uri folderUri, bool isWindowsFonts, bool tryGetCompositeFontsOnly)
public FontSourceCollection(Uri folderUri, bool tryGetCompositeFontsOnly)
{
Initialize(folderUri, isWindowsFonts, tryGetCompositeFontsOnly);
Initialize(folderUri, tryGetCompositeFontsOnly);
}

private void Initialize(Uri folderUri, bool isWindowsFonts, bool tryGetCompositeFontsOnly)
private void Initialize(Uri folderUri, bool tryGetCompositeFontsOnly)
{
_uri = folderUri;
_isWindowsFonts = isWindowsFonts;
_tryGetCompositeFontsOnly = tryGetCompositeFontsOnly;

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

if (_uri.IsFile)
{
if (_isWindowsFonts)
{
if (object.ReferenceEquals(_uri, Util.WindowsFontsUriObject))
{
// We know the local path and that it's a folder
_isFileSystemFolder = true;
}
else
{
// It's a file within the Windows Fonts folder
_isFileSystemFolder = false;
}
}
else
{
// Get the local path
string localPath = _uri.LocalPath;
// Get the local path
string localPath = _uri.LocalPath;

// Decide if it's a file or folder based on syntax, not contents of file system
_isFileSystemFolder = localPath[localPath.Length - 1] == Path.DirectorySeparatorChar;
}
// Decide if it's a file or folder based on syntax, not contents of file system
_isFileSystemFolder = localPath[localPath.Length - 1] == Path.DirectorySeparatorChar;
}
}

Expand All @@ -107,55 +90,14 @@ private void SetFontSources()
bool isOnlyCompositeFontFiles = false;
if (_isFileSystemFolder)
{
if (_isWindowsFonts)
if (_tryGetCompositeFontsOnly)
{
if (_tryGetCompositeFontsOnly)
{
files = Directory.GetFiles(_uri.LocalPath, "*" + Util.CompositeFontExtension);
isOnlyCompositeFontFiles = true;
}
else
{
// fontPaths accumulates font file paths obtained from the registry and the file system
// This collection is a set, i.e. only keys matter, not values.
HashSet<string> fontPaths = new HashSet<string>(512, StringComparer.OrdinalIgnoreCase);

using (RegistryKey fontsKey = Registry.LocalMachine.OpenSubKey(InstalledWindowsFontsRegistryKey))
{
// The registry key should be present on a valid Windows installation.
Invariant.Assert(fontsKey != null);

foreach (string fontValue in fontsKey.GetValueNames())
{
string fileName = fontsKey.GetValue(fontValue) as string;
if (fileName != null)
{
// See if the path doesn't contain any directory information.
// Shell uses the same method to determine whether to prepend the path with %windir%\fonts.
if (Path.GetFileName(fileName) == fileName)
fileName = Path.Combine(Util.WindowsFontsLocalPath, fileName);

fontPaths.Add(fileName);
}
}
}

fontPaths.UnionWith(Directory.EnumerateFiles(_uri.LocalPath));

files = fontPaths;
}
}
files = Directory.GetFiles(_uri.LocalPath, "*" + Util.CompositeFontExtension);
isOnlyCompositeFontFiles = true;
}
else
{
if (_tryGetCompositeFontsOnly)
{
files = Directory.GetFiles(_uri.LocalPath, "*" + Util.CompositeFontExtension);
isOnlyCompositeFontFiles = true;
}
else
{
files = Directory.GetFiles(_uri.LocalPath);
}
files = Directory.GetFiles(_uri.LocalPath);
}
}
else
Expand All @@ -168,7 +110,7 @@ private void SetFontSources()
{
foreach (string file in files)
{
fontSources.Add(new FontSource(new Uri(file, UriKind.Absolute), _isWindowsFonts, true));
fontSources.Add(new FontSource(new Uri(file, UriKind.Absolute), true));
}
}
else
Expand All @@ -177,7 +119,7 @@ private void SetFontSources()
foreach (string file in files)
{
if (Util.IsSupportedFontExtension(Path.GetExtension(file), out isComposite))
fontSources.Add(new FontSource(new Uri(file, UriKind.Absolute), _isWindowsFonts, isComposite));
fontSources.Add(new FontSource(new Uri(file, UriKind.Absolute), isComposite));
}
}
}
Expand All @@ -201,12 +143,12 @@ private void SetFontSources()
if (String.IsNullOrEmpty(resourceName))
{
isComposite = Util.IsCompositeFont(Path.GetExtension(_uri.AbsoluteUri));
fontSources.Add(new FontSource(_uri, _isWindowsFonts, isComposite));
fontSources.Add(new FontSource(_uri, isComposite));
}
else
{
isComposite = Util.IsCompositeFont(Path.GetExtension(resourceName));
fontSources.Add(new FontSource(new Uri(_uri, resourceName), _isWindowsFonts, isComposite));
fontSources.Add(new FontSource(new Uri(_uri, resourceName), isComposite));
}
}
}
Expand Down Expand Up @@ -240,17 +182,11 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()

private Uri _uri;

private bool _isWindowsFonts;

// _isFileSystemFolder flag makes sense only when _uri.IsFile is set to true.
private bool _isFileSystemFolder;
private volatile IList<Text.TextInterface.IFontSource> _fontSources;

// Flag to indicate that only composite fonts in the provided URI location should be retrieved.
private bool _tryGetCompositeFontsOnly;

private const string InstalledWindowsFontsRegistryKey = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts";
private const string InstalledWindowsFontsRegistryKeyFullPath = @"HKEY_LOCAL_MACHINE\" + InstalledWindowsFontsRegistryKey;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ internal GlyphTypeface(MS.Internal.Text.TextInterface.Font font)
Uri typefaceSource = new Uri(uriPath);

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

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

_fontFace = new FontFaceLayoutInfo(_font);

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


_initializationState = InitializationState.IsInitialized; // fully initialized
Expand Down

0 comments on commit b11d44d

Please sign in to comment.