Gondwana Game Engine Development (master) - 2.7.0
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
48 if (typeface == null)
49 throw new InvalidOperationException($"Failed to load font from file: {filePath}");
50
51 ReplaceInternal(key, typeface);
52 return typeface;
53 }
54
65 public SKTypeface LoadFromResource(string key, Assembly assembly, string resourceName)
66 {
67 ThrowIfDisposed();
68
69 if (string.IsNullOrWhiteSpace(key))
70 throw new ArgumentException("Font key cannot be null or whitespace.", nameof(key));
71
72 if (assembly == null)
73 throw new ArgumentNullException(nameof(assembly));
74
75 if (string.IsNullOrWhiteSpace(resourceName))
76 throw new ArgumentException("Resource name cannot be null or whitespace.", nameof(resourceName));
77
78 using var stream = assembly.GetManifestResourceStream(resourceName);
79
80 if (stream == null)
81 throw new InvalidOperationException(
82 $"Embedded font resource not found: '{resourceName}' in assembly '{assembly.FullName}'.");
83
84 var typeface = SKTypeface.FromStream(stream);
85
86 if (typeface == null)
87 throw new InvalidOperationException($"Failed to load font from embedded resource: {resourceName}");
88
89 ReplaceInternal(key, typeface);
90 return typeface;
91 }
92
100 public SKTypeface LoadFromResource(string key, string resourceName)
101 {
102 return LoadFromResource(key, Assembly.GetCallingAssembly(), resourceName);
103 }
104
111 public SKTypeface Get(string key)
112 {
113 ThrowIfDisposed();
114
115 if (string.IsNullOrWhiteSpace(key))
116 throw new ArgumentException("Font key cannot be null or whitespace.", nameof(key));
117
118 if (!_fonts.TryGetValue(key, out var typeface))
119 throw new KeyNotFoundException($"No font is registered under key '{key}'.");
120
121 return typeface;
122 }
123
130 public bool TryGet(string key, out SKTypeface? typeface)
131 {
132 ThrowIfDisposed();
133
134 if (string.IsNullOrWhiteSpace(key))
135 {
136 typeface = null;
137 return false;
138 }
139
140 return _fonts.TryGetValue(key, out typeface);
141 }
142
151 public SKTypeface GetOrDefault(string key)
152 {
153 ThrowIfDisposed();
154
155 if (string.IsNullOrWhiteSpace(key))
156 return SKTypeface.Default;
157
158 return _fonts.TryGetValue(key, out var typeface)
159 ? typeface
160 : SKTypeface.Default;
161 }
162
168 public bool Contains(string key)
169 {
170 ThrowIfDisposed();
171
172 if (string.IsNullOrWhiteSpace(key))
173 return false;
174
175 return _fonts.ContainsKey(key);
176 }
177
183 public bool Remove(string key)
184 {
185 ThrowIfDisposed();
186
187 if (string.IsNullOrWhiteSpace(key))
188 return false;
189
190 if (!_fonts.TryGetValue(key, out var existing))
191 return false;
192
193 _fonts.Remove(key);
194 existing.Dispose();
195 return true;
196 }
197
201 public void Clear()
202 {
203 ThrowIfDisposed();
204
205 foreach (var font in _fonts.Values)
206 font.Dispose();
207
208 _fonts.Clear();
209 }
210
214 public IReadOnlyCollection<string> Keys
215 {
216 get
217 {
218 ThrowIfDisposed();
219 return _fonts.Keys.ToArray();
220 }
221 }
222
223 private void ReplaceInternal(string key, SKTypeface newTypeface)
224 {
225 if (_fonts.TryGetValue(key, out var existing))
226 {
227 existing.Dispose();
228 _fonts[key] = newTypeface;
229 }
230 else
231 {
232 _fonts.Add(key, newTypeface);
233 }
234 }
235
236 private void ThrowIfDisposed()
237 {
238 if (_disposed)
239 throw new ObjectDisposedException(nameof(FontManager));
240 }
241
245 public void Dispose()
246 {
247 if (_disposed)
248 return;
249
250 Clear();
251 _disposed = true;
252 }
253}
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 ...