No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TMP_FontAssetUtilities.cs 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. using System.Collections.Generic;
  2. using UnityEngine.TextCore;
  3. using UnityEngine.TextCore.LowLevel;
  4. namespace TMPro
  5. {
  6. public class TMP_FontAssetUtilities
  7. {
  8. private static readonly TMP_FontAssetUtilities s_Instance = new TMP_FontAssetUtilities();
  9. /// <summary>
  10. /// Default constructor
  11. /// </summary>
  12. static TMP_FontAssetUtilities() { }
  13. /// <summary>
  14. /// Get a singleton instance of the Font Asset Utilities class.
  15. /// </summary>
  16. public static TMP_FontAssetUtilities instance
  17. {
  18. get { return s_Instance; }
  19. }
  20. /// <summary>
  21. /// HashSet containing instance ID of font assets already searched.
  22. /// </summary>
  23. private static HashSet<int> k_SearchedAssets;
  24. /// <summary>
  25. /// Returns the text element (character) for the given unicode value taking into consideration the requested font style and weight.
  26. /// Function searches the source font asset, its list of font assets assigned as alternative typefaces and potentially its fallbacks.
  27. /// The font asset out parameter contains a reference to the font asset containing the character.
  28. /// The typeface type indicates whether the returned font asset is the source font asset, an alternative typeface or fallback font asset.
  29. /// </summary>
  30. /// <param name="unicode">The unicode value of the requested character</param>
  31. /// <param name="sourceFontAsset">The font asset to be searched</param>
  32. /// <param name="includeFallbacks">Include the fallback font assets in the search</param>
  33. /// <param name="fontStyle">The font style</param>
  34. /// <param name="fontWeight">The font weight</param>
  35. /// <param name="isAlternativeTypeface">Indicates if the OUT font asset is an alternative typeface or fallback font asset</param>
  36. /// <param name="fontAsset">The font asset that contains the requested character</param>
  37. /// <returns></returns>
  38. public static TMP_Character GetCharacterFromFontAsset(uint unicode, TMP_FontAsset sourceFontAsset, bool includeFallbacks, FontStyles fontStyle, FontWeight fontWeight, out bool isAlternativeTypeface)
  39. {
  40. if (includeFallbacks)
  41. {
  42. if (k_SearchedAssets == null)
  43. k_SearchedAssets = new HashSet<int>();
  44. else
  45. k_SearchedAssets.Clear();
  46. }
  47. return GetCharacterFromFontAsset_Internal(unicode, sourceFontAsset, includeFallbacks, fontStyle, fontWeight, out isAlternativeTypeface);
  48. }
  49. /// <summary>
  50. /// Internal function returning the text element character for the given unicode value taking into consideration the font style and weight.
  51. /// Function searches the source font asset, list of font assets assigned as alternative typefaces and list of fallback font assets.
  52. /// </summary>
  53. private static TMP_Character GetCharacterFromFontAsset_Internal(uint unicode, TMP_FontAsset sourceFontAsset, bool includeFallbacks, FontStyles fontStyle, FontWeight fontWeight, out bool isAlternativeTypeface)
  54. {
  55. isAlternativeTypeface = false;
  56. TMP_Character character = null;
  57. #region FONT WEIGHT AND FONT STYLE HANDLING
  58. // Determine if a font weight or style is used. If so check if an alternative typeface is assigned for the given weight and / or style.
  59. bool isItalic = (fontStyle & FontStyles.Italic) == FontStyles.Italic;
  60. if (isItalic || fontWeight != FontWeight.Regular)
  61. {
  62. // Get reference to the font weight pairs of the given font asset.
  63. TMP_FontWeightPair[] fontWeights = sourceFontAsset.fontWeightTable;
  64. int fontWeightIndex = 4;
  65. switch (fontWeight)
  66. {
  67. case FontWeight.Thin:
  68. fontWeightIndex = 1;
  69. break;
  70. case FontWeight.ExtraLight:
  71. fontWeightIndex = 2;
  72. break;
  73. case FontWeight.Light:
  74. fontWeightIndex = 3;
  75. break;
  76. case FontWeight.Regular:
  77. fontWeightIndex = 4;
  78. break;
  79. case FontWeight.Medium:
  80. fontWeightIndex = 5;
  81. break;
  82. case FontWeight.SemiBold:
  83. fontWeightIndex = 6;
  84. break;
  85. case FontWeight.Bold:
  86. fontWeightIndex = 7;
  87. break;
  88. case FontWeight.Heavy:
  89. fontWeightIndex = 8;
  90. break;
  91. case FontWeight.Black:
  92. fontWeightIndex = 9;
  93. break;
  94. }
  95. TMP_FontAsset temp = isItalic ? fontWeights[fontWeightIndex].italicTypeface : fontWeights[fontWeightIndex].regularTypeface;
  96. if (temp != null)
  97. {
  98. if (temp.characterLookupTable.TryGetValue(unicode, out character))
  99. {
  100. if (character.textAsset != null)
  101. {
  102. isAlternativeTypeface = true;
  103. return character;
  104. }
  105. // Remove character from lookup table
  106. temp.characterLookupTable.Remove(unicode);
  107. }
  108. if (temp.atlasPopulationMode == AtlasPopulationMode.Dynamic || temp.atlasPopulationMode == AtlasPopulationMode.DynamicOS)
  109. {
  110. if (temp.TryAddCharacterInternal(unicode, out character))
  111. {
  112. isAlternativeTypeface = true;
  113. return character;
  114. }
  115. // Check if the source font file contains the requested character.
  116. //if (TryGetCharacterFromFontFile(unicode, fontAsset, out characterData))
  117. //{
  118. // isAlternativeTypeface = true;
  119. // return characterData;
  120. //}
  121. // If we find the requested character, we add it to the font asset character table
  122. // and return its character data.
  123. // We also add this character to the list of characters we will need to add to the font atlas.
  124. // We assume the font atlas has room otherwise this font asset should not be marked as dynamic.
  125. // Alternatively, we could also add multiple pages of font atlas textures (feature consideration).
  126. }
  127. // At this point, we were not able to find the requested character in the alternative typeface
  128. // so we check the source font asset and its potential fallbacks.
  129. }
  130. }
  131. #endregion
  132. // Search the source font asset for the requested character.
  133. if (sourceFontAsset.characterLookupTable.TryGetValue(unicode, out character))
  134. {
  135. if (character.textAsset != null)
  136. return character;
  137. // Remove character from lookup table
  138. sourceFontAsset.characterLookupTable.Remove(unicode);
  139. }
  140. if (sourceFontAsset.atlasPopulationMode == AtlasPopulationMode.Dynamic || sourceFontAsset.atlasPopulationMode == AtlasPopulationMode.DynamicOS)
  141. {
  142. if (sourceFontAsset.TryAddCharacterInternal(unicode, out character))
  143. return character;
  144. }
  145. // Search fallback font assets if we still don't have a valid character and include fallback is set to true.
  146. if (character == null && includeFallbacks && sourceFontAsset.fallbackFontAssetTable != null)
  147. {
  148. // Get reference to the list of fallback font assets.
  149. List<TMP_FontAsset> fallbackFontAssets = sourceFontAsset.fallbackFontAssetTable;
  150. int fallbackCount = fallbackFontAssets.Count;
  151. if (fallbackCount == 0)
  152. return null;
  153. for (int i = 0; i < fallbackCount; i++)
  154. {
  155. TMP_FontAsset temp = fallbackFontAssets[i];
  156. if (temp == null)
  157. continue;
  158. int id = temp.instanceID;
  159. // Try adding font asset to search list. If already present skip to the next one otherwise check if it contains the requested character.
  160. if (k_SearchedAssets.Add(id) == false)
  161. continue;
  162. // Add reference to this search query
  163. //sourceFontAsset.FallbackSearchQueryLookup.Add(id);
  164. character = GetCharacterFromFontAsset_Internal(unicode, temp, true, fontStyle, fontWeight, out isAlternativeTypeface);
  165. if (character != null)
  166. return character;
  167. }
  168. }
  169. return null;
  170. }
  171. /// <summary>
  172. /// Returns the text element (character) for the given unicode value taking into consideration the requested font style and weight.
  173. /// Function searches the provided list of font assets, the list of font assets assigned as alternative typefaces to them as well as their fallbacks.
  174. /// The font asset out parameter contains a reference to the font asset containing the character.
  175. /// The typeface type indicates whether the returned font asset is the source font asset, an alternative typeface or fallback font asset.
  176. /// </summary>
  177. /// <param name="unicode">The unicode value of the requested character</param>
  178. /// <param name="sourceFontAsset">The font asset originating the search query</param>
  179. /// <param name="fontAssets">The list of font assets to search</param>
  180. /// <param name="includeFallbacks">Determines if the fallback of each font assets on the list will be searched</param>
  181. /// <param name="fontStyle">The font style</param>
  182. /// <param name="fontWeight">The font weight</param>
  183. /// <param name="isAlternativeTypeface">Determines if the OUT font asset is an alternative typeface or fallback font asset</param>
  184. /// <returns></returns>
  185. public static TMP_Character GetCharacterFromFontAssets(uint unicode, TMP_FontAsset sourceFontAsset, List<TMP_FontAsset> fontAssets, bool includeFallbacks, FontStyles fontStyle, FontWeight fontWeight, out bool isAlternativeTypeface)
  186. {
  187. isAlternativeTypeface = false;
  188. // Make sure font asset list is valid
  189. if (fontAssets == null || fontAssets.Count == 0)
  190. return null;
  191. if (includeFallbacks)
  192. {
  193. if (k_SearchedAssets == null)
  194. k_SearchedAssets = new HashSet<int>();
  195. else
  196. k_SearchedAssets.Clear();
  197. }
  198. int fontAssetCount = fontAssets.Count;
  199. for (int i = 0; i < fontAssetCount; i++)
  200. {
  201. TMP_FontAsset fontAsset = fontAssets[i];
  202. if (fontAsset == null) continue;
  203. // Add reference to this search query
  204. //sourceFontAsset.FallbackSearchQueryLookup.Add(fontAsset.instanceID);
  205. TMP_Character character = GetCharacterFromFontAsset_Internal(unicode, fontAsset, includeFallbacks, fontStyle, fontWeight, out isAlternativeTypeface);
  206. if (character != null)
  207. return character;
  208. }
  209. return null;
  210. }
  211. internal static TMP_TextElement GetTextElementFromTextAssets(uint unicode, TMP_FontAsset sourceFontAsset, List<TMP_Asset> textAssets, bool includeFallbacks, FontStyles fontStyle, FontWeight fontWeight, out bool isAlternativeTypeface)
  212. {
  213. isAlternativeTypeface = false;
  214. // Make sure font asset list is valid
  215. if (textAssets == null || textAssets.Count == 0)
  216. return null;
  217. if (includeFallbacks)
  218. {
  219. if (k_SearchedAssets == null)
  220. k_SearchedAssets = new HashSet<int>();
  221. else
  222. k_SearchedAssets.Clear();
  223. }
  224. int textAssetCount = textAssets.Count;
  225. for (int i = 0; i < textAssetCount; i++)
  226. {
  227. TMP_Asset textAsset = textAssets[i];
  228. if (textAsset == null) continue;
  229. if (textAsset.GetType() == typeof(TMP_FontAsset))
  230. {
  231. TMP_FontAsset fontAsset = textAsset as TMP_FontAsset;
  232. TMP_Character character = GetCharacterFromFontAsset_Internal(unicode, fontAsset, includeFallbacks, fontStyle, fontWeight, out isAlternativeTypeface);
  233. if (character != null)
  234. return character;
  235. }
  236. else
  237. {
  238. TMP_SpriteAsset spriteAsset = textAsset as TMP_SpriteAsset;
  239. TMP_SpriteCharacter spriteCharacter = GetSpriteCharacterFromSpriteAsset_Internal(unicode, spriteAsset, true);
  240. if (spriteCharacter != null)
  241. return spriteCharacter;
  242. }
  243. }
  244. return null;
  245. }
  246. // =====================================================================
  247. // SPRITE ASSET - Functions
  248. // =====================================================================
  249. /// <summary>
  250. ///
  251. /// </summary>
  252. /// <param name="unicode"></param>
  253. /// <param name="spriteAsset"></param>
  254. /// <param name="includeFallbacks"></param>
  255. /// <returns></returns>
  256. public static TMP_SpriteCharacter GetSpriteCharacterFromSpriteAsset(uint unicode, TMP_SpriteAsset spriteAsset, bool includeFallbacks)
  257. {
  258. // Make sure we have a valid sprite asset to search
  259. if (spriteAsset == null)
  260. return null;
  261. TMP_SpriteCharacter spriteCharacter;
  262. // Search sprite asset for potential sprite character for the given unicode value
  263. if (spriteAsset.spriteCharacterLookupTable.TryGetValue(unicode, out spriteCharacter))
  264. return spriteCharacter;
  265. if (includeFallbacks)
  266. {
  267. // Clear searched assets
  268. if (k_SearchedAssets == null)
  269. k_SearchedAssets = new HashSet<int>();
  270. else
  271. k_SearchedAssets.Clear();
  272. // Add current sprite asset to already searched assets.
  273. k_SearchedAssets.Add(spriteAsset.instanceID);
  274. List<TMP_SpriteAsset> fallbackSpriteAsset = spriteAsset.fallbackSpriteAssets;
  275. if (fallbackSpriteAsset != null && fallbackSpriteAsset.Count > 0)
  276. {
  277. int fallbackCount = fallbackSpriteAsset.Count;
  278. for (int i = 0; i < fallbackCount; i++)
  279. {
  280. TMP_SpriteAsset temp = fallbackSpriteAsset[i];
  281. if (temp == null)
  282. continue;
  283. int id = temp.instanceID;
  284. // Try adding asset to search list. If already present skip to the next one otherwise check if it contains the requested character.
  285. if (k_SearchedAssets.Add(id) == false)
  286. continue;
  287. spriteCharacter = GetSpriteCharacterFromSpriteAsset_Internal(unicode, temp, true);
  288. if (spriteCharacter != null)
  289. return spriteCharacter;
  290. }
  291. }
  292. }
  293. return null;
  294. }
  295. /// <summary>
  296. ///
  297. /// </summary>
  298. /// <param name="unicode"></param>
  299. /// <param name="spriteAsset"></param>
  300. /// <param name="includeFallbacks"></param>
  301. /// <returns></returns>
  302. static TMP_SpriteCharacter GetSpriteCharacterFromSpriteAsset_Internal(uint unicode, TMP_SpriteAsset spriteAsset, bool includeFallbacks)
  303. {
  304. TMP_SpriteCharacter spriteCharacter;
  305. // Search sprite asset for potential sprite character for the given unicode value
  306. if (spriteAsset.spriteCharacterLookupTable.TryGetValue(unicode, out spriteCharacter))
  307. return spriteCharacter;
  308. if (includeFallbacks)
  309. {
  310. List<TMP_SpriteAsset> fallbackSpriteAsset = spriteAsset.fallbackSpriteAssets;
  311. if (fallbackSpriteAsset != null && fallbackSpriteAsset.Count > 0)
  312. {
  313. int fallbackCount = fallbackSpriteAsset.Count;
  314. for (int i = 0; i < fallbackCount; i++)
  315. {
  316. TMP_SpriteAsset temp = fallbackSpriteAsset[i];
  317. if (temp == null)
  318. continue;
  319. int id = temp.instanceID;
  320. // Try adding asset to search list. If already present skip to the next one otherwise check if it contains the requested character.
  321. if (k_SearchedAssets.Add(id) == false)
  322. continue;
  323. spriteCharacter = GetSpriteCharacterFromSpriteAsset_Internal(unicode, temp, true);
  324. if (spriteCharacter != null)
  325. return spriteCharacter;
  326. }
  327. }
  328. }
  329. return null;
  330. }
  331. // =====================================================================
  332. // FONT ENGINE & FONT FILE MANAGEMENT - Fields, Properties and Functions
  333. // =====================================================================
  334. /*
  335. private static bool k_IsFontEngineInitialized;
  336. private static bool TryGetCharacterFromFontFile(uint unicode, TMP_FontAsset fontAsset, out TMP_Character character)
  337. {
  338. character = null;
  339. // Initialize Font Engine library if not already initialized
  340. if (k_IsFontEngineInitialized == false)
  341. {
  342. FontEngineError error = FontEngine.InitializeFontEngine();
  343. if (error == 0)
  344. k_IsFontEngineInitialized = true;
  345. }
  346. // Load the font face for the given font asset.
  347. // TODO: Add manager to keep track of which font faces are currently loaded.
  348. FontEngine.LoadFontFace(fontAsset.sourceFontFile, fontAsset.faceInfo.pointSize);
  349. Glyph glyph = null;
  350. uint glyphIndex = FontEngine.GetGlyphIndex(unicode);
  351. // Check if glyph is already contained in the font asset as the same glyph might be referenced by multiple character.
  352. if (fontAsset.glyphLookupTable.TryGetValue(glyphIndex, out glyph))
  353. {
  354. character = fontAsset.AddCharacter_Internal(unicode, glyph);
  355. return true;
  356. }
  357. GlyphLoadFlags glyphLoadFlags = ((GlyphRasterModes)fontAsset.atlasRenderMode & GlyphRasterModes.RASTER_MODE_HINTED) == GlyphRasterModes.RASTER_MODE_HINTED ? GlyphLoadFlags.LOAD_RENDER : GlyphLoadFlags.LOAD_RENDER | GlyphLoadFlags.LOAD_NO_HINTING;
  358. if (FontEngine.TryGetGlyphWithUnicodeValue(unicode, glyphLoadFlags, out glyph))
  359. {
  360. // Add new character to font asset (if needed)
  361. character = fontAsset.AddCharacter_Internal(unicode, glyph);
  362. return true;
  363. }
  364. return false;
  365. }
  366. public static bool TryGetGlyphFromFontFile(uint glyphIndex, TMP_FontAsset fontAsset, out Glyph glyph)
  367. {
  368. glyph = null;
  369. // Initialize Font Engine library if not already initialized
  370. if (k_IsFontEngineInitialized == false)
  371. {
  372. FontEngineError error = FontEngine.InitializeFontEngine();
  373. if (error == 0)
  374. k_IsFontEngineInitialized = true;
  375. }
  376. // Load the font face for the given font asset.
  377. // TODO: Add manager to keep track of which font faces are currently loaded.
  378. FontEngine.LoadFontFace(fontAsset.sourceFontFile, fontAsset.faceInfo.pointSize);
  379. GlyphLoadFlags glyphLoadFlags = ((GlyphRasterModes)fontAsset.atlasRenderMode & GlyphRasterModes.RASTER_MODE_HINTED) == GlyphRasterModes.RASTER_MODE_HINTED ? GlyphLoadFlags.LOAD_RENDER : GlyphLoadFlags.LOAD_RENDER | GlyphLoadFlags.LOAD_NO_HINTING;
  380. if (FontEngine.TryGetGlyphWithIndexValue(glyphIndex, glyphLoadFlags, out glyph))
  381. {
  382. // Add new glyph to font asset (if needed)
  383. //fontAsset.AddGlyph_Internal(glyph);
  384. return true;
  385. }
  386. return false;
  387. }
  388. */
  389. }
  390. }