Add system font matcher.
This commit is contained in:
8
Assets/Cryville/Common/Culture.meta
Normal file
8
Assets/Cryville/Common/Culture.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 520554ce9a8205b4b91e0ff2b8011673
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
37
Assets/Cryville/Common/Culture/ScriptUtils.cs
Normal file
37
Assets/Cryville/Common/Culture/ScriptUtils.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Cryville.Common.Culture {
|
||||
public static class ScriptUtils {
|
||||
public static string[] Scripts = new string[] { "adlm", "afak", "aghb", "ahom", "arab", "aran", "armi", "armn", "avst", "bali", "bamu", "bass", "batk", "beng", "bhks", "blis", "bopo", "brah", "brai", "bugi", "buhd", "cakm", "cans", "cari", "cham", "cher", "chrs", "cirt", "copt", "cpmn", "cprt", "cyrl", "cyrs", "deva", "diak", "dogr", "dsrt", "dupl", "egyd", "egyh", "egyp", "elba", "elym", "ethi", "geok", "geor", "glag", "gong", "gonm", "goth", "gran", "grek", "gujr", "guru", "hanb", "hang", "hani", "hano", "hans", "hant", "hatr", "hebr", "hira", "hluw", "hmng", "hmnp", "hrkt", "hung", "inds", "ital", "jamo", "java", "jpan", "jurc", "kali", "kana", "khar", "khmr", "khoj", "kitl", "kits", "knda", "kore", "kpel", "kthi", "lana", "laoo", "latf", "latg", "latn", "leke", "lepc", "limb", "lina", "linb", "lisu", "loma", "lyci", "lydi", "mahj", "maka", "mand", "mani", "marc", "maya", "medf", "mend", "merc", "mero", "mlym", "modi", "mong", "moon", "mroo", "mtei", "mult", "mymr", "nand", "narb", "nbat", "newa", "nkdb", "nkgb", "nkoo", "nshu", "ogam", "olck", "orkh", "orya", "osge", "osma", "ougr", "palm", "pauc", "pcun", "pelm", "perm", "phag", "phli", "phlp", "phlv", "phnx", "piqd", "plrd", "prti", "psin", "qaaa", "qaai", "qabx", "ranj", "rjng", "rohg", "roro", "runr", "samr", "sara", "sarb", "saur", "sgnw", "shaw", "shrd", "shui", "sidd", "sind", "sinh", "sogd", "sogo", "sora", "soyo", "sund", "sylo", "syrc", "syre", "syrj", "syrn", "tagb", "takr", "tale", "talu", "taml", "tang", "tavt", "telu", "teng", "tfng", "tglg", "thaa", "thai", "tibt", "tirh", "toto", "ugar", "vaii", "visp", "wara", "wcho", "wole", "xpeo", "xsux", "yezi", "yiii", "zanb", "zinh", "zmth", "zsye", "zsym", "zxxx", "zyyy", "zzzz", };
|
||||
public static string UltimateFallbackScript = "zyyy";
|
||||
public static Dictionary<string, string[]> FallbackScriptMap = new Dictionary<string, string[]> {
|
||||
{ "aran", new string[] { "arab" } }, { "cyrs", new string[] { "cyrl" } },
|
||||
{ "hanb", new string[] { "hant", "bopo" } }, { "hans", new string[] { "hani" } }, { "hant", new string[] { "hani" } },
|
||||
{ "hrkt", new string[] { "hira", "kana" } }, { "jpan", new string[] { "hani", "hira", "kana" } },
|
||||
{ "jamo", new string[] { "hang" } }, { "kore", new string[] { "hang", "hani" } },
|
||||
{ "latf", new string[] { "latn" } }, { "latg", new string[] { "latn" } },
|
||||
{ "syre", new string[] { "syrc" } }, { "syrj", new string[] { "syrc" } }, { "syrn", new string[] { "syrc" } },
|
||||
{ "zsye", new string[] { "zsym" } },
|
||||
};
|
||||
public static void FillKeysWithScripts(IDictionary dict, Func<object> value) {
|
||||
foreach (var s in Scripts) dict.Add(s, value());
|
||||
}
|
||||
public static IEnumerable<string> EnumerateFallbackScripts(string script) {
|
||||
if (string.IsNullOrEmpty(script)) throw new ArgumentNullException("script");
|
||||
script = script.ToLower();
|
||||
if (script == UltimateFallbackScript) {
|
||||
yield return null;
|
||||
yield break;
|
||||
}
|
||||
string[] fblist;
|
||||
if (FallbackScriptMap.TryGetValue(script, out fblist)) {
|
||||
foreach (var fb in fblist) {
|
||||
yield return fb;
|
||||
}
|
||||
}
|
||||
else yield return UltimateFallbackScript;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Common/Culture/ScriptUtils.cs.meta
Normal file
11
Assets/Cryville/Common/Culture/ScriptUtils.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae9dab8f520fadc4194032f523ca87c1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
114
Assets/Cryville/Common/Font/FontFile.cs
Normal file
114
Assets/Cryville/Common/Font/FontFile.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using Cryville.Common.IO;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Cryville.Common.Font {
|
||||
public abstract class FontFile : IEnumerable<Typeface> {
|
||||
public abstract int Count { get; }
|
||||
public abstract Typeface this[int index] { get; }
|
||||
protected FileInfo File { get; private set; }
|
||||
protected BinaryReader Reader { get; private set; }
|
||||
public FontFile(FileInfo file) {
|
||||
File = file;
|
||||
Reader = new BinaryReaderBE(new FileStream(file.FullName, FileMode.Open, FileAccess.Read));
|
||||
}
|
||||
public void Close() { Reader.Close(); }
|
||||
|
||||
public static FontFile Create(FileInfo file) {
|
||||
switch (file.Extension) {
|
||||
case ".ttf": case ".otf": return new FontFileTTF(file);
|
||||
case ".ttc": case ".otc": return new FontFileTTC(file);
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Enumerator GetEnumerator() {
|
||||
return new Enumerator(this);
|
||||
}
|
||||
IEnumerator<Typeface> IEnumerable<Typeface>.GetEnumerator() {
|
||||
return GetEnumerator();
|
||||
}
|
||||
IEnumerator IEnumerable.GetEnumerator() {
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public struct Enumerator : IEnumerator<Typeface> {
|
||||
readonly FontFile _self;
|
||||
int _index;
|
||||
internal Enumerator(FontFile self) {
|
||||
_self = self;
|
||||
_index = -1;
|
||||
}
|
||||
|
||||
public Typeface Current {
|
||||
get {
|
||||
if (_index < 0)
|
||||
throw new InvalidOperationException(_index == -1 ? "Enum not started" : "Enum ended");
|
||||
return _self[_index];
|
||||
}
|
||||
}
|
||||
|
||||
object IEnumerator.Current { get { return Current; } }
|
||||
|
||||
public void Dispose() {
|
||||
_index = -2;
|
||||
}
|
||||
|
||||
public bool MoveNext() {
|
||||
if (_index == -2) return false;
|
||||
_index++;
|
||||
if (_index >= _self.Count) {
|
||||
_index = -2;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset() {
|
||||
_index = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
public class FontFileTTF : FontFile {
|
||||
public override int Count { get { return 1; } }
|
||||
public override Typeface this[int index] {
|
||||
get {
|
||||
if (index != 0) throw new ArgumentOutOfRangeException("index");
|
||||
try {
|
||||
return new TypefaceTTF(Reader, File, index);
|
||||
}
|
||||
catch (Exception) {
|
||||
throw new InvalidDataException("Invalid font");
|
||||
}
|
||||
}
|
||||
}
|
||||
public FontFileTTF(FileInfo file) : base(file) { }
|
||||
}
|
||||
public class FontFileTTC : FontFile {
|
||||
readonly IReadOnlyList<uint> _offsets;
|
||||
public override int Count { get { return _offsets.Count; } }
|
||||
public override Typeface this[int index] {
|
||||
get {
|
||||
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException("index");
|
||||
Reader.BaseStream.Position = _offsets[index];
|
||||
try {
|
||||
return new TypefaceTTF(Reader, File, index);
|
||||
}
|
||||
catch (Exception) {
|
||||
throw new InvalidDataException("Invalid font");
|
||||
}
|
||||
}
|
||||
}
|
||||
public FontFileTTC(FileInfo file) : base(file) {
|
||||
try {
|
||||
_offsets = new TTCHeader(Reader, 0).GetItems();
|
||||
}
|
||||
catch (Exception) {
|
||||
throw new InvalidDataException("Invalid font");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Common/Font/FontFile.cs.meta
Normal file
11
Assets/Cryville/Common/Font/FontFile.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c9f44ccf8ddd364418b4f4965414ff9c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
248
Assets/Cryville/Common/Font/FontManager.cs
Normal file
248
Assets/Cryville/Common/Font/FontManager.cs
Normal file
@@ -0,0 +1,248 @@
|
||||
using Cryville.Common.Culture;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
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 FontManager() {
|
||||
var map1 = new Dictionary<string, 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>());
|
||||
}
|
||||
set.Add(f);
|
||||
}
|
||||
MapFullNameToTypeface = map1;
|
||||
MapNameToTypefaces = map2.ToDictionary(i => i.Key, i => (IReadOnlyCollection<Typeface>)i.Value);
|
||||
}
|
||||
protected abstract IEnumerable<Typeface> EnumerateAllTypefaces();
|
||||
public abstract IEnumerable<Typeface> MatchScript(string script = null);
|
||||
public abstract IEnumerable<Typeface> MatchFamily(string[] candidates);
|
||||
protected static IEnumerable<Typeface> ScanDirectoryForTypefaces(string dir) {
|
||||
foreach (var f in new DirectoryInfo(dir).EnumerateFiles()) {
|
||||
FontFile file;
|
||||
try {
|
||||
file = FontFile.Create(f);
|
||||
}
|
||||
catch (InvalidDataException) {
|
||||
continue;
|
||||
}
|
||||
if (file == null) continue;
|
||||
var enumerator = file.GetEnumerator();
|
||||
while (enumerator.MoveNext()) {
|
||||
Typeface ret;
|
||||
try {
|
||||
ret = enumerator.Current;
|
||||
}
|
||||
catch (InvalidDataException) {
|
||||
continue;
|
||||
}
|
||||
yield return ret;
|
||||
}
|
||||
file.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
public class FontManagerAndroid : FontManager {
|
||||
protected override IEnumerable<Typeface> EnumerateAllTypefaces() {
|
||||
return ScanDirectoryForTypefaces("/system/fonts");
|
||||
}
|
||||
|
||||
public override IEnumerable<Typeface> MatchScript(string script = null) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override IEnumerable<Typeface> MatchFamily(string[] candidates) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
public class FontManagerWindows : FontManager {
|
||||
public static Dictionary<string, List<string>> MapScriptToTypefaces = new Dictionary<string, List<string>>();
|
||||
|
||||
static FontManagerWindows() {
|
||||
if (Environment.OSVersion.Platform != PlatformID.Win32NT) return;
|
||||
ScriptUtils.FillKeysWithScripts(MapScriptToTypefaces, () => new List<string>());
|
||||
// Reference: https://github.com/chromium/chromium/blob/main/third_party/blink/renderer/platform/fonts/win/font_fallback_win.cc
|
||||
MapScriptToTypefaces["zyyy"].Insert(0, "SimSun"); // Custom
|
||||
MapScriptToTypefaces["zyyy"].Insert(0, "SimHei"); // Custom
|
||||
MapScriptToTypefaces["zyyy"].Insert(0, "Arial");
|
||||
MapScriptToTypefaces["zyyy"].Insert(0, "Times New Roman");
|
||||
MapScriptToTypefaces["zyyy"].Insert(0, "Segoe UI"); // Custom
|
||||
MapScriptToTypefaces["arab"].Insert(0, "Tahoma");
|
||||
MapScriptToTypefaces["cyrl"].Insert(0, "Times New Roman");
|
||||
MapScriptToTypefaces["grek"].Insert(0, "Times New Roman");
|
||||
MapScriptToTypefaces["hebr"].Insert(0, "David");
|
||||
MapScriptToTypefaces["jpan"].Insert(0, "MS PGothic");
|
||||
MapScriptToTypefaces["latn"].Insert(0, "Times New Roman");
|
||||
MapScriptToTypefaces["hans"].Insert(0, "SimSun");
|
||||
MapScriptToTypefaces["hans"].Insert(0, "SimHei"); // Custom
|
||||
MapScriptToTypefaces["thai"].Insert(0, "Tahoma");
|
||||
MapScriptToTypefaces["hans"].Insert(0, "PMingLiU");
|
||||
// Reference: https://learn.microsoft.com/en-us/globalization/input/font-support
|
||||
var ver = Environment.OSVersion.Version;
|
||||
if (ver >= new Version(5, 0)) { // Windows 2000
|
||||
MapScriptToTypefaces["armn"].Insert(0, "Sylfaen");
|
||||
MapScriptToTypefaces["deva"].Insert(0, "Mangal");
|
||||
MapScriptToTypefaces["geor"].Insert(0, "Sylfaen");
|
||||
MapScriptToTypefaces["taml"].Insert(0, "Latha");
|
||||
}
|
||||
if (ver >= new Version(5, 1)) { // Windows XP
|
||||
MapScriptToTypefaces["gujr"].Insert(0, "Shruti");
|
||||
MapScriptToTypefaces["guru"].Insert(0, "Raavi");
|
||||
MapScriptToTypefaces["knda"].Insert(0, "Tunga");
|
||||
MapScriptToTypefaces["syrc"].Insert(0, "Estrangelo Edessa");
|
||||
MapScriptToTypefaces["telu"].Insert(0, "Gautami");
|
||||
MapScriptToTypefaces["thaa"].Insert(0, "MV Boli");
|
||||
// SP2
|
||||
MapScriptToTypefaces["beng"].Insert(0, "Vrinda");
|
||||
MapScriptToTypefaces["mlym"].Insert(0, "Kartika");
|
||||
}
|
||||
if (ver >= new Version(6, 0)) { // Windows Vista
|
||||
MapScriptToTypefaces["cans"].Insert(0, "Euphemia");
|
||||
MapScriptToTypefaces["cher"].Insert(0, "Plantagenet");
|
||||
MapScriptToTypefaces["ethi"].Insert(0, "Nyala");
|
||||
MapScriptToTypefaces["khmr"].Insert(0, "DaunPenh MoolBoran");
|
||||
MapScriptToTypefaces["laoo"].Insert(0, "DokChampa");
|
||||
MapScriptToTypefaces["mong"].Insert(0, "Mongolian Baiti");
|
||||
MapScriptToTypefaces["orya"].Insert(0, "Kalinga");
|
||||
MapScriptToTypefaces["sinh"].Insert(0, "Iskoola Pota");
|
||||
MapScriptToTypefaces["tibt"].Insert(0, "Microsoft Himalaya");
|
||||
MapScriptToTypefaces["yiii"].Insert(0, "Microsoft Yi Baiti");
|
||||
MapScriptToTypefaces["arab"].Insert(0, "Segoe UI");
|
||||
MapScriptToTypefaces["cyrl"].Insert(0, "Segoe UI");
|
||||
MapScriptToTypefaces["grek"].Insert(0, "Segoe UI");
|
||||
MapScriptToTypefaces["latn"].Insert(0, "Segoe UI");
|
||||
MapScriptToTypefaces["hans"].Add("SimSun-ExtB");
|
||||
MapScriptToTypefaces["hant"].Add("MingLiU-ExtB");
|
||||
MapScriptToTypefaces["hant"].Add("MingLiU_HKSCS-ExtB");
|
||||
MapScriptToTypefaces["arab"].Add("Microsoft Uighur");
|
||||
MapScriptToTypefaces["zmth"].Insert(0, "Cambria Math");
|
||||
// Reference: https://en.wikipedia.org/wiki/List_of_CJK_fonts
|
||||
MapScriptToTypefaces["jpan"].Insert(0, "Meiryo");
|
||||
MapScriptToTypefaces["hans"].Insert(0, "Microsoft YaHei");
|
||||
}
|
||||
if (ver >= new Version(6, 1)) { // Windows 7
|
||||
MapScriptToTypefaces["brai"].Insert(0, "Segoe UI Symbol");
|
||||
MapScriptToTypefaces["dsrt"].Insert(0, "Segoe UI Symbol");
|
||||
MapScriptToTypefaces["talu"].Insert(0, "Microsoft New Tai Lue");
|
||||
MapScriptToTypefaces["ogam"].Insert(0, "Segoe UI Symbol");
|
||||
MapScriptToTypefaces["osma"].Insert(0, "Ebrima");
|
||||
MapScriptToTypefaces["phag"].Insert(0, "Microsoft PhagsPa");
|
||||
MapScriptToTypefaces["runr"].Insert(0, "Segoe UI Symbol");
|
||||
MapScriptToTypefaces["zsym"].Insert(0, "Segoe UI Symbol");
|
||||
MapScriptToTypefaces["tale"].Insert(0, "Microsoft Tai Le");
|
||||
MapScriptToTypefaces["tfng"].Insert(0, "Ebrima");
|
||||
MapScriptToTypefaces["vaii"].Insert(0, "Ebrima");
|
||||
}
|
||||
if (ver >= new Version(6, 2)) { // Windows 8
|
||||
MapScriptToTypefaces["glag"].Insert(0, "Segoe UI Symbol");
|
||||
MapScriptToTypefaces["goth"].Insert(0, "Segoe UI Symbol");
|
||||
MapScriptToTypefaces["hang"].Add("Malgun Gothic");
|
||||
MapScriptToTypefaces["ital"].Insert(0, "Segoe UI Symbol");
|
||||
MapScriptToTypefaces["lisu"].Insert(0, "Segoe UI");
|
||||
MapScriptToTypefaces["mymr"].Insert(0, "Myanmar Text");
|
||||
MapScriptToTypefaces["nkoo"].Insert(0, "Ebrima");
|
||||
MapScriptToTypefaces["orkh"].Insert(0, "Segoe UI Symbol");
|
||||
MapScriptToTypefaces["ethi"].Insert(0, "Ebrima");
|
||||
MapScriptToTypefaces["cans"].Insert(0, "Gadugi");
|
||||
MapScriptToTypefaces["hant"].Insert(0, "Microsoft JhengHei UI");
|
||||
MapScriptToTypefaces["hans"].Insert(0, "Microsoft YaHei UI");
|
||||
MapScriptToTypefaces["beng"].Insert(0, "Nirmala UI");
|
||||
MapScriptToTypefaces["deva"].Insert(0, "Nirmala UI");
|
||||
MapScriptToTypefaces["gujr"].Insert(0, "Nirmala UI");
|
||||
MapScriptToTypefaces["guru"].Insert(0, "Nirmala UI"); // NOT DOCUMENTED, UNVERIFIED
|
||||
MapScriptToTypefaces["knda"].Insert(0, "Nirmala UI"); // NOT DOCUMENTED, UNVERIFIED
|
||||
MapScriptToTypefaces["mlym"].Insert(0, "Nirmala UI");
|
||||
MapScriptToTypefaces["orya"].Insert(0, "Nirmala UI");
|
||||
MapScriptToTypefaces["sinh"].Insert(0, "Nirmala UI"); // NOT DOCUMENTED, UNVERIFIED
|
||||
MapScriptToTypefaces["taml"].Insert(0, "Nirmala UI"); // NOT DOCUMENTED, UNVERIFIED
|
||||
MapScriptToTypefaces["telu"].Insert(0, "Nirmala UI");
|
||||
MapScriptToTypefaces["armn"].Insert(0, "Segoe UI");
|
||||
MapScriptToTypefaces["geor"].Insert(0, "Segoe UI");
|
||||
MapScriptToTypefaces["hebr"].Insert(0, "Segoe UI");
|
||||
}
|
||||
if (ver >= new Version(6, 3)) { // Windows 8.1
|
||||
MapScriptToTypefaces["bugi"].Insert(0, "Leelawadee UI");
|
||||
MapScriptToTypefaces["copt"].Insert(0, "Segoe UI Symbol");
|
||||
MapScriptToTypefaces["java"].Insert(0, "Javanese Text");
|
||||
MapScriptToTypefaces["merc"].Insert(0, "Segoe UI Symbol");
|
||||
MapScriptToTypefaces["olck"].Insert(0, "Nirmala UI");
|
||||
MapScriptToTypefaces["sora"].Insert(0, "Nirmala UI");
|
||||
MapScriptToTypefaces["khmr"].Insert(0, "Leelawadee UI");
|
||||
MapScriptToTypefaces["laoo"].Insert(0, "Leelawadee UI");
|
||||
MapScriptToTypefaces["thai"].Insert(0, "Leelawadee UI");
|
||||
MapScriptToTypefaces["zsye"].Insert(0, "Segoe UI Emoji");
|
||||
}
|
||||
if (ver >= new Version(10, 0)) { // Windows 10
|
||||
MapScriptToTypefaces["brah"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["cari"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["cprt"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["egyp"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["armi"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["phli"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["prti"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["khar"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["lyci"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["lydi"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["phnx"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["xpeo"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["sarb"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["shaw"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["xsux"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["ugar"].Insert(0, "Segoe UI Historic");
|
||||
// Segoe UI Symbol -> Segoe UI Historic
|
||||
MapScriptToTypefaces["glag"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["goth"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["merc"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["ogam"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["ital"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["orkh"].Insert(0, "Segoe UI Historic");
|
||||
MapScriptToTypefaces["runr"].Insert(0, "Segoe UI Historic");
|
||||
//
|
||||
MapScriptToTypefaces["jpan"].Insert(0, "Yu Gothic UI");
|
||||
MapScriptToTypefaces["zsym"].Add("Segoe MDL2 Assets");
|
||||
}
|
||||
}
|
||||
|
||||
protected override IEnumerable<Typeface> EnumerateAllTypefaces() {
|
||||
return ScanDirectoryForTypefaces("C:/Windows/Fonts");
|
||||
}
|
||||
|
||||
public override IEnumerable<Typeface> MatchScript(string script = null) {
|
||||
if (string.IsNullOrEmpty(script)) script = ScriptUtils.UltimateFallbackScript;
|
||||
List<string> candidates;
|
||||
IEnumerable<string> candidateScripts = new string[] { script };
|
||||
while (candidateScripts != null) {
|
||||
foreach (var candidateScript in candidateScripts) {
|
||||
if (MapScriptToTypefaces.TryGetValue(candidateScript, out candidates)) {
|
||||
foreach (var candidate in candidates) {
|
||||
Typeface typeface;
|
||||
if (MapFullNameToTypeface.TryGetValue(candidate, out typeface)) {
|
||||
yield return typeface;
|
||||
}
|
||||
IReadOnlyCollection<Typeface> typefaces;
|
||||
if (MapNameToTypefaces.TryGetValue(candidate, out typefaces)) {
|
||||
foreach (var typeface2 in typefaces) {
|
||||
if (typeface2 == typeface) continue;
|
||||
yield return typeface2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
candidateScripts = ScriptUtils.EnumerateFallbackScripts(script);
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<Typeface> MatchFamily(string[] candidates) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
189
Assets/Cryville/Common/Font/FontTable.cs
Normal file
189
Assets/Cryville/Common/Font/FontTable.cs
Normal file
@@ -0,0 +1,189 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Cryville.Common.Font {
|
||||
public abstract class FontTable<T> {
|
||||
protected UInt32 Offset { get; private set; }
|
||||
protected BinaryReader Reader { get; private set; }
|
||||
|
||||
protected FontTable(BinaryReader reader, UInt32 offset) {
|
||||
Reader = reader;
|
||||
Offset = offset;
|
||||
reader.BaseStream.Position = offset;
|
||||
}
|
||||
public abstract IReadOnlyList<T> GetItems();
|
||||
}
|
||||
public abstract class FontTable<T, U> : FontTable<T> {
|
||||
protected FontTable(BinaryReader reader, UInt32 offset) : base(reader, offset) { }
|
||||
public abstract U GetSubTable(T item);
|
||||
}
|
||||
public sealed class TTCHeader : FontTable<UInt32, TableDirectory> {
|
||||
readonly String ttcTag;
|
||||
readonly UInt16 majorVersion;
|
||||
readonly UInt16 minorVersion;
|
||||
readonly UInt32 numFonts;
|
||||
readonly List<UInt32> tableDirectoryOffsets = new List<UInt32>();
|
||||
readonly String dsigTag;
|
||||
readonly UInt32 dsigLength;
|
||||
readonly UInt32 dsigOffset;
|
||||
public TTCHeader(BinaryReader reader, UInt32 offset) : base(reader, offset) {
|
||||
ttcTag = reader.ReadTag();
|
||||
if (ttcTag != "ttcf") throw new NotImplementedException();
|
||||
majorVersion = reader.ReadUInt16();
|
||||
minorVersion = reader.ReadUInt16();
|
||||
numFonts = reader.ReadUInt32();
|
||||
for (UInt32 i = 0; i < numFonts; i++) tableDirectoryOffsets.Add(reader.ReadUInt32());
|
||||
if (majorVersion == 2) {
|
||||
dsigTag = reader.ReadTag();
|
||||
dsigLength = reader.ReadUInt32();
|
||||
dsigOffset = reader.ReadUInt32();
|
||||
}
|
||||
}
|
||||
public override IReadOnlyList<UInt32> GetItems() {
|
||||
return tableDirectoryOffsets;
|
||||
}
|
||||
public override TableDirectory GetSubTable(UInt32 item) {
|
||||
var i = (UInt32)item;
|
||||
return new TableDirectory(Reader, i);
|
||||
}
|
||||
}
|
||||
public sealed class TableDirectory : FontTable<TableRecord, object> {
|
||||
readonly UInt32 sfntVersion;
|
||||
readonly UInt16 numTables;
|
||||
readonly UInt16 searchRange;
|
||||
readonly UInt16 entrySelector;
|
||||
readonly UInt16 rangeShift;
|
||||
readonly List<TableRecord> tableRecords = new List<TableRecord>();
|
||||
public TableDirectory(BinaryReader reader, UInt32 offset) : base(reader, offset) {
|
||||
sfntVersion = reader.ReadUInt32();
|
||||
numTables = reader.ReadUInt16();
|
||||
searchRange = reader.ReadUInt16();
|
||||
entrySelector = reader.ReadUInt16();
|
||||
rangeShift = reader.ReadUInt16();
|
||||
for (int i = 0; i < numTables; i++)
|
||||
tableRecords.Add(new TableRecord {
|
||||
tableTag = reader.ReadTag(),
|
||||
checksum = reader.ReadUInt32(),
|
||||
offset = reader.ReadUInt32(),
|
||||
length = reader.ReadUInt32(),
|
||||
});
|
||||
}
|
||||
public override IReadOnlyList<TableRecord> GetItems() {
|
||||
return tableRecords;
|
||||
}
|
||||
public override object GetSubTable(TableRecord item) {
|
||||
switch (item.tableTag) {
|
||||
case "name": return new NameTable(Reader, item.offset);
|
||||
default: throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
public struct TableRecord {
|
||||
public string tableTag;
|
||||
public UInt32 checksum;
|
||||
public UInt32 offset;
|
||||
public UInt32 length;
|
||||
}
|
||||
public sealed class NameTable : FontTable<NameRecord> {
|
||||
readonly UInt16 version;
|
||||
readonly UInt16 count;
|
||||
readonly UInt16 storageOffset;
|
||||
readonly List<NameRecord> nameRecord = new List<NameRecord>();
|
||||
readonly UInt16 langTagCount;
|
||||
readonly List<LangTagRecord> langTagRecord = new List<LangTagRecord>();
|
||||
public NameTable(BinaryReader reader, UInt32 offset) : base(reader, offset) {
|
||||
version = reader.ReadUInt16();
|
||||
count = reader.ReadUInt16();
|
||||
storageOffset = reader.ReadUInt16();
|
||||
for (UInt16 i = 0; i < count; i++)
|
||||
nameRecord.Add(new NameRecord {
|
||||
platformID = reader.ReadUInt16(),
|
||||
encodingID = reader.ReadUInt16(),
|
||||
languageID = reader.ReadUInt16(),
|
||||
nameID = (NameID)reader.ReadUInt16(),
|
||||
length = reader.ReadUInt16(),
|
||||
stringOffset = reader.ReadUInt16(),
|
||||
});
|
||||
if (version == 1) {
|
||||
langTagCount = reader.ReadUInt16();
|
||||
for (UInt16 i = 0; i < langTagCount; i++)
|
||||
langTagRecord.Add(new LangTagRecord {
|
||||
length = reader.ReadUInt16(),
|
||||
langTagOffset = reader.ReadUInt16(),
|
||||
});
|
||||
}
|
||||
UInt32 origin = (UInt32)reader.BaseStream.Position;
|
||||
for (int i = 0; i < nameRecord.Count; i++) nameRecord[i] = nameRecord[i].Load(reader, origin);
|
||||
for (int i = 0; i < langTagRecord.Count; i++) langTagRecord[i] = langTagRecord[i].Load(reader, origin);
|
||||
}
|
||||
public sealed override IReadOnlyList<NameRecord> GetItems() {
|
||||
return nameRecord.Cast<NameRecord>().ToList();
|
||||
}
|
||||
}
|
||||
public struct NameRecord {
|
||||
public UInt16 platformID;
|
||||
public UInt16 encodingID;
|
||||
public UInt16 languageID;
|
||||
public NameID nameID;
|
||||
public UInt16 length;
|
||||
public UInt16 stringOffset;
|
||||
public String value { get; private set; }
|
||||
public NameRecord Load(BinaryReader reader, UInt32 origin) {
|
||||
reader.BaseStream.Position = origin + stringOffset;
|
||||
Encoding encoding;
|
||||
switch (platformID) {
|
||||
case 0: encoding = Encoding.BigEndianUnicode; break;
|
||||
case 3: encoding = Encoding.BigEndianUnicode; break;
|
||||
default: return this;
|
||||
}
|
||||
value = encoding.GetString(reader.ReadBytes(length));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public enum NameID : UInt16 {
|
||||
CopyrightNotice = 0,
|
||||
FontFamilyName = 1,
|
||||
FontSubfamilyName = 2,
|
||||
UniqueFontIdentifier = 3,
|
||||
FullFontName = 4,
|
||||
VersionString = 5,
|
||||
PostScriptName = 6,
|
||||
Trademark = 7,
|
||||
ManufacturerName = 8,
|
||||
Designer = 9,
|
||||
Description = 10,
|
||||
URLVendor = 11,
|
||||
URLDesigner = 12,
|
||||
LicenseDescription = 13,
|
||||
LicenseInfoURL = 14,
|
||||
|
||||
TypographicFamilyName = 16,
|
||||
TypographicSubfamilyName = 17,
|
||||
CompatibleFull = 18,
|
||||
SampleText = 19,
|
||||
PostScriptCIDFindfontName = 20,
|
||||
WWSFamilyName = 21,
|
||||
WWSSubfamilyName = 22,
|
||||
LightBackgroundPalette = 23,
|
||||
DarkBackgroundPalette = 24,
|
||||
VariationsPostScriptNamePrefix = 25,
|
||||
}
|
||||
public struct LangTagRecord {
|
||||
public UInt16 length;
|
||||
public UInt16 langTagOffset;
|
||||
public String value { get; private set; }
|
||||
public LangTagRecord Load(BinaryReader reader, UInt32 origin) {
|
||||
reader.BaseStream.Position = origin + langTagOffset;
|
||||
value = Encoding.BigEndianUnicode.GetString(reader.ReadBytes(length));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public static class BinaryReaderExtensions {
|
||||
public static string ReadTag(this BinaryReader reader) {
|
||||
return Encoding.ASCII.GetString(reader.ReadBytes(4));
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Common/Font/FontTable.cs.meta
Normal file
11
Assets/Cryville/Common/Font/FontTable.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3eed6aa2387582346b7b21c6f8de5e1f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -1,9 +0,0 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace Cryville.Common.Font {
|
||||
public static class FontUtil {
|
||||
/*public static string MatchFontNameWithLang(string lang) {
|
||||
|
||||
}*/
|
||||
}
|
||||
}
|
31
Assets/Cryville/Common/Font/Typeface.cs
Normal file
31
Assets/Cryville/Common/Font/Typeface.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace Cryville.Common.Font {
|
||||
public abstract class Typeface {
|
||||
public FileInfo File { get; private set; }
|
||||
public int IndexInFile { get; private set; }
|
||||
public string FamilyName { get; protected set; }
|
||||
public string SubfamilyName { get; protected set; }
|
||||
public string FullName { get; protected set; }
|
||||
protected abstract void GetName(BinaryReader reader);
|
||||
|
||||
public Typeface(BinaryReader reader, FileInfo file, int index) {
|
||||
File = file;
|
||||
IndexInFile = index;
|
||||
GetName(reader);
|
||||
}
|
||||
}
|
||||
public class TypefaceTTF : Typeface {
|
||||
public TypefaceTTF(BinaryReader reader, FileInfo file, int index)
|
||||
: base(reader, file, index) { }
|
||||
|
||||
protected override void GetName(BinaryReader reader) {
|
||||
var dir = new TableDirectory(reader, (uint)reader.BaseStream.Position);
|
||||
var nameTable = (NameTable)dir.GetSubTable((from i in dir.GetItems() where i.tableTag == "name" select i).Single());
|
||||
FamilyName = (from i in nameTable.GetItems() where i.nameID == NameID.FontFamilyName && i.value != null select i.value).First();
|
||||
SubfamilyName = (from i in nameTable.GetItems() where i.nameID == NameID.FontSubfamilyName && i.value != null select i.value).First();
|
||||
FullName = (from i in nameTable.GetItems() where i.nameID == NameID.FullFontName && i.value != null select i.value).First();
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Common/Font/Typeface.cs.meta
Normal file
11
Assets/Cryville/Common/Font/Typeface.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0968fc12b50cffb4682f0c28d0d14703
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
Assets/Cryville/Common/IO.meta
Normal file
8
Assets/Cryville/Common/IO.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aaa0d8cecafb37b46a6abe372cfefd93
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
75
Assets/Cryville/Common/IO/BinaryReaderBE.cs
Normal file
75
Assets/Cryville/Common/IO/BinaryReaderBE.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Cryville.Common.IO {
|
||||
public class BinaryReaderBE : BinaryReader {
|
||||
readonly byte[] m_buffer = new byte[8];
|
||||
|
||||
public BinaryReaderBE(Stream input) : base(input) { }
|
||||
|
||||
public BinaryReaderBE(Stream input, Encoding encoding) : base(input, encoding) { }
|
||||
|
||||
public BinaryReaderBE(Stream input, Encoding encoding, bool leaveOpen) : base(input, encoding, leaveOpen) { }
|
||||
|
||||
public override short ReadInt16() {
|
||||
FillBuffer(2);
|
||||
return (short)(m_buffer[1] | (m_buffer[0] << 8));
|
||||
}
|
||||
public override ushort ReadUInt16() {
|
||||
FillBuffer(2);
|
||||
return (ushort)(m_buffer[1] | (m_buffer[0] << 8));
|
||||
}
|
||||
|
||||
public override int ReadInt32() {
|
||||
FillBuffer(4);
|
||||
return m_buffer[3] | (m_buffer[2] << 8) | (m_buffer[1] << 16) | (m_buffer[0] << 24);
|
||||
}
|
||||
public override uint ReadUInt32() {
|
||||
FillBuffer(4);
|
||||
return (uint)(m_buffer[3] | (m_buffer[2] << 8) | (m_buffer[1] << 16) | (m_buffer[0] << 24));
|
||||
}
|
||||
|
||||
public override long ReadInt64() {
|
||||
FillBuffer(8);
|
||||
uint num = (uint)(m_buffer[7] | (m_buffer[6] << 8) | (m_buffer[5] << 16) | (m_buffer[4] << 24));
|
||||
uint num2 = (uint)(m_buffer[3] | (m_buffer[2] << 8) | (m_buffer[1] << 16) | (m_buffer[0] << 24));
|
||||
return (long)(((ulong)num2 << 32) | num);
|
||||
}
|
||||
public override ulong ReadUInt64() {
|
||||
FillBuffer(8);
|
||||
uint num = (uint)(m_buffer[7] | (m_buffer[6] << 8) | (m_buffer[5] << 16) | (m_buffer[4] << 24));
|
||||
uint num2 = (uint)(m_buffer[3] | (m_buffer[2] << 8) | (m_buffer[1] << 16) | (m_buffer[0] << 24));
|
||||
return ((ulong)num2 << 32) | num;
|
||||
}
|
||||
protected new void FillBuffer(int numBytes) {
|
||||
if (m_buffer != null && (numBytes < 0 || numBytes > m_buffer.Length)) {
|
||||
throw new ArgumentOutOfRangeException("numBytes", "Requested numBytes is larger than the internal buffer size");
|
||||
}
|
||||
|
||||
int num = 0, num2;
|
||||
if (BaseStream == null) {
|
||||
throw new IOException("File not open");
|
||||
}
|
||||
|
||||
if (numBytes == 1) {
|
||||
num2 = BaseStream.ReadByte();
|
||||
if (num2 == -1) {
|
||||
throw new EndOfStreamException("The end of the stream is reached before numBytes could be read");
|
||||
}
|
||||
m_buffer[0] = (byte)num2;
|
||||
return;
|
||||
}
|
||||
|
||||
do {
|
||||
num2 = BaseStream.Read(m_buffer, num, numBytes - num);
|
||||
if (num2 == 0) {
|
||||
throw new EndOfStreamException("The end of the stream is reached before numBytes could be read");
|
||||
}
|
||||
|
||||
num += num2;
|
||||
}
|
||||
while (num < numBytes);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Common/IO/BinaryReaderBE.cs.meta
Normal file
11
Assets/Cryville/Common/IO/BinaryReaderBE.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aee537c74ab935940b54cb5d784b7f56
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -1,6 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using Cryville.Common.Font;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TextCore.LowLevel;
|
||||
using UnityEngine.TextCore.Text;
|
||||
using IFont = UnityEngine.Font;
|
||||
|
||||
@@ -8,23 +11,29 @@ namespace Cryville.Common.Unity.UI {
|
||||
[RequireComponent(typeof(TextMeshProUGUI))]
|
||||
public class TMPAutoFont : MonoBehaviour {
|
||||
public static Shader DefaultShader;
|
||||
public static readonly List<string> Fonts = new List<string> {
|
||||
"Arial",
|
||||
};
|
||||
public static FontManager FontManager;
|
||||
static FontAsset _font;
|
||||
TextMeshProUGUI _text;
|
||||
|
||||
[SerializeField]
|
||||
Shader m_shader;
|
||||
void Awake() {
|
||||
if (FontManager == null) return;
|
||||
_text = GetComponent<TextMeshProUGUI>();
|
||||
if (_font == null) {
|
||||
var _ifont = new IFont("C:/Windows/Fonts/arial.ttf");
|
||||
_font = FontAsset.CreateFontAsset(_ifont);
|
||||
if (m_shader) _font.material.shader = m_shader;
|
||||
else if (DefaultShader) _font.material.shader = DefaultShader;
|
||||
foreach (var typeface in FontManager.MatchScript()) {
|
||||
try {
|
||||
var _ifont = new IFont(typeface.File.FullName);
|
||||
_font = (FontAsset)typeof(FontAsset).GetMethod("CreateFontAsset", BindingFlags.Static | BindingFlags.NonPublic, null, new Type[] { typeof(IFont), typeof(int), typeof(int), typeof(int), typeof(GlyphRenderMode), typeof(int), typeof(int), typeof(AtlasPopulationMode), typeof(bool) },
|
||||
null).Invoke(null, new object[] { _ifont, typeface.IndexInFile, 90, 9, GlyphRenderMode.SDFAA, 1024, 1024, Type.Missing, Type.Missing });
|
||||
break;
|
||||
}
|
||||
catch (Exception) { }
|
||||
}
|
||||
}
|
||||
_text.font = _font;
|
||||
if (m_shader) _font.material.shader = m_shader;
|
||||
else if (DefaultShader) _font.material.shader = DefaultShader;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using Cryville.Audio;
|
||||
using Cryville.Audio.Source;
|
||||
using Cryville.Common;
|
||||
using Cryville.Common.Font;
|
||||
using Cryville.Common.Unity;
|
||||
using Cryville.Common.Unity.Input;
|
||||
using Cryville.Common.Unity.UI;
|
||||
@@ -128,7 +129,16 @@ namespace Cryville.Crtr {
|
||||
Settings.Default.LastRunVersion = Application.version;
|
||||
Settings.Default.Save();
|
||||
|
||||
Logger.Log("main", 1, "UI", "Initializing font manager");
|
||||
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
|
||||
TMPAutoFont.FontManager = new FontManagerWindows();
|
||||
TMPAutoFont.DefaultShader = Resources.Load<Shader>("TextMesh Pro/Shaders/TMP_SDF SSD");
|
||||
#elif UNITY_ANDROID
|
||||
TMPAutoFont.FontManager = new FontManagerAndroid();
|
||||
TMPAutoFont.DefaultShader = Resources.Load<Shader>("TextMesh Pro/Shaders/TMP_SDF-Mobile SSD");
|
||||
#else
|
||||
#error No font manager initialization logic.
|
||||
#endif
|
||||
|
||||
Logger.Log("main", 1, "Game", "Initialized");
|
||||
}
|
||||
|
Reference in New Issue
Block a user