/// <summary> /// Creates or gets an instance of the class /// </summary> /// <param name="isReuseAllowed">If false, always constructs a new instance. If true, then it operates like a singleton. /// If you're paranoid about the available fonts changing under you, you can pass false. /// Otherwise, you can pass true if you're happy with re-using whatever fonts were available the first time this ran. /// </param> public static FontFileFinder GetInstance(bool isReuseAllowed) { if (isReuseAllowed) { if (_instance == null) { _instance = new FontFileFinder(); } return(_instance); } else { return(new FontFileFinder()); } }
/// <summary> /// Return a list of FontMetadata objects for all fonts on the system. This will be SLOW the /// first time it is called, but caches the result so that it will be fast in any later calls. /// </summary> /// <remarks> /// This method is actually called only once in Program.Main just before Run(). If something /// calls this a second time (or calls AvailableFontMetadata) before it finishes, then a partial /// list of fonts is returned. /// </remarks> public static IEnumerable <FontMetadata> GetAllFontMetadata() { // This return shouldn't be used but might be triggered by a test. if (_finder != null) { return(GetFontMetadataSortedByName()); } var starting = DateTime.Now; foreach (var name in SortedListOfFontNames()) { _finder = FontFileFinder.GetInstance(isReuseAllowed: true); try { var group = _finder.GetGroupForFont(name); if (group == null || string.IsNullOrEmpty(group.Normal)) { continue; } var meta = new FontMetadata(name, group); lock (_fontNameToMetadata) { _fontNameToMetadata[name] = meta; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Trying to get font data for \"{name}\" threw: {ex}"); } } var list = GetFontMetadataSortedByName(); var ending = DateTime.Now; System.Diagnostics.Debug.WriteLine($"DEBUG: Collecting font metadata took {ending - starting}"); return(list); }