TMP_FontAsset.cs 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. using UnityEngine.TextCore;
  5. using UnityEngine.TextCore.LowLevel;
  6. using UnityEngine.Profiling;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. namespace TMPro
  11. {
  12. public enum AtlasPopulationMode
  13. {
  14. Static = 0x0,
  15. Dynamic = 0x1,
  16. }
  17. [Serializable]
  18. public class TMP_FontAsset : TMP_Asset
  19. {
  20. /// <summary>
  21. /// The version of the font asset class.
  22. /// Version 1.1.0 adds support for the new TextCore.FontEngine and Dynamic SDF system.
  23. /// </summary>
  24. public string version
  25. {
  26. get { return m_Version; }
  27. internal set { m_Version = value; }
  28. }
  29. [SerializeField]
  30. private string m_Version;
  31. /// <summary>
  32. /// This field is set when the font asset is first created.
  33. /// </summary>
  34. [SerializeField]
  35. internal string m_SourceFontFileGUID;
  36. #if UNITY_EDITOR
  37. /// <summary>
  38. /// Persistent reference to the source font file maintained in the editor.
  39. /// </summary>
  40. [SerializeField]
  41. internal Font m_SourceFontFile_EditorRef;
  42. #endif
  43. /// <summary>
  44. /// Source font file when atlas population mode is set to dynamic. Null when the atlas population mode is set to static.
  45. /// </summary>
  46. public Font sourceFontFile
  47. {
  48. get { return m_SourceFontFile; }
  49. internal set { m_SourceFontFile = value; }
  50. }
  51. [SerializeField]
  52. private Font m_SourceFontFile;
  53. public AtlasPopulationMode atlasPopulationMode
  54. {
  55. get { return m_AtlasPopulationMode; }
  56. set
  57. {
  58. m_AtlasPopulationMode = value;
  59. #if UNITY_EDITOR
  60. if (m_AtlasPopulationMode == AtlasPopulationMode.Static)
  61. m_SourceFontFile = null;
  62. else if (m_AtlasPopulationMode == AtlasPopulationMode.Dynamic)
  63. m_SourceFontFile = m_SourceFontFile_EditorRef;
  64. #endif
  65. }
  66. }
  67. [SerializeField]
  68. private AtlasPopulationMode m_AtlasPopulationMode;
  69. /// <summary>
  70. /// Information about the font face.
  71. /// </summary>
  72. public FaceInfo faceInfo
  73. {
  74. get { return m_FaceInfo; }
  75. internal set { m_FaceInfo = value; }
  76. }
  77. [SerializeField]
  78. private FaceInfo m_FaceInfo;
  79. /// <summary>
  80. /// List of glyphs contained in the font asset.
  81. /// </summary>
  82. public List<Glyph> glyphTable
  83. {
  84. get { return m_GlyphTable; }
  85. internal set { m_GlyphTable = value; }
  86. }
  87. [SerializeField]
  88. private List<Glyph> m_GlyphTable = new List<Glyph>();
  89. /// <summary>
  90. /// Dictionary used to lookup glyphs contained in the font asset by their index.
  91. /// </summary>
  92. public Dictionary<uint, Glyph> glyphLookupTable
  93. {
  94. get
  95. {
  96. if (m_GlyphLookupDictionary == null)
  97. ReadFontAssetDefinition();
  98. return m_GlyphLookupDictionary;
  99. }
  100. }
  101. private Dictionary<uint, Glyph> m_GlyphLookupDictionary;
  102. /// <summary>
  103. /// List containing the characters of the given font asset.
  104. /// </summary>
  105. public List<TMP_Character> characterTable
  106. {
  107. get { return m_CharacterTable; }
  108. internal set { m_CharacterTable = value; }
  109. }
  110. [SerializeField]
  111. private List<TMP_Character> m_CharacterTable = new List<TMP_Character>();
  112. /// <summary>
  113. /// Dictionary used to lookup characters contained in the font asset by their unicode values.
  114. /// </summary>
  115. public Dictionary<uint, TMP_Character> characterLookupTable
  116. {
  117. get
  118. {
  119. if (m_CharacterLookupDictionary == null)
  120. ReadFontAssetDefinition();
  121. return m_CharacterLookupDictionary;
  122. }
  123. }
  124. private Dictionary<uint, TMP_Character> m_CharacterLookupDictionary;
  125. /// <summary>
  126. /// The font atlas used by this font asset.
  127. /// This is always the texture at index [0] of the fontAtlasTextures.
  128. /// </summary>
  129. public Texture2D atlasTexture
  130. {
  131. get
  132. {
  133. if (m_AtlasTexture == null)
  134. {
  135. m_AtlasTexture = atlasTextures[0];
  136. }
  137. return m_AtlasTexture;
  138. }
  139. }
  140. private Texture2D m_AtlasTexture;
  141. /// <summary>
  142. /// Array of atlas textures that contain the glyphs used by this font asset.
  143. /// </summary>
  144. public Texture2D[] atlasTextures
  145. {
  146. get
  147. {
  148. if (m_AtlasTextures == null)
  149. {
  150. //
  151. }
  152. return m_AtlasTextures;
  153. }
  154. set
  155. {
  156. m_AtlasTextures = value;
  157. }
  158. }
  159. [SerializeField]
  160. private Texture2D[] m_AtlasTextures;
  161. /// <summary>
  162. /// Index of the font atlas texture that still has available space to add new glyphs.
  163. /// </summary>
  164. [SerializeField]
  165. internal int m_AtlasTextureIndex;
  166. /// <summary>
  167. /// List of spaces occupied by glyphs in a given texture.
  168. /// </summary>
  169. internal List<GlyphRect> usedGlyphRects
  170. {
  171. get { return m_UsedGlyphRects; }
  172. set { m_UsedGlyphRects = value; }
  173. }
  174. [SerializeField]
  175. private List<GlyphRect> m_UsedGlyphRects;
  176. /// <summary>
  177. /// List of spaces available in a given texture to add new glyphs.
  178. /// </summary>
  179. internal List<GlyphRect> freeGlyphRects
  180. {
  181. get { return m_FreeGlyphRects; }
  182. set { m_FreeGlyphRects = value; }
  183. }
  184. [SerializeField]
  185. private List<GlyphRect> m_FreeGlyphRects;
  186. /// <summary>
  187. /// The general information about the font.
  188. /// This property and FaceInfo_Legacy type are not longer used in version 1.1.0 of the font asset.
  189. /// </summary>
  190. [Obsolete("The fontInfo property and underlying type is now obsolete. Please use the faceInfo property and FaceInfo type instead.")]
  191. public FaceInfo_Legacy fontInfo
  192. {
  193. get { return m_fontInfo; }
  194. }
  195. [SerializeField]
  196. private FaceInfo_Legacy m_fontInfo = null;
  197. /// <summary>
  198. ///
  199. /// </summary>
  200. [SerializeField]
  201. public Texture2D atlas; // Should add a property to make this read-only.
  202. /// <summary>
  203. /// The width of the atlas texture(s) used by this font asset.
  204. /// </summary>
  205. public int atlasWidth
  206. {
  207. get { return m_AtlasWidth; }
  208. internal set { m_AtlasWidth = value; }
  209. }
  210. [SerializeField]
  211. private int m_AtlasWidth;
  212. /// <summary>
  213. /// The height of the atlas texture(s) used by this font asset.
  214. /// </summary>
  215. public int atlasHeight
  216. {
  217. get { return m_AtlasHeight; }
  218. internal set { m_AtlasHeight = value; }
  219. }
  220. [SerializeField]
  221. private int m_AtlasHeight;
  222. /// <summary>
  223. /// The padding used between glyphs contained in the atlas texture(s) used by this font asset.
  224. /// </summary>
  225. public int atlasPadding
  226. {
  227. get { return m_AtlasPadding; }
  228. internal set { m_AtlasPadding = value; }
  229. }
  230. [SerializeField]
  231. private int m_AtlasPadding;
  232. public GlyphRenderMode atlasRenderMode
  233. {
  234. get { return m_AtlasRenderMode; }
  235. internal set { m_AtlasRenderMode = value; }
  236. }
  237. [SerializeField]
  238. private GlyphRenderMode m_AtlasRenderMode;
  239. // Legacy field that will eventually be removed.
  240. [SerializeField]
  241. internal List<TMP_Glyph> m_glyphInfoList;
  242. [SerializeField]
  243. [FormerlySerializedAs("m_kerningInfo")]
  244. internal KerningTable m_KerningTable = new KerningTable();
  245. /// <summary>
  246. /// Table containing the various font features of this font asset.
  247. /// </summary>
  248. public TMP_FontFeatureTable fontFeatureTable
  249. {
  250. get { return m_FontFeatureTable; }
  251. internal set { m_FontFeatureTable = value; }
  252. }
  253. [SerializeField]
  254. private TMP_FontFeatureTable m_FontFeatureTable = new TMP_FontFeatureTable();
  255. // Legacy field that will eventually be removed
  256. [SerializeField]
  257. #pragma warning disable 0649
  258. private List<TMP_FontAsset> fallbackFontAssets;
  259. /// <summary>
  260. /// List containing the Fallback font assets for this font.
  261. /// </summary>
  262. public List<TMP_FontAsset> fallbackFontAssetTable
  263. {
  264. get { return m_FallbackFontAssetTable; }
  265. set { m_FallbackFontAssetTable = value; }
  266. }
  267. [SerializeField]
  268. public List<TMP_FontAsset> m_FallbackFontAssetTable;
  269. /// <summary>
  270. /// The settings used in the Font Asset Creator when this font asset was created or edited.
  271. /// </summary>
  272. public FontAssetCreationSettings creationSettings
  273. {
  274. get { return m_CreationSettings; }
  275. set { m_CreationSettings = value; }
  276. }
  277. [SerializeField]
  278. internal FontAssetCreationSettings m_CreationSettings;
  279. /// <summary>
  280. /// Array containing font assets to be used as alternative typefaces for the various potential font weights of this font asset.
  281. /// </summary>
  282. public TMP_FontWeightPair[] fontWeightTable
  283. {
  284. get { return m_FontWeightTable; }
  285. internal set { m_FontWeightTable = value; }
  286. }
  287. [SerializeField]
  288. private TMP_FontWeightPair[] m_FontWeightTable = new TMP_FontWeightPair[10];
  289. // FONT WEIGHTS
  290. /// <summary>
  291. /// Font weights used by font asset prior to version 1.1.0.
  292. /// This is legacy and will be removed at some point in the future.
  293. /// </summary>
  294. [SerializeField]
  295. private TMP_FontWeightPair[] fontWeights = null;
  296. //private int[] m_characterSet; // Array containing all the characters in this font asset.
  297. /// <summary>
  298. /// Defines the dilation of the text when using regular style.
  299. /// </summary>
  300. public float normalStyle = 0;
  301. /// <summary>
  302. /// The spacing between characters when using regular style.
  303. /// </summary>
  304. public float normalSpacingOffset = 0;
  305. /// <summary>
  306. /// Defines the dilation of the text when using bold style.
  307. /// </summary>
  308. public float boldStyle = 0.75f;
  309. /// <summary>
  310. /// The spacing between characters when using regular style.
  311. /// </summary>
  312. public float boldSpacing = 7f;
  313. /// <summary>
  314. /// Defines the slant of the text when using italic style.
  315. /// </summary>
  316. public byte italicStyle = 35;
  317. public byte tabSize = 10;
  318. private byte m_oldTabSize;
  319. internal bool m_IsFontAssetLookupTablesDirty = false;
  320. /// <summary>
  321. /// Create new instance of a font asset using default settings.
  322. /// </summary>
  323. /// <param name="font"></param>
  324. /// <returns></returns>
  325. public static TMP_FontAsset CreateFontAsset(Font font)
  326. {
  327. return CreateFontAsset(font, 90, 9, GlyphRenderMode.SDFAA, 1024, 1024, AtlasPopulationMode.Dynamic);
  328. }
  329. /// <summary>
  330. /// Create new instance of a font asset.
  331. /// </summary>
  332. /// <param name="font">The source font file.</param>
  333. /// <param name="samplingPointSize">The sampling point size.</param>
  334. /// <param name="atlasPadding">The padding / spread between individual glyphs in the font asset.</param>
  335. /// <param name="renderMode"></param>
  336. /// <param name="atlasWidth">The atlas texture width.</param>
  337. /// <param name="atlasHeight">The atlas texture height.</param>
  338. /// <param name="atlasPopulationMode"></param>
  339. /// <returns></returns>
  340. public static TMP_FontAsset CreateFontAsset(Font font, int samplingPointSize, int atlasPadding, GlyphRenderMode renderMode, int atlasWidth, int atlasHeight, AtlasPopulationMode atlasPopulationMode = AtlasPopulationMode.Dynamic)
  341. {
  342. TMP_FontAsset fontAsset = ScriptableObject.CreateInstance<TMP_FontAsset>();
  343. fontAsset.m_Version = "1.1.0";
  344. // Set face information
  345. FontEngine.InitializeFontEngine();
  346. FontEngine.LoadFontFace(font, samplingPointSize);
  347. fontAsset.faceInfo = FontEngine.GetFaceInfo();
  348. // Set font reference and GUID
  349. if (atlasPopulationMode == AtlasPopulationMode.Dynamic)
  350. fontAsset.sourceFontFile = font;
  351. // Set persistent reference to source font file in the Editor only.
  352. #if UNITY_EDITOR
  353. string guid;
  354. long localID;
  355. UnityEditor.AssetDatabase.TryGetGUIDAndLocalFileIdentifier(font, out guid, out localID);
  356. fontAsset.m_SourceFontFileGUID = guid;
  357. fontAsset.m_SourceFontFile_EditorRef = font;
  358. #endif
  359. fontAsset.atlasPopulationMode = atlasPopulationMode;
  360. fontAsset.atlasWidth = atlasWidth;
  361. fontAsset.atlasHeight = atlasHeight;
  362. fontAsset.atlasPadding = atlasPadding;
  363. fontAsset.atlasRenderMode = renderMode;
  364. // Initialize array for the font atlas textures.
  365. fontAsset.atlasTextures = new Texture2D[1];
  366. // Create and add font atlas texture.
  367. Texture2D texture = new Texture2D(0, 0, TextureFormat.Alpha8, false);
  368. //texture.name = assetName + " Atlas";
  369. fontAsset.atlasTextures[0] = texture;
  370. // Add free rectangle of the size of the texture.
  371. int packingModifier;
  372. if (((GlyphRasterModes)renderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP)
  373. {
  374. packingModifier = 0;
  375. // Optimize by adding static ref to shader.
  376. Material tmp_material = new Material(ShaderUtilities.ShaderRef_MobileBitmap);
  377. //tmp_material.name = texture.name + " Material";
  378. tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
  379. tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
  380. tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
  381. fontAsset.material = tmp_material;
  382. }
  383. else
  384. {
  385. packingModifier = 1;
  386. // Optimize by adding static ref to shader.
  387. Material tmp_material = new Material(ShaderUtilities.ShaderRef_MobileSDF);
  388. //tmp_material.name = texture.name + " Material";
  389. tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
  390. tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
  391. tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
  392. tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, atlasPadding + packingModifier);
  393. tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
  394. tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
  395. fontAsset.material = tmp_material;
  396. }
  397. fontAsset.freeGlyphRects = new List<GlyphRect>() { new GlyphRect(0, 0, atlasWidth - packingModifier, atlasHeight - packingModifier) };
  398. fontAsset.usedGlyphRects = new List<GlyphRect>();
  399. // TODO: Consider adding support for extracting glyph positioning data
  400. fontAsset.ReadFontAssetDefinition();
  401. return fontAsset;
  402. }
  403. void Awake()
  404. {
  405. //Debug.Log("TMP Font Asset [" + this.name + "] with Version #" + m_Version + " has been enabled!");
  406. // Check version number of font asset to see if it needs to be upgraded.
  407. if (this.material != null && string.IsNullOrEmpty(m_Version))
  408. UpgradeFontAsset();
  409. }
  410. #if UNITY_EDITOR
  411. void OnValidate()
  412. {
  413. //if (m_oldTabSize != tabSize)
  414. //{
  415. // m_oldTabSize = tabSize;
  416. // ReadFontAssetDefinition();
  417. //}
  418. // Handle changes to atlas population mode
  419. //if (m_AtlasPopulationMode == AtlasPopulationMode.Static)
  420. // m_SourceFontFile = null;
  421. //else
  422. //{
  423. // string path = UnityEditor.AssetDatabase.GUIDToAssetPath(m_SourceFontFileGUID);
  424. // if (path != string.Empty)
  425. // m_SourceFontFile = UnityEditor.AssetDatabase.LoadAssetAtPath<Font>(path);
  426. //}
  427. }
  428. #endif
  429. /// <summary>
  430. /// Read the various data tables of the font asset to populate its different dictionaries to allow for faster lookup of related font asset data.
  431. /// </summary>
  432. internal void InitializeDictionaryLookupTables()
  433. {
  434. // Create new instance of the glyph lookup dictionary or clear the existing one.
  435. if (m_GlyphLookupDictionary == null)
  436. m_GlyphLookupDictionary = new Dictionary<uint, Glyph>();
  437. else
  438. m_GlyphLookupDictionary.Clear();
  439. int glyphCount = m_GlyphTable.Count;
  440. // Initialize glyph index array or clear the existing one.
  441. if (m_GlyphIndexList == null)
  442. m_GlyphIndexList = new List<uint>();
  443. else
  444. m_GlyphIndexList.Clear();
  445. // Add the characters contained in the character table into the dictionary for faster lookup.
  446. for (int i = 0; i < glyphCount; i++)
  447. {
  448. Glyph glyph = m_GlyphTable[i];
  449. uint index = glyph.index;
  450. // TODO: Not sure it is necessary to check here.
  451. if (m_GlyphLookupDictionary.ContainsKey(index) == false)
  452. {
  453. m_GlyphLookupDictionary.Add(index, glyph);
  454. m_GlyphIndexList.Add(index);
  455. }
  456. }
  457. // Create new instance of the character lookup dictionary or clear the existing one.
  458. if (m_CharacterLookupDictionary == null)
  459. m_CharacterLookupDictionary = new Dictionary<uint, TMP_Character>();
  460. else
  461. m_CharacterLookupDictionary.Clear();
  462. // Add the characters contained in the character table into the dictionary for faster lookup.
  463. for (int i = 0; i < m_CharacterTable.Count; i++)
  464. {
  465. TMP_Character character = m_CharacterTable[i];
  466. uint unicode = character.unicode;
  467. uint glyphIndex = character.glyphIndex;
  468. if (m_CharacterLookupDictionary.ContainsKey(unicode) == false)
  469. m_CharacterLookupDictionary.Add(unicode, character);
  470. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex))
  471. {
  472. character.glyph = m_GlyphLookupDictionary[glyphIndex];
  473. }
  474. }
  475. // Upgrade Glyph Adjustment Table to the new Font Feature table and Glyph Pair Adjustment Records
  476. if (m_KerningTable != null && m_KerningTable.kerningPairs != null && m_KerningTable.kerningPairs.Count > 0)
  477. UpgradeGlyphAdjustmentTableToFontFeatureTable();
  478. // Read Font Features which will include kerning data.
  479. if (m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary == null)
  480. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary = new Dictionary<long, TMP_GlyphPairAdjustmentRecord>();
  481. else
  482. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Clear();
  483. List<TMP_GlyphPairAdjustmentRecord> glyphPairAdjustmentRecords = m_FontFeatureTable.m_GlyphPairAdjustmentRecords;
  484. if (glyphPairAdjustmentRecords != null)
  485. {
  486. for (int i = 0; i < glyphPairAdjustmentRecords.Count; i++)
  487. {
  488. TMP_GlyphPairAdjustmentRecord record = glyphPairAdjustmentRecords[i];
  489. long key = new GlyphPairKey(record).key;
  490. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Add(key, record);
  491. }
  492. }
  493. }
  494. /// <summary>
  495. ///
  496. /// </summary>
  497. public void ReadFontAssetDefinition()
  498. {
  499. //Debug.Log("Reading Font Definition for " + this.name + ".");
  500. // Check version number of font asset to see if it needs to be upgraded.
  501. if (this.material != null && string.IsNullOrEmpty(m_Version))
  502. UpgradeFontAsset();
  503. // Initialize lookup tables for characters and glyphs.
  504. InitializeDictionaryLookupTables();
  505. // Add Tab char(9) to Dictionary.
  506. if (m_CharacterLookupDictionary.ContainsKey(9) == false)
  507. {
  508. Glyph glyph = new Glyph(0, new GlyphMetrics(0, 0, 0, 0, m_FaceInfo.tabWidth * tabSize), GlyphRect.zero, 1.0f, 0);
  509. m_CharacterLookupDictionary.Add(9, new TMP_Character(9, glyph));
  510. }
  511. // Add Linefeed LF char(10) and Carriage Return CR char(13)
  512. if (m_CharacterLookupDictionary.ContainsKey(10) == false)
  513. {
  514. Glyph glyph = new Glyph(0, new GlyphMetrics(10, 0, 0, 0, 0), GlyphRect.zero, 1.0f, 0);
  515. m_CharacterLookupDictionary.Add(10, new TMP_Character(10, glyph));
  516. if (!m_CharacterLookupDictionary.ContainsKey(13))
  517. m_CharacterLookupDictionary.Add(13, new TMP_Character(13, glyph));
  518. }
  519. // Add Zero Width Space 8203 (0x200B)
  520. if (m_CharacterLookupDictionary.ContainsKey(8203) == false)
  521. {
  522. Glyph glyph = new Glyph(0, new GlyphMetrics(0, 0, 0, 0, 0), GlyphRect.zero, 1.0f, 0);
  523. m_CharacterLookupDictionary.Add(8203, new TMP_Character(8203, glyph));
  524. }
  525. // Add Zero Width Non-Breaking Space 8288 (0x2060)
  526. if (m_CharacterLookupDictionary.ContainsKey(8288) == false)
  527. {
  528. Glyph glyph = new Glyph(0, new GlyphMetrics(0, 0, 0, 0, 0), GlyphRect.zero, 1.0f, 0);
  529. m_CharacterLookupDictionary.Add(8288, new TMP_Character(8288, glyph));
  530. }
  531. // Add Non-Breaking Hyphen 8209 (0x2011)
  532. if (m_CharacterLookupDictionary.ContainsKey(8209) == false)
  533. {
  534. TMP_Character character;
  535. if (m_CharacterLookupDictionary.TryGetValue(45, out character))
  536. m_CharacterLookupDictionary.Add(8209, new TMP_Character(8209, character.glyph));
  537. }
  538. // Set Cap Height
  539. if (m_FaceInfo.capLine == 0 && m_CharacterLookupDictionary.ContainsKey(72))
  540. {
  541. uint glyphIndex = m_CharacterLookupDictionary[72].glyphIndex;
  542. m_FaceInfo.capLine = m_GlyphLookupDictionary[glyphIndex].metrics.horizontalBearingY;
  543. }
  544. // Adjust Font Scale for compatibility reasons
  545. if (m_FaceInfo.scale == 0)
  546. m_FaceInfo.scale = 1.0f;
  547. // Set Strikethrough Offset (if needed)
  548. if (m_FaceInfo.strikethroughOffset == 0)
  549. m_FaceInfo.strikethroughOffset = m_FaceInfo.capLine / 2.5f;
  550. // Set Padding value for legacy font assets.
  551. if (m_AtlasPadding == 0)
  552. {
  553. if (material.HasProperty(ShaderUtilities.ID_GradientScale))
  554. m_AtlasPadding = (int)material.GetFloat(ShaderUtilities.ID_GradientScale) - 1;
  555. }
  556. // Compute Hashcode for the font asset name
  557. hashCode = TMP_TextUtilities.GetSimpleHashCode(this.name);
  558. // Compute Hashcode for the material name
  559. materialHashCode = TMP_TextUtilities.GetSimpleHashCode(material.name);
  560. m_IsFontAssetLookupTablesDirty = false;
  561. }
  562. /// <summary>
  563. /// Sort the Character table by Unicode values.
  564. /// </summary>
  565. internal void SortCharacterTable()
  566. {
  567. if (m_CharacterTable != null && m_CharacterTable.Count > 0)
  568. m_CharacterTable = m_CharacterTable.OrderBy(c => c.unicode).ToList();
  569. }
  570. /// <summary>
  571. /// Sort the Glyph table by index values.
  572. /// </summary>
  573. internal void SortGlyphTable()
  574. {
  575. if (m_GlyphTable != null && m_GlyphTable.Count > 0)
  576. m_GlyphTable = m_GlyphTable.OrderBy(c => c.index).ToList();
  577. }
  578. /// <summary>
  579. /// Sort both glyph and character tables.
  580. /// </summary>
  581. internal void SortGlyphAndCharacterTables()
  582. {
  583. SortGlyphTable();
  584. SortCharacterTable();
  585. }
  586. /// <summary>
  587. /// Function to check if a certain character exists in the font asset.
  588. /// </summary>
  589. /// <param name="character"></param>
  590. /// <returns></returns>
  591. public bool HasCharacter(int character)
  592. {
  593. if (m_CharacterLookupDictionary == null)
  594. return false;
  595. if (m_CharacterLookupDictionary.ContainsKey((uint)character))
  596. return true;
  597. return false;
  598. }
  599. /// <summary>
  600. /// Function to check if a certain character exists in the font asset.
  601. /// </summary>
  602. /// <param name="character"></param>
  603. /// <returns></returns>
  604. public bool HasCharacter(char character)
  605. {
  606. if (m_CharacterLookupDictionary == null)
  607. return false;
  608. if (m_CharacterLookupDictionary.ContainsKey(character))
  609. return true;
  610. return false;
  611. }
  612. /// <summary>
  613. /// Function to check if a character is contained in the font asset with the option to also check through fallback font assets.
  614. /// </summary>
  615. /// <param name="character"></param>
  616. /// <param name="searchFallbacks"></param>
  617. /// <returns></returns>
  618. public bool HasCharacter(char character, bool searchFallbacks)
  619. {
  620. // Read font asset definition if it hasn't already been done.
  621. if (m_CharacterLookupDictionary == null)
  622. {
  623. ReadFontAssetDefinition();
  624. if (m_CharacterLookupDictionary == null)
  625. return false;
  626. }
  627. // Check font asset
  628. if (m_CharacterLookupDictionary.ContainsKey(character))
  629. return true;
  630. // Check if font asset is dynamic and if so try to add the requested character to it.
  631. if (m_AtlasPopulationMode == AtlasPopulationMode.Dynamic)
  632. {
  633. TMP_Character returnedCharacter;
  634. if (TryAddCharacterInternal(character, out returnedCharacter))
  635. return true;
  636. }
  637. if (searchFallbacks)
  638. {
  639. // Check font asset fallbacks
  640. if (fallbackFontAssetTable != null && fallbackFontAssetTable.Count > 0)
  641. {
  642. for (int i = 0; i < fallbackFontAssetTable.Count && fallbackFontAssetTable[i] != null; i++)
  643. {
  644. if (fallbackFontAssetTable[i].HasCharacter_Internal(character, searchFallbacks))
  645. return true;
  646. }
  647. }
  648. // Check general fallback font assets.
  649. if (TMP_Settings.fallbackFontAssets != null && TMP_Settings.fallbackFontAssets.Count > 0)
  650. {
  651. for (int i = 0; i < TMP_Settings.fallbackFontAssets.Count && TMP_Settings.fallbackFontAssets[i] != null; i++)
  652. {
  653. if (TMP_Settings.fallbackFontAssets[i].m_CharacterLookupDictionary == null)
  654. TMP_Settings.fallbackFontAssets[i].ReadFontAssetDefinition();
  655. if (TMP_Settings.fallbackFontAssets[i].m_CharacterLookupDictionary != null && TMP_Settings.fallbackFontAssets[i].HasCharacter_Internal(character, searchFallbacks))
  656. return true;
  657. }
  658. }
  659. // Check TMP Settings Default Font Asset
  660. if (TMP_Settings.defaultFontAsset != null)
  661. {
  662. if (TMP_Settings.defaultFontAsset.m_CharacterLookupDictionary == null)
  663. TMP_Settings.defaultFontAsset.ReadFontAssetDefinition();
  664. if (TMP_Settings.defaultFontAsset.m_CharacterLookupDictionary != null && TMP_Settings.defaultFontAsset.HasCharacter_Internal(character, searchFallbacks))
  665. return true;
  666. }
  667. }
  668. return false;
  669. }
  670. /// <summary>
  671. /// Function to check if a character is contained in a font asset with the option to also check through fallback font assets.
  672. /// This private implementation does not search the fallback font asset in the TMP Settings file.
  673. /// </summary>
  674. /// <param name="character"></param>
  675. /// <param name="searchFallbacks"></param>
  676. /// <returns></returns>
  677. bool HasCharacter_Internal(char character, bool searchFallbacks)
  678. {
  679. // Read font asset definition if it hasn't already been done.
  680. if (m_CharacterLookupDictionary == null)
  681. {
  682. ReadFontAssetDefinition();
  683. if (m_CharacterLookupDictionary == null)
  684. return false;
  685. }
  686. // Check font asset
  687. if (m_CharacterLookupDictionary.ContainsKey(character))
  688. return true;
  689. if (searchFallbacks)
  690. {
  691. // Check Font Asset Fallback fonts.
  692. if (fallbackFontAssetTable != null && fallbackFontAssetTable.Count > 0)
  693. {
  694. for (int i = 0; i < fallbackFontAssetTable.Count && fallbackFontAssetTable[i] != null; i++)
  695. {
  696. if (fallbackFontAssetTable[i].HasCharacter_Internal(character, searchFallbacks))
  697. return true;
  698. }
  699. }
  700. }
  701. return false;
  702. }
  703. /// <summary>
  704. /// Function to check if certain characters exists in the font asset. Function returns a list of missing characters.
  705. /// </summary>
  706. /// <param name="character"></param>
  707. /// <returns></returns>
  708. public bool HasCharacters(string text, out List<char> missingCharacters)
  709. {
  710. if (m_CharacterLookupDictionary == null)
  711. {
  712. missingCharacters = null;
  713. return false;
  714. }
  715. missingCharacters = new List<char>();
  716. for (int i = 0; i < text.Length; i++)
  717. {
  718. if (!m_CharacterLookupDictionary.ContainsKey(text[i]))
  719. missingCharacters.Add(text[i]);
  720. }
  721. if (missingCharacters.Count == 0)
  722. return true;
  723. return false;
  724. }
  725. /// <summary>
  726. /// Function to check if certain characters exists in the font asset. Function returns false if any characters are missing.
  727. /// </summary>
  728. /// <param name="text">String containing the characters to check</param>
  729. /// <returns></returns>
  730. public bool HasCharacters(string text)
  731. {
  732. if (m_CharacterLookupDictionary == null)
  733. return false;
  734. for (int i = 0; i < text.Length; i++)
  735. {
  736. if (!m_CharacterLookupDictionary.ContainsKey(text[i]))
  737. return false;
  738. }
  739. return true;
  740. }
  741. /// <summary>
  742. /// Function to extract all the characters from a font asset.
  743. /// </summary>
  744. /// <param name="fontAsset"></param>
  745. /// <returns></returns>
  746. public static string GetCharacters(TMP_FontAsset fontAsset)
  747. {
  748. string characters = string.Empty;
  749. for (int i = 0; i < fontAsset.characterTable.Count; i++)
  750. {
  751. characters += (char)fontAsset.characterTable[i].unicode;
  752. }
  753. return characters;
  754. }
  755. /// <summary>
  756. /// Function which returns an array that contains all the characters from a font asset.
  757. /// </summary>
  758. /// <param name="fontAsset"></param>
  759. /// <returns></returns>
  760. public static int[] GetCharactersArray(TMP_FontAsset fontAsset)
  761. {
  762. int[] characters = new int[fontAsset.characterTable.Count];
  763. for (int i = 0; i < fontAsset.characterTable.Count; i++)
  764. {
  765. characters[i] = (int)fontAsset.characterTable[i].unicode;
  766. }
  767. return characters;
  768. }
  769. // ================================================================================
  770. // Properties and functions related to character and glyph additions as well as
  771. // tacking glyphs that need to be added to various font asset atlas textures.
  772. // ================================================================================
  773. /// <summary>
  774. /// Determines if the font asset is already registered to be updated.
  775. /// </summary>
  776. //private bool m_IsAlreadyRegisteredForUpdate;
  777. /// <summary>
  778. /// List of glyphs that need to be added / packed in atlas texture.
  779. /// </summary>
  780. private List<Glyph> m_GlyphsToPack = new List<Glyph>();
  781. /// <summary>
  782. /// List of glyphs that have been packed in the atlas texture and ready to be rendered.
  783. /// </summary>
  784. private List<Glyph> m_GlyphsPacked = new List<Glyph>();
  785. /// <summary>
  786. ///
  787. /// </summary>
  788. private List<Glyph> m_GlyphsToRender = new List<Glyph>();
  789. /// <summary>
  790. /// List used in the process of adding new glyphs to the atlas texture.
  791. /// </summary>
  792. private List<uint> m_GlyphIndexList = new List<uint>();
  793. private List<TMP_Character> m_CharactersToAdd = new List<TMP_Character>();
  794. /// <summary>
  795. /// Internal static array used to avoid allocations when using the GetGlyphPairAdjustmentTable().
  796. /// </summary>
  797. internal static uint[] s_GlyphIndexArray = new uint[16];
  798. /// <summary>
  799. /// Internal static list used to track characters that could not be added to the font asset.
  800. /// </summary>
  801. internal static List<uint> s_MissingCharacterList = new List<uint>(16);
  802. /// <summary>
  803. /// Try adding the characters from the provided string to the font asset.
  804. /// </summary>
  805. /// <param name="unicodes">Array that contains the characters to add to the font asset.</param>
  806. /// <returns>Returns true if all the characters were successfully added to the font asset. Return false otherwise.</returns>
  807. public bool TryAddCharacters(uint[] unicodes)
  808. {
  809. uint[] missingUnicodes;
  810. return TryAddCharacters(unicodes, out missingUnicodes);
  811. }
  812. /// <summary>
  813. /// Try adding the characters from the provided string to the font asset.
  814. /// </summary>
  815. /// <param name="unicodes">Array that contains the characters to add to the font asset.</param>
  816. /// <param name="missingUnicodes">Array containing the characters that could not be added to the font asset.</param>
  817. /// <returns>Returns true if all the characters were successfully added to the font asset. Return false otherwise.</returns>
  818. public bool TryAddCharacters(uint[] unicodes, out uint[] missingUnicodes)
  819. {
  820. s_MissingCharacterList.Clear();
  821. // Make sure font asset is set to dynamic and that we have a valid list of characters.
  822. if (unicodes == null || unicodes.Length == 0 || m_AtlasPopulationMode == AtlasPopulationMode.Static)
  823. {
  824. if (m_AtlasPopulationMode == AtlasPopulationMode.Static)
  825. Debug.LogWarning("Unable to add characters to font asset [" + this.name + "] because its AtlasPopulationMode is set to Static.", this);
  826. else
  827. {
  828. Debug.LogWarning("Unable to add characters to font asset [" + this.name + "] because the provided Unicode list is Null or Empty.", this);
  829. }
  830. missingUnicodes = unicodes.ToArray();
  831. return false;
  832. }
  833. Profiler.BeginSample("TMP.TryAddCharacter");
  834. // Load font face.
  835. if (FontEngine.LoadFontFace(m_SourceFontFile, m_FaceInfo.pointSize) != FontEngineError.Success)
  836. {
  837. Profiler.EndSample();
  838. missingUnicodes = unicodes.ToArray();
  839. return false;
  840. }
  841. // Clear data structures used to track which glyph needs to be added to atlas texture.
  842. m_GlyphIndexList.Clear();
  843. m_CharactersToAdd.Clear();
  844. bool isMissingCharacters = false;
  845. int unicodeCount = unicodes.Length;
  846. for (int i = 0; i < unicodeCount; i++)
  847. {
  848. uint unicode = unicodes[i];
  849. // Check if character is already contained in the character table.
  850. if (m_CharacterLookupDictionary.ContainsKey(unicode))
  851. continue;
  852. // Get the index of the glyph for this unicode value.
  853. uint glyphIndex = FontEngine.GetGlyphIndex(unicode);
  854. if (glyphIndex == 0)
  855. {
  856. isMissingCharacters = true;
  857. continue;
  858. }
  859. TMP_Character character = new TMP_Character(unicode, glyphIndex);
  860. // Check if glyph is already contained in the font asset as the same glyph might be referenced by multiple characters.
  861. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex))
  862. {
  863. character.glyph = m_GlyphLookupDictionary[glyphIndex];
  864. m_CharacterTable.Add(character);
  865. m_CharacterLookupDictionary.Add(unicode, character);
  866. continue;
  867. }
  868. m_GlyphIndexList.Add(glyphIndex);
  869. m_CharactersToAdd.Add(character);
  870. }
  871. if (m_GlyphIndexList.Count == 0)
  872. {
  873. //Debug.LogWarning("No characters will be added to font asset [" + this.name + "] either because they are already present in the font asset or missing from the font file.");
  874. Profiler.EndSample();
  875. missingUnicodes = unicodes.ToArray();
  876. return false;
  877. }
  878. // Resize the Atlas Texture to the appropriate size
  879. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  880. {
  881. //Debug.Log("Setting initial size of atlas texture used by font asset [" + this.name + "].");
  882. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  883. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  884. }
  885. Glyph[] glyphs;
  886. bool allCharactersAdded = FontEngine.TryAddGlyphsToTexture(m_GlyphIndexList, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex], out glyphs);
  887. // Add new glyphs to relevant data structures.
  888. for (int i = 0; i < glyphs.Length; i++)
  889. {
  890. Glyph glyph = glyphs[i];
  891. uint glyphIndex = glyph.index;
  892. // Add new glyph to glyph table.
  893. m_GlyphTable.Add(glyph);
  894. m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  895. }
  896. // Add new characters to relevant data structures.
  897. for (int i = 0; i < m_CharactersToAdd.Count; i++)
  898. {
  899. TMP_Character character = m_CharactersToAdd[i];
  900. Glyph glyph;
  901. if (m_GlyphLookupDictionary.TryGetValue(character.glyphIndex, out glyph) == false)
  902. {
  903. s_MissingCharacterList.Add(character.unicode);
  904. continue;
  905. }
  906. character.glyph = glyph;
  907. m_CharacterTable.Add(character);
  908. m_CharacterLookupDictionary.Add(character.unicode, character);
  909. }
  910. #if UNITY_EDITOR
  911. // Makes the changes to the font asset persistent.
  912. if (UnityEditor.EditorUtility.IsPersistent(this))
  913. {
  914. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  915. }
  916. #endif
  917. Profiler.EndSample();
  918. missingUnicodes = null;
  919. if (s_MissingCharacterList.Count > 0)
  920. missingUnicodes = s_MissingCharacterList.ToArray();
  921. return allCharactersAdded && !isMissingCharacters;
  922. }
  923. /// <summary>
  924. /// Try adding the characters from the provided string to the font asset.
  925. /// </summary>
  926. /// <param name="characters">String containing the characters to add to the font asset.</param>
  927. /// <returns>Returns true if all the characters were successfully added to the font asset. Return false otherwise.</returns>
  928. public bool TryAddCharacters(string characters)
  929. {
  930. string missingCharacters;
  931. return TryAddCharacters(characters, out missingCharacters);
  932. }
  933. /// <summary>
  934. /// Try adding the characters from the provided string to the font asset.
  935. /// </summary>
  936. /// <param name="characters">String containing the characters to add to the font asset.</param>
  937. /// <param name="missingCharacters">String containing the characters that could not be added to the font asset.</param>
  938. /// <returns>Returns true if all the characters were successfully added to the font asset. Return false otherwise.</returns>
  939. public bool TryAddCharacters(string characters, out string missingCharacters)
  940. {
  941. // Make sure font asset is set to dynamic and that we have a valid list of characters.
  942. if (string.IsNullOrEmpty(characters) || m_AtlasPopulationMode == AtlasPopulationMode.Static)
  943. {
  944. if (m_AtlasPopulationMode == AtlasPopulationMode.Static)
  945. Debug.LogWarning("Unable to add characters to font asset [" + this.name + "] because its AtlasPopulationMode is set to Static.", this);
  946. else
  947. {
  948. Debug.LogWarning("Unable to add characters to font asset [" + this.name + "] because the provided character list is Null or Empty.", this);
  949. }
  950. missingCharacters = characters;
  951. return false;
  952. }
  953. // Load font face.
  954. if (FontEngine.LoadFontFace(m_SourceFontFile, m_FaceInfo.pointSize) != FontEngineError.Success)
  955. {
  956. missingCharacters = characters;
  957. return false;
  958. }
  959. // Clear data structures used to track which glyph needs to be added to atlas texture.
  960. m_GlyphIndexList.Clear();
  961. m_CharactersToAdd.Clear();
  962. bool isMissingCharacters = false;
  963. int characterCount = characters.Length;
  964. // Iterate over each of the requested characters.
  965. for (int i = 0; i < characterCount; i++)
  966. {
  967. uint unicode = characters[i];
  968. // Check if character is already contained in the character table.
  969. if (m_CharacterLookupDictionary.ContainsKey(unicode))
  970. continue;
  971. // Get the index of the glyph for this unicode value.
  972. uint glyphIndex = FontEngine.GetGlyphIndex(unicode);
  973. // Skip missing glyphs
  974. if (glyphIndex == 0)
  975. {
  976. // Might want to keep track and report the missing characters.
  977. isMissingCharacters = true;
  978. continue;
  979. }
  980. TMP_Character character = new TMP_Character(unicode, glyphIndex);
  981. // Check if glyph is already contained in the font asset as the same glyph might be referenced by multiple characters.
  982. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex))
  983. {
  984. character.glyph = m_GlyphLookupDictionary[glyphIndex];
  985. m_CharacterTable.Add(character);
  986. m_CharacterLookupDictionary.Add(unicode, character);
  987. continue;
  988. }
  989. // Add glyph to list of glyphs to add and glyph lookup map.
  990. m_GlyphIndexList.Add(glyphIndex);
  991. m_CharactersToAdd.Add(character);
  992. }
  993. if (m_GlyphIndexList.Count == 0)
  994. {
  995. missingCharacters = characters;
  996. return false;
  997. }
  998. // Resize the Atlas Texture to the appropriate size
  999. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1000. {
  1001. //Debug.Log("Setting initial size of atlas texture used by font asset [" + this.name + "].");
  1002. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1003. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1004. }
  1005. Glyph[] glyphs;
  1006. bool allCharactersAdded = FontEngine.TryAddGlyphsToTexture(m_GlyphIndexList, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex], out glyphs);
  1007. for (int i = 0; i < glyphs.Length; i++)
  1008. {
  1009. Glyph glyph = glyphs[i];
  1010. uint glyphIndex = glyph.index;
  1011. // Add new glyph to glyph table.
  1012. m_GlyphTable.Add(glyph);
  1013. m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  1014. }
  1015. missingCharacters = string.Empty;
  1016. // Add new characters to relevant data structures.
  1017. for (int i = 0; i < m_CharactersToAdd.Count; i++)
  1018. {
  1019. TMP_Character character = m_CharactersToAdd[i];
  1020. Glyph glyph;
  1021. if (m_GlyphLookupDictionary.TryGetValue(character.glyphIndex, out glyph) == false)
  1022. {
  1023. // TODO: Revise to avoid string concatenation.
  1024. missingCharacters += (char)character.unicode;
  1025. continue;
  1026. }
  1027. character.glyph = glyph;
  1028. m_CharacterTable.Add(character);
  1029. m_CharacterLookupDictionary.Add(character.unicode, character);
  1030. }
  1031. #if UNITY_EDITOR
  1032. // Makes the changes to the font asset persistent.
  1033. if (UnityEditor.EditorUtility.IsPersistent(this))
  1034. {
  1035. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1036. }
  1037. #endif
  1038. return allCharactersAdded && !isMissingCharacters;
  1039. }
  1040. /// <summary>
  1041. /// NOT USED CURRENTLY - Try adding character using Unicode value to font asset.
  1042. /// </summary>
  1043. /// <param name="unicode">The Unicode value of the character.</param>
  1044. /// <param name="character">The character data if successfully added to the font asset. Null otherwise.</param>
  1045. /// <returns>Returns true if the character has been added. False otherwise.</returns>
  1046. internal bool TryAddCharacter_Internal(uint unicode)
  1047. {
  1048. TMP_Character character = null;
  1049. // Check if character is already contained in the character table.
  1050. if (m_CharacterLookupDictionary.ContainsKey(unicode))
  1051. return true;
  1052. uint glyphIndex = FontEngine.GetGlyphIndex(unicode);
  1053. if (glyphIndex == 0)
  1054. return false;
  1055. // Check if glyph is already contained in the font asset as the same glyph might be referenced by multiple characters.
  1056. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex))
  1057. {
  1058. character = new TMP_Character(unicode, m_GlyphLookupDictionary[glyphIndex]);
  1059. m_CharacterTable.Add(character);
  1060. m_CharacterLookupDictionary.Add(unicode, character);
  1061. //#if UNITY_EDITOR
  1062. // Makes the changes to the font asset persistent.
  1063. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1064. // Could also add some update registry to handle this.
  1065. //SortGlyphTable();
  1066. //UnityEditor.EditorUtility.SetDirty(this);
  1067. //#endif
  1068. return true;
  1069. }
  1070. // Resize the Atlas Texture to the appropriate size
  1071. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1072. {
  1073. //Debug.Log("Setting initial size of atlas texture used by font asset [" + this.name + "].");
  1074. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1075. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1076. }
  1077. Glyph glyph;
  1078. if (FontEngine.TryAddGlyphToTexture(glyphIndex, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex], out glyph))
  1079. {
  1080. // Add new glyph to glyph table.
  1081. m_GlyphTable.Add(glyph);
  1082. m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  1083. // Add new character
  1084. character = new TMP_Character(unicode, glyph);
  1085. m_CharacterTable.Add(character);
  1086. m_CharacterLookupDictionary.Add(unicode, character);
  1087. //#if UNITY_EDITOR
  1088. // Makes the changes to the font asset persistent.
  1089. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1090. // Could also add some update registry to handle this.
  1091. //SortGlyphTable();
  1092. //UnityEditor.EditorUtility.SetDirty(this);
  1093. //#endif
  1094. return true;
  1095. }
  1096. return false;
  1097. }
  1098. /// <summary>
  1099. /// To be removed.
  1100. /// </summary>
  1101. /// <param name="unicode"></param>
  1102. /// <param name="glyph"></param>
  1103. internal TMP_Character AddCharacter_Internal(uint unicode, Glyph glyph)
  1104. {
  1105. // Check if character is already contained in the character table.
  1106. if (m_CharacterLookupDictionary.ContainsKey(unicode))
  1107. return m_CharacterLookupDictionary[unicode];
  1108. uint glyphIndex = glyph.index;
  1109. // Resize the Atlas Texture to the appropriate size
  1110. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1111. {
  1112. //Debug.Log("Setting initial size of atlas texture used by font asset [" + this.name + "].");
  1113. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1114. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1115. }
  1116. // Check if glyph is already contained in the glyph table.
  1117. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex) == false)
  1118. {
  1119. if (glyph.glyphRect.width == 0 || glyph.glyphRect.width == 0)
  1120. {
  1121. // Glyphs with zero width and / or height can be automatically added to font asset.
  1122. m_GlyphTable.Add(glyph);
  1123. }
  1124. else
  1125. {
  1126. // Try packing new glyph
  1127. if (FontEngine.TryPackGlyphInAtlas(glyph, m_AtlasPadding, GlyphPackingMode.ContactPointRule, m_AtlasRenderMode, m_AtlasWidth, m_AtlasHeight, m_FreeGlyphRects, m_UsedGlyphRects) == false)
  1128. {
  1129. // TODO: Add handling to create new atlas texture to fit glyph.
  1130. return null;
  1131. }
  1132. m_GlyphsToRender.Add(glyph);
  1133. }
  1134. }
  1135. // Add character to font asset.
  1136. TMP_Character character = new TMP_Character(unicode, glyph);
  1137. m_CharacterTable.Add(character);
  1138. m_CharacterLookupDictionary.Add(unicode, character);
  1139. //Debug.Log("Adding character [" + (char)unicode + "] with Unicode (" + unicode + ") to [" + this.name + "] font asset.");
  1140. // Schedule glyph to be added to the font atlas texture
  1141. //TM_FontAssetUpdateManager.RegisterFontAssetForUpdate(this);
  1142. UpdateAtlasTexture(); // Temporary until callback system is revised.
  1143. //#if UNITY_EDITOR
  1144. // Makes the changes to the font asset persistent.
  1145. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1146. // Could also add some update registry to handle this.
  1147. //SortGlyphTable();
  1148. //UnityEditor.EditorUtility.SetDirty(this);
  1149. //#endif
  1150. return character;
  1151. }
  1152. /// <summary>
  1153. /// Try adding character using Unicode value to font asset.
  1154. /// Function assumes internal user has already checked to make sure the character is not already contained in the font asset.
  1155. /// </summary>
  1156. /// <param name="unicode">The Unicode value of the character.</param>
  1157. /// <param name="character">The character data if successfully added to the font asset. Null otherwise.</param>
  1158. /// <returns>Returns true if the character has been added. False otherwise.</returns>
  1159. internal bool TryAddCharacterInternal(uint unicode, out TMP_Character character)
  1160. {
  1161. character = null;
  1162. // Load font face.
  1163. if (FontEngine.LoadFontFace(sourceFontFile, m_FaceInfo.pointSize) != FontEngineError.Success)
  1164. return false;
  1165. uint glyphIndex = FontEngine.GetGlyphIndex(unicode);
  1166. if (glyphIndex == 0)
  1167. return false;
  1168. Profiler.BeginSample("TMP.TryAddCharacter");
  1169. // Check if glyph is already contained in the font asset as the same glyph might be referenced by multiple characters.
  1170. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex))
  1171. {
  1172. character = new TMP_Character(unicode, m_GlyphLookupDictionary[glyphIndex]);
  1173. m_CharacterTable.Add(character);
  1174. m_CharacterLookupDictionary.Add(unicode, character);
  1175. if (TMP_Settings.getFontFeaturesAtRuntime)
  1176. UpdateGlyphAdjustmentRecords(unicode, glyphIndex);
  1177. #if UNITY_EDITOR
  1178. // Makes the changes to the font asset persistent.
  1179. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1180. // Could also add some update registry to handle this.
  1181. //SortGlyphTable();
  1182. if (UnityEditor.EditorUtility.IsPersistent(this))
  1183. {
  1184. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1185. }
  1186. #endif
  1187. Profiler.EndSample();
  1188. return true;
  1189. }
  1190. // Resize the Atlas Texture to the appropriate size
  1191. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1192. {
  1193. // TODO: Need texture to be readable.
  1194. if (m_AtlasTextures[m_AtlasTextureIndex].isReadable == false)
  1195. {
  1196. Debug.LogWarning("Unable to add the requested character to font asset [" + this.name + "]'s atlas texture. Please make the texture [" + m_AtlasTextures[m_AtlasTextureIndex].name + "] readable.", m_AtlasTextures[m_AtlasTextureIndex]);
  1197. Profiler.EndSample();
  1198. return false;
  1199. }
  1200. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1201. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1202. }
  1203. Glyph glyph;
  1204. if (FontEngine.TryAddGlyphToTexture(glyphIndex, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex], out glyph))
  1205. {
  1206. // Add new glyph to glyph table.
  1207. m_GlyphTable.Add(glyph);
  1208. m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  1209. // Add new character
  1210. character = new TMP_Character(unicode, glyph);
  1211. m_CharacterTable.Add(character);
  1212. m_CharacterLookupDictionary.Add(unicode, character);
  1213. m_GlyphIndexList.Add(glyphIndex);
  1214. if (TMP_Settings.getFontFeaturesAtRuntime)
  1215. UpdateGlyphAdjustmentRecords(unicode, glyphIndex);
  1216. #if UNITY_EDITOR
  1217. // Makes the changes to the font asset persistent.
  1218. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1219. // Could also add some update registry to handle this.
  1220. //SortGlyphTable();
  1221. if (UnityEditor.EditorUtility.IsPersistent(this))
  1222. {
  1223. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1224. }
  1225. #endif
  1226. Profiler.EndSample();
  1227. return true;
  1228. }
  1229. Profiler.EndSample();
  1230. return false;
  1231. }
  1232. /// <summary>
  1233. /// Internal function used to get the glyph index for the given unicode.
  1234. /// </summary>
  1235. /// <param name="unicode"></param>
  1236. /// <returns></returns>
  1237. internal uint GetGlyphIndex(uint unicode)
  1238. {
  1239. // Load font face.
  1240. if (FontEngine.LoadFontFace(sourceFontFile, m_FaceInfo.pointSize) != FontEngineError.Success)
  1241. return 0;
  1242. return FontEngine.GetGlyphIndex(unicode);
  1243. }
  1244. internal void UpdateAtlasTexture()
  1245. {
  1246. // Return if we don't have any glyphs to add to atlas texture.
  1247. // This is possible if UpdateAtlasTexture() was called manually.
  1248. //if (m_GlyphsToPack.Count == 0)
  1249. // return;
  1250. if (m_GlyphsToRender.Count == 0)
  1251. return;
  1252. //Debug.Log("Updating [" + this.name + "]'s atlas texture.");
  1253. // Pack glyphs in the given atlas texture size.
  1254. // TODO: Packing and glyph render modes should be defined in the font asset.
  1255. //FontEngine.PackGlyphsInAtlas(m_GlyphsToPack, m_GlyphsPacked, m_AtlasPadding, GlyphPackingMode.ContactPointRule, GlyphRenderMode.SDFAA, m_AtlasWidth, m_AtlasHeight, m_FreeGlyphRects, m_UsedGlyphRects);
  1256. //FontEngine.RenderGlyphsToTexture(m_GlyphsPacked, m_AtlasPadding, GlyphRenderMode.SDFAA, m_AtlasTextures[m_AtlasTextureIndex]);
  1257. // Resize the Atlas Texture to the appropriate size
  1258. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1259. {
  1260. //Debug.Log("Setting initial size of atlas texture used by font asset [" + this.name + "].");
  1261. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1262. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1263. }
  1264. FontEngine.RenderGlyphsToTexture(m_GlyphsToRender, m_AtlasPadding, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex]);
  1265. // Apply changes to atlas texture
  1266. m_AtlasTextures[m_AtlasTextureIndex].Apply(false, false);
  1267. // Add glyphs that were successfully packed to the glyph table.
  1268. for (int i = 0; i < m_GlyphsToRender.Count /* m_GlyphsPacked.Count */; i++)
  1269. {
  1270. Glyph glyph = m_GlyphsToRender[i]; // m_GlyphsPacked[i];
  1271. // Update atlas texture index
  1272. glyph.atlasIndex = m_AtlasTextureIndex;
  1273. m_GlyphTable.Add(glyph);
  1274. m_GlyphLookupDictionary.Add(glyph.index, glyph);
  1275. }
  1276. // Clear list of glyphs
  1277. m_GlyphsPacked.Clear();
  1278. m_GlyphsToRender.Clear();
  1279. // Add any remaining glyphs into new atlas texture if multi texture support if enabled.
  1280. if (m_GlyphsToPack.Count > 0)
  1281. {
  1282. /*
  1283. // Create new atlas texture
  1284. Texture2D tex = new Texture2D(m_AtlasWidth, m_AtlasHeight, TextureFormat.Alpha8, false, true);
  1285. tex.SetPixels32(new Color32[m_AtlasWidth * m_AtlasHeight]);
  1286. tex.Apply();
  1287. m_AtlasTextureIndex++;
  1288. if (m_AtlasTextures.Length == m_AtlasTextureIndex)
  1289. Array.Resize(ref m_AtlasTextures, Mathf.NextPowerOfTwo(m_AtlasTextureIndex + 1));
  1290. m_AtlasTextures[m_AtlasTextureIndex] = tex;
  1291. */
  1292. }
  1293. #if UNITY_EDITOR
  1294. // Makes the changes to the font asset persistent.
  1295. SortGlyphAndCharacterTables();
  1296. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1297. #endif
  1298. }
  1299. internal void UpdateGlyphAdjustmentRecords(uint unicode, uint glyphIndex)
  1300. {
  1301. Profiler.BeginSample("TMP.UpdateGlyphAdjustmentRecords");
  1302. int glyphCount = m_GlyphIndexList.Count;
  1303. if (s_GlyphIndexArray.Length <= glyphCount)
  1304. s_GlyphIndexArray = new uint[Mathf.NextPowerOfTwo(glyphCount + 1)];
  1305. for (int i = 0; i < glyphCount; i++)
  1306. s_GlyphIndexArray[i] = m_GlyphIndexList[i];
  1307. // Clear unused array elements
  1308. Array.Clear(s_GlyphIndexArray, glyphCount, s_GlyphIndexArray.Length - glyphCount);
  1309. // Get glyph pair adjustment records from font file.
  1310. // TODO: Revise FontEngine bindings to use a more efficient function where only the new glyph index is passed.
  1311. GlyphPairAdjustmentRecord[] pairAdjustmentRecords = FontEngine.GetGlyphPairAdjustmentTable(s_GlyphIndexArray);
  1312. if (pairAdjustmentRecords == null || pairAdjustmentRecords.Length == 0)
  1313. {
  1314. Profiler.EndSample();
  1315. return;
  1316. }
  1317. if (m_FontFeatureTable == null)
  1318. m_FontFeatureTable = new TMP_FontFeatureTable();
  1319. for (int i = 0; i < pairAdjustmentRecords.Length && pairAdjustmentRecords[i].firstAdjustmentRecord.glyphIndex != 0; i++)
  1320. {
  1321. long pairKey = (long)pairAdjustmentRecords[i].secondAdjustmentRecord.glyphIndex << 32 | pairAdjustmentRecords[i].firstAdjustmentRecord.glyphIndex;
  1322. // Check if table already contains a pair adjustment record for this key.
  1323. if (m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.ContainsKey(pairKey))
  1324. continue;
  1325. TMP_GlyphPairAdjustmentRecord record = new TMP_GlyphPairAdjustmentRecord(pairAdjustmentRecords[i]);
  1326. m_FontFeatureTable.m_GlyphPairAdjustmentRecords.Add(record);
  1327. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Add(pairKey, record);
  1328. }
  1329. #if UNITY_EDITOR
  1330. m_FontFeatureTable.SortGlyphPairAdjustmentRecords();
  1331. #endif
  1332. Profiler.EndSample();
  1333. }
  1334. /// <summary>
  1335. /// Clears font asset data including the glyph and character tables and textures.
  1336. /// Function might be changed to Internal and only used in tests.
  1337. /// </summary>
  1338. /// <param name="setAtlasSizeToZero">Will set the atlas texture size to zero width and height if true.</param>
  1339. public void ClearFontAssetData(bool setAtlasSizeToZero = false)
  1340. {
  1341. #if UNITY_EDITOR
  1342. // Record full object undo in the Editor.
  1343. //UnityEditor.Undo.RecordObjects(new UnityEngine.Object[] { this, this.atlasTexture }, "Resetting Font Asset");
  1344. #endif
  1345. // Clear glyph and character tables
  1346. if (m_GlyphTable != null)
  1347. m_GlyphTable.Clear();
  1348. if (m_CharacterTable != null)
  1349. m_CharacterTable.Clear();
  1350. // Clear glyph rectangles
  1351. if (m_UsedGlyphRects != null)
  1352. m_UsedGlyphRects.Clear();
  1353. if (m_FreeGlyphRects != null)
  1354. {
  1355. int packingModifier = ((GlyphRasterModes)m_AtlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP ? 0 : 1;
  1356. m_FreeGlyphRects = new List<GlyphRect>() { new GlyphRect(0, 0, m_AtlasWidth - packingModifier, m_AtlasHeight - packingModifier) };
  1357. }
  1358. if (m_GlyphsToPack != null)
  1359. m_GlyphsToPack.Clear();
  1360. if (m_GlyphsPacked != null)
  1361. m_GlyphsPacked.Clear();
  1362. // Clear Glyph Adjustment Table
  1363. if (m_FontFeatureTable != null && m_FontFeatureTable.m_GlyphPairAdjustmentRecords != null)
  1364. m_FontFeatureTable.glyphPairAdjustmentRecords.Clear();
  1365. m_AtlasTextureIndex = 0;
  1366. // Clear atlas textures
  1367. if (m_AtlasTextures != null)
  1368. {
  1369. for (int i = 0; i < m_AtlasTextures.Length; i++)
  1370. {
  1371. Texture2D texture = m_AtlasTextures[i];
  1372. if (i > 0)
  1373. DestroyImmediate(texture, true);
  1374. if (texture == null)
  1375. continue;
  1376. // TODO: Need texture to be readable.
  1377. if (m_AtlasTextures[i].isReadable == false)
  1378. {
  1379. Debug.LogWarning("Unable to reset font asset [" + this.name + "]'s atlas texture. Please make the texture [" + m_AtlasTextures[i].name + "] readable.", m_AtlasTextures[i]);
  1380. continue;
  1381. }
  1382. if (setAtlasSizeToZero)
  1383. {
  1384. texture.Resize(0, 0, TextureFormat.Alpha8, false);
  1385. }
  1386. else if (texture.width != m_AtlasWidth || texture.height != m_AtlasHeight)
  1387. {
  1388. texture.Resize(m_AtlasWidth, m_AtlasHeight, TextureFormat.Alpha8, false);
  1389. }
  1390. // Clear texture atlas
  1391. FontEngine.ResetAtlasTexture(texture);
  1392. texture.Apply();
  1393. if (i == 0)
  1394. m_AtlasTexture = texture;
  1395. m_AtlasTextures[i] = texture;
  1396. }
  1397. }
  1398. #if UNITY_EDITOR
  1399. if (UnityEditor.EditorUtility.IsPersistent(this))
  1400. {
  1401. TMP_EditorResourceManager.RegisterResourceForReimport(this);
  1402. }
  1403. #endif
  1404. ReadFontAssetDefinition();
  1405. }
  1406. /// <summary>
  1407. /// Internal method used to upgrade font asset to support Dynamic SDF.
  1408. /// </summary>
  1409. private void UpgradeFontAsset()
  1410. {
  1411. m_Version = "1.1.0";
  1412. Debug.Log("Upgrading font asset [" + this.name + "] to version " + m_Version + ".", this);
  1413. m_FaceInfo.familyName = m_fontInfo.Name;
  1414. m_FaceInfo.styleName = string.Empty;
  1415. m_FaceInfo.pointSize = (int)m_fontInfo.PointSize;
  1416. m_FaceInfo.scale = m_fontInfo.Scale;
  1417. m_FaceInfo.lineHeight = m_fontInfo.LineHeight;
  1418. m_FaceInfo.ascentLine = m_fontInfo.Ascender;
  1419. m_FaceInfo.capLine = m_fontInfo.CapHeight;
  1420. m_FaceInfo.meanLine = m_fontInfo.CenterLine;
  1421. m_FaceInfo.baseline = m_fontInfo.Baseline;
  1422. m_FaceInfo.descentLine = m_fontInfo.Descender;
  1423. m_FaceInfo.superscriptOffset = m_fontInfo.SuperscriptOffset;
  1424. m_FaceInfo.superscriptSize = m_fontInfo.SubSize;
  1425. m_FaceInfo.subscriptOffset = m_fontInfo.SubscriptOffset;
  1426. m_FaceInfo.subscriptSize = m_fontInfo.SubSize;
  1427. m_FaceInfo.underlineOffset = m_fontInfo.Underline;
  1428. m_FaceInfo.underlineThickness = m_fontInfo.UnderlineThickness;
  1429. m_FaceInfo.strikethroughOffset = m_fontInfo.strikethrough;
  1430. m_FaceInfo.strikethroughThickness = m_fontInfo.strikethroughThickness;
  1431. m_FaceInfo.tabWidth = m_fontInfo.TabWidth;
  1432. if (m_AtlasTextures == null || m_AtlasTextures.Length == 0)
  1433. m_AtlasTextures = new Texture2D[1];
  1434. m_AtlasTextures[0] = atlas;
  1435. //atlas = null;
  1436. m_AtlasWidth = (int)m_fontInfo.AtlasWidth;
  1437. m_AtlasHeight = (int)m_fontInfo.AtlasHeight;
  1438. m_AtlasPadding = (int)m_fontInfo.Padding;
  1439. switch(m_CreationSettings.renderMode)
  1440. {
  1441. case 0:
  1442. m_AtlasRenderMode = GlyphRenderMode.SMOOTH_HINTED;
  1443. break;
  1444. case 1:
  1445. m_AtlasRenderMode = GlyphRenderMode.SMOOTH;
  1446. break;
  1447. case 2:
  1448. m_AtlasRenderMode = GlyphRenderMode.RASTER_HINTED;
  1449. break;
  1450. case 3:
  1451. m_AtlasRenderMode = GlyphRenderMode.RASTER;
  1452. break;
  1453. case 6:
  1454. m_AtlasRenderMode = GlyphRenderMode.SDF16;
  1455. break;
  1456. case 7:
  1457. m_AtlasRenderMode = GlyphRenderMode.SDF32;
  1458. break;
  1459. }
  1460. //m_fontInfo = null;
  1461. // Convert font weight table
  1462. if (fontWeights != null)
  1463. {
  1464. m_FontWeightTable[4] = fontWeights[4];
  1465. m_FontWeightTable[7] = fontWeights[7];
  1466. // Clear old fontWeight
  1467. //fontWeights = null;
  1468. }
  1469. // Convert font fallbacks
  1470. if (fallbackFontAssets != null && fallbackFontAssets.Count > 0)
  1471. {
  1472. if (m_FallbackFontAssetTable == null)
  1473. m_FallbackFontAssetTable = new List<TMP_FontAsset>(fallbackFontAssets.Count);
  1474. for (int i = 0; i < fallbackFontAssets.Count; i++)
  1475. m_FallbackFontAssetTable.Add(fallbackFontAssets[i]);
  1476. // Clear old fallbackFontAssets list
  1477. //fallbackFontAssets = null;
  1478. }
  1479. // Check if font asset creation settings contains a reference to the source font file GUID
  1480. if (m_CreationSettings.sourceFontFileGUID != null || m_CreationSettings.sourceFontFileGUID != string.Empty)
  1481. {
  1482. m_SourceFontFileGUID = m_CreationSettings.sourceFontFileGUID;
  1483. }
  1484. else
  1485. {
  1486. Debug.LogWarning("Font asset [" + this.name + "] doesn't have a reference to its source font file. Please assign the appropriate source font file for this asset in the Font Atlas & Material section of font asset inspector.", this);
  1487. }
  1488. // Convert legacy glyph and character tables to new format
  1489. m_GlyphTable.Clear();
  1490. m_CharacterTable.Clear();
  1491. //#if UNITY_EDITOR
  1492. // TODO: This is causing a crash in Unity and related to AssetDatabase.LoadAssetAtPath and Resources.Load()
  1493. // Load font to allow us to get the glyph index.
  1494. //string path = UnityEditor.AssetDatabase.GUIDToAssetPath(m_SourceFontFileGUID);
  1495. //if (path != string.Empty)
  1496. //{
  1497. //m_SourceFontFile_EditorRef = UnityEditor.AssetDatabase.LoadAssetAtPath<Font>(path);
  1498. //FontEngine.LoadFontFace(m_SourceFontFile_EditorRef);
  1499. //}
  1500. //#endif
  1501. bool isSpaceCharacterPresent = false;
  1502. for (int i = 0; i < m_glyphInfoList.Count; i++)
  1503. {
  1504. TMP_Glyph oldGlyph = m_glyphInfoList[i];
  1505. Glyph glyph = new Glyph();
  1506. uint glyphIndex = (uint)i + 1;
  1507. //#if UNITY_EDITOR
  1508. //if (m_SourceFontFile_EditorRef != null)
  1509. // glyphIndex = FontEngine.GetGlyphIndex((uint)oldGlyph.id);
  1510. //#endif
  1511. glyph.index = glyphIndex;
  1512. glyph.glyphRect = new GlyphRect((int)oldGlyph.x, m_AtlasHeight - (int)(oldGlyph.y + oldGlyph.height + 0.5f), (int)(oldGlyph.width + 0.5f), (int)(oldGlyph.height + 0.5f));
  1513. glyph.metrics = new GlyphMetrics(oldGlyph.width, oldGlyph.height, oldGlyph.xOffset, oldGlyph.yOffset, oldGlyph.xAdvance);
  1514. glyph.scale = oldGlyph.scale;
  1515. glyph.atlasIndex = 0;
  1516. m_GlyphTable.Add(glyph);
  1517. TMP_Character character = new TMP_Character((uint)oldGlyph.id, glyph);
  1518. if (oldGlyph.id == 32)
  1519. isSpaceCharacterPresent = true;
  1520. m_CharacterTable.Add(character);
  1521. }
  1522. // Special handling for the synthesized space character
  1523. if (!isSpaceCharacterPresent)
  1524. {
  1525. Debug.Log("Synthesizing Space for [" + this.name + "]");
  1526. Glyph glyph = new Glyph(0, new GlyphMetrics(0, 0, 0, 0, m_FaceInfo.ascentLine / 5), GlyphRect.zero, 1.0f, 0);
  1527. m_GlyphTable.Add(glyph);
  1528. m_CharacterTable.Add(new TMP_Character(32, glyph));
  1529. }
  1530. // Clear legacy glyph info list.
  1531. //m_glyphInfoList.Clear();
  1532. ReadFontAssetDefinition();
  1533. // Convert atlas textures data to new format
  1534. // TODO
  1535. #if UNITY_EDITOR
  1536. if (UnityEditor.EditorUtility.IsPersistent(this))
  1537. {
  1538. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1539. }
  1540. #endif
  1541. }
  1542. /// <summary>
  1543. ///
  1544. /// </summary>
  1545. void UpgradeGlyphAdjustmentTableToFontFeatureTable()
  1546. {
  1547. Debug.Log("Upgrading font asset [" + this.name + "] Glyph Adjustment Table.", this);
  1548. if (m_FontFeatureTable == null)
  1549. m_FontFeatureTable = new TMP_FontFeatureTable();
  1550. int pairCount = m_KerningTable.kerningPairs.Count;
  1551. m_FontFeatureTable.m_GlyphPairAdjustmentRecords = new List<TMP_GlyphPairAdjustmentRecord>(pairCount);
  1552. for (int i = 0; i < pairCount; i++)
  1553. {
  1554. KerningPair pair = m_KerningTable.kerningPairs[i];
  1555. uint firstGlyphIndex = 0;
  1556. TMP_Character firstCharacter;
  1557. if (m_CharacterLookupDictionary.TryGetValue(pair.firstGlyph, out firstCharacter))
  1558. firstGlyphIndex = firstCharacter.glyphIndex;
  1559. uint secondGlyphIndex = 0;
  1560. TMP_Character secondCharacter;
  1561. if (m_CharacterLookupDictionary.TryGetValue(pair.secondGlyph, out secondCharacter))
  1562. secondGlyphIndex = secondCharacter.glyphIndex;
  1563. TMP_GlyphAdjustmentRecord firstAdjustmentRecord = new TMP_GlyphAdjustmentRecord(firstGlyphIndex, new TMP_GlyphValueRecord(pair.firstGlyphAdjustments.xPlacement, pair.firstGlyphAdjustments.yPlacement, pair.firstGlyphAdjustments.xAdvance, pair.firstGlyphAdjustments.yAdvance));
  1564. TMP_GlyphAdjustmentRecord secondAdjustmentRecord = new TMP_GlyphAdjustmentRecord(secondGlyphIndex, new TMP_GlyphValueRecord(pair.secondGlyphAdjustments.xPlacement, pair.secondGlyphAdjustments.yPlacement, pair.secondGlyphAdjustments.xAdvance, pair.secondGlyphAdjustments.yAdvance));
  1565. TMP_GlyphPairAdjustmentRecord record = new TMP_GlyphPairAdjustmentRecord(firstAdjustmentRecord, secondAdjustmentRecord);
  1566. m_FontFeatureTable.m_GlyphPairAdjustmentRecords.Add(record);
  1567. }
  1568. // TODO: Should clear legacy kerning table.
  1569. m_KerningTable.kerningPairs = null;
  1570. m_KerningTable = null;
  1571. #if UNITY_EDITOR
  1572. if (UnityEditor.EditorUtility.IsPersistent(this))
  1573. {
  1574. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1575. }
  1576. #endif
  1577. }
  1578. }
  1579. }