Support multiple fonts with the same full name.

This commit is contained in:
2023-02-05 15:51:18 +08:00
parent b407ba88c4
commit 6d0fd0f9ec

View File

@@ -6,20 +6,24 @@ using System.Linq;
namespace Cryville.Common.Font {
public abstract class FontManager {
protected IReadOnlyDictionary<string, Typeface> MapFullNameToTypeface { get; private set; }
protected IReadOnlyDictionary<string, IReadOnlyCollection<Typeface>> MapNameToTypefaces { get; private set; }
public IReadOnlyDictionary<string, IReadOnlyCollection<Typeface>> MapFullNameToTypeface { get; private set; }
public IReadOnlyDictionary<string, IReadOnlyCollection<Typeface>> MapNameToTypefaces { get; private set; }
public FontManager() {
var map1 = new Dictionary<string, Typeface>();
var map1 = new Dictionary<string, List<Typeface>>();
var map2 = new Dictionary<string, List<Typeface>>();
foreach (var f in EnumerateAllTypefaces()) {
map1.Add(f.FullName, f);
List<Typeface> set;
if (!map2.TryGetValue(f.FamilyName, out set)) {
map2.Add(f.FamilyName, set = new List<Typeface>());
List<Typeface> set1;
if (!map1.TryGetValue(f.FullName, out set1)) {
map1.Add(f.FullName, set1 = new List<Typeface>());
}
set.Add(f);
set1.Add(f);
List<Typeface> set2;
if (!map2.TryGetValue(f.FamilyName, out set2)) {
map2.Add(f.FamilyName, set2 = new List<Typeface>());
}
MapFullNameToTypeface = map1;
set2.Add(f);
}
MapFullNameToTypeface = map1.ToDictionary(i => i.Key, i => (IReadOnlyCollection<Typeface>)i.Value);
MapNameToTypefaces = map2.ToDictionary(i => i.Key, i => (IReadOnlyCollection<Typeface>)i.Value);
}
protected abstract IEnumerable<Typeface> EnumerateAllTypefaces();