123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- using System.Collections.Generic;
- using UnityEngine.TextCore;
- using UnityEngine.TextCore.LowLevel;
- namespace TMPro
- {
- public class TMP_FontAssetUtilities
- {
- private static readonly TMP_FontAssetUtilities s_Instance = new TMP_FontAssetUtilities();
-
-
-
- static TMP_FontAssetUtilities() { }
-
-
-
- public static TMP_FontAssetUtilities instance
- {
- get { return s_Instance; }
- }
-
-
-
- private static HashSet<int> k_SearchedAssets;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static TMP_Character GetCharacterFromFontAsset(uint unicode, TMP_FontAsset sourceFontAsset, bool includeFallbacks, FontStyles fontStyle, FontWeight fontWeight, out bool isAlternativeTypeface)
- {
- if (includeFallbacks)
- {
- if (k_SearchedAssets == null)
- k_SearchedAssets = new HashSet<int>();
- else
- k_SearchedAssets.Clear();
- }
- return GetCharacterFromFontAsset_Internal(unicode, sourceFontAsset, includeFallbacks, fontStyle, fontWeight, out isAlternativeTypeface);
- }
-
-
-
-
- private static TMP_Character GetCharacterFromFontAsset_Internal(uint unicode, TMP_FontAsset sourceFontAsset, bool includeFallbacks, FontStyles fontStyle, FontWeight fontWeight, out bool isAlternativeTypeface)
- {
- isAlternativeTypeface = false;
- TMP_Character character = null;
- #region FONT WEIGHT AND FONT STYLE HANDLING
-
- bool isItalic = (fontStyle & FontStyles.Italic) == FontStyles.Italic;
- if (isItalic || fontWeight != FontWeight.Regular)
- {
-
- TMP_FontWeightPair[] fontWeights = sourceFontAsset.fontWeightTable;
- int fontWeightIndex = 4;
- switch (fontWeight)
- {
- case FontWeight.Thin:
- fontWeightIndex = 1;
- break;
- case FontWeight.ExtraLight:
- fontWeightIndex = 2;
- break;
- case FontWeight.Light:
- fontWeightIndex = 3;
- break;
- case FontWeight.Regular:
- fontWeightIndex = 4;
- break;
- case FontWeight.Medium:
- fontWeightIndex = 5;
- break;
- case FontWeight.SemiBold:
- fontWeightIndex = 6;
- break;
- case FontWeight.Bold:
- fontWeightIndex = 7;
- break;
- case FontWeight.Heavy:
- fontWeightIndex = 8;
- break;
- case FontWeight.Black:
- fontWeightIndex = 9;
- break;
- }
- TMP_FontAsset temp = isItalic ? fontWeights[fontWeightIndex].italicTypeface : fontWeights[fontWeightIndex].regularTypeface;
- if (temp != null)
- {
- if (temp.characterLookupTable.TryGetValue(unicode, out character))
- {
- isAlternativeTypeface = true;
- return character;
- }
- if (temp.atlasPopulationMode == AtlasPopulationMode.Dynamic)
- {
- if (temp.TryAddCharacterInternal(unicode, out character))
- {
- isAlternativeTypeface = true;
- return character;
- }
-
-
-
-
-
-
-
-
-
-
-
- }
-
-
- }
- }
- #endregion
-
- if (sourceFontAsset.characterLookupTable.TryGetValue(unicode, out character))
- return character;
- if (sourceFontAsset.atlasPopulationMode == AtlasPopulationMode.Dynamic)
- {
- if (sourceFontAsset.TryAddCharacterInternal(unicode, out character))
- return character;
- }
-
- if (character == null && includeFallbacks && sourceFontAsset.fallbackFontAssetTable != null)
- {
-
- List<TMP_FontAsset> fallbackFontAssets = sourceFontAsset.fallbackFontAssetTable;
- int fallbackCount = fallbackFontAssets.Count;
- if (fallbackFontAssets != null && fallbackCount > 0)
- {
- for (int i = 0; i < fallbackCount; i++)
- {
- TMP_FontAsset temp = fallbackFontAssets[i];
- if (temp == null)
- continue;
- int id = temp.instanceID;
-
- if (k_SearchedAssets.Add(id) == false)
- continue;
-
- sourceFontAsset.FallbackSearchQueryLookup.Add(id);
- character = GetCharacterFromFontAsset_Internal(unicode, temp, true, fontStyle, fontWeight, out isAlternativeTypeface);
- if (character != null)
- return character;
- }
- }
- }
- return null;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static TMP_Character GetCharacterFromFontAssets(uint unicode, TMP_FontAsset sourceFontAsset, List<TMP_FontAsset> fontAssets, bool includeFallbacks, FontStyles fontStyle, FontWeight fontWeight, out bool isAlternativeTypeface)
- {
- isAlternativeTypeface = false;
-
- if (fontAssets == null || fontAssets.Count == 0)
- return null;
- if (includeFallbacks)
- {
- if (k_SearchedAssets == null)
- k_SearchedAssets = new HashSet<int>();
- else
- k_SearchedAssets.Clear();
- }
- int fontAssetCount = fontAssets.Count;
- for (int i = 0; i < fontAssetCount; i++)
- {
- TMP_FontAsset fontAsset = fontAssets[i];
- if (fontAsset == null) continue;
-
- sourceFontAsset.FallbackSearchQueryLookup.Add(fontAsset.instanceID);
- TMP_Character character = GetCharacterFromFontAsset_Internal(unicode, fontAsset, includeFallbacks, fontStyle, fontWeight, out isAlternativeTypeface);
- if (character != null)
- return character;
- }
- return null;
- }
-
-
-
-
-
-
-
-
-
-
- public static TMP_SpriteCharacter GetSpriteCharacterFromSpriteAsset(uint unicode, TMP_SpriteAsset spriteAsset, bool includeFallbacks)
- {
-
- if (spriteAsset == null)
- return null;
- TMP_SpriteCharacter spriteCharacter;
-
- if (spriteAsset.spriteCharacterLookupTable.TryGetValue(unicode, out spriteCharacter))
- return spriteCharacter;
- if (includeFallbacks)
- {
-
- if (k_SearchedAssets == null)
- k_SearchedAssets = new HashSet<int>();
- else
- k_SearchedAssets.Clear();
-
- k_SearchedAssets.Add(spriteAsset.instanceID);
- List<TMP_SpriteAsset> fallbackSpriteAsset = spriteAsset.fallbackSpriteAssets;
- if (fallbackSpriteAsset != null && fallbackSpriteAsset.Count > 0)
- {
- int fallbackCount = fallbackSpriteAsset.Count;
- for (int i = 0; i < fallbackCount; i++)
- {
- TMP_SpriteAsset temp = fallbackSpriteAsset[i];
- if (temp == null)
- continue;
- int id = temp.instanceID;
-
- if (k_SearchedAssets.Add(id) == false)
- continue;
- spriteCharacter = GetSpriteCharacterFromSpriteAsset_Internal(unicode, temp, true);
- if (spriteCharacter != null)
- return spriteCharacter;
- }
- }
- }
- return null;
- }
-
-
-
-
-
-
-
- static TMP_SpriteCharacter GetSpriteCharacterFromSpriteAsset_Internal(uint unicode, TMP_SpriteAsset spriteAsset, bool includeFallbacks)
- {
- TMP_SpriteCharacter spriteCharacter;
-
- if (spriteAsset.spriteCharacterLookupTable.TryGetValue(unicode, out spriteCharacter))
- return spriteCharacter;
- if (includeFallbacks)
- {
- List<TMP_SpriteAsset> fallbackSpriteAsset = spriteAsset.fallbackSpriteAssets;
- if (fallbackSpriteAsset != null && fallbackSpriteAsset.Count > 0)
- {
- int fallbackCount = fallbackSpriteAsset.Count;
- for (int i = 0; i < fallbackCount; i++)
- {
- TMP_SpriteAsset temp = fallbackSpriteAsset[i];
- if (temp == null)
- continue;
- int id = temp.instanceID;
-
- if (k_SearchedAssets.Add(id) == false)
- continue;
- spriteCharacter = GetSpriteCharacterFromSpriteAsset_Internal(unicode, temp, true);
- if (spriteCharacter != null)
- return spriteCharacter;
- }
- }
- }
- return null;
- }
-
-
-
- private static bool k_IsFontEngineInitialized;
-
- }
- }
|