TMP_FontAsset.cs 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060
  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.Generic;
  8. using System.Linq;
  9. #if UNITY_EDITOR
  10. using UnityEditor.TextCore.LowLevel;
  11. #endif
  12. namespace TMPro
  13. {
  14. public enum AtlasPopulationMode
  15. {
  16. Static = 0x0,
  17. Dynamic = 0x1,
  18. }
  19. [Serializable][ExcludeFromPresetAttribute]
  20. public class TMP_FontAsset : TMP_Asset
  21. {
  22. /// <summary>
  23. /// The version of the font asset class.
  24. /// Version 1.1.0 adds support for the new TextCore.FontEngine and Dynamic SDF system.
  25. /// </summary>
  26. public string version
  27. {
  28. get { return m_Version; }
  29. internal set { m_Version = value; }
  30. }
  31. [SerializeField]
  32. private string m_Version;
  33. /// <summary>
  34. /// This field is set when the font asset is first created.
  35. /// </summary>
  36. [SerializeField]
  37. internal string m_SourceFontFileGUID;
  38. #if UNITY_EDITOR
  39. /// <summary>
  40. /// Persistent reference to the source font file maintained in the editor.
  41. /// </summary>
  42. [SerializeField]
  43. internal Font m_SourceFontFile_EditorRef;
  44. #endif
  45. /// <summary>
  46. /// Source font file when atlas population mode is set to dynamic. Null when the atlas population mode is set to static.
  47. /// </summary>
  48. public Font sourceFontFile
  49. {
  50. get { return m_SourceFontFile; }
  51. internal set { m_SourceFontFile = value; }
  52. }
  53. [SerializeField]
  54. private Font m_SourceFontFile;
  55. public AtlasPopulationMode atlasPopulationMode
  56. {
  57. get { return m_AtlasPopulationMode; }
  58. set
  59. {
  60. m_AtlasPopulationMode = value;
  61. #if UNITY_EDITOR
  62. if (m_AtlasPopulationMode == AtlasPopulationMode.Static)
  63. m_SourceFontFile = null;
  64. else if (m_AtlasPopulationMode == AtlasPopulationMode.Dynamic)
  65. m_SourceFontFile = m_SourceFontFile_EditorRef;
  66. #endif
  67. }
  68. }
  69. [SerializeField]
  70. private AtlasPopulationMode m_AtlasPopulationMode;
  71. /// <summary>
  72. /// Information about the font's face.
  73. /// </summary>
  74. public FaceInfo faceInfo
  75. {
  76. get { return m_FaceInfo; }
  77. set { m_FaceInfo = value; }
  78. }
  79. [SerializeField]
  80. internal FaceInfo m_FaceInfo;
  81. /// <summary>
  82. /// List of glyphs contained in the font asset.
  83. /// </summary>
  84. public List<Glyph> glyphTable
  85. {
  86. get { return m_GlyphTable; }
  87. internal set { m_GlyphTable = value; }
  88. }
  89. [SerializeField]
  90. internal List<Glyph> m_GlyphTable = new List<Glyph>();
  91. /// <summary>
  92. /// Dictionary used to lookup glyphs contained in the font asset by their index.
  93. /// </summary>
  94. public Dictionary<uint, Glyph> glyphLookupTable
  95. {
  96. get
  97. {
  98. if (m_GlyphLookupDictionary == null)
  99. ReadFontAssetDefinition();
  100. return m_GlyphLookupDictionary;
  101. }
  102. }
  103. internal Dictionary<uint, Glyph> m_GlyphLookupDictionary;
  104. /// <summary>
  105. /// List containing the characters of the given font asset.
  106. /// </summary>
  107. public List<TMP_Character> characterTable
  108. {
  109. get { return m_CharacterTable; }
  110. internal set { m_CharacterTable = value; }
  111. }
  112. [SerializeField]
  113. internal List<TMP_Character> m_CharacterTable = new List<TMP_Character>();
  114. /// <summary>
  115. /// Dictionary used to lookup characters contained in the font asset by their unicode values.
  116. /// </summary>
  117. public Dictionary<uint, TMP_Character> characterLookupTable
  118. {
  119. get
  120. {
  121. if (m_CharacterLookupDictionary == null)
  122. ReadFontAssetDefinition();
  123. return m_CharacterLookupDictionary;
  124. }
  125. }
  126. internal Dictionary<uint, TMP_Character> m_CharacterLookupDictionary;
  127. /// <summary>
  128. /// Determines if the font asset is using a shared atlas texture(s)
  129. /// </summary>
  130. //public bool isUsingDynamicTextures
  131. //{
  132. // get { return m_IsUsingDynamicTextures; }
  133. // set { m_IsUsingDynamicTextures = value; }
  134. //}
  135. //[SerializeField]
  136. //private bool m_IsUsingDynamicTextures;
  137. /// <summary>
  138. /// The font atlas used by this font asset.
  139. /// This is always the texture at index [0] of the fontAtlasTextures.
  140. /// </summary>
  141. public Texture2D atlasTexture
  142. {
  143. get
  144. {
  145. if (m_AtlasTexture == null)
  146. {
  147. m_AtlasTexture = atlasTextures[0];
  148. }
  149. return m_AtlasTexture;
  150. }
  151. }
  152. internal Texture2D m_AtlasTexture;
  153. /// <summary>
  154. /// Array of atlas textures that contain the glyphs used by this font asset.
  155. /// </summary>
  156. public Texture2D[] atlasTextures
  157. {
  158. get
  159. {
  160. if (m_AtlasTextures == null)
  161. {
  162. //
  163. }
  164. //if (m_IsUsingDynamicTextures)
  165. // return TMP_DynamicAtlasTextureGroup.managedDynamicTextures;
  166. return m_AtlasTextures;
  167. }
  168. set
  169. {
  170. m_AtlasTextures = value;
  171. }
  172. }
  173. [SerializeField]
  174. internal Texture2D[] m_AtlasTextures;
  175. /// <summary>
  176. /// Index of the font atlas texture that still has available space to add new glyphs.
  177. /// </summary>
  178. [SerializeField]
  179. internal int m_AtlasTextureIndex;
  180. /// <summary>
  181. /// Number of atlas textures used by this font asset.
  182. /// </summary>
  183. public int atlasTextureCount { get { return m_AtlasTextureIndex + 1; } }
  184. /// <summary>
  185. ///
  186. /// </summary>
  187. public bool isMultiAtlasTexturesEnabled
  188. {
  189. get { return m_IsMultiAtlasTexturesEnabled; }
  190. set { m_IsMultiAtlasTexturesEnabled = value; }
  191. }
  192. [SerializeField]
  193. private bool m_IsMultiAtlasTexturesEnabled;
  194. /// <summary>
  195. /// List of spaces occupied by glyphs in a given texture.
  196. /// </summary>
  197. internal List<GlyphRect> usedGlyphRects
  198. {
  199. get { return m_UsedGlyphRects; }
  200. set { m_UsedGlyphRects = value; }
  201. }
  202. [SerializeField]
  203. private List<GlyphRect> m_UsedGlyphRects;
  204. /// <summary>
  205. /// List of spaces available in a given texture to add new glyphs.
  206. /// </summary>
  207. internal List<GlyphRect> freeGlyphRects
  208. {
  209. get { return m_FreeGlyphRects; }
  210. set { m_FreeGlyphRects = value; }
  211. }
  212. [SerializeField]
  213. private List<GlyphRect> m_FreeGlyphRects;
  214. /// <summary>
  215. /// The general information about the font.
  216. /// This property and FaceInfo_Legacy type are no longer used in version 1.1.0 of the font asset.
  217. /// </summary>
  218. [Obsolete("The fontInfo property and underlying type is now obsolete. Please use the faceInfo property and FaceInfo type instead.")]
  219. public FaceInfo_Legacy fontInfo
  220. {
  221. get { return m_fontInfo; }
  222. }
  223. [SerializeField]
  224. private FaceInfo_Legacy m_fontInfo = null;
  225. /// <summary>
  226. ///
  227. /// </summary>
  228. [SerializeField]
  229. public Texture2D atlas; // Should add a property to make this read-only.
  230. /// <summary>
  231. /// The width of the atlas texture(s) used by this font asset.
  232. /// </summary>
  233. public int atlasWidth
  234. {
  235. get { return m_AtlasWidth; }
  236. internal set { m_AtlasWidth = value; }
  237. }
  238. [SerializeField]
  239. internal int m_AtlasWidth;
  240. /// <summary>
  241. /// The height of the atlas texture(s) used by this font asset.
  242. /// </summary>
  243. public int atlasHeight
  244. {
  245. get { return m_AtlasHeight; }
  246. internal set { m_AtlasHeight = value; }
  247. }
  248. [SerializeField]
  249. internal int m_AtlasHeight;
  250. /// <summary>
  251. /// The padding used between glyphs contained in the atlas texture(s) used by this font asset.
  252. /// </summary>
  253. public int atlasPadding
  254. {
  255. get { return m_AtlasPadding; }
  256. internal set { m_AtlasPadding = value; }
  257. }
  258. [SerializeField]
  259. internal int m_AtlasPadding;
  260. public GlyphRenderMode atlasRenderMode
  261. {
  262. get { return m_AtlasRenderMode; }
  263. internal set { m_AtlasRenderMode = value; }
  264. }
  265. [SerializeField]
  266. internal GlyphRenderMode m_AtlasRenderMode;
  267. // Legacy field that will eventually be removed.
  268. [SerializeField]
  269. internal List<TMP_Glyph> m_glyphInfoList;
  270. [SerializeField]
  271. [FormerlySerializedAs("m_kerningInfo")]
  272. internal KerningTable m_KerningTable = new KerningTable();
  273. /// <summary>
  274. /// Table containing the various font features of this font asset.
  275. /// </summary>
  276. public TMP_FontFeatureTable fontFeatureTable
  277. {
  278. get { return m_FontFeatureTable; }
  279. internal set { m_FontFeatureTable = value; }
  280. }
  281. [SerializeField]
  282. internal TMP_FontFeatureTable m_FontFeatureTable = new TMP_FontFeatureTable();
  283. // Legacy field that will eventually be removed
  284. [SerializeField]
  285. #pragma warning disable 0649
  286. private List<TMP_FontAsset> fallbackFontAssets;
  287. /// <summary>
  288. /// List containing the Fallback font assets for this font.
  289. /// </summary>
  290. public List<TMP_FontAsset> fallbackFontAssetTable
  291. {
  292. get { return m_FallbackFontAssetTable; }
  293. set { m_FallbackFontAssetTable = value; }
  294. }
  295. [SerializeField]
  296. internal List<TMP_FontAsset> m_FallbackFontAssetTable;
  297. /// <summary>
  298. /// The settings used in the Font Asset Creator when this font asset was created or edited.
  299. /// </summary>
  300. public FontAssetCreationSettings creationSettings
  301. {
  302. get { return m_CreationSettings; }
  303. set { m_CreationSettings = value; }
  304. }
  305. [SerializeField]
  306. internal FontAssetCreationSettings m_CreationSettings;
  307. /// <summary>
  308. /// Array containing font assets to be used as alternative typefaces for the various potential font weights of this font asset.
  309. /// </summary>
  310. public TMP_FontWeightPair[] fontWeightTable
  311. {
  312. get { return m_FontWeightTable; }
  313. internal set { m_FontWeightTable = value; }
  314. }
  315. [SerializeField]
  316. private TMP_FontWeightPair[] m_FontWeightTable = new TMP_FontWeightPair[10];
  317. // FONT WEIGHTS
  318. /// <summary>
  319. /// Font weights used by font asset prior to version 1.1.0.
  320. /// This is legacy and will be removed at some point in the future.
  321. /// </summary>
  322. [SerializeField]
  323. private TMP_FontWeightPair[] fontWeights = null;
  324. //private int[] m_characterSet; // Array containing all the characters in this font asset.
  325. /// <summary>
  326. /// Defines the dilation of the text when using regular style.
  327. /// </summary>
  328. public float normalStyle = 0;
  329. /// <summary>
  330. /// The spacing between characters when using regular style.
  331. /// </summary>
  332. public float normalSpacingOffset = 0;
  333. /// <summary>
  334. /// Defines the dilation of the text when using bold style.
  335. /// </summary>
  336. public float boldStyle = 0.75f;
  337. /// <summary>
  338. /// The spacing between characters when using regular style.
  339. /// </summary>
  340. public float boldSpacing = 7f;
  341. /// <summary>
  342. /// Defines the slant of the text when using italic style.
  343. /// </summary>
  344. public byte italicStyle = 35;
  345. public byte tabSize = 10;
  346. internal bool IsFontAssetLookupTablesDirty;
  347. /// <summary>
  348. /// Create new instance of a font asset using default settings.
  349. /// </summary>
  350. /// <param name="font"></param>
  351. /// <returns></returns>
  352. public static TMP_FontAsset CreateFontAsset(Font font)
  353. {
  354. return CreateFontAsset(font, 90, 9, GlyphRenderMode.SDFAA, 1024, 1024, AtlasPopulationMode.Dynamic);
  355. }
  356. /// <summary>
  357. /// Create new instance of a font asset.
  358. /// </summary>
  359. /// <param name="font">The source font file.</param>
  360. /// <param name="samplingPointSize">The sampling point size.</param>
  361. /// <param name="atlasPadding">The padding / spread between individual glyphs in the font asset.</param>
  362. /// <param name="renderMode"></param>
  363. /// <param name="atlasWidth">The atlas texture width.</param>
  364. /// <param name="atlasHeight">The atlas texture height.</param>
  365. /// <param name="atlasPopulationMode"></param>
  366. /// <returns></returns>
  367. public static TMP_FontAsset CreateFontAsset(Font font, int samplingPointSize, int atlasPadding, GlyphRenderMode renderMode, int atlasWidth, int atlasHeight, AtlasPopulationMode atlasPopulationMode = AtlasPopulationMode.Dynamic, bool enableMultiAtlasSupport = true)
  368. {
  369. // Initialize FontEngine
  370. FontEngine.InitializeFontEngine();
  371. // Load Font Face
  372. if (FontEngine.LoadFontFace(font, samplingPointSize) != FontEngineError.Success)
  373. {
  374. Debug.LogWarning("Unable to load font face for [" + font.name + "]. Make sure \"Include Font Data\" is enabled in the Font Import Settings.", font);
  375. return null;
  376. }
  377. // Create new font asset
  378. TMP_FontAsset fontAsset = ScriptableObject.CreateInstance<TMP_FontAsset>();
  379. fontAsset.m_Version = "1.1.0";
  380. fontAsset.faceInfo = FontEngine.GetFaceInfo();
  381. // Set font reference and GUID
  382. if (atlasPopulationMode == AtlasPopulationMode.Dynamic)
  383. fontAsset.sourceFontFile = font;
  384. // Set persistent reference to source font file in the Editor only.
  385. #if UNITY_EDITOR
  386. string guid;
  387. long localID;
  388. UnityEditor.AssetDatabase.TryGetGUIDAndLocalFileIdentifier(font, out guid, out localID);
  389. fontAsset.m_SourceFontFileGUID = guid;
  390. fontAsset.m_SourceFontFile_EditorRef = font;
  391. #endif
  392. fontAsset.atlasPopulationMode = atlasPopulationMode;
  393. fontAsset.atlasWidth = atlasWidth;
  394. fontAsset.atlasHeight = atlasHeight;
  395. fontAsset.atlasPadding = atlasPadding;
  396. fontAsset.atlasRenderMode = renderMode;
  397. // Initialize array for the font atlas textures.
  398. fontAsset.atlasTextures = new Texture2D[1];
  399. // Create and add font atlas texture.
  400. Texture2D texture = new Texture2D(0, 0, TextureFormat.Alpha8, false);
  401. //texture.name = assetName + " Atlas";
  402. fontAsset.atlasTextures[0] = texture;
  403. fontAsset.isMultiAtlasTexturesEnabled = enableMultiAtlasSupport;
  404. // Add free rectangle of the size of the texture.
  405. int packingModifier;
  406. if (((GlyphRasterModes)renderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP)
  407. {
  408. packingModifier = 0;
  409. // Optimize by adding static ref to shader.
  410. Material tmp_material = new Material(ShaderUtilities.ShaderRef_MobileBitmap);
  411. //tmp_material.name = texture.name + " Material";
  412. tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
  413. tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
  414. tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
  415. fontAsset.material = tmp_material;
  416. }
  417. else
  418. {
  419. packingModifier = 1;
  420. // Optimize by adding static ref to shader.
  421. Material tmp_material = new Material(ShaderUtilities.ShaderRef_MobileSDF);
  422. //tmp_material.name = texture.name + " Material";
  423. tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
  424. tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
  425. tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
  426. tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, atlasPadding + packingModifier);
  427. tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
  428. tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
  429. fontAsset.material = tmp_material;
  430. }
  431. fontAsset.freeGlyphRects = new List<GlyphRect>(8) { new GlyphRect(0, 0, atlasWidth - packingModifier, atlasHeight - packingModifier) };
  432. fontAsset.usedGlyphRects = new List<GlyphRect>(8);
  433. // TODO: Consider adding support for extracting glyph positioning data
  434. fontAsset.ReadFontAssetDefinition();
  435. return fontAsset;
  436. }
  437. void Awake()
  438. {
  439. // Check version number of font asset to see if it needs to be upgraded.
  440. if (this.material != null && string.IsNullOrEmpty(m_Version))
  441. UpgradeFontAsset();
  442. }
  443. #if UNITY_EDITOR
  444. private void OnValidate()
  445. {
  446. // Make sure our lookup dictionary have been initialized.
  447. if (m_CharacterLookupDictionary == null || m_GlyphLookupDictionary == null)
  448. ReadFontAssetDefinition();
  449. }
  450. #endif
  451. private static string s_DefaultMaterialSuffix = " Atlas Material";
  452. public void ReadFontAssetDefinition()
  453. {
  454. Profiler.BeginSample("TMP.ReadFontAssetDefinition");
  455. //Debug.Log("Reading Font Asset Definition for " + this.name + ".");
  456. // Check version number of font asset to see if it needs to be upgraded.
  457. if (this.material != null && string.IsNullOrEmpty(m_Version))
  458. UpgradeFontAsset();
  459. // Initialize lookup tables for characters and glyphs.
  460. InitializeDictionaryLookupTables();
  461. // Add synthesized characters and adjust face metrics
  462. AddSynthesizedCharactersAndFaceMetrics();
  463. // Adjust Font Scale for compatibility reasons
  464. if (m_FaceInfo.scale == 0)
  465. m_FaceInfo.scale = 1.0f;
  466. // Set Strikethrough Offset (if needed)
  467. if (m_FaceInfo.strikethroughOffset == 0)
  468. m_FaceInfo.strikethroughOffset = m_FaceInfo.capLine / 2.5f;
  469. // Set Padding value for legacy font assets.
  470. if (m_AtlasPadding == 0)
  471. {
  472. if (material.HasProperty(ShaderUtilities.ID_GradientScale))
  473. m_AtlasPadding = (int)material.GetFloat(ShaderUtilities.ID_GradientScale) - 1;
  474. }
  475. // Compute Hashcode for the font asset name
  476. hashCode = TMP_TextUtilities.GetSimpleHashCode(this.name);
  477. // Compute Hashcode for the material name
  478. materialHashCode = TMP_TextUtilities.GetSimpleHashCode(this.name + s_DefaultMaterialSuffix);
  479. // Add reference to font asset in TMP Resource Manager
  480. //TMP_ResourceManager.AddFontAsset(this);
  481. IsFontAssetLookupTablesDirty = false;
  482. Profiler.EndSample();
  483. }
  484. /// <summary>
  485. /// Read the various data tables of the font asset to populate its different dictionaries to allow for faster lookup of related font asset data.
  486. /// </summary>
  487. internal void InitializeDictionaryLookupTables()
  488. {
  489. // Initialize and populate glyph lookup dictionary
  490. InitializeGlyphLookupDictionary();
  491. // Initialize and populate character lookup dictionary
  492. InitializeCharacterLookupDictionary();
  493. // Initialize and populate character lookup dictionary
  494. InitializeGlyphPaidAdjustmentRecordsLookupDictionary();
  495. }
  496. internal void InitializeGlyphLookupDictionary()
  497. {
  498. // Create new instance of the glyph lookup dictionary or clear the existing one.
  499. if (m_GlyphLookupDictionary == null)
  500. m_GlyphLookupDictionary = new Dictionary<uint, Glyph>();
  501. else
  502. m_GlyphLookupDictionary.Clear();
  503. // Initialize or clear list of glyph indexes.
  504. if (m_GlyphIndexList == null)
  505. m_GlyphIndexList = new List<uint>();
  506. else
  507. m_GlyphIndexList.Clear();
  508. // Initialize or clear list of glyph indexes.
  509. if (m_GlyphIndexListNewlyAdded == null)
  510. m_GlyphIndexListNewlyAdded = new List<uint>();
  511. else
  512. m_GlyphIndexListNewlyAdded.Clear();
  513. //
  514. int glyphCount = m_GlyphTable.Count;
  515. // Add glyphs contained in the glyph table to dictionary for faster lookup.
  516. for (int i = 0; i < glyphCount; i++)
  517. {
  518. Glyph glyph = m_GlyphTable[i];
  519. uint index = glyph.index;
  520. // TODO: Not sure it is necessary to check here.
  521. if (m_GlyphLookupDictionary.ContainsKey(index) == false)
  522. {
  523. m_GlyphLookupDictionary.Add(index, glyph);
  524. m_GlyphIndexList.Add(index);
  525. }
  526. }
  527. }
  528. internal void InitializeCharacterLookupDictionary()
  529. {
  530. // Create new instance of the character lookup dictionary or clear the existing one.
  531. if (m_CharacterLookupDictionary == null)
  532. m_CharacterLookupDictionary = new Dictionary<uint, TMP_Character>();
  533. else
  534. m_CharacterLookupDictionary.Clear();
  535. // Add the characters contained in the character table to the dictionary for faster lookup.
  536. for (int i = 0; i < m_CharacterTable.Count; i++)
  537. {
  538. TMP_Character character = m_CharacterTable[i];
  539. uint unicode = character.unicode;
  540. uint glyphIndex = character.glyphIndex;
  541. // Add character along with reference to text asset and glyph
  542. if (m_CharacterLookupDictionary.ContainsKey(unicode) == false)
  543. {
  544. m_CharacterLookupDictionary.Add(unicode, character);
  545. character.textAsset = this;
  546. character.glyph = m_GlyphLookupDictionary[glyphIndex];
  547. }
  548. }
  549. // Clear internal fallback references
  550. if (FallbackSearchQueryLookup == null)
  551. FallbackSearchQueryLookup = new HashSet<int>();
  552. else
  553. FallbackSearchQueryLookup.Clear();
  554. }
  555. internal void InitializeGlyphPaidAdjustmentRecordsLookupDictionary()
  556. {
  557. // Upgrade Glyph Adjustment Table to the new Font Feature table and Glyph Pair Adjustment Records
  558. if (m_KerningTable != null && m_KerningTable.kerningPairs != null && m_KerningTable.kerningPairs.Count > 0)
  559. UpgradeGlyphAdjustmentTableToFontFeatureTable();
  560. // Read Font Features which will include kerning data.
  561. if (m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary == null)
  562. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary = new Dictionary<uint, TMP_GlyphPairAdjustmentRecord>();
  563. else
  564. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Clear();
  565. List<TMP_GlyphPairAdjustmentRecord> glyphPairAdjustmentRecords = m_FontFeatureTable.m_GlyphPairAdjustmentRecords;
  566. if (glyphPairAdjustmentRecords != null)
  567. {
  568. for (int i = 0; i < glyphPairAdjustmentRecords.Count; i++)
  569. {
  570. TMP_GlyphPairAdjustmentRecord record = glyphPairAdjustmentRecords[i];
  571. uint key = new GlyphPairKey(record).key;
  572. if (!m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.ContainsKey(key))
  573. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Add(key, record);
  574. }
  575. }
  576. }
  577. internal void AddSynthesizedCharactersAndFaceMetrics()
  578. {
  579. Profiler.BeginSample("TMP.AddSynthesizedCharacters");
  580. if (m_AtlasPopulationMode == AtlasPopulationMode.Dynamic)
  581. FontEngine.LoadFontFace(sourceFontFile, m_FaceInfo.pointSize);
  582. // Only characters not present in the source font file will be synthesized.
  583. // Non visible and control characters with no metrics
  584. // Add End of Text \u0003
  585. AddSynthesizedCharacter(0x03, true);
  586. // Add Tab \u0009
  587. AddSynthesizedCharacter(0x09, true);
  588. // Add Line Feed (LF) \u000A
  589. AddSynthesizedCharacter(0x0A);
  590. // Add Vertical Tab (VT) \u000B
  591. AddSynthesizedCharacter(0x0B);
  592. // Add Carriage Return (CR) \u000D
  593. AddSynthesizedCharacter(0x0D);
  594. // Add Arabic Letter Mark \u061C
  595. AddSynthesizedCharacter(0x061C);
  596. // Add Zero Width Space <ZWSP> \u2000B
  597. AddSynthesizedCharacter(0x200B);
  598. // Add Left-To-Right Mark \u200E
  599. AddSynthesizedCharacter(0x200E);
  600. // Add Right-To-Left Mark \u200F
  601. AddSynthesizedCharacter(0x200F);
  602. // Add Line Separator \u2028
  603. AddSynthesizedCharacter(0x2028);
  604. // Add Paragraph Separator \u2029
  605. AddSynthesizedCharacter(0x2029);
  606. // Add Word Joiner <WJ> / Zero Width Non-Breaking Space \u2060
  607. AddSynthesizedCharacter(0x2060);
  608. // Set Cap Line using the capital letter 'X'
  609. if (m_FaceInfo.capLine == 0 && m_CharacterLookupDictionary.ContainsKey('X'))
  610. {
  611. uint glyphIndex = m_CharacterLookupDictionary['X'].glyphIndex;
  612. m_FaceInfo.capLine = m_GlyphLookupDictionary[glyphIndex].metrics.horizontalBearingY;
  613. }
  614. // Set Mean Line using the lowercase letter 'x'
  615. if (m_FaceInfo.meanLine == 0 && m_CharacterLookupDictionary.ContainsKey('x'))
  616. {
  617. uint glyphIndex = m_CharacterLookupDictionary['x'].glyphIndex;
  618. m_FaceInfo.meanLine = m_GlyphLookupDictionary[glyphIndex].metrics.horizontalBearingY;
  619. }
  620. Profiler.EndSample();
  621. }
  622. void AddSynthesizedCharacter(uint unicode, bool addImmediately = false)
  623. {
  624. // Check if unicode is already present in the font asset
  625. if (m_CharacterLookupDictionary.ContainsKey(unicode))
  626. return;
  627. Glyph glyph;
  628. if (m_AtlasPopulationMode == AtlasPopulationMode.Dynamic)
  629. {
  630. // Check if unicode is present in font file
  631. if (FontEngine.GetGlyphIndex(unicode) != 0)
  632. {
  633. if (addImmediately == false)
  634. return;
  635. //Debug.Log("Adding Unicode [" + unicode.ToString("X4") + "].");
  636. GlyphLoadFlags glyphLoadFlags = ((GlyphRasterModes)m_AtlasRenderMode & GlyphRasterModes.RASTER_MODE_NO_HINTING) == GlyphRasterModes.RASTER_MODE_NO_HINTING
  637. ? GlyphLoadFlags.LOAD_NO_BITMAP | GlyphLoadFlags.LOAD_NO_HINTING
  638. : GlyphLoadFlags.LOAD_NO_BITMAP;
  639. if (FontEngine.TryGetGlyphWithUnicodeValue(unicode, glyphLoadFlags, out glyph))
  640. m_CharacterLookupDictionary.Add(unicode, new TMP_Character(unicode, this, glyph));
  641. return;
  642. }
  643. }
  644. //Debug.Log("Synthesizing Unicode [" + unicode.ToString("X4") + "].");
  645. // Synthesize and add missing glyph and character
  646. glyph = new Glyph(0, new GlyphMetrics(0, 0, 0, 0, 0), GlyphRect.zero, 1.0f, 0);
  647. m_CharacterLookupDictionary.Add(unicode, new TMP_Character(unicode, this, glyph));
  648. }
  649. internal HashSet<int> FallbackSearchQueryLookup = new HashSet<int>();
  650. internal void AddCharacterToLookupCache(uint unicode, TMP_Character character)
  651. {
  652. m_CharacterLookupDictionary.Add(unicode, character);
  653. // Add font asset to fallback references.
  654. FallbackSearchQueryLookup.Add(character.textAsset.instanceID);
  655. }
  656. /// <summary>
  657. /// Sort the Character table by Unicode values.
  658. /// </summary>
  659. internal void SortCharacterTable()
  660. {
  661. if (m_CharacterTable != null && m_CharacterTable.Count > 0)
  662. m_CharacterTable = m_CharacterTable.OrderBy(c => c.unicode).ToList();
  663. }
  664. /// <summary>
  665. /// Sort the Glyph table by index values.
  666. /// </summary>
  667. internal void SortGlyphTable()
  668. {
  669. if (m_GlyphTable != null && m_GlyphTable.Count > 0)
  670. m_GlyphTable = m_GlyphTable.OrderBy(c => c.index).ToList();
  671. }
  672. internal void SortFontFeatureTable()
  673. {
  674. m_FontFeatureTable.SortGlyphPairAdjustmentRecords();
  675. }
  676. /// <summary>
  677. /// Sort both glyph and character tables.
  678. /// </summary>
  679. internal void SortAllTables()
  680. {
  681. SortGlyphTable();
  682. SortCharacterTable();
  683. SortFontFeatureTable();
  684. }
  685. /// <summary>
  686. /// HashSet of font asset instance ID used in the process of searching for through fallback font assets for a given character or characters.
  687. /// </summary>
  688. private static HashSet<int> k_SearchedFontAssetLookup;
  689. /// <summary>
  690. /// Function to check if a certain character exists in the font asset.
  691. /// </summary>
  692. /// <param name="character"></param>
  693. /// <returns></returns>
  694. public bool HasCharacter(int character)
  695. {
  696. if (m_CharacterLookupDictionary == null)
  697. return false;
  698. if (m_CharacterLookupDictionary.ContainsKey((uint)character))
  699. return true;
  700. return false;
  701. }
  702. /// <summary>
  703. /// Function to check if a character is contained in the font asset with the option to also check through fallback font assets.
  704. /// </summary>
  705. /// <param name="character"></param>
  706. /// <param name="searchFallbacks"></param>
  707. /// <param name="tryAddCharacter"></param>
  708. /// <returns></returns>
  709. public bool HasCharacter(char character, bool searchFallbacks = false, bool tryAddCharacter = false)
  710. {
  711. // Read font asset definition if it hasn't already been done.
  712. if (m_CharacterLookupDictionary == null)
  713. {
  714. ReadFontAssetDefinition();
  715. if (m_CharacterLookupDictionary == null)
  716. return false;
  717. }
  718. // Check font asset
  719. if (m_CharacterLookupDictionary.ContainsKey(character))
  720. return true;
  721. // Check if font asset is dynamic and if so try to add the requested character to it.
  722. if (tryAddCharacter && m_AtlasPopulationMode == AtlasPopulationMode.Dynamic)
  723. {
  724. TMP_Character returnedCharacter;
  725. if (TryAddCharacterInternal(character, out returnedCharacter))
  726. return true;
  727. }
  728. if (searchFallbacks)
  729. {
  730. // Initialize or clear font asset lookup
  731. if (k_SearchedFontAssetLookup == null)
  732. k_SearchedFontAssetLookup = new HashSet<int>();
  733. else
  734. k_SearchedFontAssetLookup.Clear();
  735. // Add current font asset to lookup
  736. k_SearchedFontAssetLookup.Add(GetInstanceID());
  737. // Check font asset fallbacks
  738. if (fallbackFontAssetTable != null && fallbackFontAssetTable.Count > 0)
  739. {
  740. for (int i = 0; i < fallbackFontAssetTable.Count && fallbackFontAssetTable[i] != null; i++)
  741. {
  742. TMP_FontAsset fallback = fallbackFontAssetTable[i];
  743. int fallbackID = fallback.GetInstanceID();
  744. // Search fallback if not already contained in lookup
  745. if (k_SearchedFontAssetLookup.Add(fallbackID))
  746. {
  747. if (fallback.HasCharacter_Internal(character, true, tryAddCharacter))
  748. return true;
  749. }
  750. }
  751. }
  752. // Check general fallback font assets.
  753. if (TMP_Settings.fallbackFontAssets != null && TMP_Settings.fallbackFontAssets.Count > 0)
  754. {
  755. for (int i = 0; i < TMP_Settings.fallbackFontAssets.Count && TMP_Settings.fallbackFontAssets[i] != null; i++)
  756. {
  757. TMP_FontAsset fallback = TMP_Settings.fallbackFontAssets[i];
  758. int fallbackID = fallback.GetInstanceID();
  759. // Search fallback if not already contained in lookup
  760. if (k_SearchedFontAssetLookup.Add(fallbackID))
  761. {
  762. if (fallback.HasCharacter_Internal(character, true, tryAddCharacter))
  763. return true;
  764. }
  765. }
  766. }
  767. // Check TMP Settings Default Font Asset
  768. if (TMP_Settings.defaultFontAsset != null)
  769. {
  770. TMP_FontAsset fallback = TMP_Settings.defaultFontAsset;
  771. int fallbackID = fallback.GetInstanceID();
  772. // Search fallback if it has not already been searched
  773. if (k_SearchedFontAssetLookup.Add(fallbackID))
  774. {
  775. if (fallback.HasCharacter_Internal(character, true, tryAddCharacter))
  776. return true;
  777. }
  778. }
  779. }
  780. return false;
  781. }
  782. /// <summary>
  783. /// Function to check if a character is contained in a font asset with the option to also check through fallback font assets.
  784. /// This private implementation does not search the fallback font asset in the TMP Settings file.
  785. /// </summary>
  786. /// <param name="character"></param>
  787. /// <param name="searchFallbacks"></param>
  788. /// <param name="tryAddCharacter"></param>
  789. /// <returns></returns>
  790. bool HasCharacter_Internal(uint character, bool searchFallbacks = false, bool tryAddCharacter = false)
  791. {
  792. // Read font asset definition if it hasn't already been done.
  793. if (m_CharacterLookupDictionary == null)
  794. {
  795. ReadFontAssetDefinition();
  796. if (m_CharacterLookupDictionary == null)
  797. return false;
  798. }
  799. // Check font asset
  800. if (m_CharacterLookupDictionary.ContainsKey(character))
  801. return true;
  802. // Check if fallback is dynamic and if so try to add the requested character to it.
  803. if (tryAddCharacter && atlasPopulationMode == AtlasPopulationMode.Dynamic)
  804. {
  805. TMP_Character returnedCharacter;
  806. if (TryAddCharacterInternal(character, out returnedCharacter))
  807. return true;
  808. }
  809. if (searchFallbacks)
  810. {
  811. // Check Font Asset Fallback fonts.
  812. if (fallbackFontAssetTable == null || fallbackFontAssetTable.Count == 0)
  813. return false;
  814. for (int i = 0; i < fallbackFontAssetTable.Count && fallbackFontAssetTable[i] != null; i++)
  815. {
  816. TMP_FontAsset fallback = fallbackFontAssetTable[i];
  817. int fallbackID = fallback.GetInstanceID();
  818. // Search fallback if it has not already been searched
  819. if (k_SearchedFontAssetLookup.Add(fallbackID))
  820. {
  821. if (fallback.HasCharacter_Internal(character, true, tryAddCharacter))
  822. return true;
  823. }
  824. }
  825. }
  826. return false;
  827. }
  828. /// <summary>
  829. /// Function to check if certain characters exists in the font asset. Function returns a list of missing characters.
  830. /// </summary>
  831. /// <param name="character"></param>
  832. /// <param name="missingCharacters"></param>
  833. /// <returns></returns>
  834. public bool HasCharacters(string text, out List<char> missingCharacters)
  835. {
  836. if (m_CharacterLookupDictionary == null)
  837. {
  838. missingCharacters = null;
  839. return false;
  840. }
  841. missingCharacters = new List<char>();
  842. for (int i = 0; i < text.Length; i++)
  843. {
  844. if (!m_CharacterLookupDictionary.ContainsKey(text[i]))
  845. missingCharacters.Add(text[i]);
  846. }
  847. if (missingCharacters.Count == 0)
  848. return true;
  849. return false;
  850. }
  851. /// <summary>
  852. ///
  853. /// </summary>
  854. /// <param name="text"></param>
  855. /// <param name="missingCharacters"></param>
  856. /// <param name="searchFallbacks"></param>
  857. /// <param name="tryAddCharacter"></param>
  858. /// <returns></returns>
  859. public bool HasCharacters(string text, out uint[] missingCharacters, bool searchFallbacks = false, bool tryAddCharacter = false)
  860. {
  861. missingCharacters = null;
  862. // Read font asset definition if it hasn't already been done.
  863. if (m_CharacterLookupDictionary == null)
  864. {
  865. ReadFontAssetDefinition();
  866. if (m_CharacterLookupDictionary == null)
  867. return false;
  868. }
  869. // Clear internal list of
  870. s_MissingCharacterList.Clear();
  871. for (int i = 0; i < text.Length; i++)
  872. {
  873. bool isMissingCharacter = true;
  874. uint character = text[i];
  875. if (m_CharacterLookupDictionary.ContainsKey(character))
  876. continue;
  877. // Check if fallback is dynamic and if so try to add the requested character to it.
  878. if (tryAddCharacter && atlasPopulationMode == AtlasPopulationMode.Dynamic)
  879. {
  880. TMP_Character returnedCharacter;
  881. if (TryAddCharacterInternal(character, out returnedCharacter))
  882. continue;
  883. }
  884. if (searchFallbacks)
  885. {
  886. // Initialize or clear font asset lookup
  887. if (k_SearchedFontAssetLookup == null)
  888. k_SearchedFontAssetLookup = new HashSet<int>();
  889. else
  890. k_SearchedFontAssetLookup.Clear();
  891. // Add current font asset to lookup
  892. k_SearchedFontAssetLookup.Add(GetInstanceID());
  893. // Check font asset fallbacks
  894. if (fallbackFontAssetTable != null && fallbackFontAssetTable.Count > 0)
  895. {
  896. for (int j = 0; j < fallbackFontAssetTable.Count && fallbackFontAssetTable[j] != null; j++)
  897. {
  898. TMP_FontAsset fallback = fallbackFontAssetTable[j];
  899. int fallbackID = fallback.GetInstanceID();
  900. // Search fallback if it has not already been searched
  901. if (k_SearchedFontAssetLookup.Add(fallbackID))
  902. {
  903. if (fallback.HasCharacter_Internal(character, true, tryAddCharacter) == false)
  904. continue;
  905. isMissingCharacter = false;
  906. break;
  907. }
  908. }
  909. }
  910. // Check general fallback font assets.
  911. if (isMissingCharacter && TMP_Settings.fallbackFontAssets != null && TMP_Settings.fallbackFontAssets.Count > 0)
  912. {
  913. for (int j = 0; j < TMP_Settings.fallbackFontAssets.Count && TMP_Settings.fallbackFontAssets[j] != null; j++)
  914. {
  915. TMP_FontAsset fallback = TMP_Settings.fallbackFontAssets[j];
  916. int fallbackID = fallback.GetInstanceID();
  917. // Search fallback if it has not already been searched
  918. if (k_SearchedFontAssetLookup.Add(fallbackID))
  919. {
  920. if (fallback.HasCharacter_Internal(character, true, tryAddCharacter) == false)
  921. continue;
  922. isMissingCharacter = false;
  923. break;
  924. }
  925. }
  926. }
  927. // Check TMP Settings Default Font Asset
  928. if (isMissingCharacter && TMP_Settings.defaultFontAsset != null)
  929. {
  930. TMP_FontAsset fallback = TMP_Settings.defaultFontAsset;
  931. int fallbackID = fallback.GetInstanceID();
  932. // Search fallback if it has not already been searched
  933. if (k_SearchedFontAssetLookup.Add(fallbackID))
  934. {
  935. if (fallback.HasCharacter_Internal(character, true, tryAddCharacter))
  936. isMissingCharacter = false;
  937. }
  938. }
  939. }
  940. if (isMissingCharacter)
  941. s_MissingCharacterList.Add(character);
  942. }
  943. if (s_MissingCharacterList.Count > 0)
  944. {
  945. missingCharacters = s_MissingCharacterList.ToArray();
  946. return false;
  947. }
  948. return true;
  949. }
  950. /// <summary>
  951. /// Function to check if certain characters exists in the font asset. Function returns false if any characters are missing.
  952. /// </summary>
  953. /// <param name="text">String containing the characters to check</param>
  954. /// <returns></returns>
  955. public bool HasCharacters(string text)
  956. {
  957. if (m_CharacterLookupDictionary == null)
  958. return false;
  959. for (int i = 0; i < text.Length; i++)
  960. {
  961. if (!m_CharacterLookupDictionary.ContainsKey(text[i]))
  962. return false;
  963. }
  964. return true;
  965. }
  966. /// <summary>
  967. /// Function to extract all the characters from a font asset.
  968. /// </summary>
  969. /// <param name="fontAsset"></param>
  970. /// <returns></returns>
  971. public static string GetCharacters(TMP_FontAsset fontAsset)
  972. {
  973. string characters = string.Empty;
  974. for (int i = 0; i < fontAsset.characterTable.Count; i++)
  975. {
  976. characters += (char)fontAsset.characterTable[i].unicode;
  977. }
  978. return characters;
  979. }
  980. /// <summary>
  981. /// Function which returns an array that contains all the characters from a font asset.
  982. /// </summary>
  983. /// <param name="fontAsset"></param>
  984. /// <returns></returns>
  985. public static int[] GetCharactersArray(TMP_FontAsset fontAsset)
  986. {
  987. int[] characters = new int[fontAsset.characterTable.Count];
  988. for (int i = 0; i < fontAsset.characterTable.Count; i++)
  989. {
  990. characters[i] = (int)fontAsset.characterTable[i].unicode;
  991. }
  992. return characters;
  993. }
  994. /// <summary>
  995. /// Internal function used to get the glyph index for the given Unicode.
  996. /// </summary>
  997. /// <param name="unicode"></param>
  998. /// <returns></returns>
  999. internal uint GetGlyphIndex(uint unicode)
  1000. {
  1001. // Check if glyph already exists in font asset.
  1002. if (m_CharacterLookupDictionary.ContainsKey(unicode))
  1003. return m_CharacterLookupDictionary[unicode].glyphIndex;
  1004. // Load font face.
  1005. if (FontEngine.LoadFontFace(sourceFontFile, m_FaceInfo.pointSize) != FontEngineError.Success)
  1006. return 0;
  1007. return FontEngine.GetGlyphIndex(unicode);
  1008. }
  1009. // ================================================================================
  1010. // Properties and functions related to character and glyph additions as well as
  1011. // tacking glyphs that need to be added to various font asset atlas textures.
  1012. // ================================================================================
  1013. // List and HashSet used for tracking font assets whose font atlas texture and character data needs updating.
  1014. private static List<TMP_FontAsset> k_FontAssets_FontFeaturesUpdateQueue = new List<TMP_FontAsset>();
  1015. private static HashSet<int> k_FontAssets_FontFeaturesUpdateQueueLookup = new HashSet<int>();
  1016. private static List<TMP_FontAsset> k_FontAssets_AtlasTexturesUpdateQueue = new List<TMP_FontAsset>();
  1017. private static HashSet<int> k_FontAssets_AtlasTexturesUpdateQueueLookup = new HashSet<int>();
  1018. /// <summary>
  1019. ///
  1020. /// </summary>
  1021. /// <param name="fontAsset"></param>
  1022. internal static void RegisterFontAssetForFontFeatureUpdate(TMP_FontAsset fontAsset)
  1023. {
  1024. int instanceID = fontAsset.instanceID;
  1025. if (k_FontAssets_FontFeaturesUpdateQueueLookup.Add(instanceID))
  1026. k_FontAssets_FontFeaturesUpdateQueue.Add(fontAsset);
  1027. }
  1028. /// <summary>
  1029. /// Function called to update the font atlas texture and character data of font assets to which
  1030. /// new characters were added.
  1031. /// </summary>
  1032. internal static void UpdateFontFeaturesForFontAssetsInQueue()
  1033. {
  1034. int count = k_FontAssets_FontFeaturesUpdateQueue.Count;
  1035. for (int i = 0; i < count; i++)
  1036. k_FontAssets_FontFeaturesUpdateQueue[i].UpdateGlyphAdjustmentRecords();
  1037. if (count > 0)
  1038. {
  1039. k_FontAssets_FontFeaturesUpdateQueue.Clear();
  1040. k_FontAssets_FontFeaturesUpdateQueueLookup.Clear();
  1041. }
  1042. }
  1043. /// <summary>
  1044. ///
  1045. /// </summary>
  1046. /// <param name="fontAsset"></param>
  1047. internal static void RegisterFontAssetForAtlasTextureUpdate(TMP_FontAsset fontAsset)
  1048. {
  1049. int instanceID = fontAsset.instanceID;
  1050. if (k_FontAssets_AtlasTexturesUpdateQueueLookup.Add(instanceID))
  1051. k_FontAssets_AtlasTexturesUpdateQueue.Add(fontAsset);
  1052. }
  1053. /// <summary>
  1054. ///
  1055. /// </summary>
  1056. internal static void UpdateAtlasTexturesForFontAssetsInQueue()
  1057. {
  1058. int count = k_FontAssets_AtlasTexturesUpdateQueueLookup.Count;
  1059. for (int i = 0; i < count; i++)
  1060. k_FontAssets_AtlasTexturesUpdateQueue[i].TryAddGlyphsToAtlasTextures();
  1061. if (count > 0)
  1062. {
  1063. k_FontAssets_AtlasTexturesUpdateQueue.Clear();
  1064. k_FontAssets_AtlasTexturesUpdateQueueLookup.Clear();
  1065. }
  1066. }
  1067. /// <summary>
  1068. ///
  1069. /// </summary>
  1070. //private bool m_IsAlreadyRegisteredForUpdate;
  1071. /// <summary>
  1072. /// List of glyphs that need to be rendered and added to an atlas texture.
  1073. /// </summary>
  1074. private List<Glyph> m_GlyphsToRender = new List<Glyph>();
  1075. /// <summary>
  1076. /// List of glyphs that we just rendered and added to an atlas texture.
  1077. /// </summary>
  1078. private List<Glyph> m_GlyphsRendered = new List<Glyph>();
  1079. /// <summary>
  1080. /// List of all the glyph indexes contained in the font asset.
  1081. /// </summary>
  1082. private List<uint> m_GlyphIndexList = new List<uint>();
  1083. /// <summary>
  1084. /// List of glyph indexes newly added to the font asset.
  1085. /// This list is used in the process of retrieving font features.
  1086. /// </summary>
  1087. private List<uint> m_GlyphIndexListNewlyAdded = new List<uint>();
  1088. /// <summary>
  1089. ///
  1090. /// </summary>
  1091. internal List<uint> m_GlyphsToAdd = new List<uint>();
  1092. internal HashSet<uint> m_GlyphsToAddLookup = new HashSet<uint>();
  1093. internal List<TMP_Character> m_CharactersToAdd = new List<TMP_Character>();
  1094. internal HashSet<uint> m_CharactersToAddLookup = new HashSet<uint>();
  1095. /// <summary>
  1096. /// Internal list used to track characters that could not be added to the font asset.
  1097. /// </summary>
  1098. internal List<uint> s_MissingCharacterList = new List<uint>();
  1099. /// <summary>
  1100. /// Hash table used to track characters that are known to be missing from the font file.
  1101. /// </summary>
  1102. internal HashSet<uint> m_MissingUnicodesFromFontFile = new HashSet<uint>();
  1103. /// <summary>
  1104. /// Internal static array used to avoid allocations when using the GetGlyphPairAdjustmentTable().
  1105. /// </summary>
  1106. internal static uint[] k_GlyphIndexArray;
  1107. /// <summary>
  1108. /// Try adding the characters from the provided string to the font asset.
  1109. /// </summary>
  1110. /// <param name="unicodes">Array that contains the characters to add to the font asset.</param>
  1111. /// <param name="includeFontFeatures"></param>
  1112. /// <returns>Returns true if all the characters were successfully added to the font asset. Return false otherwise.</returns>
  1113. public bool TryAddCharacters(uint[] unicodes, bool includeFontFeatures = false)
  1114. {
  1115. uint[] missingUnicodes;
  1116. return TryAddCharacters(unicodes, out missingUnicodes, includeFontFeatures);
  1117. }
  1118. /// <summary>
  1119. /// Try adding the characters from the provided string to the font asset.
  1120. /// </summary>
  1121. /// <param name="unicodes">Array that contains the characters to add to the font asset.</param>
  1122. /// <param name="missingUnicodes">Array containing the characters that could not be added to the font asset.</param>
  1123. /// <param name="includeFontFeatures"></param>
  1124. /// <returns>Returns true if all the characters were successfully added to the font asset. Return false otherwise.</returns>
  1125. public bool TryAddCharacters(uint[] unicodes, out uint[] missingUnicodes, bool includeFontFeatures = false)
  1126. {
  1127. Profiler.BeginSample("TMP.TryAddCharacters");
  1128. // Make sure font asset is set to dynamic and that we have a valid list of characters.
  1129. if (unicodes == null || unicodes.Length == 0 || m_AtlasPopulationMode == AtlasPopulationMode.Static)
  1130. {
  1131. if (m_AtlasPopulationMode == AtlasPopulationMode.Static)
  1132. Debug.LogWarning("Unable to add characters to font asset [" + this.name + "] because its AtlasPopulationMode is set to Static.", this);
  1133. else
  1134. Debug.LogWarning("Unable to add characters to font asset [" + this.name + "] because the provided Unicode list is Null or Empty.", this);
  1135. missingUnicodes = unicodes.ToArray();
  1136. Profiler.EndSample();
  1137. return false;
  1138. }
  1139. // Load font face.
  1140. if (FontEngine.LoadFontFace(m_SourceFontFile, m_FaceInfo.pointSize) != FontEngineError.Success)
  1141. {
  1142. missingUnicodes = unicodes.ToArray();
  1143. Profiler.EndSample();
  1144. return false;
  1145. }
  1146. // Make sure font asset has been initialized
  1147. if (m_CharacterLookupDictionary == null || m_GlyphLookupDictionary == null)
  1148. ReadFontAssetDefinition();
  1149. // Clear lists used to track which character and glyphs were added or missing.
  1150. m_GlyphsToAdd.Clear();
  1151. m_GlyphsToAddLookup.Clear();
  1152. m_CharactersToAdd.Clear();
  1153. m_CharactersToAddLookup.Clear();
  1154. s_MissingCharacterList.Clear();
  1155. bool isMissingCharacters = false;
  1156. int unicodeCount = unicodes.Length;
  1157. for (int i = 0; i < unicodeCount; i++)
  1158. {
  1159. uint unicode = unicodes[i];
  1160. // Check if character is already contained in the character table.
  1161. if (m_CharacterLookupDictionary.ContainsKey(unicode))
  1162. continue;
  1163. // Get the index of the glyph for this Unicode value.
  1164. uint glyphIndex = FontEngine.GetGlyphIndex(unicode);
  1165. // Skip missing glyphs
  1166. if (glyphIndex == 0)
  1167. {
  1168. // Special handling for characters with potential alternative glyph representations
  1169. switch (unicode)
  1170. {
  1171. case 0xA0: // Non Breaking Space <NBSP>
  1172. // Use Space
  1173. glyphIndex = FontEngine.GetGlyphIndex(0x20);
  1174. break;
  1175. case 0xAD: // Soft Hyphen <SHY>
  1176. case 0x2011: // Non Breaking Hyphen
  1177. // Use Hyphen Minus
  1178. glyphIndex = FontEngine.GetGlyphIndex(0x2D);
  1179. break;
  1180. }
  1181. // Skip to next character if no potential alternative glyph representation is present in font file.
  1182. if (glyphIndex == 0)
  1183. {
  1184. // Add character to list of missing characters.
  1185. s_MissingCharacterList.Add(unicode);
  1186. isMissingCharacters = true;
  1187. continue;
  1188. }
  1189. }
  1190. TMP_Character character = new TMP_Character(unicode, glyphIndex);
  1191. // Check if glyph is already contained in the font asset as the same glyph might be referenced by multiple characters.
  1192. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex))
  1193. {
  1194. // Add a reference to the source text asset and glyph
  1195. character.glyph = m_GlyphLookupDictionary[glyphIndex];
  1196. character.textAsset = this;
  1197. m_CharacterTable.Add(character);
  1198. m_CharacterLookupDictionary.Add(unicode, character);
  1199. continue;
  1200. }
  1201. // Make sure glyph index has not already been added to list of glyphs to add.
  1202. if (m_GlyphsToAddLookup.Add(glyphIndex))
  1203. m_GlyphsToAdd.Add(glyphIndex);
  1204. // Make sure unicode / character has not already been added.
  1205. if (m_CharactersToAddLookup.Add(unicode))
  1206. m_CharactersToAdd.Add(character);
  1207. }
  1208. if (m_GlyphsToAdd.Count == 0)
  1209. {
  1210. //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.");
  1211. missingUnicodes = unicodes;
  1212. Profiler.EndSample();
  1213. return false;
  1214. }
  1215. // Resize the Atlas Texture to the appropriate size
  1216. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1217. {
  1218. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1219. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1220. }
  1221. Glyph[] glyphs;
  1222. bool allGlyphsAddedToTexture = FontEngine.TryAddGlyphsToTexture(m_GlyphsToAdd, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex], out glyphs);
  1223. // Add new glyphs to relevant font asset data structure
  1224. for (int i = 0; i < glyphs.Length && glyphs[i] != null; i++)
  1225. {
  1226. Glyph glyph = glyphs[i];
  1227. uint glyphIndex = glyph.index;
  1228. glyph.atlasIndex = m_AtlasTextureIndex;
  1229. // Add new glyph to glyph table.
  1230. m_GlyphTable.Add(glyph);
  1231. m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  1232. m_GlyphIndexListNewlyAdded.Add(glyphIndex);
  1233. m_GlyphIndexList.Add(glyphIndex);
  1234. }
  1235. // Clear glyph index list to allow
  1236. m_GlyphsToAdd.Clear();
  1237. // Add new characters to relevant data structures as well as track glyphs that could not be added to the current atlas texture.
  1238. for (int i = 0; i < m_CharactersToAdd.Count; i++)
  1239. {
  1240. TMP_Character character = m_CharactersToAdd[i];
  1241. Glyph glyph;
  1242. if (m_GlyphLookupDictionary.TryGetValue(character.glyphIndex, out glyph) == false)
  1243. {
  1244. m_GlyphsToAdd.Add(character.glyphIndex);
  1245. continue;
  1246. }
  1247. // Add a reference to the source text asset and glyph
  1248. character.glyph = glyph;
  1249. character.textAsset = this;
  1250. m_CharacterTable.Add(character);
  1251. m_CharacterLookupDictionary.Add(character.unicode, character);
  1252. // Remove character from list to add
  1253. m_CharactersToAdd.RemoveAt(i);
  1254. i -= 1;
  1255. }
  1256. // Try adding missing glyphs to
  1257. if (m_IsMultiAtlasTexturesEnabled && allGlyphsAddedToTexture == false)
  1258. {
  1259. while (allGlyphsAddedToTexture == false)
  1260. allGlyphsAddedToTexture = TryAddGlyphsToNewAtlasTexture();
  1261. }
  1262. // Get Font Features for the given characters
  1263. if (includeFontFeatures)
  1264. UpdateGlyphAdjustmentRecords();
  1265. #if UNITY_EDITOR
  1266. // Makes the changes to the font asset persistent.
  1267. if (UnityEditor.EditorUtility.IsPersistent(this))
  1268. {
  1269. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1270. }
  1271. #endif
  1272. // Populate list of missing characters
  1273. for (int i = 0; i < m_CharactersToAdd.Count; i++)
  1274. {
  1275. TMP_Character character = m_CharactersToAdd[i];
  1276. s_MissingCharacterList.Add(character.unicode);
  1277. }
  1278. missingUnicodes = null;
  1279. if (s_MissingCharacterList.Count > 0)
  1280. missingUnicodes = s_MissingCharacterList.ToArray();
  1281. Profiler.EndSample();
  1282. return allGlyphsAddedToTexture && !isMissingCharacters;
  1283. }
  1284. /// <summary>
  1285. /// Try adding the characters from the provided string to the font asset.
  1286. /// </summary>
  1287. /// <param name="characters">String containing the characters to add to the font asset.</param>
  1288. /// <param name="includeFontFeatures"></param>
  1289. /// <returns>Returns true if all the characters were successfully added to the font asset. Return false otherwise.</returns>
  1290. public bool TryAddCharacters(string characters, bool includeFontFeatures = false)
  1291. {
  1292. string missingCharacters;
  1293. return TryAddCharacters(characters, out missingCharacters, includeFontFeatures);
  1294. }
  1295. /// <summary>
  1296. /// Try adding the characters from the provided string to the font asset.
  1297. /// </summary>
  1298. /// <param name="characters">String containing the characters to add to the font asset.</param>
  1299. /// <param name="missingCharacters">String containing the characters that could not be added to the font asset.</param>
  1300. /// <param name="includeFontFeatures"></param>
  1301. /// <returns>Returns true if all the characters were successfully added to the font asset. Return false otherwise.</returns>
  1302. public bool TryAddCharacters(string characters, out string missingCharacters, bool includeFontFeatures = false)
  1303. {
  1304. Profiler.BeginSample("TMP.TryAddCharacters");
  1305. // Make sure font asset is set to dynamic and that we have a valid list of characters.
  1306. if (string.IsNullOrEmpty(characters) || m_AtlasPopulationMode == AtlasPopulationMode.Static)
  1307. {
  1308. if (m_AtlasPopulationMode == AtlasPopulationMode.Static)
  1309. Debug.LogWarning("Unable to add characters to font asset [" + this.name + "] because its AtlasPopulationMode is set to Static.", this);
  1310. else
  1311. {
  1312. Debug.LogWarning("Unable to add characters to font asset [" + this.name + "] because the provided character list is Null or Empty.", this);
  1313. }
  1314. missingCharacters = characters;
  1315. Profiler.EndSample();
  1316. return false;
  1317. }
  1318. // Load font face.
  1319. if (FontEngine.LoadFontFace(m_SourceFontFile, m_FaceInfo.pointSize) != FontEngineError.Success)
  1320. {
  1321. missingCharacters = characters;
  1322. Profiler.EndSample();
  1323. return false;
  1324. }
  1325. // Make sure font asset has been initialized
  1326. if (m_CharacterLookupDictionary == null || m_GlyphLookupDictionary == null)
  1327. ReadFontAssetDefinition();
  1328. // Clear data structures used to track which glyph needs to be added to atlas texture.
  1329. m_GlyphsToAdd.Clear();
  1330. m_GlyphsToAddLookup.Clear();
  1331. m_CharactersToAdd.Clear();
  1332. m_CharactersToAddLookup.Clear();
  1333. s_MissingCharacterList.Clear();
  1334. bool isMissingCharacters = false;
  1335. int characterCount = characters.Length;
  1336. // Iterate over each of the requested characters.
  1337. for (int i = 0; i < characterCount; i++)
  1338. {
  1339. uint unicode = characters[i];
  1340. // Check if character is already contained in the character table.
  1341. if (m_CharacterLookupDictionary.ContainsKey(unicode))
  1342. continue;
  1343. // Get the index of the glyph for this unicode value.
  1344. uint glyphIndex = FontEngine.GetGlyphIndex(unicode);
  1345. // Skip missing glyphs
  1346. if (glyphIndex == 0)
  1347. {
  1348. // Special handling for characters with potential alternative glyph representations
  1349. switch (unicode)
  1350. {
  1351. case 0xA0: // Non Breaking Space <NBSP>
  1352. // Use Space
  1353. glyphIndex = FontEngine.GetGlyphIndex(0x20);
  1354. break;
  1355. case 0xAD: // Soft Hyphen <SHY>
  1356. case 0x2011: // Non Breaking Hyphen
  1357. // Use Hyphen Minus
  1358. glyphIndex = FontEngine.GetGlyphIndex(0x2D);
  1359. break;
  1360. }
  1361. // Skip to next character if no potential alternative glyph representation is present in font file.
  1362. if (glyphIndex == 0)
  1363. {
  1364. // Add character to list of missing characters.
  1365. s_MissingCharacterList.Add(unicode);
  1366. isMissingCharacters = true;
  1367. continue;
  1368. }
  1369. }
  1370. TMP_Character character = new TMP_Character(unicode, glyphIndex);
  1371. // Check if glyph is already contained in the font asset as the same glyph might be referenced by multiple characters.
  1372. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex))
  1373. {
  1374. // Add a reference to the source text asset and glyph
  1375. character.glyph = m_GlyphLookupDictionary[glyphIndex];
  1376. character.textAsset = this;
  1377. m_CharacterTable.Add(character);
  1378. m_CharacterLookupDictionary.Add(unicode, character);
  1379. continue;
  1380. }
  1381. // Make sure glyph index has not already been added to list of glyphs to add.
  1382. if (m_GlyphsToAddLookup.Add(glyphIndex))
  1383. m_GlyphsToAdd.Add(glyphIndex);
  1384. // Make sure unicode / character has not already been added.
  1385. if (m_CharactersToAddLookup.Add(unicode))
  1386. m_CharactersToAdd.Add(character);
  1387. }
  1388. if (m_GlyphsToAdd.Count == 0)
  1389. {
  1390. missingCharacters = characters;
  1391. Profiler.EndSample();
  1392. return false;
  1393. }
  1394. // Resize the Atlas Texture to the appropriate size
  1395. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1396. {
  1397. //Debug.Log("Setting initial size of atlas texture used by font asset [" + this.name + "].");
  1398. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1399. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1400. }
  1401. Glyph[] glyphs;
  1402. bool allGlyphsAddedToTexture = FontEngine.TryAddGlyphsToTexture(m_GlyphsToAdd, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex], out glyphs);
  1403. for (int i = 0; i < glyphs.Length && glyphs[i] != null; i++)
  1404. {
  1405. Glyph glyph = glyphs[i];
  1406. uint glyphIndex = glyph.index;
  1407. glyph.atlasIndex = m_AtlasTextureIndex;
  1408. // Add new glyph to glyph table.
  1409. m_GlyphTable.Add(glyph);
  1410. m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  1411. m_GlyphIndexListNewlyAdded.Add(glyphIndex);
  1412. m_GlyphIndexList.Add(glyphIndex);
  1413. }
  1414. // Clear glyph index list to track glyphs that were not added to the atlas texture
  1415. m_GlyphsToAdd.Clear();
  1416. // Add new characters to relevant data structures.
  1417. for (int i = 0; i < m_CharactersToAdd.Count; i++)
  1418. {
  1419. TMP_Character character = m_CharactersToAdd[i];
  1420. Glyph glyph;
  1421. if (m_GlyphLookupDictionary.TryGetValue(character.glyphIndex, out glyph) == false)
  1422. {
  1423. m_GlyphsToAdd.Add(character.glyphIndex);
  1424. continue;
  1425. }
  1426. // Add a reference to the source text asset and glyph
  1427. character.glyph = glyph;
  1428. character.textAsset = this;
  1429. m_CharacterTable.Add(character);
  1430. m_CharacterLookupDictionary.Add(character.unicode, character);
  1431. // Remove character from list to add
  1432. m_CharactersToAdd.RemoveAt(i);
  1433. i -= 1;
  1434. }
  1435. // Try adding glyphs that didn't fit in the current atlas texture to new atlas texture
  1436. if (m_IsMultiAtlasTexturesEnabled && allGlyphsAddedToTexture == false)
  1437. {
  1438. while (allGlyphsAddedToTexture == false)
  1439. allGlyphsAddedToTexture = TryAddGlyphsToNewAtlasTexture();
  1440. }
  1441. // Get Font Features for the given characters
  1442. if (includeFontFeatures)
  1443. UpdateGlyphAdjustmentRecords();
  1444. #if UNITY_EDITOR
  1445. // Makes the changes to the font asset persistent.
  1446. if (UnityEditor.EditorUtility.IsPersistent(this))
  1447. {
  1448. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1449. }
  1450. #endif
  1451. missingCharacters = string.Empty;
  1452. // Populate list of missing characters
  1453. for (int i = 0; i < m_CharactersToAdd.Count; i++)
  1454. {
  1455. TMP_Character character = m_CharactersToAdd[i];
  1456. s_MissingCharacterList.Add(character.unicode);
  1457. }
  1458. if (s_MissingCharacterList.Count > 0)
  1459. missingCharacters = s_MissingCharacterList.UintToString();
  1460. Profiler.EndSample();
  1461. return allGlyphsAddedToTexture && !isMissingCharacters;
  1462. }
  1463. // Functions to remove...
  1464. /*
  1465. /// <summary>
  1466. /// NOT USED CURRENTLY - Try adding character using Unicode value to font asset.
  1467. /// </summary>
  1468. /// <param name="unicode">The Unicode value of the character.</param>
  1469. /// <param name="character">The character data if successfully added to the font asset. Null otherwise.</param>
  1470. /// <returns>Returns true if the character has been added. False otherwise.</returns>
  1471. internal bool TryAddCharacter_Internal(uint unicode)
  1472. {
  1473. TMP_Character character = null;
  1474. // Check if character is already contained in the character table.
  1475. if (m_CharacterLookupDictionary.ContainsKey(unicode))
  1476. return true;
  1477. uint glyphIndex = FontEngine.GetGlyphIndex(unicode);
  1478. if (glyphIndex == 0)
  1479. return false;
  1480. // Check if glyph is already contained in the font asset as the same glyph might be referenced by multiple characters.
  1481. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex))
  1482. {
  1483. character = new TMP_Character(unicode, m_GlyphLookupDictionary[glyphIndex]);
  1484. m_CharacterTable.Add(character);
  1485. m_CharacterLookupDictionary.Add(unicode, character);
  1486. //#if UNITY_EDITOR
  1487. // Makes the changes to the font asset persistent.
  1488. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1489. // Could also add some update registry to handle this.
  1490. //SortGlyphTable();
  1491. //UnityEditor.EditorUtility.SetDirty(this);
  1492. //#endif
  1493. return true;
  1494. }
  1495. // Resize the Atlas Texture to the appropriate size
  1496. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1497. {
  1498. //Debug.Log("Setting initial size of atlas texture used by font asset [" + this.name + "].");
  1499. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1500. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1501. }
  1502. Glyph glyph;
  1503. if (FontEngine.TryAddGlyphToTexture(glyphIndex, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex], out glyph))
  1504. {
  1505. // Add new glyph to glyph table.
  1506. m_GlyphTable.Add(glyph);
  1507. m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  1508. // Add new character
  1509. character = new TMP_Character(unicode, glyph);
  1510. m_CharacterTable.Add(character);
  1511. m_CharacterLookupDictionary.Add(unicode, character);
  1512. //#if UNITY_EDITOR
  1513. // Makes the changes to the font asset persistent.
  1514. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1515. // Could also add some update registry to handle this.
  1516. //SortGlyphTable();
  1517. //UnityEditor.EditorUtility.SetDirty(this);
  1518. //#endif
  1519. return true;
  1520. }
  1521. return false;
  1522. }
  1523. /// <summary>
  1524. /// To be removed.
  1525. /// </summary>
  1526. /// <param name="unicode"></param>
  1527. /// <param name="glyph"></param>
  1528. internal TMP_Character AddCharacter_Internal(uint unicode, Glyph glyph)
  1529. {
  1530. // Check if character is already contained in the character table.
  1531. if (m_CharacterLookupDictionary.ContainsKey(unicode))
  1532. return m_CharacterLookupDictionary[unicode];
  1533. uint glyphIndex = glyph.index;
  1534. // Resize the Atlas Texture to the appropriate size
  1535. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1536. {
  1537. //Debug.Log("Setting initial size of atlas texture used by font asset [" + this.name + "].");
  1538. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1539. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1540. }
  1541. // Check if glyph is already contained in the glyph table.
  1542. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex) == false)
  1543. {
  1544. if (glyph.glyphRect.width == 0 || glyph.glyphRect.width == 0)
  1545. {
  1546. // Glyphs with zero width and / or height can be automatically added to font asset.
  1547. m_GlyphTable.Add(glyph);
  1548. }
  1549. else
  1550. {
  1551. // Try packing new glyph
  1552. if (FontEngine.TryPackGlyphInAtlas(glyph, m_AtlasPadding, GlyphPackingMode.ContactPointRule, m_AtlasRenderMode, m_AtlasWidth, m_AtlasHeight, m_FreeGlyphRects, m_UsedGlyphRects) == false)
  1553. {
  1554. // TODO: Add handling to create new atlas texture to fit glyph.
  1555. return null;
  1556. }
  1557. //m_GlyphsToRender.Add(glyph);
  1558. }
  1559. }
  1560. // Add character to font asset.
  1561. TMP_Character character = new TMP_Character(unicode, glyph);
  1562. m_CharacterTable.Add(character);
  1563. m_CharacterLookupDictionary.Add(unicode, character);
  1564. //Debug.Log("Adding character [" + (char)unicode + "] with Unicode (" + unicode + ") to [" + this.name + "] font asset.");
  1565. // Schedule glyph to be added to the font atlas texture
  1566. //TM_FontAssetUpdateManager.RegisterFontAssetForUpdate(this);
  1567. //UpdateAtlasTexture(); // Temporary until callback system is revised.
  1568. //#if UNITY_EDITOR
  1569. // Makes the changes to the font asset persistent.
  1570. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1571. // Could also add some update registry to handle this.
  1572. //SortGlyphTable();
  1573. //UnityEditor.EditorUtility.SetDirty(this);
  1574. //#endif
  1575. return character;
  1576. }
  1577. */
  1578. /// <summary>
  1579. /// Try adding character using Unicode value to font asset.
  1580. /// Function assumes internal user has already checked to make sure the character is not already contained in the font asset.
  1581. /// </summary>
  1582. /// <param name="unicode">The Unicode value of the character.</param>
  1583. /// <param name="character">The character data if successfully added to the font asset. Null otherwise.</param>
  1584. /// <returns>Returns true if the character has been added. False otherwise.</returns>
  1585. internal bool TryAddCharacterInternal(uint unicode, out TMP_Character character)
  1586. {
  1587. Profiler.BeginSample("TMP.TryAddCharacter");
  1588. character = null;
  1589. // Check if the Unicode character is already known to be missing from the source font file.
  1590. if (m_MissingUnicodesFromFontFile.Contains(unicode))
  1591. {
  1592. Profiler.EndSample();
  1593. return false;
  1594. }
  1595. // Load font face.
  1596. if (FontEngine.LoadFontFace(sourceFontFile, m_FaceInfo.pointSize) != FontEngineError.Success)
  1597. return false;
  1598. uint glyphIndex = FontEngine.GetGlyphIndex(unicode);
  1599. if (glyphIndex == 0)
  1600. {
  1601. // Special handling for characters with potential alternative glyph representations
  1602. switch (unicode)
  1603. {
  1604. case 0xA0: // Non Breaking Space <NBSP>
  1605. // Use Space
  1606. glyphIndex = FontEngine.GetGlyphIndex(0x20);
  1607. break;
  1608. case 0xAD: // Soft Hyphen <SHY>
  1609. case 0x2011: // Non Breaking Hyphen
  1610. // Use Hyphen Minus
  1611. glyphIndex = FontEngine.GetGlyphIndex(0x2D);
  1612. break;
  1613. }
  1614. // Return if no potential alternative glyph representation is present in font file.
  1615. if (glyphIndex == 0)
  1616. {
  1617. m_MissingUnicodesFromFontFile.Add(unicode);
  1618. Profiler.EndSample();
  1619. return false;
  1620. }
  1621. }
  1622. // Check if glyph is already contained in the font asset as the same glyph might be referenced by multiple characters.
  1623. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex))
  1624. {
  1625. character = new TMP_Character(unicode, this, m_GlyphLookupDictionary[glyphIndex]);
  1626. m_CharacterTable.Add(character);
  1627. m_CharacterLookupDictionary.Add(unicode, character);
  1628. #if UNITY_EDITOR
  1629. // Makes the changes to the font asset persistent.
  1630. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1631. // Could also add some update registry to handle this.
  1632. //SortGlyphTable();
  1633. if (UnityEditor.EditorUtility.IsPersistent(this))
  1634. {
  1635. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1636. }
  1637. #endif
  1638. Profiler.EndSample();
  1639. return true;
  1640. }
  1641. Glyph glyph = null;
  1642. // TODO: Potential new feature to explore where selected font assets share the same atlas texture(s).
  1643. // Handling if using Dynamic Textures
  1644. //if (m_IsUsingDynamicTextures)
  1645. //{
  1646. // if (TMP_DynamicAtlasTextureGroup.AddGlyphToManagedDynamicTexture(this, glyphIndex, m_AtlasPadding, m_AtlasRenderMode, out glyph))
  1647. // {
  1648. // // Add new glyph to glyph table.
  1649. // m_GlyphTable.Add(glyph);
  1650. // m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  1651. // // Add new character
  1652. // character = new TMP_Character(unicode, glyph);
  1653. // m_CharacterTable.Add(character);
  1654. // m_CharacterLookupDictionary.Add(unicode, character);
  1655. // m_GlyphIndexList.Add(glyphIndex);
  1656. // if (TMP_Settings.getFontFeaturesAtRuntime)
  1657. // {
  1658. // if (k_FontAssetsToUpdateLookup.Add(instanceID))
  1659. // k_FontAssetsToUpdate.Add(this);
  1660. // }
  1661. // //Profiler.EndSample();
  1662. // return true;
  1663. // }
  1664. //}
  1665. // Resize the Atlas Texture to the appropriate size
  1666. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1667. {
  1668. // TODO: Need texture to be readable.
  1669. if (m_AtlasTextures[m_AtlasTextureIndex].isReadable == false)
  1670. {
  1671. 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]);
  1672. Profiler.EndSample();
  1673. return false;
  1674. }
  1675. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1676. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1677. }
  1678. // Try adding glyph to local atlas texture
  1679. if (FontEngine.TryAddGlyphToTexture(glyphIndex, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex], out glyph))
  1680. {
  1681. // Update glyph atlas index
  1682. glyph.atlasIndex = m_AtlasTextureIndex;
  1683. // Add new glyph to glyph table.
  1684. m_GlyphTable.Add(glyph);
  1685. m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  1686. // Add new character
  1687. character = new TMP_Character(unicode, this, glyph);
  1688. m_CharacterTable.Add(character);
  1689. m_CharacterLookupDictionary.Add(unicode, character);
  1690. m_GlyphIndexList.Add(glyphIndex);
  1691. m_GlyphIndexListNewlyAdded.Add(glyphIndex);
  1692. if (TMP_Settings.getFontFeaturesAtRuntime)
  1693. RegisterFontAssetForFontFeatureUpdate(this);
  1694. #if UNITY_EDITOR
  1695. // Makes the changes to the font asset persistent.
  1696. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1697. // Could also add some update registry to handle this.
  1698. //SortGlyphTable();
  1699. if (UnityEditor.EditorUtility.IsPersistent(this))
  1700. {
  1701. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1702. }
  1703. #endif
  1704. Profiler.EndSample();
  1705. return true;
  1706. }
  1707. // Add glyph which did not fit in current atlas texture to new atlas texture.
  1708. if (m_IsMultiAtlasTexturesEnabled)
  1709. {
  1710. // Create new atlas texture
  1711. SetupNewAtlasTexture();
  1712. // Try adding glyph to newly created atlas texture
  1713. if (FontEngine.TryAddGlyphToTexture(glyphIndex, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex], out glyph))
  1714. {
  1715. // Update glyph atlas index
  1716. glyph.atlasIndex = m_AtlasTextureIndex;
  1717. // Add new glyph to glyph table.
  1718. m_GlyphTable.Add(glyph);
  1719. m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  1720. // Add new character
  1721. character = new TMP_Character(unicode, this, glyph);
  1722. m_CharacterTable.Add(character);
  1723. m_CharacterLookupDictionary.Add(unicode, character);
  1724. m_GlyphIndexList.Add(glyphIndex);
  1725. m_GlyphIndexListNewlyAdded.Add(glyphIndex);
  1726. if (TMP_Settings.getFontFeaturesAtRuntime)
  1727. RegisterFontAssetForFontFeatureUpdate(this);
  1728. #if UNITY_EDITOR
  1729. //SortGlyphTable();
  1730. if (UnityEditor.EditorUtility.IsPersistent(this))
  1731. {
  1732. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1733. }
  1734. #endif
  1735. Profiler.EndSample();
  1736. return true;
  1737. }
  1738. }
  1739. Profiler.EndSample();
  1740. return false;
  1741. }
  1742. internal bool TryGetCharacter_and_QueueRenderToTexture(uint unicode, out TMP_Character character)
  1743. {
  1744. Profiler.BeginSample("TMP.TryAddCharacter");
  1745. character = null;
  1746. // Check if the Unicode character is already known to be missing from the source font file.
  1747. if (m_MissingUnicodesFromFontFile.Contains(unicode))
  1748. {
  1749. Profiler.EndSample();
  1750. return false;
  1751. }
  1752. // Load font face.
  1753. if (FontEngine.LoadFontFace(sourceFontFile, m_FaceInfo.pointSize) != FontEngineError.Success)
  1754. return false;
  1755. uint glyphIndex = FontEngine.GetGlyphIndex(unicode);
  1756. if (glyphIndex == 0)
  1757. {
  1758. // Special handling for characters with potential alternative glyph representations
  1759. switch (unicode)
  1760. {
  1761. case 0xA0: // Non Breaking Space <NBSP>
  1762. // Use Space
  1763. glyphIndex = FontEngine.GetGlyphIndex(0x20);
  1764. break;
  1765. case 0xAD: // Soft Hyphen <SHY>
  1766. case 0x2011: // Non Breaking Hyphen
  1767. // Use Hyphen Minus
  1768. glyphIndex = FontEngine.GetGlyphIndex(0x2D);
  1769. break;
  1770. }
  1771. // Return if no potential alternative glyph representation is present in font file.
  1772. if (glyphIndex == 0)
  1773. {
  1774. m_MissingUnicodesFromFontFile.Add(unicode);
  1775. Profiler.EndSample();
  1776. return false;
  1777. }
  1778. }
  1779. // Check if glyph is already contained in the font asset as the same glyph might be referenced by multiple characters.
  1780. if (m_GlyphLookupDictionary.ContainsKey(glyphIndex))
  1781. {
  1782. character = new TMP_Character(unicode, this, m_GlyphLookupDictionary[glyphIndex]);
  1783. m_CharacterTable.Add(character);
  1784. m_CharacterLookupDictionary.Add(unicode, character);
  1785. #if UNITY_EDITOR
  1786. // Makes the changes to the font asset persistent.
  1787. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1788. // Could also add some update registry to handle this.
  1789. //SortGlyphTable();
  1790. if (UnityEditor.EditorUtility.IsPersistent(this))
  1791. {
  1792. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1793. }
  1794. #endif
  1795. Profiler.EndSample();
  1796. return true;
  1797. }
  1798. GlyphLoadFlags glyphLoadFlags = (GlyphRasterModes.RASTER_MODE_NO_HINTING & (GlyphRasterModes)m_AtlasRenderMode) == GlyphRasterModes.RASTER_MODE_NO_HINTING
  1799. ? GlyphLoadFlags.LOAD_NO_BITMAP | GlyphLoadFlags.LOAD_NO_HINTING
  1800. : GlyphLoadFlags.LOAD_NO_BITMAP;
  1801. Glyph glyph = null;
  1802. if (FontEngine.TryGetGlyphWithIndexValue(glyphIndex, glyphLoadFlags, out glyph))
  1803. {
  1804. // Add new glyph to glyph table.
  1805. m_GlyphTable.Add(glyph);
  1806. m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  1807. // Add new character
  1808. character = new TMP_Character(unicode, this, glyph);
  1809. m_CharacterTable.Add(character);
  1810. m_CharacterLookupDictionary.Add(unicode, character);
  1811. m_GlyphIndexList.Add(glyphIndex);
  1812. m_GlyphIndexListNewlyAdded.Add(glyphIndex);
  1813. if (TMP_Settings.getFontFeaturesAtRuntime)
  1814. RegisterFontAssetForFontFeatureUpdate(this);
  1815. // Add glyph to list of glyphs to be rendered
  1816. m_GlyphsToRender.Add(glyph);
  1817. // Register font asset to render and add glyphs to atlas textures
  1818. RegisterFontAssetForAtlasTextureUpdate(this);
  1819. #if UNITY_EDITOR
  1820. // Makes the changes to the font asset persistent.
  1821. // OPTIMIZATION: This could be handled when exiting Play mode if we added any new characters to the asset.
  1822. // Could also add some update registry to handle this.
  1823. //SortGlyphTable();
  1824. if (UnityEditor.EditorUtility.IsPersistent(this))
  1825. {
  1826. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1827. }
  1828. #endif
  1829. Profiler.EndSample();
  1830. return true;
  1831. }
  1832. Profiler.EndSample();
  1833. return false;
  1834. }
  1835. /// <summary>
  1836. /// This function requires an update to the TextCore:FontEngine
  1837. /// </summary>
  1838. internal void TryAddGlyphsToAtlasTextures()
  1839. {
  1840. /*
  1841. // Return if we don't have any glyphs to add.
  1842. if (m_GlyphsToRender.Count == 0)
  1843. return;
  1844. Profiler.BeginSample("TMP.TryAddGlyphsToAtlasTextures");
  1845. // Resize the Atlas Texture to the appropriate size
  1846. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1847. {
  1848. //Debug.Log("Setting initial size of atlas texture used by font asset [" + this.name + "].");
  1849. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1850. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1851. }
  1852. bool allGlyphsAddedToTexture = FontEngine.TryAddGlyphsToTexture(m_GlyphsToRender, m_GlyphsRendered, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex]);
  1853. // Try adding glyphs that didn't fit in the current atlas texture to new atlas texture
  1854. if (m_IsMultiAtlasTexturesEnabled && allGlyphsAddedToTexture == false)
  1855. {
  1856. while (allGlyphsAddedToTexture == false)
  1857. {
  1858. // Create and prepare new atlas texture
  1859. SetupNewAtlasTexture();
  1860. // Try adding remaining glyphs in the newly created atlas texture
  1861. allGlyphsAddedToTexture = FontEngine.TryAddGlyphsToTexture(m_GlyphsToRender, m_GlyphsRendered, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex]);
  1862. }
  1863. }
  1864. if (allGlyphsAddedToTexture == false)
  1865. {
  1866. // TODO: Handle case when we have left over glyph to render that didn't fit in the atlas texture.
  1867. Debug.LogError("Unable to add some glyphs to atlas texture.");
  1868. }
  1869. #if UNITY_EDITOR
  1870. // Makes the changes to the font asset persistent.
  1871. if (UnityEditor.EditorUtility.IsPersistent(this))
  1872. {
  1873. //SortGlyphAndCharacterTables();
  1874. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  1875. }
  1876. #endif
  1877. Profiler.EndSample();
  1878. */
  1879. }
  1880. /// <summary>
  1881. ///
  1882. /// </summary>
  1883. /// <returns></returns>
  1884. bool TryAddGlyphsToNewAtlasTexture()
  1885. {
  1886. // Create and prepare new atlas texture
  1887. SetupNewAtlasTexture();
  1888. Glyph[] glyphs;
  1889. // Try adding remaining glyphs in the newly created atlas texture
  1890. bool allGlyphsAddedToTexture = FontEngine.TryAddGlyphsToTexture(m_GlyphsToAdd, m_AtlasPadding, GlyphPackingMode.BestShortSideFit, m_FreeGlyphRects, m_UsedGlyphRects, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex], out glyphs);
  1891. // Add new glyphs to relevant data structures.
  1892. for (int i = 0; i < glyphs.Length && glyphs[i] != null; i++)
  1893. {
  1894. Glyph glyph = glyphs[i];
  1895. uint glyphIndex = glyph.index;
  1896. glyph.atlasIndex = m_AtlasTextureIndex;
  1897. // Add new glyph to glyph table.
  1898. m_GlyphTable.Add(glyph);
  1899. m_GlyphLookupDictionary.Add(glyphIndex, glyph);
  1900. m_GlyphIndexListNewlyAdded.Add(glyphIndex);
  1901. m_GlyphIndexList.Add(glyphIndex);
  1902. }
  1903. // Clear glyph index list to allow us to track glyphs
  1904. m_GlyphsToAdd.Clear();
  1905. // Add new characters to relevant data structures as well as track glyphs that could not be added to the current atlas texture.
  1906. for (int i = 0; i < m_CharactersToAdd.Count; i++)
  1907. {
  1908. TMP_Character character = m_CharactersToAdd[i];
  1909. Glyph glyph;
  1910. if (m_GlyphLookupDictionary.TryGetValue(character.glyphIndex, out glyph) == false)
  1911. {
  1912. m_GlyphsToAdd.Add(character.glyphIndex);
  1913. continue;
  1914. }
  1915. // Add a reference to the source text asset and glyph
  1916. character.glyph = glyph;
  1917. character.textAsset = this;
  1918. m_CharacterTable.Add(character);
  1919. m_CharacterLookupDictionary.Add(character.unicode, character);
  1920. // Remove character
  1921. m_CharactersToAdd.RemoveAt(i);
  1922. i -= 1;
  1923. }
  1924. return allGlyphsAddedToTexture;
  1925. }
  1926. /// <summary>
  1927. ///
  1928. /// </summary>
  1929. void SetupNewAtlasTexture()
  1930. {
  1931. m_AtlasTextureIndex += 1;
  1932. // Check size of atlas texture array
  1933. if (m_AtlasTextures.Length == m_AtlasTextureIndex)
  1934. Array.Resize(ref m_AtlasTextures, m_AtlasTextures.Length * 2);
  1935. // Initialize new atlas texture
  1936. m_AtlasTextures[m_AtlasTextureIndex] = new Texture2D(m_AtlasWidth, m_AtlasHeight, TextureFormat.Alpha8, false);
  1937. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1938. // Clear packing GlyphRects
  1939. int packingModifier = ((GlyphRasterModes)m_AtlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP ? 0 : 1;
  1940. m_FreeGlyphRects.Clear();
  1941. m_FreeGlyphRects.Add(new GlyphRect(0, 0, m_AtlasWidth - packingModifier, m_AtlasHeight - packingModifier));
  1942. m_UsedGlyphRects.Clear();
  1943. #if UNITY_EDITOR
  1944. // Add new texture as sub asset to font asset
  1945. if (UnityEditor.EditorUtility.IsPersistent(this))
  1946. {
  1947. Texture2D tex = m_AtlasTextures[m_AtlasTextureIndex];
  1948. tex.name = m_AtlasTexture.name + " " + m_AtlasTextureIndex;
  1949. UnityEditor.AssetDatabase.AddObjectToAsset(m_AtlasTextures[m_AtlasTextureIndex], this);
  1950. TMP_EditorResourceManager.RegisterResourceForReimport(this);
  1951. }
  1952. #endif
  1953. }
  1954. /// <summary>
  1955. /// Not used currently
  1956. /// </summary>
  1957. internal void UpdateAtlasTexture()
  1958. {
  1959. // Return if we don't have any glyphs to add to atlas texture.
  1960. if (m_GlyphsToRender.Count == 0)
  1961. return;
  1962. //Debug.Log("Updating [" + this.name + "]'s atlas texture.");
  1963. // Pack glyphs in the given atlas texture size.
  1964. // TODO: Packing and glyph render modes should be defined in the font asset.
  1965. //FontEngine.PackGlyphsInAtlas(m_GlyphsToPack, m_GlyphsPacked, m_AtlasPadding, GlyphPackingMode.ContactPointRule, GlyphRenderMode.SDFAA, m_AtlasWidth, m_AtlasHeight, m_FreeGlyphRects, m_UsedGlyphRects);
  1966. //FontEngine.RenderGlyphsToTexture(m_GlyphsPacked, m_AtlasPadding, GlyphRenderMode.SDFAA, m_AtlasTextures[m_AtlasTextureIndex]);
  1967. // Resize the Atlas Texture to the appropriate size
  1968. if (m_AtlasTextures[m_AtlasTextureIndex].width == 0 || m_AtlasTextures[m_AtlasTextureIndex].height == 0)
  1969. {
  1970. //Debug.Log("Setting initial size of atlas texture used by font asset [" + this.name + "].");
  1971. m_AtlasTextures[m_AtlasTextureIndex].Resize(m_AtlasWidth, m_AtlasHeight);
  1972. FontEngine.ResetAtlasTexture(m_AtlasTextures[m_AtlasTextureIndex]);
  1973. }
  1974. //FontEngine.RenderGlyphsToTexture(m_GlyphsToRender, m_AtlasPadding, m_AtlasRenderMode, m_AtlasTextures[m_AtlasTextureIndex]);
  1975. // Apply changes to atlas texture
  1976. m_AtlasTextures[m_AtlasTextureIndex].Apply(false, false);
  1977. // Add glyphs that were successfully packed to the glyph table.
  1978. //for (int i = 0; i < m_GlyphsToRender.Count /* m_GlyphsPacked.Count */; i++)
  1979. //{
  1980. // Glyph glyph = m_GlyphsToRender[i]; // m_GlyphsPacked[i];
  1981. // // Update atlas texture index
  1982. // glyph.atlasIndex = m_AtlasTextureIndex;
  1983. // m_GlyphTable.Add(glyph);
  1984. // m_GlyphLookupDictionary.Add(glyph.index, glyph);
  1985. //}
  1986. // Clear list of glyphs
  1987. //m_GlyphsPacked.Clear();
  1988. //m_GlyphsToRender.Clear();
  1989. // Add any remaining glyphs into new atlas texture if multi texture support if enabled.
  1990. //if (m_GlyphsToPack.Count > 0)
  1991. //{
  1992. /*
  1993. // Create new atlas texture
  1994. Texture2D tex = new Texture2D(m_AtlasWidth, m_AtlasHeight, TextureFormat.Alpha8, false, true);
  1995. tex.SetPixels32(new Color32[m_AtlasWidth * m_AtlasHeight]);
  1996. tex.Apply();
  1997. m_AtlasTextureIndex++;
  1998. if (m_AtlasTextures.Length == m_AtlasTextureIndex)
  1999. Array.Resize(ref m_AtlasTextures, Mathf.NextPowerOfTwo(m_AtlasTextureIndex + 1));
  2000. m_AtlasTextures[m_AtlasTextureIndex] = tex;
  2001. */
  2002. //}
  2003. #if UNITY_EDITOR
  2004. // Makes the changes to the font asset persistent.
  2005. //SortAllTables();
  2006. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  2007. #endif
  2008. }
  2009. /// <summary>
  2010. ///
  2011. /// </summary>
  2012. internal void UpdateGlyphAdjustmentRecords()
  2013. {
  2014. Profiler.BeginSample("TMP.UpdateGlyphAdjustmentRecords");
  2015. // TODO: This copying of glyph index from list to array is temporary and will be replaced once an updated version of the FontEngine is released.
  2016. CopyListDataToArray(m_GlyphIndexList, ref k_GlyphIndexArray);
  2017. // Get glyph pair adjustment records from font file.
  2018. GlyphPairAdjustmentRecord[] pairAdjustmentRecords = FontEngine.GetGlyphPairAdjustmentTable(k_GlyphIndexArray);
  2019. // TODO: The GetGlyphPairAdjustmentTable will be replaced by the more efficient GetGlyphPairAdjustmentRecords once the updated version of the FontEngine is released.
  2020. //GlyphPairAdjustmentRecord[] pairAdjustmentRecords = FontEngine.GetGlyphPairAdjustmentRecords(m_GlyphIndexListNewlyAdded, m_GlyphIndexList);
  2021. // Clear newly added glyph list
  2022. m_GlyphIndexListNewlyAdded.Clear();
  2023. if (pairAdjustmentRecords == null || pairAdjustmentRecords.Length == 0)
  2024. {
  2025. Profiler.EndSample();
  2026. return;
  2027. }
  2028. if (m_FontFeatureTable == null)
  2029. m_FontFeatureTable = new TMP_FontFeatureTable();
  2030. for (int i = 0; i < pairAdjustmentRecords.Length && pairAdjustmentRecords[i].firstAdjustmentRecord.glyphIndex != 0; i++)
  2031. {
  2032. uint pairKey = pairAdjustmentRecords[i].secondAdjustmentRecord.glyphIndex << 16 | pairAdjustmentRecords[i].firstAdjustmentRecord.glyphIndex;
  2033. // Check if table already contains a pair adjustment record for this key.
  2034. if (m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.ContainsKey(pairKey))
  2035. continue;
  2036. TMP_GlyphPairAdjustmentRecord record = new TMP_GlyphPairAdjustmentRecord(pairAdjustmentRecords[i]);
  2037. m_FontFeatureTable.m_GlyphPairAdjustmentRecords.Add(record);
  2038. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Add(pairKey, record);
  2039. }
  2040. Profiler.EndSample();
  2041. }
  2042. /// <summary>
  2043. /// Function used for debugging and performance testing.
  2044. /// </summary>
  2045. /// <param name="glyphIndexes"></param>
  2046. internal void UpdateGlyphAdjustmentRecords(uint[] glyphIndexes)
  2047. {
  2048. Profiler.BeginSample("TMP.UpdateGlyphAdjustmentRecords");
  2049. // Get glyph pair adjustment records from font file.
  2050. GlyphPairAdjustmentRecord[] pairAdjustmentRecords = FontEngine.GetGlyphPairAdjustmentTable(glyphIndexes);
  2051. // Clear newly added glyph list
  2052. //m_GlyphIndexListNewlyAdded.Clear();
  2053. if (pairAdjustmentRecords == null || pairAdjustmentRecords.Length == 0)
  2054. {
  2055. Profiler.EndSample();
  2056. return;
  2057. }
  2058. if (m_FontFeatureTable == null)
  2059. m_FontFeatureTable = new TMP_FontFeatureTable();
  2060. for (int i = 0; i < pairAdjustmentRecords.Length && pairAdjustmentRecords[i].firstAdjustmentRecord.glyphIndex != 0; i++)
  2061. {
  2062. uint pairKey = pairAdjustmentRecords[i].secondAdjustmentRecord.glyphIndex << 16 | pairAdjustmentRecords[i].firstAdjustmentRecord.glyphIndex;
  2063. // Check if table already contains a pair adjustment record for this key.
  2064. if (m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.ContainsKey(pairKey))
  2065. continue;
  2066. TMP_GlyphPairAdjustmentRecord record = new TMP_GlyphPairAdjustmentRecord(pairAdjustmentRecords[i]);
  2067. m_FontFeatureTable.m_GlyphPairAdjustmentRecords.Add(record);
  2068. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Add(pairKey, record);
  2069. }
  2070. Profiler.EndSample();
  2071. }
  2072. /// <summary>
  2073. /// Function requires an update to the TextCore:FontEngine
  2074. /// </summary>
  2075. /// <param name="glyphIndexes"></param>
  2076. internal void UpdateGlyphAdjustmentRecords(List<uint> glyphIndexes)
  2077. {
  2078. /*
  2079. Profiler.BeginSample("TMP.UpdateGlyphAdjustmentRecords");
  2080. // Get glyph pair adjustment records from font file.
  2081. int recordCount;
  2082. GlyphPairAdjustmentRecord[] pairAdjustmentRecords = FontEngine.GetGlyphPairAdjustmentRecords(glyphIndexes, out recordCount);
  2083. // Clear newly added glyph list
  2084. //m_GlyphIndexListNewlyAdded.Clear();
  2085. if (pairAdjustmentRecords == null || pairAdjustmentRecords.Length == 0)
  2086. {
  2087. Profiler.EndSample();
  2088. return;
  2089. }
  2090. if (m_FontFeatureTable == null)
  2091. m_FontFeatureTable = new TMP_FontFeatureTable();
  2092. for (int i = 0; i < pairAdjustmentRecords.Length && pairAdjustmentRecords[i].firstAdjustmentRecord.glyphIndex != 0; i++)
  2093. {
  2094. uint pairKey = pairAdjustmentRecords[i].secondAdjustmentRecord.glyphIndex << 16 | pairAdjustmentRecords[i].firstAdjustmentRecord.glyphIndex;
  2095. // Check if table already contains a pair adjustment record for this key.
  2096. if (m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.ContainsKey(pairKey))
  2097. continue;
  2098. TMP_GlyphPairAdjustmentRecord record = new TMP_GlyphPairAdjustmentRecord(pairAdjustmentRecords[i]);
  2099. m_FontFeatureTable.m_GlyphPairAdjustmentRecords.Add(record);
  2100. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Add(pairKey, record);
  2101. }
  2102. #if UNITY_EDITOR
  2103. m_FontFeatureTable.SortGlyphPairAdjustmentRecords();
  2104. #endif
  2105. Profiler.EndSample();
  2106. */
  2107. }
  2108. /// <summary>
  2109. /// Function requires an update to the TextCore:FontEngine
  2110. /// </summary>
  2111. /// <param name="newGlyphIndexes"></param>
  2112. /// <param name="allGlyphIndexes"></param>
  2113. internal void UpdateGlyphAdjustmentRecords(List<uint> newGlyphIndexes, List<uint> allGlyphIndexes)
  2114. {
  2115. /*
  2116. Profiler.BeginSample("TMP.UpdateGlyphAdjustmentRecords");
  2117. // Get glyph pair adjustment records from font file.
  2118. GlyphPairAdjustmentRecord[] pairAdjustmentRecords = FontEngine.GetGlyphPairAdjustmentRecords(newGlyphIndexes, allGlyphIndexes);
  2119. // Clear newly added glyph list
  2120. m_GlyphIndexListNewlyAdded.Clear();
  2121. if (pairAdjustmentRecords == null || pairAdjustmentRecords.Length == 0)
  2122. {
  2123. Profiler.EndSample();
  2124. return;
  2125. }
  2126. if (m_FontFeatureTable == null)
  2127. m_FontFeatureTable = new TMP_FontFeatureTable();
  2128. for (int i = 0; i < pairAdjustmentRecords.Length && pairAdjustmentRecords[i].firstAdjustmentRecord.glyphIndex != 0; i++)
  2129. {
  2130. uint pairKey = pairAdjustmentRecords[i].secondAdjustmentRecord.glyphIndex << 16 | pairAdjustmentRecords[i].firstAdjustmentRecord.glyphIndex;
  2131. // Check if table already contains a pair adjustment record for this key.
  2132. if (m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.ContainsKey(pairKey))
  2133. continue;
  2134. TMP_GlyphPairAdjustmentRecord record = new TMP_GlyphPairAdjustmentRecord(pairAdjustmentRecords[i]);
  2135. m_FontFeatureTable.m_GlyphPairAdjustmentRecords.Add(record);
  2136. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Add(pairKey, record);
  2137. }
  2138. #if UNITY_EDITOR
  2139. m_FontFeatureTable.SortGlyphPairAdjustmentRecords();
  2140. #endif
  2141. Profiler.EndSample();
  2142. */
  2143. }
  2144. /// <summary>
  2145. /// Internal method to copy generic list data to generic array of the same type.
  2146. /// </summary>
  2147. /// <param name="srcList">Source</param>
  2148. /// <param name="dstArray">Destination</param>
  2149. /// <typeparam name="T">Element type</typeparam>
  2150. void CopyListDataToArray<T>(List<T> srcList, ref T[] dstArray)
  2151. {
  2152. int size = srcList.Count;
  2153. // Make sure destination array is appropriately sized.
  2154. if (dstArray == null)
  2155. dstArray = new T[size];
  2156. else
  2157. Array.Resize(ref dstArray, size);
  2158. for (int i = 0; i < size; i++)
  2159. dstArray[i] = srcList[i];
  2160. }
  2161. /// <summary>
  2162. /// Clears font asset data including the glyph and character tables and textures.
  2163. /// Function might be changed to Internal and only used in tests.
  2164. /// </summary>
  2165. /// <param name="setAtlasSizeToZero">Will set the atlas texture size to zero width and height if true.</param>
  2166. public void ClearFontAssetData(bool setAtlasSizeToZero = false)
  2167. {
  2168. Profiler.BeginSample("TMP.ClearFontAssetData");
  2169. #if UNITY_EDITOR
  2170. // Record full object undo in the Editor.
  2171. //UnityEditor.Undo.RecordObjects(new UnityEngine.Object[] { this, this.atlasTexture }, "Resetting Font Asset");
  2172. #endif
  2173. // Clear glyph, character and font feature tables
  2174. ClearFontAssetTables();
  2175. // Clear atlas textures
  2176. ClearAtlasTextures(setAtlasSizeToZero);
  2177. ReadFontAssetDefinition();
  2178. //TMP_ResourceManager.RebuildFontAssetCache(instanceID);
  2179. #if UNITY_EDITOR
  2180. // Makes the changes to the font asset persistent.
  2181. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  2182. #endif
  2183. Profiler.EndSample();
  2184. }
  2185. /// <summary>
  2186. ///
  2187. /// </summary>
  2188. internal void UpdateFontAssetData()
  2189. {
  2190. Profiler.BeginSample("TMP.UpdateFontAssetData");
  2191. // Get list of all characters currently contained in the font asset.
  2192. uint[] unicodeCharacters = new uint[m_CharacterTable.Count];
  2193. for (int i = 0; i < m_CharacterTable.Count; i++)
  2194. unicodeCharacters[i] = m_CharacterTable[i].unicode;
  2195. // Clear glyph, character and font feature tables
  2196. ClearFontAssetTables();
  2197. // Clear atlas textures
  2198. ClearAtlasTextures(true);
  2199. ReadFontAssetDefinition();
  2200. //TMP_ResourceManager.RebuildFontAssetCache(instanceID);
  2201. // Add existing glyphs and characters back in the font asset (if any)
  2202. if (unicodeCharacters.Length > 0)
  2203. TryAddCharacters(unicodeCharacters, true);
  2204. Profiler.EndSample();
  2205. }
  2206. /// <summary>
  2207. ///
  2208. /// </summary>
  2209. internal void ClearFontAssetTables()
  2210. {
  2211. // Clear glyph and character tables
  2212. if (m_GlyphTable != null)
  2213. m_GlyphTable.Clear();
  2214. if (m_CharacterTable != null)
  2215. m_CharacterTable.Clear();
  2216. // Clear glyph rectangles
  2217. if (m_UsedGlyphRects != null)
  2218. m_UsedGlyphRects.Clear();
  2219. if (m_FreeGlyphRects != null)
  2220. {
  2221. int packingModifier = ((GlyphRasterModes)m_AtlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP ? 0 : 1;
  2222. m_FreeGlyphRects.Clear();
  2223. m_FreeGlyphRects.Add(new GlyphRect(0, 0, m_AtlasWidth - packingModifier, m_AtlasHeight - packingModifier));
  2224. }
  2225. if (m_GlyphsToRender != null)
  2226. m_GlyphsToRender.Clear();
  2227. if (m_GlyphsRendered != null)
  2228. m_GlyphsRendered.Clear();
  2229. // Clear Glyph Adjustment Table
  2230. if (m_FontFeatureTable != null && m_FontFeatureTable.m_GlyphPairAdjustmentRecords != null)
  2231. m_FontFeatureTable.glyphPairAdjustmentRecords.Clear();
  2232. }
  2233. /// <summary>
  2234. /// Internal function to clear all atlas textures.
  2235. /// </summary>
  2236. /// <param name="setAtlasSizeToZero">Set main atlas texture size to zero if true.</param>
  2237. internal void ClearAtlasTextures(bool setAtlasSizeToZero = false)
  2238. {
  2239. m_AtlasTextureIndex = 0;
  2240. // Return if we don't have any atlas textures
  2241. if (m_AtlasTextures == null)
  2242. return;
  2243. // Clear all atlas textures
  2244. for (int i = 0; i < m_AtlasTextures.Length; i++)
  2245. {
  2246. Texture2D texture = m_AtlasTextures[i];
  2247. if (i > 0 && texture != null)
  2248. {
  2249. DestroyImmediate(texture, true);
  2250. #if UNITY_EDITOR
  2251. if (UnityEditor.EditorUtility.IsPersistent(this))
  2252. TMP_EditorResourceManager.RegisterResourceForReimport(this);
  2253. #endif
  2254. }
  2255. if (texture == null)
  2256. continue;
  2257. // TODO: Need texture to be readable.
  2258. if (m_AtlasTextures[i].isReadable == false)
  2259. {
  2260. #if UNITY_EDITOR
  2261. FontEngineEditorUtilities.SetAtlasTextureIsReadable(m_AtlasTextures[i], true);
  2262. #endif
  2263. }
  2264. if (setAtlasSizeToZero)
  2265. {
  2266. texture.Resize(0, 0, TextureFormat.Alpha8, false);
  2267. }
  2268. else if (texture.width != m_AtlasWidth || texture.height != m_AtlasHeight)
  2269. {
  2270. texture.Resize(m_AtlasWidth, m_AtlasHeight, TextureFormat.Alpha8, false);
  2271. }
  2272. // Clear texture atlas
  2273. FontEngine.ResetAtlasTexture(texture);
  2274. texture.Apply();
  2275. if (i == 0)
  2276. m_AtlasTexture = texture;
  2277. m_AtlasTextures[i] = texture;
  2278. }
  2279. }
  2280. /// <summary>
  2281. /// Internal method used to upgrade font asset to support Dynamic SDF.
  2282. /// </summary>
  2283. internal void UpgradeFontAsset()
  2284. {
  2285. m_Version = "1.1.0";
  2286. Debug.Log("Upgrading font asset [" + this.name + "] to version " + m_Version + ".", this);
  2287. m_FaceInfo.familyName = m_fontInfo.Name;
  2288. m_FaceInfo.styleName = string.Empty;
  2289. m_FaceInfo.pointSize = (int)m_fontInfo.PointSize;
  2290. m_FaceInfo.scale = m_fontInfo.Scale;
  2291. m_FaceInfo.lineHeight = m_fontInfo.LineHeight;
  2292. m_FaceInfo.ascentLine = m_fontInfo.Ascender;
  2293. m_FaceInfo.capLine = m_fontInfo.CapHeight;
  2294. m_FaceInfo.meanLine = m_fontInfo.CenterLine;
  2295. m_FaceInfo.baseline = m_fontInfo.Baseline;
  2296. m_FaceInfo.descentLine = m_fontInfo.Descender;
  2297. m_FaceInfo.superscriptOffset = m_fontInfo.SuperscriptOffset;
  2298. m_FaceInfo.superscriptSize = m_fontInfo.SubSize;
  2299. m_FaceInfo.subscriptOffset = m_fontInfo.SubscriptOffset;
  2300. m_FaceInfo.subscriptSize = m_fontInfo.SubSize;
  2301. m_FaceInfo.underlineOffset = m_fontInfo.Underline;
  2302. m_FaceInfo.underlineThickness = m_fontInfo.UnderlineThickness;
  2303. m_FaceInfo.strikethroughOffset = m_fontInfo.strikethrough;
  2304. m_FaceInfo.strikethroughThickness = m_fontInfo.strikethroughThickness;
  2305. m_FaceInfo.tabWidth = m_fontInfo.TabWidth;
  2306. if (m_AtlasTextures == null || m_AtlasTextures.Length == 0)
  2307. m_AtlasTextures = new Texture2D[1];
  2308. m_AtlasTextures[0] = atlas;
  2309. //atlas = null;
  2310. m_AtlasWidth = (int)m_fontInfo.AtlasWidth;
  2311. m_AtlasHeight = (int)m_fontInfo.AtlasHeight;
  2312. m_AtlasPadding = (int)m_fontInfo.Padding;
  2313. switch(m_CreationSettings.renderMode)
  2314. {
  2315. case 0:
  2316. m_AtlasRenderMode = GlyphRenderMode.SMOOTH_HINTED;
  2317. break;
  2318. case 1:
  2319. m_AtlasRenderMode = GlyphRenderMode.SMOOTH;
  2320. break;
  2321. case 2:
  2322. m_AtlasRenderMode = GlyphRenderMode.RASTER_HINTED;
  2323. break;
  2324. case 3:
  2325. m_AtlasRenderMode = GlyphRenderMode.RASTER;
  2326. break;
  2327. case 6:
  2328. m_AtlasRenderMode = GlyphRenderMode.SDF16;
  2329. break;
  2330. case 7:
  2331. m_AtlasRenderMode = GlyphRenderMode.SDF32;
  2332. break;
  2333. }
  2334. //m_fontInfo = null;
  2335. // Convert font weight table
  2336. if (fontWeights != null && fontWeights.Length > 0)
  2337. {
  2338. m_FontWeightTable[4] = fontWeights[4];
  2339. m_FontWeightTable[7] = fontWeights[7];
  2340. // Clear old fontWeight
  2341. //fontWeights = null;
  2342. }
  2343. // Convert font fallbacks
  2344. if (fallbackFontAssets != null && fallbackFontAssets.Count > 0)
  2345. {
  2346. if (m_FallbackFontAssetTable == null)
  2347. m_FallbackFontAssetTable = new List<TMP_FontAsset>(fallbackFontAssets.Count);
  2348. for (int i = 0; i < fallbackFontAssets.Count; i++)
  2349. m_FallbackFontAssetTable.Add(fallbackFontAssets[i]);
  2350. // Clear old fallbackFontAssets list
  2351. //fallbackFontAssets = null;
  2352. }
  2353. // Check if font asset creation settings contains a reference to the source font file GUID
  2354. if (m_CreationSettings.sourceFontFileGUID != null || m_CreationSettings.sourceFontFileGUID != string.Empty)
  2355. {
  2356. m_SourceFontFileGUID = m_CreationSettings.sourceFontFileGUID;
  2357. }
  2358. else
  2359. {
  2360. 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);
  2361. }
  2362. // Convert legacy glyph and character tables to new format
  2363. m_GlyphTable.Clear();
  2364. m_CharacterTable.Clear();
  2365. //#if UNITY_EDITOR
  2366. // TODO: This is causing a crash in Unity and related to AssetDatabase.LoadAssetAtPath and Resources.Load()
  2367. // Load font to allow us to get the glyph index.
  2368. //string path = UnityEditor.AssetDatabase.GUIDToAssetPath(m_SourceFontFileGUID);
  2369. //if (path != string.Empty)
  2370. //{
  2371. //m_SourceFontFile_EditorRef = UnityEditor.AssetDatabase.LoadAssetAtPath<Font>(path);
  2372. //FontEngine.LoadFontFace(m_SourceFontFile_EditorRef);
  2373. //}
  2374. //#endif
  2375. bool isSpaceCharacterPresent = false;
  2376. for (int i = 0; i < m_glyphInfoList.Count; i++)
  2377. {
  2378. TMP_Glyph oldGlyph = m_glyphInfoList[i];
  2379. Glyph glyph = new Glyph();
  2380. uint glyphIndex = (uint)i + 1;
  2381. //#if UNITY_EDITOR
  2382. //if (m_SourceFontFile_EditorRef != null)
  2383. // glyphIndex = FontEngine.GetGlyphIndex((uint)oldGlyph.id);
  2384. //#endif
  2385. glyph.index = glyphIndex;
  2386. 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));
  2387. glyph.metrics = new GlyphMetrics(oldGlyph.width, oldGlyph.height, oldGlyph.xOffset, oldGlyph.yOffset, oldGlyph.xAdvance);
  2388. glyph.scale = oldGlyph.scale;
  2389. glyph.atlasIndex = 0;
  2390. m_GlyphTable.Add(glyph);
  2391. TMP_Character character = new TMP_Character((uint)oldGlyph.id, this, glyph);
  2392. if (oldGlyph.id == 32)
  2393. isSpaceCharacterPresent = true;
  2394. m_CharacterTable.Add(character);
  2395. }
  2396. // Special handling for the synthesized space character
  2397. if (!isSpaceCharacterPresent)
  2398. {
  2399. Debug.Log("Synthesizing Space for [" + this.name + "]");
  2400. Glyph glyph = new Glyph(0, new GlyphMetrics(0, 0, 0, 0, m_FaceInfo.ascentLine / 5), GlyphRect.zero, 1.0f, 0);
  2401. m_GlyphTable.Add(glyph);
  2402. m_CharacterTable.Add(new TMP_Character(32, this, glyph));
  2403. }
  2404. // Clear legacy glyph info list.
  2405. //m_glyphInfoList.Clear();
  2406. ReadFontAssetDefinition();
  2407. // Convert atlas textures data to new format
  2408. // TODO
  2409. #if UNITY_EDITOR
  2410. if (UnityEditor.EditorUtility.IsPersistent(this))
  2411. {
  2412. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  2413. }
  2414. #endif
  2415. }
  2416. /// <summary>
  2417. ///
  2418. /// </summary>
  2419. void UpgradeGlyphAdjustmentTableToFontFeatureTable()
  2420. {
  2421. Debug.Log("Upgrading font asset [" + this.name + "] Glyph Adjustment Table.", this);
  2422. if (m_FontFeatureTable == null)
  2423. m_FontFeatureTable = new TMP_FontFeatureTable();
  2424. int pairCount = m_KerningTable.kerningPairs.Count;
  2425. m_FontFeatureTable.m_GlyphPairAdjustmentRecords = new List<TMP_GlyphPairAdjustmentRecord>(pairCount);
  2426. for (int i = 0; i < pairCount; i++)
  2427. {
  2428. KerningPair pair = m_KerningTable.kerningPairs[i];
  2429. uint firstGlyphIndex = 0;
  2430. TMP_Character firstCharacter;
  2431. if (m_CharacterLookupDictionary.TryGetValue(pair.firstGlyph, out firstCharacter))
  2432. firstGlyphIndex = firstCharacter.glyphIndex;
  2433. uint secondGlyphIndex = 0;
  2434. TMP_Character secondCharacter;
  2435. if (m_CharacterLookupDictionary.TryGetValue(pair.secondGlyph, out secondCharacter))
  2436. secondGlyphIndex = secondCharacter.glyphIndex;
  2437. TMP_GlyphAdjustmentRecord firstAdjustmentRecord = new TMP_GlyphAdjustmentRecord(firstGlyphIndex, new TMP_GlyphValueRecord(pair.firstGlyphAdjustments.xPlacement, pair.firstGlyphAdjustments.yPlacement, pair.firstGlyphAdjustments.xAdvance, pair.firstGlyphAdjustments.yAdvance));
  2438. TMP_GlyphAdjustmentRecord secondAdjustmentRecord = new TMP_GlyphAdjustmentRecord(secondGlyphIndex, new TMP_GlyphValueRecord(pair.secondGlyphAdjustments.xPlacement, pair.secondGlyphAdjustments.yPlacement, pair.secondGlyphAdjustments.xAdvance, pair.secondGlyphAdjustments.yAdvance));
  2439. TMP_GlyphPairAdjustmentRecord record = new TMP_GlyphPairAdjustmentRecord(firstAdjustmentRecord, secondAdjustmentRecord);
  2440. m_FontFeatureTable.m_GlyphPairAdjustmentRecords.Add(record);
  2441. }
  2442. // TODO: Should clear legacy kerning table.
  2443. m_KerningTable.kerningPairs = null;
  2444. m_KerningTable = null;
  2445. #if UNITY_EDITOR
  2446. if (UnityEditor.EditorUtility.IsPersistent(this))
  2447. {
  2448. TMP_EditorResourceManager.RegisterResourceForUpdate(this);
  2449. }
  2450. #endif
  2451. }
  2452. }
  2453. }