-
Notifications
You must be signed in to change notification settings - Fork 419
/
FontStore.cs
222 lines (182 loc) · 8.04 KB
/
FontStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Graphics.Textures;
using System.Collections.Generic;
using System.Threading.Tasks;
using osu.Framework.Logging;
using System.Collections.Concurrent;
using JetBrains.Annotations;
using osu.Framework.Platform;
using osu.Framework.Text;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics.OpenGL.Textures;
using osuTK.Graphics.ES30;
namespace osu.Framework.IO.Stores
{
public class FontStore : TextureStore, ITexturedGlyphLookupStore
{
private readonly List<GlyphStore> glyphStores = new List<GlyphStore>();
private readonly List<FontStore> nestedFontStores = new List<FontStore>();
private Storage cacheStorage;
/// <summary>
/// A local cache to avoid string allocation overhead. Can be changed to (string,char)=>string if this ever becomes an issue,
/// but as long as we directly inherit <see cref="TextureStore"/> this is a slight optimisation.
/// </summary>
private readonly ConcurrentDictionary<(string, char), ITexturedCharacterGlyph> namespacedGlyphCache = new ConcurrentDictionary<(string, char), ITexturedCharacterGlyph>();
/// <summary>
/// Construct a font store to be added to a parent font store via <see cref="AddStore"/>.
/// </summary>
/// <param name="store">The texture source.</param>
/// <param name="scaleAdjust">The raw pixel height of the font. Can be used to apply a global scale or metric to font usages.</param>
public FontStore(IResourceStore<TextureUpload> store = null, float scaleAdjust = 100)
: this(store, scaleAdjust, false)
{
}
/// <summary>
/// Construct a font store with a custom filtering mode to be added to a parent font store via <see cref="AddStore"/>.
/// All fonts that use the specified filter mode should be nested inside this store to make optimal use of texture atlases.
/// </summary>
/// <param name="store">The texture source.</param>
/// <param name="scaleAdjust">The raw pixel height of the font. Can be used to apply a global scale or metric to font usages.</param>
/// <param name="minFilterMode">The texture minification filtering mode to use.</param>
public FontStore(IResourceStore<TextureUpload> store = null, float scaleAdjust = 100, All minFilterMode = All.Linear)
: this(store, scaleAdjust, true, filteringMode: minFilterMode)
{
}
internal FontStore(IResourceStore<TextureUpload> store = null, float scaleAdjust = 100, bool useAtlas = false, Storage cacheStorage = null, All filteringMode = All.Linear)
: base(store, scaleAdjust: scaleAdjust, useAtlas: useAtlas, filteringMode: filteringMode)
{
this.cacheStorage = cacheStorage;
}
protected override IEnumerable<string> GetFilenames(string name) =>
// extensions should not be used as they interfere with character lookup.
name.Yield();
public override void AddStore(IResourceStore<TextureUpload> store)
{
switch (store)
{
case FontStore fs:
// if null, share the main store's atlas.
fs.Atlas ??= Atlas;
fs.cacheStorage ??= cacheStorage;
nestedFontStores.Add(fs);
return;
case GlyphStore gs:
if (gs is RawCachingGlyphStore raw && raw.CacheStorage == null)
raw.CacheStorage = cacheStorage;
glyphStores.Add(gs);
queueLoad(gs);
break;
}
base.AddStore(store);
}
private Task childStoreLoadTasks;
/// <summary>
/// Append child stores to a single threaded load task.
/// </summary>
private void queueLoad(GlyphStore store)
{
var previousLoadStream = childStoreLoadTasks;
childStoreLoadTasks = Task.Run(async () =>
{
if (previousLoadStream != null)
await previousLoadStream.ConfigureAwait(false);
try
{
Logger.Log($"Loading Font {store.FontName}...", level: LogLevel.Debug);
await store.LoadFontAsync().ConfigureAwait(false);
Logger.Log($"Loaded Font {store.FontName}!", level: LogLevel.Debug);
}
catch
{
// Errors are logged by LoadFontAsync() but also propagated outwards.
// We can gracefully continue when loading a font fails, so the exception shouldn't trigger the unobserved exception handler of GameHost and potentially crash the game.
}
});
}
public override void RemoveStore(IResourceStore<TextureUpload> store)
{
switch (store)
{
case FontStore fs:
nestedFontStores.Remove(fs);
return;
case GlyphStore gs:
glyphStores.Remove(gs);
break;
}
base.RemoveStore(store);
}
public new Texture Get(string name)
{
var found = base.Get(name, WrapMode.None, WrapMode.None);
if (found == null)
{
foreach (var store in nestedFontStores)
{
if ((found = store.Get(name)) != null)
break;
}
}
return found;
}
[CanBeNull]
public ITexturedCharacterGlyph Get(string fontName, char character)
{
var key = (fontName, character);
if (namespacedGlyphCache.TryGetValue(key, out var existing))
return existing;
string textureName = string.IsNullOrEmpty(fontName) ? character.ToString() : $"{fontName}/{character}";
foreach (var store in glyphStores)
{
if ((string.IsNullOrEmpty(fontName) || fontName == store.FontName) && store.HasGlyph(character))
return namespacedGlyphCache[key] = new TexturedCharacterGlyph(store.Get(character), Get(textureName), 1 / ScaleAdjust);
}
foreach (var store in nestedFontStores)
{
var glyph = store.Get(fontName, character);
if (glyph != null)
return namespacedGlyphCache[key] = glyph;
}
return namespacedGlyphCache[key] = null;
}
public Task<ITexturedCharacterGlyph> GetAsync(string fontName, char character) => Task.Run(() => Get(fontName, character));
public float? GetBaseHeight(char c)
{
foreach (var store in glyphStores)
{
if (store.HasGlyph(c))
return store.GetBaseHeight() / ScaleAdjust;
}
foreach (var store in nestedFontStores)
{
var height = store.GetBaseHeight(c);
if (height.HasValue)
return height;
}
return null;
}
public float? GetBaseHeight(string fontName)
{
foreach (var store in glyphStores)
{
var bh = store.GetBaseHeight(fontName);
if (bh.HasValue)
return bh.Value / ScaleAdjust;
}
foreach (var store in nestedFontStores)
{
var height = store.GetBaseHeight(fontName);
if (height.HasValue)
return height;
}
return null;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
nestedFontStores.ForEach(f => f.Dispose());
glyphStores.ForEach(g => g.Dispose());
}
}
}