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);
49 throw new InvalidOperationException($
"Failed to load font from file: {filePath}");
51 ReplaceInternal(key, typeface);
69 if (
string.IsNullOrWhiteSpace(key))
70 throw new ArgumentException(
"Font key cannot be null or whitespace.", nameof(key));
73 throw new ArgumentNullException(nameof(assembly));
75 if (
string.IsNullOrWhiteSpace(resourceName))
76 throw new ArgumentException(
"Resource name cannot be null or whitespace.", nameof(resourceName));
78 using var stream = assembly.GetManifestResourceStream(resourceName);
81 throw new InvalidOperationException(
82 $
"Embedded font resource not found: '{resourceName}' in assembly '{assembly.FullName}'.");
84 var typeface = SKTypeface.FromStream(stream);
87 throw new InvalidOperationException($
"Failed to load font from embedded resource: {resourceName}");
89 ReplaceInternal(key, typeface);
111 public SKTypeface
Get(
string key)
115 if (
string.IsNullOrWhiteSpace(key))
116 throw new ArgumentException(
"Font key cannot be null or whitespace.", nameof(key));
118 if (!_fonts.TryGetValue(key, out var typeface))
119 throw new KeyNotFoundException($
"No font is registered under key '{key}'.");
130 public bool TryGet(
string key, out SKTypeface? typeface)
134 if (
string.IsNullOrWhiteSpace(key))
140 return _fonts.TryGetValue(key, out typeface);
155 if (
string.IsNullOrWhiteSpace(key))
156 return SKTypeface.Default;
158 return _fonts.TryGetValue(key, out var typeface)
160 : SKTypeface.Default;
172 if (
string.IsNullOrWhiteSpace(key))
175 return _fonts.ContainsKey(key);
187 if (
string.IsNullOrWhiteSpace(key))
190 if (!_fonts.TryGetValue(key, out var existing))
205 foreach (var font
in _fonts.Values)
214 public IReadOnlyCollection<string>
Keys
219 return _fonts.Keys.ToArray();
223 private void ReplaceInternal(
string key, SKTypeface newTypeface)
225 if (_fonts.TryGetValue(key, out var existing))
228 _fonts[key] = newTypeface;
232 _fonts.Add(key, newTypeface);
236 private void ThrowIfDisposed()
239 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 ...