TMP_FontAssetUtilities.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. isAlternativeTypeface = true;
  101. return character;
  102. }
  103. if (temp.atlasPopulationMode == AtlasPopulationMode.Dynamic)
  104. {
  105. if (temp.TryAddCharacterInternal(unicode, out character))
  106. {
  107. isAlternativeTypeface = true;
  108. return character;
  109. }
  110. // Check if the source font file contains the requested character.
  111. //if (TryGetCharacterFromFontFile(unicode, fontAsset, out characterData))
  112. //{
  113. // isAlternativeTypeface = true;
  114. // return characterData;
  115. //}
  116. // If we find the requested character, we add it to the font asset character table
  117. // and return its character data.
  118. // We also add this character to the list of characters we will need to add to the font atlas.
  119. // We assume the font atlas has room otherwise this font asset should not be marked as dynamic.
  120. // Alternatively, we could also add multiple pages of font atlas textures (feature consideration).
  121. }
  122. // At this point, we were not able to find the requested character in the alternative typeface
  123. // so we check the source font asset and its potential fallbacks.
  124. }
  125. }
  126. #endregion
  127. // Search the source font asset for the requested character.
  128. if (sourceFontAsset.characterLookupTable.TryGetValue(unicode, out character))
  129. return character;
  130. if (sourceFontAsset.atlasPopulationMode == AtlasPopulationMode.Dynamic)
  131. {
  132. if (sourceFontAsset.TryAddCharacterInternal(unicode, out character))
  133. return character;
  134. }
  135. // Search fallback font assets if we still don't have a valid character and include fallback is set to true.
  136. if (character == null && includeFallbacks && sourceFontAsset.fallbackFontAssetTable != null)
  137. {
  138. // Get reference to the list of fallback font assets.
  139. List<TMP_FontAsset> fallbackFontAssets = sourceFontAsset.fallbackFontAssetTable;
  140. int fallbackCount = fallbackFontAssets.Count;
  141. if (fallbackFontAssets != null && fallbackCount > 0)
  142. {
  143. for (int i = 0; i < fallbackCount; i++)
  144. {
  145. TMP_FontAsset temp = fallbackFontAssets[i];
  146. if (temp == null)
  147. continue;
  148. int id = temp.instanceID;
  149. // Try adding font asset to search list. If already present skip to the next one otherwise check if it contains the requested character.
  150. if (k_SearchedAssets.Add(id) == false)
  151. continue;
  152. // Add reference to this search query
  153. sourceFontAsset.FallbackSearchQueryLookup.Add(id);
  154. character = GetCharacterFromFontAsset_Internal(unicode, temp, true, fontStyle, fontWeight, out isAlternativeTypeface);
  155. if (character != null)
  156. return character;
  157. }
  158. }
  159. }
  160. return null;
  161. }
  162. /// <summary>
  163. /// Returns the text element (character) for the given unicode value taking into consideration the requested font style and weight.
  164. /// Function searches the provided list of font assets, the list of font assets assigned as alternative typefaces to them as well as their fallbacks.
  165. /// The font asset out parameter contains a reference to the font asset containing the character.
  166. /// The typeface type indicates whether the returned font asset is the source font asset, an alternative typeface or fallback font asset.
  167. /// </summary>
  168. /// <param name="unicode">The unicode value of the requested character</param>
  169. /// <param name="sourceFontAsset">The font asset originating the search query</param>
  170. /// <param name="fontAssets">The list of font assets to search</param>
  171. /// <param name="includeFallbacks">Determines if the fallback of each font assets on the list will be searched</param>
  172. /// <param name="fontStyle">The font style</param>
  173. /// <param name="fontWeight">The font weight</param>
  174. /// <param name="isAlternativeTypeface">Determines if the OUT font asset is an alternative typeface or fallback font asset</param>
  175. /// <returns></returns>
  176. public static TMP_Character GetCharacterFromFontAssets(uint unicode, TMP_FontAsset sourceFontAsset, List<TMP_FontAsset> fontAssets, bool includeFallbacks, FontStyles fontStyle, FontWeight fontWeight, out bool isAlternativeTypeface)
  177. {
  178. isAlternativeTypeface = false;
  179. // Make sure font asset list is valid
  180. if (fontAssets == null || fontAssets.Count == 0)
  181. return null;
  182. if (includeFallbacks)
  183. {
  184. if (k_SearchedAssets == null)
  185. k_SearchedAssets = new HashSet<int>();
  186. else
  187. k_SearchedAssets.Clear();
  188. }
  189. int fontAssetCount = fontAssets.Count;
  190. for (int i = 0; i < fontAssetCount; i++)
  191. {
  192. TMP_FontAsset fontAsset = fontAssets[i];
  193. if (fontAsset == null) continue;
  194. // Add reference to this search query
  195. sourceFontAsset.FallbackSearchQueryLookup.Add(fontAsset.instanceID);
  196. TMP_Character character = GetCharacterFromFontAsset_Internal(unicode, fontAsset, includeFallbacks, fontStyle, fontWeight, out isAlternativeTypeface);
  197. if (character != null)
  198. return character;
  199. }
  200. return null;
  201. }
  202. // =====================================================================
  203. // SPRITE ASSET - Functions
  204. // =====================================================================
  205. /// <summary>
  206. ///
  207. /// </summary>
  208. /// <param name="unicode"></param>
  209. /// <param name="spriteAsset"></param>
  210. /// <param name="includeFallbacks"></param>
  211. /// <returns></returns>
  212. public static TMP_SpriteCharacter GetSpriteCharacterFromSpriteAsset(uint unicode, TMP_SpriteAsset spriteAsset, bool includeFallbacks)
  213. {
  214. // Make sure we have a valid sprite asset to search
  215. if (spriteAsset == null)
  216. return null;
  217. TMP_SpriteCharacter spriteCharacter;
  218. // Search sprite asset for potential sprite character for the given unicode value
  219. if (spriteAsset.spriteCharacterLookupTable.TryGetValue(unicode, out spriteCharacter))
  220. return spriteCharacter;
  221. if (includeFallbacks)
  222. {
  223. // Clear searched assets
  224. if (k_SearchedAssets == null)
  225. k_SearchedAssets = new HashSet<int>();
  226. else
  227. k_SearchedAssets.Clear();
  228. // Add current sprite asset to already searched assets.
  229. k_SearchedAssets.Add(spriteAsset.instanceID);
  230. List<TMP_SpriteAsset> fallbackSpriteAsset = spriteAsset.fallbackSpriteAssets;
  231. if (fallbackSpriteAsset != null && fallbackSpriteAsset.Count > 0)
  232. {
  233. int fallbackCount = fallbackSpriteAsset.Count;
  234. for (int i = 0; i < fallbackCount; i++)
  235. {
  236. TMP_SpriteAsset temp = fallbackSpriteAsset[i];
  237. if (temp == null)
  238. continue;
  239. int id = temp.instanceID;
  240. // Try adding asset to search list. If already present skip to the next one otherwise check if it contains the requested character.
  241. if (k_SearchedAssets.Add(id) == false)
  242. continue;
  243. spriteCharacter = GetSpriteCharacterFromSpriteAsset_Internal(unicode, temp, true);
  244. if (spriteCharacter != null)
  245. return spriteCharacter;
  246. }
  247. }
  248. }
  249. return null;
  250. }
  251. /// <summary>
  252. ///
  253. /// </summary>
  254. /// <param name="unicode"></param>
  255. /// <param name="spriteAsset"></param>
  256. /// <param name="includeFallbacks"></param>
  257. /// <returns></returns>
  258. static TMP_SpriteCharacter GetSpriteCharacterFromSpriteAsset_Internal(uint unicode, TMP_SpriteAsset spriteAsset, bool includeFallbacks)
  259. {
  260. TMP_SpriteCharacter spriteCharacter;
  261. // Search sprite asset for potential sprite character for the given unicode value
  262. if (spriteAsset.spriteCharacterLookupTable.TryGetValue(unicode, out spriteCharacter))
  263. return spriteCharacter;
  264. if (includeFallbacks)
  265. {
  266. List<TMP_SpriteAsset> fallbackSpriteAsset = spriteAsset.fallbackSpriteAssets;
  267. if (fallbackSpriteAsset != null && fallbackSpriteAsset.Count > 0)
  268. {
  269. int fallbackCount = fallbackSpriteAsset.Count;
  270. for (int i = 0; i < fallbackCount; i++)
  271. {
  272. TMP_SpriteAsset temp = fallbackSpriteAsset[i];
  273. if (temp == null)
  274. continue;
  275. int id = temp.instanceID;
  276. // Try adding asset to search list. If already present skip to the next one otherwise check if it contains the requested character.
  277. if (k_SearchedAssets.Add(id) == false)
  278. continue;
  279. spriteCharacter = GetSpriteCharacterFromSpriteAsset_Internal(unicode, temp, true);
  280. if (spriteCharacter != null)
  281. return spriteCharacter;
  282. }
  283. }
  284. }
  285. return null;
  286. }
  287. // =====================================================================
  288. // FONT ENGINE & FONT FILE MANAGEMENT - Fields, Properties and Functions
  289. // =====================================================================
  290. private static bool k_IsFontEngineInitialized;
  291. /*
  292. private static bool TryGetCharacterFromFontFile(uint unicode, TMP_FontAsset fontAsset, out TMP_Character character)
  293. {
  294. character = null;
  295. // Initialize Font Engine library if not already initialized
  296. if (k_IsFontEngineInitialized == false)
  297. {
  298. FontEngineError error = FontEngine.InitializeFontEngine();
  299. if (error == 0)
  300. k_IsFontEngineInitialized = true;
  301. }
  302. // Load the font face for the given font asset.
  303. // TODO: Add manager to keep track of which font faces are currently loaded.
  304. FontEngine.LoadFontFace(fontAsset.sourceFontFile, fontAsset.faceInfo.pointSize);
  305. Glyph glyph = null;
  306. uint glyphIndex = FontEngine.GetGlyphIndex(unicode);
  307. // Check if glyph is already contained in the font asset as the same glyph might be referenced by multiple character.
  308. if (fontAsset.glyphLookupTable.TryGetValue(glyphIndex, out glyph))
  309. {
  310. character = fontAsset.AddCharacter_Internal(unicode, glyph);
  311. return true;
  312. }
  313. GlyphLoadFlags glyphLoadFlags = ((GlyphRasterModes)fontAsset.atlasRenderMode & GlyphRasterModes.RASTER_MODE_HINTED) == GlyphRasterModes.RASTER_MODE_HINTED ? GlyphLoadFlags.LOAD_RENDER : GlyphLoadFlags.LOAD_RENDER | GlyphLoadFlags.LOAD_NO_HINTING;
  314. if (FontEngine.TryGetGlyphWithUnicodeValue(unicode, glyphLoadFlags, out glyph))
  315. {
  316. // Add new character to font asset (if needed)
  317. character = fontAsset.AddCharacter_Internal(unicode, glyph);
  318. return true;
  319. }
  320. return false;
  321. }
  322. public static bool TryGetGlyphFromFontFile(uint glyphIndex, TMP_FontAsset fontAsset, out Glyph glyph)
  323. {
  324. glyph = null;
  325. // Initialize Font Engine library if not already initialized
  326. if (k_IsFontEngineInitialized == false)
  327. {
  328. FontEngineError error = FontEngine.InitializeFontEngine();
  329. if (error == 0)
  330. k_IsFontEngineInitialized = true;
  331. }
  332. // Load the font face for the given font asset.
  333. // TODO: Add manager to keep track of which font faces are currently loaded.
  334. FontEngine.LoadFontFace(fontAsset.sourceFontFile, fontAsset.faceInfo.pointSize);
  335. GlyphLoadFlags glyphLoadFlags = ((GlyphRasterModes)fontAsset.atlasRenderMode & GlyphRasterModes.RASTER_MODE_HINTED) == GlyphRasterModes.RASTER_MODE_HINTED ? GlyphLoadFlags.LOAD_RENDER : GlyphLoadFlags.LOAD_RENDER | GlyphLoadFlags.LOAD_NO_HINTING;
  336. if (FontEngine.TryGetGlyphWithIndexValue(glyphIndex, glyphLoadFlags, out glyph))
  337. {
  338. // Add new glyph to font asset (if needed)
  339. //fontAsset.AddGlyph_Internal(glyph);
  340. return true;
  341. }
  342. return false;
  343. }
  344. */
  345. }
  346. }