Gondwana Game Engine Development (master) - 2.6.1
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
FontManager.cs
1using System.Reflection;
2using SkiaSharp;
3
5
10public sealed class FontManager : IDisposable
11{
12 private static readonly Lazy<FontManager> _instance = new(() => new FontManager());
13
14 private readonly Dictionary<string, SKTypeface> _fonts = new(StringComparer.OrdinalIgnoreCase);
15 private bool _disposed;
16
20 public static FontManager Instance => _instance.Value;
21
25 private FontManager() { }
26
36 public SKTypeface LoadFromFile(string key, string filePath)
37 {
38 ThrowIfDisposed();
39
40 if (string.IsNullOrWhiteSpace(key))
41 throw new ArgumentException("Font key cannot be null or whitespace.", nameof(key));
42
43 if (string.IsNullOrWhiteSpace(filePath))
44 throw new ArgumentException("Font file path cannot be null or whitespace.", nameof(filePath));
45
46 var typeface = SKTypeface.FromFile(filePath);
47 if (typeface == null)
48 throw new InvalidOperationException($"Failed to load font from file: {filePath}");
49
50 ReplaceInternal(key, typeface);
51 return typeface;
52 }
53
64 public SKTypeface LoadFromResource(string key, Assembly assembly, string resourceName)
65 {
66 ThrowIfDisposed();
67
68 if (string.IsNullOrWhiteSpace(key))
69 throw new ArgumentException("Font key cannot be null or whitespace.", nameof(key));
70
71 if (assembly == null)
72 throw new ArgumentNullException(nameof(assembly));
73
74 if (string.IsNullOrWhiteSpace(resourceName))
75 throw new ArgumentException("Resource name cannot be null or whitespace.", nameof(resourceName));
76
77 using var stream = assembly.GetManifestResourceStream(resourceName);
78 if (stream == null)
79 throw new InvalidOperationException(
80 $"Embedded font resource not found: '{resourceName}' in assembly '{assembly.FullName}'.");
81
82 var typeface = SKTypeface.FromStream(stream);
83 if (typeface == null)
84 throw new InvalidOperationException($"Failed to load font from embedded resource: {resourceName}");
85
86 ReplaceInternal(key, typeface);
87 return typeface;
88 }
89
97 public SKTypeface LoadFromResource(string key, string resourceName)
98 {
99 return LoadFromResource(key, Assembly.GetCallingAssembly(), resourceName);
100 }
101
108 public SKTypeface Get(string key)
109 {
110 ThrowIfDisposed();
111
112 if (string.IsNullOrWhiteSpace(key))
113 throw new ArgumentException("Font key cannot be null or whitespace.", nameof(key));
114
115 if (!_fonts.TryGetValue(key, out var typeface))
116 throw new KeyNotFoundException($"No font is registered under key '{key}'.");
117
118 return typeface;
119 }
120
127 public bool TryGet(string key, out SKTypeface? typeface)
128 {
129 ThrowIfDisposed();
130
131 if (string.IsNullOrWhiteSpace(key))
132 {
133 typeface = null;
134 return false;
135 }
136
137 return _fonts.TryGetValue(key, out typeface);
138 }
139
148 public SKTypeface GetOrDefault(string key)
149 {
150 ThrowIfDisposed();
151
152 if (string.IsNullOrWhiteSpace(key))
153 return SKTypeface.Default;
154
155 return _fonts.TryGetValue(key, out var typeface)
156 ? typeface
157 : SKTypeface.Default;
158 }
159
165 public bool Contains(string key)
166 {
167 ThrowIfDisposed();
168
169 if (string.IsNullOrWhiteSpace(key))
170 return false;
171
172 return _fonts.ContainsKey(key);
173 }
174
180 public bool Remove(string key)
181 {
182 ThrowIfDisposed();
183
184 if (string.IsNullOrWhiteSpace(key))
185 return false;
186
187 if (!_fonts.TryGetValue(key, out var existing))
188 return false;
189
190 _fonts.Remove(key);
191 existing.Dispose();
192 return true;
193 }
194
198 public void Clear()
199 {
200 ThrowIfDisposed();
201
202 foreach (var font in _fonts.Values)
203 font.Dispose();
204
205 _fonts.Clear();
206 }
207
211 public IReadOnlyCollection<string> Keys
212 {
213 get
214 {
215 ThrowIfDisposed();
216 return _fonts.Keys.ToArray();
217 }
218 }
219
220 private void ReplaceInternal(string key, SKTypeface newTypeface)
221 {
222 if (_fonts.TryGetValue(key, out var existing))
223 {
224 existing.Dispose();
225 _fonts[key] = newTypeface;
226 }
227 else
228 {
229 _fonts.Add(key, newTypeface);
230 }
231 }
232
233 private void ThrowIfDisposed()
234 {
235 if (_disposed)
236 throw new ObjectDisposedException(nameof(FontManager));
237 }
238
242 public void Dispose()
243 {
244 if (_disposed)
245 return;
246
247 Clear();
248 _disposed = true;
249 }
250}
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 ...