Forces one font instance per full name.
This commit is contained in:
@@ -1,27 +1,30 @@
|
|||||||
|
using Cryville.Common.Logging;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace Cryville.Common.Font {
|
namespace Cryville.Common.Font {
|
||||||
public abstract class FontManager {
|
public abstract class FontManager {
|
||||||
public IReadOnlyDictionary<string, IReadOnlyCollection<Typeface>> MapFullNameToTypeface { get; private set; }
|
public IReadOnlyDictionary<string, Typeface> MapFullNameToTypeface { get; private set; }
|
||||||
public IReadOnlyDictionary<string, IReadOnlyCollection<Typeface>> MapNameToTypefaces { get; private set; }
|
public IReadOnlyDictionary<string, IReadOnlyCollection<Typeface>> MapNameToTypefaces { get; private set; }
|
||||||
public FontManager() {
|
public FontManager() {
|
||||||
var map1 = new Dictionary<string, List<Typeface>>();
|
var map1 = new Dictionary<string, Typeface>();
|
||||||
var map2 = new Dictionary<string, List<Typeface>>();
|
var map2 = new Dictionary<string, List<Typeface>>();
|
||||||
foreach (var f in EnumerateAllTypefaces()) {
|
foreach (var f in EnumerateAllTypefaces()) {
|
||||||
List<Typeface> set1;
|
if (!map1.ContainsKey(f.FullName)) {
|
||||||
if (!map1.TryGetValue(f.FullName, out set1)) {
|
map1.Add(f.FullName, f);
|
||||||
map1.Add(f.FullName, set1 = new List<Typeface>());
|
}
|
||||||
|
else {
|
||||||
|
Logger.Log("main", 3, "UI", "Discarding a font with a duplicate full name {0}", f.FullName);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
set1.Add(f);
|
|
||||||
List<Typeface> set2;
|
List<Typeface> set2;
|
||||||
if (!map2.TryGetValue(f.FamilyName, out set2)) {
|
if (!map2.TryGetValue(f.FamilyName, out set2)) {
|
||||||
map2.Add(f.FamilyName, set2 = new List<Typeface>());
|
map2.Add(f.FamilyName, set2 = new List<Typeface>());
|
||||||
}
|
}
|
||||||
set2.Add(f);
|
set2.Add(f);
|
||||||
}
|
}
|
||||||
MapFullNameToTypeface = map1.ToDictionary(i => i.Key, i => (IReadOnlyCollection<Typeface>)i.Value);
|
MapFullNameToTypeface = map1;
|
||||||
MapNameToTypefaces = map2.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();
|
protected abstract IEnumerable<Typeface> EnumerateAllTypefaces();
|
||||||
|
|||||||
@@ -324,17 +324,14 @@ namespace Cryville.Common.Font {
|
|||||||
}
|
}
|
||||||
IEnumerable<Typeface> EnumerateTypefaces(List<string> candidates, bool distinctFamily) {
|
IEnumerable<Typeface> EnumerateTypefaces(List<string> candidates, bool distinctFamily) {
|
||||||
foreach (var candidate in candidates) {
|
foreach (var candidate in candidates) {
|
||||||
IReadOnlyCollection<Typeface> typefaces1;
|
if (Manager.MapFullNameToTypeface.TryGetValue(candidate, out var typeface1)) {
|
||||||
if (Manager.MapFullNameToTypeface.TryGetValue(candidate, out typefaces1)) {
|
yield return typeface1;
|
||||||
foreach (var typeface in typefaces1) {
|
|
||||||
yield return typeface;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (distinctFamily) continue;
|
if (distinctFamily) continue;
|
||||||
IReadOnlyCollection<Typeface> typefaces2;
|
IReadOnlyCollection<Typeface> typefaces2;
|
||||||
if (Manager.MapNameToTypefaces.TryGetValue(candidate, out typefaces2)) {
|
if (Manager.MapNameToTypefaces.TryGetValue(candidate, out typefaces2)) {
|
||||||
foreach (var typeface in typefaces2) {
|
foreach (var typeface in typefaces2) {
|
||||||
if (typefaces1.Contains(typeface)) continue;
|
if (typeface1 == typeface) continue;
|
||||||
yield return typeface;
|
yield return typeface;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user