1using System.Reflection;
10public sealed class FontManager : IDisposable
12 private static readonly Lazy<FontManager> _instance =
new(() =>
new FontManager());
14 private readonly Dictionary<string, SKTypeface> _fonts =
new(StringComparer.OrdinalIgnoreCase);
15 private bool _disposed;
20 public static FontManager
Instance => _instance.Value;
25 private FontManager() { }
40 if (
string.IsNullOrWhiteSpace(key))
41 throw new ArgumentException(
"Font key cannot be null or whitespace.", nameof(key));
43 if (
string.IsNullOrWhiteSpace(filePath))
44 throw new ArgumentException(
"Font file path cannot be null or whitespace.", nameof(filePath));
46 var typeface = SKTypeface.FromFile(filePath);
48 throw new InvalidOperationException($
"Failed to load font from file: {filePath}");
50 ReplaceInternal(key, typeface);
68 if (
string.IsNullOrWhiteSpace(key))
69 throw new ArgumentException(
"Font key cannot be null or whitespace.", nameof(key));
72 throw new ArgumentNullException(nameof(assembly));
74 if (
string.IsNullOrWhiteSpace(resourceName))
75 throw new ArgumentException(
"Resource name cannot be null or whitespace.", nameof(resourceName));
77 using var stream = assembly.GetManifestResourceStream(resourceName);
79 throw new InvalidOperationException(
80 $
"Embedded font resource not found: '{resourceName}' in assembly '{assembly.FullName}'.");
82 var typeface = SKTypeface.FromStream(stream);
84 throw new InvalidOperationException($
"Failed to load font from embedded resource: {resourceName}");
86 ReplaceInternal(key, typeface);
108 public SKTypeface
Get(
string key)
112 if (
string.IsNullOrWhiteSpace(key))
113 throw new ArgumentException(
"Font key cannot be null or whitespace.", nameof(key));
115 if (!_fonts.TryGetValue(key, out var typeface))
116 throw new KeyNotFoundException($
"No font is registered under key '{key}'.");
127 public bool TryGet(
string key, out SKTypeface? typeface)
131 if (
string.IsNullOrWhiteSpace(key))
137 return _fonts.TryGetValue(key, out typeface);
152 if (
string.IsNullOrWhiteSpace(key))
153 return SKTypeface.Default;
155 return _fonts.TryGetValue(key, out var typeface)
157 : SKTypeface.Default;
169 if (
string.IsNullOrWhiteSpace(key))
172 return _fonts.ContainsKey(key);
184 if (
string.IsNullOrWhiteSpace(key))
187 if (!_fonts.TryGetValue(key, out var existing))
202 foreach (var font
in _fonts.Values)
211 public IReadOnlyCollection<string>
Keys
216 return _fonts.Keys.ToArray();
220 private void ReplaceInternal(
string key, SKTypeface newTypeface)
222 if (_fonts.TryGetValue(key, out var existing))
225 _fonts[key] = newTypeface;
229 _fonts.Add(key, newTypeface);
233 private void ThrowIfDisposed()
236 throw new ObjectDisposedException(nameof(FontManager));
bool Remove(string key)
Removes a single font entry by key and disposes the stored typeface.
SKTypeface LoadFromResource(string key, Assembly assembly, string resourceName)
Loads a font from an embedded resource in the specified assembly and stores it under the given key....
void Dispose()
Disposes all loaded fonts managed by this instance and prevents further use.
bool Contains(string key)
Returns true if a font exists for the given key.
SKTypeface LoadFromFile(string key, string filePath)
Loads a font from a file path and stores it under the given key. If the key already exists,...
SKTypeface LoadFromResource(string key, string resourceName)
Loads a font from an embedded resource in the calling assembly and stores it under the given key....
static FontManager Instance
Gets the singleton instance of the FontManager.
IReadOnlyCollection< string > Keys
Gets all currently registered font keys.
bool TryGet(string key, out SKTypeface? typeface)
Tries to get a font by key.
SKTypeface Get(string key)
Gets a font by key.
void Clear()
Disposes all loaded fonts and clears the manager.
SKTypeface GetOrDefault(string key)
Retrieves the typeface associated with the specified key, or returns the default typeface if the key ...