TMPro_UGUI_Private.cs 215 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314
  1. //#define TMP_PROFILE_ON
  2. //#define TMP_PROFILE_PHASES_ON
  3. using UnityEngine;
  4. using UnityEngine.TextCore;
  5. using System;
  6. using System.Collections.Generic;
  7. using UnityEngine.UI;
  8. #pragma warning disable 0414 // Disabled a few warnings related to serialized variables not used in this script but used in the editor.
  9. #pragma warning disable 0618 // Disabled warning due to SetVertices being deprecated until new release with SetMesh() is available.
  10. namespace TMPro
  11. {
  12. public partial class TextMeshProUGUI
  13. {
  14. [SerializeField]
  15. private bool m_hasFontAssetChanged = false; // Used to track when font properties have changed.
  16. [SerializeField]
  17. protected TMP_SubMeshUI[] m_subTextObjects = new TMP_SubMeshUI[8];
  18. private float m_previousLossyScaleY = -1; // Used for Tracking lossy scale changes in the transform;
  19. private Vector3[] m_RectTransformCorners = new Vector3[4];
  20. private CanvasRenderer m_canvasRenderer;
  21. private Canvas m_canvas;
  22. private bool m_isFirstAllocation; // Flag to determine if this is the first allocation of the buffers.
  23. private int m_max_characters = 8; // Determines the initial allocation and size of the character array / buffer.
  24. //private int m_max_numberOfLines = 4; // Determines the initial allocation and maximum number of lines of text.
  25. // MASKING RELATED PROPERTIES
  26. private bool m_isMaskingEnabled;
  27. // This property is now obsolete and used for compatibility with previous releases (prior to release 0.1.54).
  28. [SerializeField]
  29. private Material m_baseMaterial;
  30. private bool m_isScrollRegionSet;
  31. //private Mask m_mask;
  32. private int m_stencilID = 0;
  33. [SerializeField]
  34. private Vector4 m_maskOffset;
  35. // Matrix used to animated Env Map
  36. private Matrix4x4 m_EnvMapMatrix = new Matrix4x4();
  37. //private bool m_isEnabled;
  38. [NonSerialized]
  39. private bool m_isRegisteredForEvents;
  40. // DEBUG Variables
  41. //private System.Diagnostics.Stopwatch m_StopWatch;
  42. //private int frame = 0;
  43. //private int m_recursiveCount = 0;
  44. private int m_recursiveCountA = 0;
  45. private int loopCountA = 0;
  46. //private int loopCountB = 0;
  47. //private int loopCountC = 0;
  48. //private int loopCountD = 0;
  49. //private int loopCountE = 0;
  50. //[SerializeField]
  51. //private new Material m_MaskMaterial;
  52. protected override void Awake()
  53. {
  54. //Debug.Log("***** Awake() called on object ID " + GetInstanceID() + ". *****");
  55. #if UNITY_EDITOR
  56. // Special handling for TMP Settings and importing Essential Resources
  57. if (TMP_Settings.instance == null)
  58. {
  59. if (m_isWaitingOnResourceLoad == false)
  60. TMPro_EventManager.RESOURCE_LOAD_EVENT.Add(ON_RESOURCES_LOADED);
  61. m_isWaitingOnResourceLoad = true;
  62. return;
  63. }
  64. #endif
  65. // Cache Reference to the Canvas
  66. m_canvas = this.canvas;
  67. m_isOrthographic = true;
  68. // Cache Reference to RectTransform.
  69. m_rectTransform = gameObject.GetComponent<RectTransform>();
  70. if (m_rectTransform == null)
  71. m_rectTransform = gameObject.AddComponent<RectTransform>();
  72. // Cache a reference to the CanvasRenderer.
  73. m_canvasRenderer = GetComponent<CanvasRenderer>();
  74. if (m_canvasRenderer == null)
  75. m_canvasRenderer = gameObject.AddComponent<CanvasRenderer> ();
  76. if (m_mesh == null)
  77. {
  78. m_mesh = new Mesh();
  79. m_mesh.hideFlags = HideFlags.HideAndDontSave;
  80. // Create new TextInfo for the text object.
  81. m_textInfo = new TMP_TextInfo(this);
  82. }
  83. // Load TMP Settings for new text object instances.
  84. LoadDefaultSettings();
  85. // Load the font asset and assign material to renderer.
  86. LoadFontAsset();
  87. // Load Default TMP StyleSheet
  88. TMP_StyleSheet.LoadDefaultStyleSheet();
  89. // Allocate our initial buffers.
  90. if (m_TextParsingBuffer == null)
  91. m_TextParsingBuffer = new UnicodeChar[m_max_characters];
  92. m_cached_TextElement = new TMP_Character();
  93. m_isFirstAllocation = true;
  94. // Check if we have a font asset assigned. Return if we don't because no one likes to see purple squares on screen.
  95. if (m_fontAsset == null)
  96. {
  97. Debug.LogWarning("Please assign a Font Asset to this " + transform.name + " gameobject.", this);
  98. return;
  99. }
  100. // Check to make sure Sub Text Objects are tracked correctly in the event a Prefab is used.
  101. TMP_SubMeshUI[] subTextObjects = GetComponentsInChildren<TMP_SubMeshUI>();
  102. if (subTextObjects.Length > 0)
  103. {
  104. for (int i = 0; i < subTextObjects.Length; i++)
  105. m_subTextObjects[i + 1] = subTextObjects[i];
  106. }
  107. // Set flags to ensure our text is parsed and redrawn.
  108. m_isInputParsingRequired = true;
  109. m_havePropertiesChanged = true;
  110. m_isCalculateSizeRequired = true;
  111. m_isAwake = true;
  112. }
  113. protected override void OnEnable()
  114. {
  115. //Debug.Log("***** OnEnable() called on object ID " + GetInstanceID() + ". *****");
  116. // Return if Awake() has not been called on the text object.
  117. if (m_isAwake == false)
  118. return;
  119. if (!m_isRegisteredForEvents)
  120. {
  121. //Debug.Log("Registering for Events.");
  122. #if UNITY_EDITOR
  123. // Register Callbacks for various events.
  124. TMPro_EventManager.MATERIAL_PROPERTY_EVENT.Add(ON_MATERIAL_PROPERTY_CHANGED);
  125. TMPro_EventManager.FONT_PROPERTY_EVENT.Add(ON_FONT_PROPERTY_CHANGED);
  126. TMPro_EventManager.TEXTMESHPRO_UGUI_PROPERTY_EVENT.Add(ON_TEXTMESHPRO_UGUI_PROPERTY_CHANGED);
  127. TMPro_EventManager.DRAG_AND_DROP_MATERIAL_EVENT.Add(ON_DRAG_AND_DROP_MATERIAL);
  128. TMPro_EventManager.TEXT_STYLE_PROPERTY_EVENT.Add(ON_TEXT_STYLE_CHANGED);
  129. TMPro_EventManager.COLOR_GRADIENT_PROPERTY_EVENT.Add(ON_COLOR_GRADIENT_CHANGED);
  130. TMPro_EventManager.TMP_SETTINGS_PROPERTY_EVENT.Add(ON_TMP_SETTINGS_CHANGED);
  131. #endif
  132. m_isRegisteredForEvents = true;
  133. }
  134. // Cache Reference to the Canvas
  135. m_canvas = GetCanvas();
  136. SetActiveSubMeshes(true);
  137. // Register Graphic Component to receive event triggers
  138. GraphicRegistry.RegisterGraphicForCanvas(m_canvas, this);
  139. // Register text object for updates
  140. TMP_UpdateManager.RegisterTextObjectForUpdate(this);
  141. ComputeMarginSize();
  142. m_verticesAlreadyDirty = false;
  143. m_layoutAlreadyDirty = false;
  144. m_ShouldRecalculateStencil = true;
  145. m_isInputParsingRequired = true;
  146. SetAllDirty();
  147. RecalculateClipping();
  148. }
  149. protected override void OnDisable()
  150. {
  151. //Debug.Log("***** OnDisable() called on object ID " + GetInstanceID() + ". *****");
  152. // Return if Awake() has not been called on the text object.
  153. if (m_isAwake == false)
  154. return;
  155. if (m_MaskMaterial != null)
  156. {
  157. TMP_MaterialManager.ReleaseStencilMaterial(m_MaskMaterial);
  158. m_MaskMaterial = null;
  159. }
  160. // UnRegister Graphic Component
  161. GraphicRegistry.UnregisterGraphicForCanvas(m_canvas, this);
  162. CanvasUpdateRegistry.UnRegisterCanvasElementForRebuild((ICanvasElement)this);
  163. TMP_UpdateManager.UnRegisterTextObjectForUpdate(this);
  164. if (m_canvasRenderer != null)
  165. m_canvasRenderer.Clear();
  166. SetActiveSubMeshes(false);
  167. LayoutRebuilder.MarkLayoutForRebuild(m_rectTransform);
  168. RecalculateClipping();
  169. }
  170. protected override void OnDestroy()
  171. {
  172. //Debug.Log("***** OnDestroy() called on object ID " + GetInstanceID() + ". *****");
  173. // UnRegister Graphic Component
  174. GraphicRegistry.UnregisterGraphicForCanvas(m_canvas, this);
  175. TMP_UpdateManager.UnRegisterTextObjectForUpdate(this);
  176. // Clean up remaining mesh
  177. if (m_mesh != null)
  178. DestroyImmediate(m_mesh);
  179. // Clean up mask material
  180. if (m_MaskMaterial != null)
  181. {
  182. TMP_MaterialManager.ReleaseStencilMaterial(m_MaskMaterial);
  183. m_MaskMaterial = null;
  184. }
  185. #if UNITY_EDITOR
  186. // Unregister the event this object was listening to
  187. TMPro_EventManager.MATERIAL_PROPERTY_EVENT.Remove(ON_MATERIAL_PROPERTY_CHANGED);
  188. TMPro_EventManager.FONT_PROPERTY_EVENT.Remove(ON_FONT_PROPERTY_CHANGED);
  189. TMPro_EventManager.TEXTMESHPRO_UGUI_PROPERTY_EVENT.Remove(ON_TEXTMESHPRO_UGUI_PROPERTY_CHANGED);
  190. TMPro_EventManager.DRAG_AND_DROP_MATERIAL_EVENT.Remove(ON_DRAG_AND_DROP_MATERIAL);
  191. TMPro_EventManager.TEXT_STYLE_PROPERTY_EVENT.Remove(ON_TEXT_STYLE_CHANGED);
  192. TMPro_EventManager.COLOR_GRADIENT_PROPERTY_EVENT.Remove(ON_COLOR_GRADIENT_CHANGED);
  193. TMPro_EventManager.TMP_SETTINGS_PROPERTY_EVENT.Remove(ON_TMP_SETTINGS_CHANGED);
  194. TMPro_EventManager.RESOURCE_LOAD_EVENT.Remove(ON_RESOURCES_LOADED);
  195. #endif
  196. m_isRegisteredForEvents = false;
  197. }
  198. #if UNITY_EDITOR
  199. protected override void Reset()
  200. {
  201. //Debug.Log("***** Reset() *****"); //has been called.");
  202. // Return if Awake() has not been called on the text object.
  203. if (m_isAwake == false)
  204. return;
  205. LoadDefaultSettings();
  206. LoadFontAsset();
  207. m_isInputParsingRequired = true;
  208. m_havePropertiesChanged = true;
  209. }
  210. protected override void OnValidate()
  211. {
  212. //Debug.Log("***** OnValidate() ***** Frame:" + Time.frameCount); // ID " + GetInstanceID()); // New Material [" + m_sharedMaterial.name + "] with ID " + m_sharedMaterial.GetInstanceID() + ". Base Material is [" + m_baseMaterial.name + "] with ID " + m_baseMaterial.GetInstanceID() + ". Previous Base Material is [" + (m_lastBaseMaterial == null ? "Null" : m_lastBaseMaterial.name) + "].");
  213. // Return if Awake() has not been called on the text object.
  214. if (m_isAwake == false)
  215. return;
  216. // Handle Font Asset changes in the inspector.
  217. if (m_fontAsset == null || m_hasFontAssetChanged)
  218. {
  219. LoadFontAsset();
  220. m_isCalculateSizeRequired = true;
  221. m_hasFontAssetChanged = false;
  222. }
  223. if (m_canvasRenderer == null || m_canvasRenderer.GetMaterial() == null || m_canvasRenderer.GetMaterial().GetTexture(ShaderUtilities.ID_MainTex) == null || m_fontAsset == null || m_fontAsset.atlasTexture.GetInstanceID() != m_canvasRenderer.GetMaterial().GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID())
  224. {
  225. LoadFontAsset();
  226. m_isCalculateSizeRequired = true;
  227. m_hasFontAssetChanged = false;
  228. }
  229. m_padding = GetPaddingForMaterial();
  230. ComputeMarginSize();
  231. m_isInputParsingRequired = true;
  232. m_inputSource = TextInputSources.Text;
  233. m_havePropertiesChanged = true;
  234. m_isCalculateSizeRequired = true;
  235. m_isPreferredWidthDirty = true;
  236. m_isPreferredHeightDirty = true;
  237. SetAllDirty();
  238. }
  239. // Event received when TMP resources have been loaded.
  240. void ON_RESOURCES_LOADED()
  241. {
  242. TMPro_EventManager.RESOURCE_LOAD_EVENT.Remove(ON_RESOURCES_LOADED);
  243. if (this == null)
  244. return;
  245. Awake();
  246. OnEnable();
  247. }
  248. // Event received when custom material editor properties are changed.
  249. void ON_MATERIAL_PROPERTY_CHANGED(bool isChanged, Material mat)
  250. {
  251. //Debug.Log("ON_MATERIAL_PROPERTY_CHANGED event received."); // Targeted Material is: " + mat.name + " m_sharedMaterial: " + m_sharedMaterial.name + " with ID:" + m_sharedMaterial.GetInstanceID() + " m_renderer.sharedMaterial: " + m_canvasRenderer.GetMaterial() + " Masking Material:" + m_MaskMaterial.GetInstanceID());
  252. ShaderUtilities.GetShaderPropertyIDs(); // Initialize ShaderUtilities and get shader property IDs.
  253. int materialID = mat.GetInstanceID();
  254. int sharedMaterialID = m_sharedMaterial.GetInstanceID();
  255. int maskingMaterialID = m_MaskMaterial == null ? 0 : m_MaskMaterial.GetInstanceID();
  256. if (m_canvasRenderer == null || m_canvasRenderer.GetMaterial() == null)
  257. {
  258. if (m_canvasRenderer == null) return;
  259. if (m_fontAsset != null)
  260. {
  261. m_canvasRenderer.SetMaterial(m_fontAsset.material, m_sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex));
  262. //Debug.LogWarning("No Material was assigned to " + name + ". " + m_fontAsset.material.name + " was assigned.");
  263. }
  264. else
  265. Debug.LogWarning("No Font Asset assigned to " + name + ". Please assign a Font Asset.", this);
  266. }
  267. if (m_canvasRenderer.GetMaterial() != m_sharedMaterial && m_fontAsset == null) // || m_renderer.sharedMaterials.Contains(mat))
  268. {
  269. //Debug.Log("ON_MATERIAL_PROPERTY_CHANGED Called on Target ID: " + GetInstanceID() + ". Previous Material:" + m_sharedMaterial + " New Material:" + m_uiRenderer.GetMaterial()); // on Object ID:" + GetInstanceID() + ". m_sharedMaterial: " + m_sharedMaterial.name + " m_renderer.sharedMaterial: " + m_renderer.sharedMaterial.name);
  270. m_sharedMaterial = m_canvasRenderer.GetMaterial();
  271. }
  272. // Make sure material properties are synchronized between the assigned material and masking material.
  273. if (m_MaskMaterial != null)
  274. {
  275. UnityEditor.Undo.RecordObject(m_MaskMaterial, "Material Property Changes");
  276. UnityEditor.Undo.RecordObject(m_sharedMaterial, "Material Property Changes");
  277. if (materialID == sharedMaterialID)
  278. {
  279. //Debug.Log("Copy base material properties to masking material if not null.");
  280. float stencilID = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilID);
  281. float stencilComp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilComp);
  282. //float stencilOp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilOp);
  283. //float stencilRead = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilReadMask);
  284. //float stencilWrite = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilWriteMask);
  285. m_MaskMaterial.CopyPropertiesFromMaterial(mat);
  286. m_MaskMaterial.shaderKeywords = mat.shaderKeywords;
  287. m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID);
  288. m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
  289. //m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilOp, stencilOp);
  290. //m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilReadMask, stencilID);
  291. //m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilWriteMask, 0);
  292. }
  293. else if (materialID == maskingMaterialID)
  294. {
  295. // Update the padding
  296. GetPaddingForMaterial(mat);
  297. m_sharedMaterial.CopyPropertiesFromMaterial(mat);
  298. m_sharedMaterial.shaderKeywords = mat.shaderKeywords;
  299. m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilID, 0);
  300. m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 8);
  301. //m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilOp, 0);
  302. //m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilReadMask, 255);
  303. //m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilWriteMask, 255);
  304. }
  305. }
  306. m_padding = GetPaddingForMaterial();
  307. m_havePropertiesChanged = true;
  308. SetVerticesDirty();
  309. //SetMaterialDirty();
  310. }
  311. // Event received when font asset properties are changed in Font Inspector
  312. void ON_FONT_PROPERTY_CHANGED(bool isChanged, TMP_FontAsset font)
  313. {
  314. if (MaterialReference.Contains(m_materialReferences, font))
  315. {
  316. //Debug.Log("ON_FONT_PROPERTY_CHANGED event received.");
  317. m_isInputParsingRequired = true;
  318. m_havePropertiesChanged = true;
  319. UpdateMeshPadding();
  320. SetLayoutDirty();
  321. SetVerticesDirty();
  322. }
  323. }
  324. // Event received when UNDO / REDO Event alters the properties of the object.
  325. void ON_TEXTMESHPRO_UGUI_PROPERTY_CHANGED(bool isChanged, TextMeshProUGUI obj)
  326. {
  327. //Debug.Log("Event Received by " + obj);
  328. if (obj == this)
  329. {
  330. //Debug.Log("Undo / Redo Event Received by Object ID:" + GetInstanceID());
  331. m_havePropertiesChanged = true;
  332. m_isInputParsingRequired = true;
  333. ComputeMarginSize(); // Review this change
  334. SetVerticesDirty();
  335. }
  336. }
  337. // Event to Track Material Changed resulting from Drag-n-drop.
  338. void ON_DRAG_AND_DROP_MATERIAL(GameObject obj, Material currentMaterial, Material newMaterial)
  339. {
  340. //Debug.Log("Drag-n-Drop Event - Receiving Object ID " + GetInstanceID() + ". Sender ID " + obj.GetInstanceID()); // + ". Prefab Parent is " + UnityEditor.PrefabUtility.GetPrefabParent(gameObject).GetInstanceID()); // + ". New Material is " + newMaterial.name + " with ID " + newMaterial.GetInstanceID() + ". Base Material is " + m_baseMaterial.name + " with ID " + m_baseMaterial.GetInstanceID());
  341. // Check if event applies to this current object
  342. #if UNITY_2018_2_OR_NEWER
  343. if (obj == gameObject || UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(gameObject) == obj)
  344. #else
  345. if (obj == gameObject || UnityEditor.PrefabUtility.GetPrefabParent(gameObject) == obj)
  346. #endif
  347. {
  348. UnityEditor.Undo.RecordObject(this, "Material Assignment");
  349. UnityEditor.Undo.RecordObject(m_canvasRenderer, "Material Assignment");
  350. m_sharedMaterial = newMaterial;
  351. m_padding = GetPaddingForMaterial();
  352. m_havePropertiesChanged = true;
  353. SetVerticesDirty();
  354. SetMaterialDirty();
  355. }
  356. }
  357. // Event received when Text Styles are changed.
  358. void ON_TEXT_STYLE_CHANGED(bool isChanged)
  359. {
  360. m_havePropertiesChanged = true;
  361. m_isInputParsingRequired = true;
  362. SetVerticesDirty();
  363. }
  364. /// <summary>
  365. /// Event received when a Color Gradient Preset is modified.
  366. /// </summary>
  367. /// <param name="textObject"></param>
  368. void ON_COLOR_GRADIENT_CHANGED(TMP_ColorGradient gradient)
  369. {
  370. if (m_fontColorGradientPreset != null && gradient.GetInstanceID() == m_fontColorGradientPreset.GetInstanceID())
  371. {
  372. m_havePropertiesChanged = true;
  373. SetVerticesDirty();
  374. }
  375. }
  376. /// <summary>
  377. /// Event received when the TMP Settings are changed.
  378. /// </summary>
  379. void ON_TMP_SETTINGS_CHANGED()
  380. {
  381. m_defaultSpriteAsset = null;
  382. m_havePropertiesChanged = true;
  383. m_isInputParsingRequired = true;
  384. SetAllDirty();
  385. }
  386. #endif
  387. // Function which loads either the default font or a newly assigned font asset. This function also assigned the appropriate material to the renderer.
  388. protected override void LoadFontAsset()
  389. {
  390. //Debug.Log("***** LoadFontAsset() *****"); //TextMeshPro LoadFontAsset() has been called."); // Current Font Asset is " + (font != null ? font.name: "Null") );
  391. ShaderUtilities.GetShaderPropertyIDs(); // Initialize & Get shader property IDs.
  392. if (m_fontAsset == null)
  393. {
  394. if (TMP_Settings.defaultFontAsset != null)
  395. m_fontAsset = TMP_Settings.defaultFontAsset;
  396. else
  397. m_fontAsset = Resources.Load<TMP_FontAsset>("Fonts & Materials/LiberationSans SDF");
  398. if (m_fontAsset == null)
  399. {
  400. Debug.LogWarning("The LiberationSans SDF Font Asset was not found. There is no Font Asset assigned to " + gameObject.name + ".", this);
  401. return;
  402. }
  403. if (m_fontAsset.characterLookupTable == null)
  404. {
  405. Debug.Log("Dictionary is Null!");
  406. }
  407. m_sharedMaterial = m_fontAsset.material;
  408. }
  409. else
  410. {
  411. // Read font definition if needed.
  412. if (m_fontAsset.characterLookupTable == null)
  413. m_fontAsset.ReadFontAssetDefinition();
  414. // Added for compatibility with previous releases.
  415. if (m_sharedMaterial == null && m_baseMaterial != null)
  416. {
  417. m_sharedMaterial = m_baseMaterial;
  418. m_baseMaterial = null;
  419. }
  420. // If font atlas texture doesn't match the assigned material font atlas, switch back to default material specified in the Font Asset.
  421. if (m_sharedMaterial == null || m_sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex) == null || m_fontAsset.atlasTexture.GetInstanceID() != m_sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID())
  422. {
  423. if (m_fontAsset.material == null)
  424. Debug.LogWarning("The Font Atlas Texture of the Font Asset " + m_fontAsset.name + " assigned to " + gameObject.name + " is missing.", this);
  425. else
  426. m_sharedMaterial = m_fontAsset.material;
  427. }
  428. }
  429. // Find and cache Underline & Ellipsis characters.
  430. GetSpecialCharacters(m_fontAsset);
  431. m_padding = GetPaddingForMaterial();
  432. SetMaterialDirty();
  433. }
  434. /// <summary>
  435. /// Method to retrieve the parent Canvas.
  436. /// </summary>
  437. private Canvas GetCanvas()
  438. {
  439. Canvas canvas = null;
  440. var list = TMP_ListPool<Canvas>.Get();
  441. gameObject.GetComponentsInParent(false, list);
  442. if (list.Count > 0)
  443. {
  444. // Find the first active and enabled canvas.
  445. for (int i = 0; i < list.Count; ++i)
  446. {
  447. if (list[i].isActiveAndEnabled)
  448. {
  449. canvas = list[i];
  450. break;
  451. }
  452. }
  453. }
  454. TMP_ListPool<Canvas>.Release(list);
  455. return canvas;
  456. }
  457. /// <summary>
  458. /// Method used when animating the Env Map on the material.
  459. /// </summary>
  460. void UpdateEnvMapMatrix()
  461. {
  462. if (!m_sharedMaterial.HasProperty(ShaderUtilities.ID_EnvMap) || m_sharedMaterial.GetTexture(ShaderUtilities.ID_EnvMap) == null)
  463. return;
  464. //Debug.Log("Updating Env Matrix...");
  465. Vector3 rotation = m_sharedMaterial.GetVector(ShaderUtilities.ID_EnvMatrixRotation);
  466. m_EnvMapMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(rotation), Vector3.one);
  467. m_sharedMaterial.SetMatrix(ShaderUtilities.ID_EnvMatrix, m_EnvMapMatrix);
  468. }
  469. // Enable Masking in the Shader
  470. void EnableMasking()
  471. {
  472. if (m_fontMaterial == null)
  473. {
  474. m_fontMaterial = CreateMaterialInstance(m_sharedMaterial);
  475. m_canvasRenderer.SetMaterial(m_fontMaterial, m_sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex));
  476. }
  477. m_sharedMaterial = m_fontMaterial;
  478. if (m_sharedMaterial.HasProperty(ShaderUtilities.ID_ClipRect))
  479. {
  480. m_sharedMaterial.EnableKeyword(ShaderUtilities.Keyword_MASK_SOFT);
  481. m_sharedMaterial.DisableKeyword(ShaderUtilities.Keyword_MASK_HARD);
  482. m_sharedMaterial.DisableKeyword(ShaderUtilities.Keyword_MASK_TEX);
  483. UpdateMask(); // Update Masking Coordinates
  484. }
  485. m_isMaskingEnabled = true;
  486. //m_uiRenderer.SetMaterial(m_sharedMaterial, null);
  487. //m_padding = ShaderUtilities.GetPadding(m_sharedMaterial, m_enableExtraPadding, m_isUsingBold);
  488. //m_alignmentPadding = ShaderUtilities.GetFontExtent(m_sharedMaterial);
  489. /*
  490. Material mat = m_uiRenderer.GetMaterial();
  491. if (mat.HasProperty(ShaderUtilities.ID_MaskCoord))
  492. {
  493. mat.EnableKeyword("MASK_SOFT");
  494. mat.DisableKeyword("MASK_HARD");
  495. mat.DisableKeyword("MASK_OFF");
  496. m_isMaskingEnabled = true;
  497. UpdateMask();
  498. }
  499. */
  500. }
  501. // Enable Masking in the Shader
  502. void DisableMasking()
  503. {
  504. if (m_fontMaterial != null)
  505. {
  506. if (m_stencilID > 0)
  507. m_sharedMaterial = m_MaskMaterial;
  508. else
  509. m_sharedMaterial = m_baseMaterial;
  510. m_canvasRenderer.SetMaterial(m_sharedMaterial, m_sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex));
  511. DestroyImmediate(m_fontMaterial);
  512. }
  513. m_isMaskingEnabled = false;
  514. /*
  515. if (m_maskingMaterial != null && m_stencilID == 0)
  516. {
  517. m_sharedMaterial = m_baseMaterial;
  518. m_uiRenderer.SetMaterial(m_sharedMaterial, null);
  519. }
  520. else if (m_stencilID > 0)
  521. {
  522. m_sharedMaterial.EnableKeyword("MASK_OFF");
  523. m_sharedMaterial.DisableKeyword("MASK_HARD");
  524. m_sharedMaterial.DisableKeyword("MASK_SOFT");
  525. }
  526. */
  527. /*
  528. Material mat = m_uiRenderer.GetMaterial();
  529. if (mat.HasProperty(ShaderUtilities.ID_MaskCoord))
  530. {
  531. mat.EnableKeyword("MASK_OFF");
  532. mat.DisableKeyword("MASK_HARD");
  533. mat.DisableKeyword("MASK_SOFT");
  534. m_isMaskingEnabled = false;
  535. UpdateMask();
  536. }
  537. */
  538. }
  539. // Update & recompute Mask offset
  540. void UpdateMask()
  541. {
  542. //Debug.Log("Updating Mask...");
  543. if (m_rectTransform != null)
  544. {
  545. //Material mat = m_uiRenderer.GetMaterial();
  546. //if (mat == null || (m_overflowMode == TextOverflowModes.ScrollRect && m_isScrollRegionSet))
  547. // return;
  548. if (!ShaderUtilities.isInitialized)
  549. ShaderUtilities.GetShaderPropertyIDs();
  550. //Debug.Log("Setting Mask for the first time.");
  551. m_isScrollRegionSet = true;
  552. float softnessX = Mathf.Min(Mathf.Min(m_margin.x, m_margin.z), m_sharedMaterial.GetFloat(ShaderUtilities.ID_MaskSoftnessX));
  553. float softnessY = Mathf.Min(Mathf.Min(m_margin.y, m_margin.w), m_sharedMaterial.GetFloat(ShaderUtilities.ID_MaskSoftnessY));
  554. softnessX = softnessX > 0 ? softnessX : 0;
  555. softnessY = softnessY > 0 ? softnessY : 0;
  556. float width = (m_rectTransform.rect.width - Mathf.Max(m_margin.x, 0) - Mathf.Max(m_margin.z, 0)) / 2 + softnessX;
  557. float height = (m_rectTransform.rect.height - Mathf.Max(m_margin.y, 0) - Mathf.Max(m_margin.w, 0)) / 2 + softnessY;
  558. Vector2 center = m_rectTransform.localPosition + new Vector3((0.5f - m_rectTransform.pivot.x) * m_rectTransform.rect.width + (Mathf.Max(m_margin.x, 0) - Mathf.Max(m_margin.z, 0)) / 2, (0.5f - m_rectTransform.pivot.y) * m_rectTransform.rect.height + (-Mathf.Max(m_margin.y, 0) + Mathf.Max(m_margin.w, 0)) / 2);
  559. //Vector2 center = m_rectTransform.localPosition + new Vector3((0.5f - m_rectTransform.pivot.x) * m_rectTransform.rect.width + (margin.x - margin.z) / 2, (0.5f - m_rectTransform.pivot.y) * m_rectTransform.rect.height + (-margin.y + margin.w) / 2);
  560. Vector4 mask = new Vector4(center.x, center.y, width, height);
  561. //Debug.Log(mask);
  562. //Rect rect = new Rect(0, 0, m_rectTransform.rect.width + margin.x + margin.z, m_rectTransform.rect.height + margin.y + margin.w);
  563. //int softness = (int)m_sharedMaterial.GetFloat(ShaderUtilities.ID_MaskSoftnessX) / 2;
  564. m_sharedMaterial.SetVector(ShaderUtilities.ID_ClipRect, mask);
  565. }
  566. }
  567. // Function called internally when a new material is assigned via the fontMaterial property.
  568. protected override Material GetMaterial(Material mat)
  569. {
  570. // Get Shader PropertyIDs if they haven't been cached already.
  571. ShaderUtilities.GetShaderPropertyIDs();
  572. // Check in case Object is disabled. If so, we don't have a valid reference to the Renderer.
  573. // This can occur when the Duplicate Material Context menu is used on an inactive object.
  574. //if (m_canvasRenderer == null)
  575. // m_canvasRenderer = GetComponent<CanvasRenderer>();
  576. // Create Instance Material only if the new material is not the same instance previously used.
  577. if (m_fontMaterial == null || m_fontMaterial.GetInstanceID() != mat.GetInstanceID())
  578. m_fontMaterial = CreateMaterialInstance(mat);
  579. m_sharedMaterial = m_fontMaterial;
  580. m_padding = GetPaddingForMaterial();
  581. m_ShouldRecalculateStencil = true;
  582. SetVerticesDirty();
  583. SetMaterialDirty();
  584. return m_sharedMaterial;
  585. }
  586. /// <summary>
  587. /// Method returning instances of the materials used by the text object.
  588. /// </summary>
  589. /// <returns></returns>
  590. protected override Material[] GetMaterials(Material[] mats)
  591. {
  592. int materialCount = m_textInfo.materialCount;
  593. if (m_fontMaterials == null)
  594. m_fontMaterials = new Material[materialCount];
  595. else if (m_fontMaterials.Length != materialCount)
  596. TMP_TextInfo.Resize(ref m_fontMaterials, materialCount, false);
  597. // Get instances of the materials
  598. for (int i = 0; i < materialCount; i++)
  599. {
  600. if (i == 0)
  601. m_fontMaterials[i] = fontMaterial;
  602. else
  603. m_fontMaterials[i] = m_subTextObjects[i].material;
  604. }
  605. m_fontSharedMaterials = m_fontMaterials;
  606. return m_fontMaterials;
  607. }
  608. // Function called internally when a new shared material is assigned via the fontSharedMaterial property.
  609. protected override void SetSharedMaterial(Material mat)
  610. {
  611. // Check in case Object is disabled. If so, we don't have a valid reference to the Renderer.
  612. // This can occur when the Duplicate Material Context menu is used on an inactive object.
  613. //if (m_canvasRenderer == null)
  614. // m_canvasRenderer = GetComponent<CanvasRenderer>();
  615. m_sharedMaterial = mat;
  616. m_padding = GetPaddingForMaterial();
  617. SetMaterialDirty();
  618. }
  619. /// <summary>
  620. /// Method returning an array containing the materials used by the text object.
  621. /// </summary>
  622. /// <returns></returns>
  623. protected override Material[] GetSharedMaterials()
  624. {
  625. int materialCount = m_textInfo.materialCount;
  626. if (m_fontSharedMaterials == null)
  627. m_fontSharedMaterials = new Material[materialCount];
  628. else if (m_fontSharedMaterials.Length != materialCount)
  629. TMP_TextInfo.Resize(ref m_fontSharedMaterials, materialCount, false);
  630. for (int i = 0; i < materialCount; i++)
  631. {
  632. if (i == 0)
  633. m_fontSharedMaterials[i] = m_sharedMaterial;
  634. else
  635. m_fontSharedMaterials[i] = m_subTextObjects[i].sharedMaterial;
  636. }
  637. return m_fontSharedMaterials;
  638. }
  639. /// <summary>
  640. /// Method used to assign new materials to the text and sub text objects.
  641. /// </summary>
  642. protected override void SetSharedMaterials(Material[] materials)
  643. {
  644. int materialCount = m_textInfo.materialCount;
  645. // Check allocation of the fontSharedMaterials array.
  646. if (m_fontSharedMaterials == null)
  647. m_fontSharedMaterials = new Material[materialCount];
  648. else if (m_fontSharedMaterials.Length != materialCount)
  649. TMP_TextInfo.Resize(ref m_fontSharedMaterials, materialCount, false);
  650. // Only assign as many materials as the text object contains.
  651. for (int i = 0; i < materialCount; i++)
  652. {
  653. if (i == 0)
  654. {
  655. // Only assign new material if the font atlas textures match.
  656. if (materials[i].GetTexture(ShaderUtilities.ID_MainTex) == null || materials[i].GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID() != m_sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID())
  657. continue;
  658. m_sharedMaterial = m_fontSharedMaterials[i] = materials[i];
  659. m_padding = GetPaddingForMaterial(m_sharedMaterial);
  660. }
  661. else
  662. {
  663. // Only assign new material if the font atlas textures match.
  664. if (materials[i].GetTexture(ShaderUtilities.ID_MainTex) == null || materials[i].GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID() != m_subTextObjects[i].sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID())
  665. continue;
  666. // Only assign a new material if none were specified in the text input.
  667. if (m_subTextObjects[i].isDefaultMaterial)
  668. m_subTextObjects[i].sharedMaterial = m_fontSharedMaterials[i] = materials[i];
  669. }
  670. }
  671. }
  672. // This function will create an instance of the Font Material.
  673. protected override void SetOutlineThickness(float thickness)
  674. {
  675. // Use material instance if one exists. Otherwise, create a new instance of the shared material.
  676. if (m_fontMaterial != null && m_sharedMaterial.GetInstanceID() != m_fontMaterial.GetInstanceID())
  677. {
  678. m_sharedMaterial = m_fontMaterial;
  679. m_canvasRenderer.SetMaterial(m_sharedMaterial, m_sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex));
  680. }
  681. else if(m_fontMaterial == null)
  682. {
  683. m_fontMaterial = CreateMaterialInstance(m_sharedMaterial);
  684. m_sharedMaterial = m_fontMaterial;
  685. m_canvasRenderer.SetMaterial(m_sharedMaterial, m_sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex));
  686. }
  687. thickness = Mathf.Clamp01(thickness);
  688. m_sharedMaterial.SetFloat(ShaderUtilities.ID_OutlineWidth, thickness);
  689. m_padding = GetPaddingForMaterial();
  690. }
  691. // This function will create an instance of the Font Material.
  692. protected override void SetFaceColor(Color32 color)
  693. {
  694. // Use material instance if one exists. Otherwise, create a new instance of the shared material.
  695. if (m_fontMaterial == null)
  696. m_fontMaterial = CreateMaterialInstance(m_sharedMaterial);
  697. m_sharedMaterial = m_fontMaterial;
  698. m_padding = GetPaddingForMaterial();
  699. m_sharedMaterial.SetColor(ShaderUtilities.ID_FaceColor, color);
  700. }
  701. // This function will create an instance of the Font Material.
  702. protected override void SetOutlineColor(Color32 color)
  703. {
  704. // Use material instance if one exists. Otherwise, create a new instance of the shared material.
  705. if (m_fontMaterial == null)
  706. m_fontMaterial = CreateMaterialInstance(m_sharedMaterial);
  707. m_sharedMaterial = m_fontMaterial;
  708. m_padding = GetPaddingForMaterial();
  709. m_sharedMaterial.SetColor(ShaderUtilities.ID_OutlineColor, color);
  710. }
  711. // Sets the Render Queue and Ztest mode
  712. protected override void SetShaderDepth()
  713. {
  714. if (m_canvas == null || m_sharedMaterial == null)
  715. return;
  716. if (m_canvas.renderMode == RenderMode.ScreenSpaceOverlay || m_isOverlay)
  717. {
  718. // Should this use an instanced material?
  719. m_sharedMaterial.SetFloat(ShaderUtilities.ShaderTag_ZTestMode, 0);
  720. }
  721. else
  722. { // TODO: This section needs to be tested.
  723. m_sharedMaterial.SetFloat(ShaderUtilities.ShaderTag_ZTestMode, 4);
  724. }
  725. }
  726. // Sets the Culling mode of the material
  727. protected override void SetCulling()
  728. {
  729. if (m_isCullingEnabled)
  730. {
  731. Material mat = materialForRendering;
  732. if (mat != null)
  733. mat.SetFloat("_CullMode", 2);
  734. for (int i = 1; i < m_subTextObjects.Length && m_subTextObjects[i] != null; i++)
  735. {
  736. mat = m_subTextObjects[i].materialForRendering;
  737. if (mat != null)
  738. {
  739. mat.SetFloat(ShaderUtilities.ShaderTag_CullMode, 2);
  740. }
  741. }
  742. }
  743. else
  744. {
  745. Material mat = materialForRendering;
  746. if (mat != null)
  747. mat.SetFloat("_CullMode", 0);
  748. for (int i = 1; i < m_subTextObjects.Length && m_subTextObjects[i] != null; i++)
  749. {
  750. mat = m_subTextObjects[i].materialForRendering;
  751. if (mat != null)
  752. {
  753. mat.SetFloat(ShaderUtilities.ShaderTag_CullMode, 0);
  754. }
  755. }
  756. }
  757. }
  758. // Set Perspective Correction Mode based on whether Camera is Orthographic or Perspective
  759. void SetPerspectiveCorrection()
  760. {
  761. if (m_isOrthographic)
  762. m_sharedMaterial.SetFloat(ShaderUtilities.ID_PerspectiveFilter, 0.0f);
  763. else
  764. m_sharedMaterial.SetFloat(ShaderUtilities.ID_PerspectiveFilter, 0.875f);
  765. }
  766. /// <summary>
  767. /// Get the padding value for the currently assigned material.
  768. /// </summary>
  769. /// <returns></returns>
  770. protected override float GetPaddingForMaterial(Material mat)
  771. {
  772. m_padding = ShaderUtilities.GetPadding(mat, m_enableExtraPadding, m_isUsingBold);
  773. m_isMaskingEnabled = ShaderUtilities.IsMaskingEnabled(m_sharedMaterial);
  774. m_isSDFShader = mat.HasProperty(ShaderUtilities.ID_WeightNormal);
  775. return m_padding;
  776. }
  777. /// <summary>
  778. /// Get the padding value for the currently assigned material.
  779. /// </summary>
  780. /// <returns></returns>
  781. protected override float GetPaddingForMaterial()
  782. {
  783. ShaderUtilities.GetShaderPropertyIDs();
  784. m_padding = ShaderUtilities.GetPadding(m_sharedMaterial, m_enableExtraPadding, m_isUsingBold);
  785. m_isMaskingEnabled = ShaderUtilities.IsMaskingEnabled(m_sharedMaterial);
  786. m_isSDFShader = m_sharedMaterial.HasProperty(ShaderUtilities.ID_WeightNormal);
  787. return m_padding;
  788. }
  789. // Function to allocate the necessary buffers to render the text. This function is called whenever the buffer size needs to be increased.
  790. void SetMeshArrays(int size)
  791. {
  792. m_textInfo.meshInfo[0].ResizeMeshInfo(size);
  793. m_canvasRenderer.SetMesh(m_textInfo.meshInfo[0].mesh);
  794. }
  795. // This function parses through the Char[] to determine how many characters will be visible. It then makes sure the arrays are large enough for all those characters.
  796. protected override int SetArraySizes(UnicodeChar[] chars)
  797. {
  798. //Debug.Log("*** SetArraySizes() on Instance ID (" + GetInstanceID() + ") ***");
  799. #if TMP_PROFILE_ON
  800. Profiler.BeginSample("SetArraySizes");
  801. #endif
  802. int spriteCount = 0;
  803. m_totalCharacterCount = 0;
  804. m_isUsingBold = false;
  805. m_isParsingText = false;
  806. tag_NoParsing = false;
  807. m_FontStyleInternal = m_fontStyle;
  808. m_FontWeightInternal = (m_FontStyleInternal & FontStyles.Bold) == FontStyles.Bold ? FontWeight.Bold : m_fontWeight;
  809. m_FontWeightStack.SetDefault(m_FontWeightInternal);
  810. m_currentFontAsset = m_fontAsset;
  811. m_currentMaterial = m_sharedMaterial;
  812. m_currentMaterialIndex = 0;
  813. m_materialReferenceStack.SetDefault(new MaterialReference(m_currentMaterialIndex, m_currentFontAsset, null, m_currentMaterial, m_padding));
  814. m_materialReferenceIndexLookup.Clear();
  815. MaterialReference.AddMaterialReference(m_currentMaterial, m_currentFontAsset, m_materialReferences, m_materialReferenceIndexLookup);
  816. if (m_textInfo == null) m_textInfo = new TMP_TextInfo();
  817. m_textElementType = TMP_TextElementType.Character;
  818. // Clear Linked Text object if we have one.
  819. if (m_linkedTextComponent != null)
  820. {
  821. m_linkedTextComponent.text = string.Empty;
  822. m_linkedTextComponent.ForceMeshUpdate();
  823. }
  824. // Parsing XML tags in the text
  825. for (int i = 0; i < chars.Length && chars[i].unicode != 0; i++)
  826. {
  827. //Make sure the characterInfo array can hold the next text element.
  828. if (m_textInfo.characterInfo == null || m_totalCharacterCount >= m_textInfo.characterInfo.Length)
  829. TMP_TextInfo.Resize(ref m_textInfo.characterInfo, m_totalCharacterCount + 1, true);
  830. int unicode = chars[i].unicode;
  831. // PARSE XML TAGS
  832. #region PARSE XML TAGS
  833. if (m_isRichText && unicode == 60) // if Char '<'
  834. {
  835. int prev_MaterialIndex = m_currentMaterialIndex;
  836. // Check if Tag is Valid
  837. if (ValidateHtmlTag(chars, i + 1, out int tagEnd))
  838. {
  839. int tagStartIndex = chars[i].stringIndex;
  840. i = tagEnd;
  841. if ((m_FontStyleInternal & FontStyles.Bold) == FontStyles.Bold) m_isUsingBold = true;
  842. if (m_textElementType == TMP_TextElementType.Sprite)
  843. {
  844. m_materialReferences[m_currentMaterialIndex].referenceCount += 1;
  845. m_textInfo.characterInfo[m_totalCharacterCount].character = (char)(57344 + m_spriteIndex);
  846. m_textInfo.characterInfo[m_totalCharacterCount].spriteIndex = m_spriteIndex;
  847. m_textInfo.characterInfo[m_totalCharacterCount].fontAsset = m_currentFontAsset;
  848. m_textInfo.characterInfo[m_totalCharacterCount].spriteAsset = m_currentSpriteAsset;
  849. m_textInfo.characterInfo[m_totalCharacterCount].materialReferenceIndex = m_currentMaterialIndex;
  850. m_textInfo.characterInfo[m_totalCharacterCount].textElement = m_currentSpriteAsset.spriteCharacterTable[m_spriteIndex];
  851. m_textInfo.characterInfo[m_totalCharacterCount].elementType = m_textElementType;
  852. m_textInfo.characterInfo[m_totalCharacterCount].index = tagStartIndex;
  853. m_textInfo.characterInfo[m_totalCharacterCount].stringLength = chars[i].stringIndex - tagStartIndex + 1;
  854. // Restore element type and material index to previous values.
  855. m_textElementType = TMP_TextElementType.Character;
  856. m_currentMaterialIndex = prev_MaterialIndex;
  857. spriteCount += 1;
  858. m_totalCharacterCount += 1;
  859. }
  860. continue;
  861. }
  862. }
  863. #endregion
  864. bool isUsingAlternativeTypeface = false;
  865. bool isUsingFallbackOrAlternativeTypeface = false;
  866. TMP_Character character;
  867. TMP_FontAsset tempFontAsset;
  868. TMP_FontAsset prev_fontAsset = m_currentFontAsset;
  869. Material prev_material = m_currentMaterial;
  870. int prev_materialIndex = m_currentMaterialIndex;
  871. // Handle Font Styles like LowerCase, UpperCase and SmallCaps.
  872. #region Handling of LowerCase, UpperCase and SmallCaps Font Styles
  873. if (m_textElementType == TMP_TextElementType.Character)
  874. {
  875. if ((m_FontStyleInternal & FontStyles.UpperCase) == FontStyles.UpperCase)
  876. {
  877. // If this character is lowercase, switch to uppercase.
  878. if (char.IsLower((char)unicode))
  879. unicode = char.ToUpper((char)unicode);
  880. }
  881. else if ((m_FontStyleInternal & FontStyles.LowerCase) == FontStyles.LowerCase)
  882. {
  883. // If this character is uppercase, switch to lowercase.
  884. if (char.IsUpper((char)unicode))
  885. unicode = char.ToLower((char)unicode);
  886. }
  887. else if ((m_FontStyleInternal & FontStyles.SmallCaps) == FontStyles.SmallCaps)
  888. {
  889. // Only convert lowercase characters to uppercase.
  890. if (char.IsLower((char)unicode))
  891. unicode = char.ToUpper((char)unicode);
  892. }
  893. }
  894. #endregion
  895. // Lookup the Glyph data for each character and cache it.
  896. #region LOOKUP GLYPH
  897. character = TMP_FontAssetUtilities.GetCharacterFromFontAsset((uint)unicode, m_currentFontAsset, false, m_FontStyleInternal, m_FontWeightInternal, out isUsingAlternativeTypeface, out tempFontAsset);
  898. // Search for the glyph in the list of fallback assigned to the primary font asset.
  899. if (character == null)
  900. {
  901. if (m_currentFontAsset.fallbackFontAssetTable != null && m_currentFontAsset.fallbackFontAssetTable.Count > 0)
  902. character = TMP_FontAssetUtilities.GetCharacterFromFontAssets((uint)unicode, m_currentFontAsset.fallbackFontAssetTable, true, m_FontStyleInternal, m_FontWeightInternal, out isUsingAlternativeTypeface, out tempFontAsset);
  903. }
  904. // Search for the glyph in the Sprite Asset assigned to the text object.
  905. if (character == null)
  906. {
  907. TMP_SpriteAsset spriteAsset = this.spriteAsset;
  908. if (spriteAsset != null)
  909. {
  910. int spriteIndex = -1;
  911. // Check Default Sprite Asset and its Fallbacks
  912. spriteAsset = TMP_SpriteAsset.SearchForSpriteByUnicode(spriteAsset, (uint)unicode, true, out spriteIndex);
  913. if (spriteIndex != -1)
  914. {
  915. m_textElementType = TMP_TextElementType.Sprite;
  916. m_textInfo.characterInfo[m_totalCharacterCount].elementType = m_textElementType;
  917. m_currentMaterialIndex = MaterialReference.AddMaterialReference(spriteAsset.material, spriteAsset, m_materialReferences, m_materialReferenceIndexLookup);
  918. m_materialReferences[m_currentMaterialIndex].referenceCount += 1;
  919. m_textInfo.characterInfo[m_totalCharacterCount].character = (char)unicode;
  920. m_textInfo.characterInfo[m_totalCharacterCount].spriteIndex = spriteIndex;
  921. m_textInfo.characterInfo[m_totalCharacterCount].fontAsset = m_currentFontAsset;
  922. m_textInfo.characterInfo[m_totalCharacterCount].spriteAsset = spriteAsset;
  923. m_textInfo.characterInfo[m_totalCharacterCount].textElement = spriteAsset.spriteCharacterTable[m_spriteIndex];
  924. m_textInfo.characterInfo[m_totalCharacterCount].materialReferenceIndex = m_currentMaterialIndex;
  925. m_textInfo.characterInfo[m_totalCharacterCount].index = chars[i].stringIndex;
  926. m_textInfo.characterInfo[m_totalCharacterCount].stringLength = chars[i].length;
  927. // Restore element type and material index to previous values.
  928. m_textElementType = TMP_TextElementType.Character;
  929. m_currentMaterialIndex = prev_materialIndex;
  930. spriteCount += 1;
  931. m_totalCharacterCount += 1;
  932. continue;
  933. }
  934. }
  935. }
  936. // Search for the glyph in the list of fallback assigned in the TMP Settings (General Fallbacks).
  937. if (character == null)
  938. {
  939. if (TMP_Settings.fallbackFontAssets != null && TMP_Settings.fallbackFontAssets.Count > 0)
  940. character = TMP_FontAssetUtilities.GetCharacterFromFontAssets((uint)unicode, TMP_Settings.fallbackFontAssets, true, m_FontStyleInternal, m_FontWeightInternal, out isUsingAlternativeTypeface, out tempFontAsset);
  941. }
  942. // Search for the glyph in the Default Font Asset assigned in the TMP Settings file.
  943. if (character == null)
  944. {
  945. if (TMP_Settings.defaultFontAsset != null)
  946. character = TMP_FontAssetUtilities.GetCharacterFromFontAsset((uint)unicode, TMP_Settings.defaultFontAsset, true, m_FontStyleInternal, m_FontWeightInternal, out isUsingAlternativeTypeface, out tempFontAsset);
  947. }
  948. // TODO: Add support for using Sprite Assets like a special Emoji only Sprite Asset when UTF16 or UTF32 glyphs are requested.
  949. // This would kind of mirror native Emoji support.
  950. if (character == null)
  951. {
  952. TMP_SpriteAsset spriteAsset = TMP_Settings.defaultSpriteAsset;
  953. if (spriteAsset != null)
  954. {
  955. int spriteIndex = -1;
  956. // Check Default Sprite Asset and its Fallbacks
  957. spriteAsset = TMP_SpriteAsset.SearchForSpriteByUnicode(spriteAsset, (uint)unicode, true, out spriteIndex);
  958. if (spriteIndex != -1)
  959. {
  960. m_textElementType = TMP_TextElementType.Sprite;
  961. m_textInfo.characterInfo[m_totalCharacterCount].elementType = m_textElementType;
  962. m_currentMaterialIndex = MaterialReference.AddMaterialReference(spriteAsset.material, spriteAsset, m_materialReferences, m_materialReferenceIndexLookup);
  963. m_materialReferences[m_currentMaterialIndex].referenceCount += 1;
  964. m_textInfo.characterInfo[m_totalCharacterCount].character = (char)unicode;
  965. m_textInfo.characterInfo[m_totalCharacterCount].spriteIndex = spriteIndex;
  966. m_textInfo.characterInfo[m_totalCharacterCount].fontAsset = m_currentFontAsset;
  967. m_textInfo.characterInfo[m_totalCharacterCount].spriteAsset = spriteAsset;
  968. m_textInfo.characterInfo[m_totalCharacterCount].textElement = spriteAsset.spriteCharacterTable[m_spriteIndex];
  969. m_textInfo.characterInfo[m_totalCharacterCount].materialReferenceIndex = m_currentMaterialIndex;
  970. m_textInfo.characterInfo[m_totalCharacterCount].index = chars[i].stringIndex;
  971. m_textInfo.characterInfo[m_totalCharacterCount].stringLength = chars[i].length;
  972. // Restore element type and material index to previous values.
  973. m_textElementType = TMP_TextElementType.Character;
  974. m_currentMaterialIndex = prev_materialIndex;
  975. spriteCount += 1;
  976. m_totalCharacterCount += 1;
  977. continue;
  978. }
  979. }
  980. }
  981. //Check if Lowercase or Uppercase variant of the character is available.
  982. // Not sure this is necessary anyone as it is very unlikely with recursive search through fallback fonts.
  983. //if (glyph == null)
  984. //{
  985. // if (char.IsLower((char)c))
  986. // {
  987. // if (m_currentFontAsset.characterDictionary.TryGetValue(char.ToUpper((char)c), out glyph))
  988. // c = chars[i] = char.ToUpper((char)c);
  989. // }
  990. // else if (char.IsUpper((char)c))
  991. // {
  992. // if (m_currentFontAsset.characterDictionary.TryGetValue(char.ToLower((char)c), out glyph))
  993. // c = chars[i] = char.ToLower((char)c);
  994. // }
  995. //}
  996. // Replace missing glyph by the Square (9633) glyph or possibly the Space (32) glyph.
  997. if (character == null)
  998. {
  999. // Save the original unicode character
  1000. int srcGlyph = unicode;
  1001. // Try replacing the missing glyph character by TMP Settings Missing Glyph or Square (9633) character.
  1002. unicode = chars[i].unicode = TMP_Settings.missingGlyphCharacter == 0 ? 9633 : TMP_Settings.missingGlyphCharacter;
  1003. // Check for the missing glyph character in the currently assigned font asset and its fallbacks
  1004. character = TMP_FontAssetUtilities.GetCharacterFromFontAsset((uint)unicode, m_currentFontAsset, true, m_FontStyleInternal, m_FontWeightInternal, out isUsingAlternativeTypeface, out tempFontAsset);
  1005. if (character == null)
  1006. {
  1007. // Search for the missing glyph character in the TMP Settings Fallback list.
  1008. if (TMP_Settings.fallbackFontAssets != null && TMP_Settings.fallbackFontAssets.Count > 0)
  1009. character = TMP_FontAssetUtilities.GetCharacterFromFontAssets((uint)unicode, TMP_Settings.fallbackFontAssets, true, m_FontStyleInternal, m_FontWeightInternal, out isUsingAlternativeTypeface, out tempFontAsset);
  1010. }
  1011. if (character == null)
  1012. {
  1013. // Search for the missing glyph in the TMP Settings Default Font Asset.
  1014. if (TMP_Settings.defaultFontAsset != null)
  1015. character = TMP_FontAssetUtilities.GetCharacterFromFontAsset((uint)unicode, TMP_Settings.defaultFontAsset, true, m_FontStyleInternal, m_FontWeightInternal, out isUsingAlternativeTypeface, out tempFontAsset);
  1016. }
  1017. if (character == null)
  1018. {
  1019. // Use Space (32) Glyph from the currently assigned font asset.
  1020. unicode = chars[i].unicode = 32;
  1021. character = TMP_FontAssetUtilities.GetCharacterFromFontAsset((uint)unicode, m_currentFontAsset, true, m_FontStyleInternal, m_FontWeightInternal, out isUsingAlternativeTypeface, out tempFontAsset);
  1022. if (!TMP_Settings.warningsDisabled) Debug.LogWarning("Character with ASCII value of " + srcGlyph + " was not found in the Font Asset Glyph Table. It was replaced by a space.", this);
  1023. }
  1024. }
  1025. // Determine if the font asset is still the current font asset or a fallback.
  1026. if (tempFontAsset != null)
  1027. {
  1028. if (tempFontAsset.GetInstanceID() != m_currentFontAsset.GetInstanceID())
  1029. {
  1030. isUsingFallbackOrAlternativeTypeface = true;
  1031. m_currentFontAsset = tempFontAsset;
  1032. }
  1033. }
  1034. #endregion
  1035. m_textInfo.characterInfo[m_totalCharacterCount].elementType = TMP_TextElementType.Character;
  1036. m_textInfo.characterInfo[m_totalCharacterCount].textElement = character;
  1037. m_textInfo.characterInfo[m_totalCharacterCount].isUsingAlternateTypeface = isUsingAlternativeTypeface;
  1038. m_textInfo.characterInfo[m_totalCharacterCount].character = (char)unicode;
  1039. m_textInfo.characterInfo[m_totalCharacterCount].fontAsset = m_currentFontAsset;
  1040. m_textInfo.characterInfo[m_totalCharacterCount].index = chars[i].stringIndex;
  1041. m_textInfo.characterInfo[m_totalCharacterCount].stringLength = chars[i].length;
  1042. if (isUsingFallbackOrAlternativeTypeface)
  1043. {
  1044. // Create Fallback material instance matching current material preset if necessary
  1045. if (TMP_Settings.matchMaterialPreset)
  1046. m_currentMaterial = TMP_MaterialManager.GetFallbackMaterial(m_currentMaterial, m_currentFontAsset.material);
  1047. else
  1048. m_currentMaterial = m_currentFontAsset.material;
  1049. m_currentMaterialIndex = MaterialReference.AddMaterialReference(m_currentMaterial, m_currentFontAsset, m_materialReferences, m_materialReferenceIndexLookup);
  1050. }
  1051. if (!char.IsWhiteSpace((char)unicode) && unicode != 0x200B)
  1052. {
  1053. // Limit the mesh of the main text object to 65535 vertices and use sub objects for the overflow.
  1054. if (m_materialReferences[m_currentMaterialIndex].referenceCount < 16383)
  1055. m_materialReferences[m_currentMaterialIndex].referenceCount += 1;
  1056. else
  1057. {
  1058. m_currentMaterialIndex = MaterialReference.AddMaterialReference(new Material(m_currentMaterial), m_currentFontAsset, m_materialReferences, m_materialReferenceIndexLookup);
  1059. m_materialReferences[m_currentMaterialIndex].referenceCount += 1;
  1060. }
  1061. }
  1062. m_textInfo.characterInfo[m_totalCharacterCount].material = m_currentMaterial;
  1063. m_textInfo.characterInfo[m_totalCharacterCount].materialReferenceIndex = m_currentMaterialIndex;
  1064. m_materialReferences[m_currentMaterialIndex].isFallbackMaterial = isUsingFallbackOrAlternativeTypeface;
  1065. // Restore previous font asset and material if fallback font was used.
  1066. if (isUsingFallbackOrAlternativeTypeface)
  1067. {
  1068. m_materialReferences[m_currentMaterialIndex].fallbackMaterial = prev_material;
  1069. m_currentFontAsset = prev_fontAsset;
  1070. m_currentMaterial = prev_material;
  1071. m_currentMaterialIndex = prev_materialIndex;
  1072. }
  1073. m_totalCharacterCount += 1;
  1074. }
  1075. // Early return if we are calculating the preferred values.
  1076. if (m_isCalculatingPreferredValues)
  1077. {
  1078. m_isCalculatingPreferredValues = false;
  1079. m_isInputParsingRequired = true;
  1080. return m_totalCharacterCount;
  1081. }
  1082. // Save material and sprite count.
  1083. m_textInfo.spriteCount = spriteCount;
  1084. int materialCount = m_textInfo.materialCount = m_materialReferenceIndexLookup.Count;
  1085. // Check if we need to resize the MeshInfo array for handling different materials.
  1086. if (materialCount > m_textInfo.meshInfo.Length)
  1087. TMP_TextInfo.Resize(ref m_textInfo.meshInfo, materialCount, false);
  1088. // Resize SubTextObject array if necessary
  1089. if (materialCount > m_subTextObjects.Length)
  1090. TMP_TextInfo.Resize(ref m_subTextObjects, Mathf.NextPowerOfTwo(materialCount + 1));
  1091. // Resize CharacterInfo[] if allocations are excessive
  1092. if (m_textInfo.characterInfo.Length - m_totalCharacterCount > 256)
  1093. TMP_TextInfo.Resize(ref m_textInfo.characterInfo, Mathf.Max(m_totalCharacterCount + 1, 256), true);
  1094. // Iterate through the material references to set the mesh buffer allocations
  1095. for (int i = 0; i < materialCount; i++)
  1096. {
  1097. // Add new sub text object for each material reference
  1098. if (i > 0)
  1099. {
  1100. if (m_subTextObjects[i] == null)
  1101. {
  1102. m_subTextObjects[i] = TMP_SubMeshUI.AddSubTextObject(this, m_materialReferences[i]);
  1103. // Not sure this is necessary
  1104. m_textInfo.meshInfo[i].vertices = null;
  1105. }
  1106. //else if (m_subTextObjects[i].gameObject.activeInHierarchy == false)
  1107. // m_subTextObjects[i].gameObject.SetActive(true);
  1108. // Make sure the pivots are synchronized
  1109. if (m_rectTransform.pivot != m_subTextObjects[i].rectTransform.pivot)
  1110. m_subTextObjects[i].rectTransform.pivot = m_rectTransform.pivot;
  1111. // Check if the material has changed.
  1112. if (m_subTextObjects[i].sharedMaterial == null || m_subTextObjects[i].sharedMaterial.GetInstanceID() != m_materialReferences[i].material.GetInstanceID())
  1113. {
  1114. bool isDefaultMaterial = m_materialReferences[i].isDefaultMaterial;
  1115. m_subTextObjects[i].isDefaultMaterial = isDefaultMaterial;
  1116. // Assign new material if we are not using the default material or if the font asset has changed.
  1117. if (!isDefaultMaterial || m_subTextObjects[i].sharedMaterial == null || m_subTextObjects[i].sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID() != m_materialReferences[i].material.GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID())
  1118. {
  1119. m_subTextObjects[i].sharedMaterial = m_materialReferences[i].material;
  1120. m_subTextObjects[i].fontAsset = m_materialReferences[i].fontAsset;
  1121. m_subTextObjects[i].spriteAsset = m_materialReferences[i].spriteAsset;
  1122. }
  1123. }
  1124. // Check if we need to use a Fallback Material
  1125. if (m_materialReferences[i].isFallbackMaterial)
  1126. {
  1127. m_subTextObjects[i].fallbackMaterial = m_materialReferences[i].material;
  1128. m_subTextObjects[i].fallbackSourceMaterial = m_materialReferences[i].fallbackMaterial;
  1129. }
  1130. }
  1131. int referenceCount = m_materialReferences[i].referenceCount;
  1132. // Check to make sure our buffers allocations can accommodate the required text elements.
  1133. if (m_textInfo.meshInfo[i].vertices == null || m_textInfo.meshInfo[i].vertices.Length < referenceCount * 4)
  1134. {
  1135. if (m_textInfo.meshInfo[i].vertices == null)
  1136. {
  1137. if (i == 0)
  1138. m_textInfo.meshInfo[i] = new TMP_MeshInfo(m_mesh, referenceCount + 1);
  1139. else
  1140. m_textInfo.meshInfo[i] = new TMP_MeshInfo(m_subTextObjects[i].mesh, referenceCount + 1);
  1141. }
  1142. else
  1143. m_textInfo.meshInfo[i].ResizeMeshInfo(referenceCount > 1024 ? referenceCount + 256 : Mathf.NextPowerOfTwo(referenceCount + 1));
  1144. }
  1145. else if (m_VertexBufferAutoSizeReduction && referenceCount > 0 && m_textInfo.meshInfo[i].vertices.Length / 4 - referenceCount > 256)
  1146. {
  1147. // Resize vertex buffers if allocations are excessive.
  1148. //Debug.Log("Reducing the size of the vertex buffers.");
  1149. m_textInfo.meshInfo[i].ResizeMeshInfo(referenceCount > 1024 ? referenceCount + 256 : Mathf.NextPowerOfTwo(referenceCount + 1));
  1150. }
  1151. }
  1152. //TMP_MaterialManager.CleanupFallbackMaterials();
  1153. // Clean up unused SubMeshes
  1154. for (int i = materialCount; i < m_subTextObjects.Length && m_subTextObjects[i] != null; i++)
  1155. {
  1156. if (i < m_textInfo.meshInfo.Length)
  1157. {
  1158. m_subTextObjects[i].canvasRenderer.SetMesh(null);
  1159. // TODO: Figure out a way to handle this without running into Unity's Rebuild loop issue.
  1160. //m_subTextObjects[i].gameObject.SetActive(false);
  1161. }
  1162. }
  1163. #if TMP_PROFILE_ON
  1164. Profiler.EndSample();
  1165. #endif
  1166. return m_totalCharacterCount;
  1167. }
  1168. // Added to sort handle the potential issue with OnWillRenderObject() not getting called when objects are not visible by camera.
  1169. //void OnBecameInvisible()
  1170. //{
  1171. // if (m_mesh != null)
  1172. // m_mesh.bounds = new Bounds(transform.position, new Vector3(1000, 1000, 0));
  1173. //}
  1174. /// <summary>
  1175. /// Update the margin width and height
  1176. /// </summary>
  1177. public override void ComputeMarginSize()
  1178. {
  1179. if (this.rectTransform != null)
  1180. {
  1181. //Debug.Log("*** ComputeMarginSize() *** Current RectTransform's Width is " + m_rectTransform.rect.width + " and Height is " + m_rectTransform.rect.height); // + " and size delta is " + m_rectTransform.sizeDelta);
  1182. m_marginWidth = m_rectTransform.rect.width - m_margin.x - m_margin.z;
  1183. m_marginHeight = m_rectTransform.rect.height - m_margin.y - m_margin.w;
  1184. // Update the corners of the RectTransform
  1185. m_RectTransformCorners = GetTextContainerLocalCorners();
  1186. }
  1187. }
  1188. /// <summary>
  1189. ///
  1190. /// </summary>
  1191. protected override void OnDidApplyAnimationProperties()
  1192. {
  1193. m_havePropertiesChanged = true;
  1194. SetVerticesDirty();
  1195. SetLayoutDirty();
  1196. //Debug.Log("Animation Properties have changed.");
  1197. }
  1198. protected override void OnCanvasHierarchyChanged()
  1199. {
  1200. base.OnCanvasHierarchyChanged();
  1201. m_canvas = this.canvas;
  1202. }
  1203. protected override void OnTransformParentChanged()
  1204. {
  1205. //Debug.Log("***** OnTransformParentChanged *****");
  1206. base.OnTransformParentChanged();
  1207. m_canvas = this.canvas;
  1208. ComputeMarginSize();
  1209. m_havePropertiesChanged = true;
  1210. }
  1211. protected override void OnRectTransformDimensionsChange()
  1212. {
  1213. //Debug.Log("*** OnRectTransformDimensionsChange() *** ActiveInHierarchy: " + this.gameObject.activeInHierarchy + " Frame: " + Time.frameCount);
  1214. // Make sure object is active in Hierarchy
  1215. if (!this.gameObject.activeInHierarchy)
  1216. return;
  1217. ComputeMarginSize();
  1218. UpdateSubObjectPivot();
  1219. SetVerticesDirty();
  1220. SetLayoutDirty();
  1221. }
  1222. /// <summary>
  1223. /// Function used as a replacement for LateUpdate to check if the transform or scale of the text object has changed.
  1224. /// </summary>
  1225. internal override void InternalUpdate()
  1226. {
  1227. // We need to update the SDF scale or possibly regenerate the text object if lossy scale has changed.
  1228. if (m_havePropertiesChanged == false)
  1229. {
  1230. float lossyScaleY = m_rectTransform.lossyScale.y;
  1231. if (lossyScaleY != m_previousLossyScaleY && m_text != string.Empty && m_text != null)
  1232. {
  1233. float scaleDelta = lossyScaleY / m_previousLossyScaleY;
  1234. UpdateSDFScale(scaleDelta);
  1235. m_previousLossyScaleY = lossyScaleY;
  1236. }
  1237. }
  1238. // Added to handle legacy animation mode.
  1239. if (m_isUsingLegacyAnimationComponent)
  1240. {
  1241. //if (m_havePropertiesChanged)
  1242. m_havePropertiesChanged = true;
  1243. OnPreRenderCanvas();
  1244. }
  1245. }
  1246. // Called just before the Canvas is rendered.
  1247. void OnPreRenderCanvas()
  1248. {
  1249. //Debug.Log("*** OnPreRenderCanvas() *** Frame: " + Time.frameCount);
  1250. // Make sure object is active and that we have a valid Canvas.
  1251. if (!m_isAwake || (this.IsActive() == false && m_ignoreActiveState == false)) return;
  1252. if (m_canvas == null) { m_canvas = this.canvas; if (m_canvas == null) return; }
  1253. // Debug Variables
  1254. loopCountA = 0;
  1255. //loopCountB = 0;
  1256. //loopCountC = 0;
  1257. //loopCountD = 0;
  1258. //loopCountE = 0;
  1259. // Update Margins
  1260. //ComputeMarginSize();
  1261. // Update Mask
  1262. // if (m_isMaskingEnabled)
  1263. // {
  1264. // UpdateMask();
  1265. // }
  1266. if (m_havePropertiesChanged || m_isLayoutDirty)
  1267. {
  1268. //Debug.Log("Properties have changed!"); // Assigned Material is:" + m_sharedMaterial); // New Text is: " + m_text + ".");
  1269. // Update mesh padding if necessary.
  1270. if (checkPaddingRequired)
  1271. UpdateMeshPadding();
  1272. // Reparse the text if the input has changed or text was truncated.
  1273. if (m_isInputParsingRequired || m_isTextTruncated)
  1274. ParseInputText();
  1275. // Reset Font min / max used with Auto-sizing
  1276. if (m_enableAutoSizing)
  1277. m_fontSize = Mathf.Clamp(m_fontSizeBase, m_fontSizeMin, m_fontSizeMax);
  1278. m_maxFontSize = m_fontSizeMax;
  1279. m_minFontSize = m_fontSizeMin;
  1280. m_lineSpacingDelta = 0;
  1281. m_charWidthAdjDelta = 0;
  1282. //m_recursiveCount = 0;
  1283. m_isCharacterWrappingEnabled = false;
  1284. m_isTextTruncated = false;
  1285. m_havePropertiesChanged = false;
  1286. m_isLayoutDirty = false;
  1287. m_ignoreActiveState = false;
  1288. GenerateTextMesh();
  1289. }
  1290. }
  1291. /// <summary>
  1292. /// This is the main function that is responsible for creating / displaying the text.
  1293. /// </summary>
  1294. protected override void GenerateTextMesh()
  1295. {
  1296. //Debug.Log("*** GenerateTextMesh() ***"); // ***** Frame: " + Time.frameCount); // + ". Point Size: " + m_fontSize + ". Margins are (W) " + m_marginWidth + " (H) " + m_marginHeight); // ". Iteration Count: " + loopCountA + ". Min: " + m_minFontSize + " Max: " + m_maxFontSize + " Delta: " + (m_maxFontSize - m_minFontSize) + " Font size is " + m_fontSize); //called for Object with ID " + GetInstanceID()); // Assigned Material is " + m_uiRenderer.GetMaterial().name); // IncludeForMasking " + this.m_IncludeForMasking); // and text is " + m_text);
  1297. //Profiler.BeginSample("TMP Generate Text - Pre");
  1298. // Early exit if no font asset was assigned. This should not be needed since LiberationSans SDF will be assigned by default.
  1299. if (m_fontAsset == null || m_fontAsset.characterLookupTable == null)
  1300. {
  1301. Debug.LogWarning("Can't Generate Mesh! No Font Asset has been assigned to Object ID: " + this.GetInstanceID());
  1302. return;
  1303. }
  1304. // Clear TextInfo
  1305. if (m_textInfo != null)
  1306. m_textInfo.Clear();
  1307. // Early exit if we don't have any Text to generate.
  1308. if (m_TextParsingBuffer == null || m_TextParsingBuffer.Length == 0 || m_TextParsingBuffer[0].unicode == (char)0)
  1309. {
  1310. // Clear mesh and upload changes to the mesh.
  1311. ClearMesh();
  1312. m_preferredWidth = 0;
  1313. m_preferredHeight = 0;
  1314. // Event indicating the text has been regenerated.
  1315. TMPro_EventManager.ON_TEXT_CHANGED(this);
  1316. return;
  1317. }
  1318. m_currentFontAsset = m_fontAsset;
  1319. m_currentMaterial = m_sharedMaterial;
  1320. m_currentMaterialIndex = 0;
  1321. m_materialReferenceStack.SetDefault(new MaterialReference(m_currentMaterialIndex, m_currentFontAsset, null, m_currentMaterial, m_padding));
  1322. m_currentSpriteAsset = m_spriteAsset;
  1323. // Stop all Sprite Animations
  1324. if (m_spriteAnimator != null)
  1325. m_spriteAnimator.StopAllAnimations();
  1326. // Total character count is computed when the text is parsed.
  1327. int totalCharacterCount = m_totalCharacterCount;
  1328. // Calculate the scale of the font based on selected font size and sampling point size.
  1329. // baseScale is calculated using the font asset assigned to the text object.
  1330. float baseScale = m_fontScale = (m_fontSize / m_fontAsset.faceInfo.pointSize * m_fontAsset.faceInfo.scale);
  1331. float currentElementScale = baseScale;
  1332. m_fontScaleMultiplier = 1;
  1333. m_currentFontSize = m_fontSize;
  1334. m_sizeStack.SetDefault(m_currentFontSize);
  1335. float fontSizeDelta = 0;
  1336. int charCode = 0; // Holds the character code of the currently being processed character.
  1337. m_FontStyleInternal = m_fontStyle; // Set the default style.
  1338. m_FontWeightInternal = (m_FontStyleInternal & FontStyles.Bold) == FontStyles.Bold ? FontWeight.Bold : m_fontWeight;
  1339. m_FontWeightStack.SetDefault(m_FontWeightInternal);
  1340. m_fontStyleStack.Clear();
  1341. m_lineJustification = m_textAlignment; // Sets the line justification mode to match editor alignment.
  1342. m_lineJustificationStack.SetDefault(m_lineJustification);
  1343. float padding = 0;
  1344. float style_padding = 0; // Extra padding required to accommodate Bold style.
  1345. float bold_xAdvance_multiplier = 1; // Used to increase spacing between character when style is bold.
  1346. m_baselineOffset = 0; // Used by subscript characters.
  1347. m_baselineOffsetStack.Clear();
  1348. // Underline
  1349. bool beginUnderline = false;
  1350. Vector3 underline_start = Vector3.zero; // Used to track where underline starts & ends.
  1351. Vector3 underline_end = Vector3.zero;
  1352. // Strike-through
  1353. bool beginStrikethrough = false;
  1354. Vector3 strikethrough_start = Vector3.zero;
  1355. Vector3 strikethrough_end = Vector3.zero;
  1356. // Text Highlight
  1357. bool beginHighlight = false;
  1358. Vector3 highlight_start = Vector3.zero;
  1359. Vector3 highlight_end = Vector3.zero;
  1360. m_fontColor32 = m_fontColor;
  1361. Color32 vertexColor;
  1362. m_htmlColor = m_fontColor32;
  1363. m_underlineColor = m_htmlColor;
  1364. m_strikethroughColor = m_htmlColor;
  1365. m_colorStack.SetDefault(m_htmlColor);
  1366. m_underlineColorStack.SetDefault(m_htmlColor);
  1367. m_strikethroughColorStack.SetDefault(m_htmlColor);
  1368. m_highlightColorStack.SetDefault(m_htmlColor);
  1369. m_colorGradientPreset = null;
  1370. m_colorGradientStack.SetDefault(null);
  1371. // Clear the Style stack.
  1372. //m_styleStack.Clear();
  1373. // Clear the Action stack.
  1374. m_actionStack.Clear();
  1375. m_isFXMatrixSet = false;
  1376. m_lineOffset = 0; // Amount of space between lines (font line spacing + m_linespacing).
  1377. m_lineHeight = TMP_Math.FLOAT_UNSET;
  1378. float lineGap = m_currentFontAsset.faceInfo.lineHeight - (m_currentFontAsset.faceInfo.ascentLine - m_currentFontAsset.faceInfo.descentLine);
  1379. m_cSpacing = 0; // Amount of space added between characters as a result of the use of the <cspace> tag.
  1380. m_monoSpacing = 0;
  1381. float lineOffsetDelta = 0;
  1382. m_xAdvance = 0; // Used to track the position of each character.
  1383. tag_LineIndent = 0; // Used for indentation of text.
  1384. tag_Indent = 0;
  1385. m_indentStack.SetDefault(0);
  1386. tag_NoParsing = false;
  1387. //m_isIgnoringAlignment = false;
  1388. m_characterCount = 0; // Total characters in the char[]
  1389. // Tracking of line information
  1390. m_firstCharacterOfLine = 0;
  1391. m_lastCharacterOfLine = 0;
  1392. m_firstVisibleCharacterOfLine = 0;
  1393. m_lastVisibleCharacterOfLine = 0;
  1394. m_maxLineAscender = k_LargeNegativeFloat;
  1395. m_maxLineDescender = k_LargePositiveFloat;
  1396. m_lineNumber = 0;
  1397. m_lineVisibleCharacterCount = 0;
  1398. bool isStartOfNewLine = true;
  1399. m_firstOverflowCharacterIndex = -1;
  1400. m_pageNumber = 0;
  1401. int pageToDisplay = Mathf.Clamp(m_pageToDisplay - 1, 0, m_textInfo.pageInfo.Length - 1);
  1402. int previousPageOverflowChar = 0;
  1403. int ellipsisIndex = 0;
  1404. Vector4 margins = m_margin;
  1405. float marginWidth = m_marginWidth;
  1406. float marginHeight = m_marginHeight;
  1407. m_marginLeft = 0;
  1408. m_marginRight = 0;
  1409. m_width = -1;
  1410. float width = marginWidth + 0.0001f - m_marginLeft - m_marginRight;
  1411. // Need to initialize these Extents structures
  1412. m_meshExtents.min = k_LargePositiveVector2;
  1413. m_meshExtents.max = k_LargeNegativeVector2;
  1414. // Initialize lineInfo
  1415. m_textInfo.ClearLineInfo();
  1416. // Tracking of the highest Ascender
  1417. m_maxCapHeight = 0;
  1418. m_maxAscender = 0;
  1419. m_maxDescender = 0;
  1420. float pageAscender = 0;
  1421. float maxVisibleDescender = 0;
  1422. bool isMaxVisibleDescenderSet = false;
  1423. m_isNewPage = false;
  1424. // Initialize struct to track states of word wrapping
  1425. bool isFirstWord = true;
  1426. m_isNonBreakingSpace = false;
  1427. bool ignoreNonBreakingSpace = false;
  1428. bool isLastBreakingChar = false;
  1429. float linebreakingWidth = 0;
  1430. int wrappingIndex = 0;
  1431. // Save character and line state before we begin layout.
  1432. SaveWordWrappingState(ref m_SavedWordWrapState, -1, -1);
  1433. SaveWordWrappingState(ref m_SavedLineState, -1, -1);
  1434. loopCountA += 1;
  1435. //Profiler.EndSample();
  1436. #if TMP_PROFILE_PHASES_ON
  1437. Profiler.BeginSample("TMP Generate Text - Phase I");
  1438. #endif
  1439. // Parse through Character buffer to read HTML tags and begin creating mesh.
  1440. for (int i = 0; i < m_TextParsingBuffer.Length && m_TextParsingBuffer[i].unicode != 0; i++)
  1441. {
  1442. charCode = m_TextParsingBuffer[i].unicode;
  1443. // Parse Rich Text Tag
  1444. #region Parse Rich Text Tag
  1445. if (m_isRichText && charCode == 60) // '<'
  1446. {
  1447. m_isParsingText = true;
  1448. m_textElementType = TMP_TextElementType.Character;
  1449. // Check if Tag is valid. If valid, skip to the end of the validated tag.
  1450. if (ValidateHtmlTag(m_TextParsingBuffer, i + 1, out int tagEndIndex))
  1451. {
  1452. i = tagEndIndex;
  1453. // Continue to next character or handle the sprite element
  1454. if (m_textElementType == TMP_TextElementType.Character)
  1455. continue;
  1456. }
  1457. }
  1458. else
  1459. {
  1460. m_textElementType = m_textInfo.characterInfo[m_characterCount].elementType;
  1461. m_currentMaterialIndex = m_textInfo.characterInfo[m_characterCount].materialReferenceIndex;
  1462. m_currentFontAsset = m_textInfo.characterInfo[m_characterCount].fontAsset;
  1463. }
  1464. #endregion End Parse Rich Text Tag
  1465. int prev_MaterialIndex = m_currentMaterialIndex;
  1466. bool isUsingAltTypeface = m_textInfo.characterInfo[m_characterCount].isUsingAlternateTypeface;
  1467. m_isParsingText = false;
  1468. // When using Linked text, mark character as ignored and skip to next character.
  1469. if (m_characterCount < m_firstVisibleCharacter)
  1470. {
  1471. m_textInfo.characterInfo[m_characterCount].isVisible = false;
  1472. m_textInfo.characterInfo[m_characterCount].character = (char)0x200B;
  1473. m_characterCount += 1;
  1474. continue;
  1475. }
  1476. // Handle Font Styles like LowerCase, UpperCase and SmallCaps.
  1477. #region Handling of LowerCase, UpperCase and SmallCaps Font Styles
  1478. #if TMP_PROFILE_ON
  1479. Profiler.BeginSample("Handle Font Style");
  1480. #endif
  1481. float smallCapsMultiplier = 1.0f;
  1482. if (m_textElementType == TMP_TextElementType.Character)
  1483. {
  1484. if ((m_FontStyleInternal & FontStyles.UpperCase) == FontStyles.UpperCase)
  1485. {
  1486. // If this character is lowercase, switch to uppercase.
  1487. if (char.IsLower((char)charCode))
  1488. charCode = char.ToUpper((char)charCode);
  1489. }
  1490. else if ((m_FontStyleInternal & FontStyles.LowerCase) == FontStyles.LowerCase)
  1491. {
  1492. // If this character is uppercase, switch to lowercase.
  1493. if (char.IsUpper((char)charCode))
  1494. charCode = char.ToLower((char)charCode);
  1495. }
  1496. else if ((m_FontStyleInternal & FontStyles.SmallCaps) == FontStyles.SmallCaps)
  1497. {
  1498. if (char.IsLower((char)charCode))
  1499. {
  1500. smallCapsMultiplier = 0.8f;
  1501. charCode = char.ToUpper((char)charCode);
  1502. }
  1503. }
  1504. }
  1505. #if TMP_PROFILE_ON
  1506. Profiler.EndSample();
  1507. #endif
  1508. #endregion
  1509. // Look up Character Data from Dictionary and cache it.
  1510. #region Look up Character Data
  1511. #if TMP_PROFILE_ON
  1512. Profiler.BeginSample("Lookup Character Data");
  1513. #endif
  1514. if (m_textElementType == TMP_TextElementType.Sprite)
  1515. {
  1516. // If a sprite is used as a fallback then get a reference to it and set the color to white.
  1517. m_currentSpriteAsset = m_textInfo.characterInfo[m_characterCount].spriteAsset;
  1518. m_spriteIndex = m_textInfo.characterInfo[m_characterCount].spriteIndex;
  1519. TMP_SpriteCharacter sprite = m_currentSpriteAsset.spriteCharacterTable[m_spriteIndex];
  1520. if (sprite == null) continue;
  1521. // Sprites are assigned in the E000 Private Area + sprite Index
  1522. if (charCode == 60)
  1523. charCode = 57344 + m_spriteIndex;
  1524. else
  1525. m_spriteColor = s_colorWhite;
  1526. // The sprite scale calculations are based on the font asset assigned to the text object.
  1527. float spriteScale = (m_currentFontSize / m_currentFontAsset.faceInfo.pointSize * m_currentFontAsset.faceInfo.scale);
  1528. currentElementScale = m_currentFontAsset.faceInfo.ascentLine / sprite.glyph.metrics.height * sprite.scale * sprite.glyph.scale * spriteScale;
  1529. m_cached_TextElement = sprite;
  1530. m_textInfo.characterInfo[m_characterCount].elementType = TMP_TextElementType.Sprite;
  1531. m_textInfo.characterInfo[m_characterCount].scale = spriteScale;
  1532. m_textInfo.characterInfo[m_characterCount].spriteAsset = m_currentSpriteAsset;
  1533. m_textInfo.characterInfo[m_characterCount].fontAsset = m_currentFontAsset;
  1534. m_textInfo.characterInfo[m_characterCount].materialReferenceIndex = m_currentMaterialIndex;
  1535. m_currentMaterialIndex = prev_MaterialIndex;
  1536. padding = 0;
  1537. }
  1538. else if (m_textElementType == TMP_TextElementType.Character)
  1539. {
  1540. m_cached_TextElement = m_textInfo.characterInfo[m_characterCount].textElement;
  1541. if (m_cached_TextElement == null) continue;
  1542. m_currentFontAsset = m_textInfo.characterInfo[m_characterCount].fontAsset;
  1543. m_currentMaterial = m_textInfo.characterInfo[m_characterCount].material;
  1544. m_currentMaterialIndex = m_textInfo.characterInfo[m_characterCount].materialReferenceIndex;
  1545. // Re-calculate font scale as the font asset may have changed.
  1546. m_fontScale = m_currentFontSize * smallCapsMultiplier / m_currentFontAsset.faceInfo.pointSize * m_currentFontAsset.faceInfo.scale;
  1547. currentElementScale = m_fontScale * m_fontScaleMultiplier * m_cached_TextElement.scale * m_cached_TextElement.glyph.scale;
  1548. m_textInfo.characterInfo[m_characterCount].elementType = TMP_TextElementType.Character;
  1549. m_textInfo.characterInfo[m_characterCount].scale = currentElementScale;
  1550. padding = m_currentMaterialIndex == 0 ? m_padding : m_subTextObjects[m_currentMaterialIndex].padding;
  1551. }
  1552. #if TMP_PROFILE_ON
  1553. Profiler.EndSample();
  1554. #endif
  1555. #endregion
  1556. // Handle Soft Hyphen
  1557. #region Handle Soft Hyphen
  1558. float old_scale = currentElementScale;
  1559. if (charCode == 0xAD)
  1560. {
  1561. currentElementScale = 0;
  1562. }
  1563. #endregion
  1564. // Store some of the text object's information
  1565. m_textInfo.characterInfo[m_characterCount].character = (char)charCode;
  1566. m_textInfo.characterInfo[m_characterCount].pointSize = m_currentFontSize;
  1567. m_textInfo.characterInfo[m_characterCount].color = m_htmlColor;
  1568. m_textInfo.characterInfo[m_characterCount].underlineColor = m_underlineColor;
  1569. m_textInfo.characterInfo[m_characterCount].strikethroughColor = m_strikethroughColor;
  1570. m_textInfo.characterInfo[m_characterCount].highlightColor = m_highlightColor;
  1571. m_textInfo.characterInfo[m_characterCount].style = m_FontStyleInternal;
  1572. //m_textInfo.characterInfo[m_characterCount].index = m_TextParsingBuffer[i].stringIndex;
  1573. //m_textInfo.characterInfo[m_characterCount].isIgnoringAlignment = m_isIgnoringAlignment;
  1574. // Handle Kerning if Enabled.
  1575. #region Handle Kerning
  1576. TMP_GlyphValueRecord glyphAdjustments = new TMP_GlyphValueRecord();
  1577. float characterSpacingAdjustment = m_characterSpacing;
  1578. if (m_enableKerning)
  1579. {
  1580. if (m_characterCount < totalCharacterCount - 1)
  1581. {
  1582. uint firstGlyphIndex = m_cached_TextElement.glyphIndex;
  1583. uint secondGlyphIndex = m_textInfo.characterInfo[m_characterCount + 1].textElement.glyphIndex;
  1584. long key = new GlyphPairKey(firstGlyphIndex, secondGlyphIndex).key;
  1585. if (m_currentFontAsset.fontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.TryGetValue(key, out TMP_GlyphPairAdjustmentRecord adjustmentPair))
  1586. {
  1587. glyphAdjustments = adjustmentPair.firstAdjustmentRecord.glyphValueRecord;
  1588. characterSpacingAdjustment = (adjustmentPair.featureLookupFlags & FontFeatureLookupFlags.IgnoreSpacingAdjustments) == FontFeatureLookupFlags.IgnoreSpacingAdjustments ? 0 : characterSpacingAdjustment;
  1589. }
  1590. }
  1591. if (m_characterCount >= 1)
  1592. {
  1593. uint firstGlyphIndex = m_textInfo.characterInfo[m_characterCount - 1].textElement.glyphIndex;
  1594. uint secondGlyphIndex = m_cached_TextElement.glyphIndex;
  1595. long key = new GlyphPairKey(firstGlyphIndex, secondGlyphIndex).key;
  1596. if (m_currentFontAsset.fontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.TryGetValue(key, out TMP_GlyphPairAdjustmentRecord adjustmentPair))
  1597. {
  1598. glyphAdjustments += adjustmentPair.secondAdjustmentRecord.glyphValueRecord;
  1599. characterSpacingAdjustment = (adjustmentPair.featureLookupFlags & FontFeatureLookupFlags.IgnoreSpacingAdjustments) == FontFeatureLookupFlags.IgnoreSpacingAdjustments ? 0 : characterSpacingAdjustment;
  1600. }
  1601. }
  1602. }
  1603. #endregion
  1604. // Initial Implementation for RTL support.
  1605. #region Handle Right-to-Left
  1606. if (m_isRightToLeft)
  1607. {
  1608. m_xAdvance -= ((m_cached_TextElement.glyph.metrics.horizontalAdvance * bold_xAdvance_multiplier + characterSpacingAdjustment + m_wordSpacing + m_currentFontAsset.normalSpacingOffset) * currentElementScale + m_cSpacing) * (1 - m_charWidthAdjDelta);
  1609. if (char.IsWhiteSpace((char)charCode) || charCode == 0x200B)
  1610. m_xAdvance -= m_wordSpacing * currentElementScale;
  1611. }
  1612. #endregion
  1613. // Handle Mono Spacing
  1614. #region Handle Mono Spacing
  1615. float monoAdvance = 0;
  1616. if (m_monoSpacing != 0)
  1617. {
  1618. monoAdvance = (m_monoSpacing / 2 - (m_cached_TextElement.glyph.metrics.width / 2 + m_cached_TextElement.glyph.metrics.horizontalBearingX) * currentElementScale) * (1 - m_charWidthAdjDelta);
  1619. m_xAdvance += monoAdvance;
  1620. }
  1621. #endregion
  1622. // Set Padding based on selected font style
  1623. #region Handle Style Padding
  1624. if (m_textElementType == TMP_TextElementType.Character && !isUsingAltTypeface && ((m_FontStyleInternal & FontStyles.Bold) == FontStyles.Bold)) // Checks for any combination of Bold Style.
  1625. {
  1626. if (m_currentMaterial.HasProperty(ShaderUtilities.ID_GradientScale))
  1627. {
  1628. float gradientScale = m_currentMaterial.GetFloat(ShaderUtilities.ID_GradientScale);
  1629. style_padding = m_currentFontAsset.boldStyle / 4.0f * gradientScale * m_currentMaterial.GetFloat(ShaderUtilities.ID_ScaleRatio_A);
  1630. // Clamp overall padding to Gradient Scale size.
  1631. if (style_padding + padding > gradientScale)
  1632. padding = gradientScale - style_padding;
  1633. }
  1634. else
  1635. style_padding = 0;
  1636. bold_xAdvance_multiplier = 1 + m_currentFontAsset.boldSpacing * 0.01f;
  1637. }
  1638. else
  1639. {
  1640. if (m_currentMaterial.HasProperty(ShaderUtilities.ID_GradientScale))
  1641. {
  1642. float gradientScale = m_currentMaterial.GetFloat(ShaderUtilities.ID_GradientScale);
  1643. style_padding = m_currentFontAsset.normalStyle / 4.0f * gradientScale * m_currentMaterial.GetFloat(ShaderUtilities.ID_ScaleRatio_A);
  1644. // Clamp overall padding to Gradient Scale size.
  1645. if (style_padding + padding > gradientScale)
  1646. padding = gradientScale - style_padding;
  1647. }
  1648. else
  1649. style_padding = 0;
  1650. bold_xAdvance_multiplier = 1.0f;
  1651. }
  1652. #endregion Handle Style Padding
  1653. // Determine the position of the vertices of the Character or Sprite.
  1654. #region Calculate Vertices Position
  1655. #if TMP_PROFILE_ON
  1656. Profiler.BeginSample("Calculate Vertices Position");
  1657. #endif
  1658. float fontBaseLineOffset = m_currentFontAsset.faceInfo.baseline * m_fontScale * m_fontScaleMultiplier * m_currentFontAsset.faceInfo.scale;
  1659. Vector3 top_left;
  1660. top_left.x = m_xAdvance + ((m_cached_TextElement.glyph.metrics.horizontalBearingX - padding - style_padding + glyphAdjustments.xPlacement) * currentElementScale * (1 - m_charWidthAdjDelta));
  1661. top_left.y = fontBaseLineOffset + (m_cached_TextElement.glyph.metrics.horizontalBearingY + padding + glyphAdjustments.yPlacement) * currentElementScale - m_lineOffset + m_baselineOffset;
  1662. top_left.z = 0;
  1663. Vector3 bottom_left;
  1664. bottom_left.x = top_left.x;
  1665. bottom_left.y = top_left.y - ((m_cached_TextElement.glyph.metrics.height + padding * 2) * currentElementScale);
  1666. bottom_left.z = 0;
  1667. Vector3 top_right;
  1668. top_right.x = bottom_left.x + ((m_cached_TextElement.glyph.metrics.width + padding * 2 + style_padding * 2) * currentElementScale * (1 - m_charWidthAdjDelta));
  1669. top_right.y = top_left.y;
  1670. top_right.z = 0;
  1671. Vector3 bottom_right;
  1672. bottom_right.x = top_right.x;
  1673. bottom_right.y = bottom_left.y;
  1674. bottom_right.z = 0;
  1675. #if TMP_PROFILE_ON
  1676. Profiler.EndSample();
  1677. #endif
  1678. #endregion
  1679. // Check if we need to Shear the rectangles for Italic styles
  1680. #region Handle Italic & Shearing
  1681. if (m_textElementType == TMP_TextElementType.Character && !isUsingAltTypeface && ((m_FontStyleInternal & FontStyles.Italic) == FontStyles.Italic))
  1682. {
  1683. // Shift Top vertices forward by half (Shear Value * height of character) and Bottom vertices back by same amount.
  1684. float shear_value = m_currentFontAsset.italicStyle * 0.01f;
  1685. Vector3 topShear = new Vector3(shear_value * ((m_cached_TextElement.glyph.metrics.horizontalBearingY + padding + style_padding) * currentElementScale), 0, 0);
  1686. Vector3 bottomShear = new Vector3(shear_value * (((m_cached_TextElement.glyph.metrics.horizontalBearingY - m_cached_TextElement.glyph.metrics.height - padding - style_padding)) * currentElementScale), 0, 0);
  1687. top_left = top_left + topShear;
  1688. bottom_left = bottom_left + bottomShear;
  1689. top_right = top_right + topShear;
  1690. bottom_right = bottom_right + bottomShear;
  1691. }
  1692. #endregion Handle Italics & Shearing
  1693. // Handle Character Rotation
  1694. #region Handle Character Rotation
  1695. if (m_isFXMatrixSet)
  1696. {
  1697. // Apply scale matrix when simulating Condensed text.
  1698. if (m_FXMatrix.lossyScale.x != 1)
  1699. {
  1700. //top_left = m_FXMatrix.MultiplyPoint3x4(top_left);
  1701. //bottom_left = m_FXMatrix.MultiplyPoint3x4(bottom_left);
  1702. //top_right = m_FXMatrix.MultiplyPoint3x4(top_right);
  1703. //bottom_right = m_FXMatrix.MultiplyPoint3x4(bottom_right);
  1704. }
  1705. Vector3 positionOffset = (top_right + bottom_left) / 2;
  1706. top_left = m_FXMatrix.MultiplyPoint3x4(top_left - positionOffset) + positionOffset;
  1707. bottom_left = m_FXMatrix.MultiplyPoint3x4(bottom_left - positionOffset) + positionOffset;
  1708. top_right = m_FXMatrix.MultiplyPoint3x4(top_right - positionOffset) + positionOffset;
  1709. bottom_right = m_FXMatrix.MultiplyPoint3x4(bottom_right - positionOffset) + positionOffset;
  1710. }
  1711. #endregion
  1712. // Store vertex information for the character or sprite.
  1713. m_textInfo.characterInfo[m_characterCount].bottomLeft = bottom_left;
  1714. m_textInfo.characterInfo[m_characterCount].topLeft = top_left;
  1715. m_textInfo.characterInfo[m_characterCount].topRight = top_right;
  1716. m_textInfo.characterInfo[m_characterCount].bottomRight = bottom_right;
  1717. m_textInfo.characterInfo[m_characterCount].origin = m_xAdvance;
  1718. m_textInfo.characterInfo[m_characterCount].baseLine = fontBaseLineOffset - m_lineOffset + m_baselineOffset;
  1719. m_textInfo.characterInfo[m_characterCount].aspectRatio = (top_right.x - bottom_left.x) / (top_left.y - bottom_left.y);
  1720. // Compute and save text element Ascender and maximum line Ascender.
  1721. float elementAscender = m_currentFontAsset.faceInfo.ascentLine * (m_textElementType == TMP_TextElementType.Character ? currentElementScale / smallCapsMultiplier : m_textInfo.characterInfo[m_characterCount].scale) + m_baselineOffset;
  1722. m_textInfo.characterInfo[m_characterCount].ascender = elementAscender - m_lineOffset;
  1723. m_maxLineAscender = elementAscender > m_maxLineAscender ? elementAscender : m_maxLineAscender;
  1724. // Compute and save text element Descender and maximum line Descender.
  1725. float elementDescender = m_currentFontAsset.faceInfo.descentLine * (m_textElementType == TMP_TextElementType.Character ? currentElementScale / smallCapsMultiplier : m_textInfo.characterInfo[m_characterCount].scale) + m_baselineOffset;
  1726. float elementDescenderII = m_textInfo.characterInfo[m_characterCount].descender = elementDescender - m_lineOffset;
  1727. m_maxLineDescender = elementDescender < m_maxLineDescender ? elementDescender : m_maxLineDescender;
  1728. // Adjust maxLineAscender and maxLineDescender if style is superscript or subscript
  1729. if ((m_FontStyleInternal & FontStyles.Subscript) == FontStyles.Subscript || (m_FontStyleInternal & FontStyles.Superscript) == FontStyles.Superscript)
  1730. {
  1731. float baseAscender = (elementAscender - m_baselineOffset) / m_currentFontAsset.faceInfo.subscriptSize;
  1732. elementAscender = m_maxLineAscender;
  1733. m_maxLineAscender = baseAscender > m_maxLineAscender ? baseAscender : m_maxLineAscender;
  1734. float baseDescender = (elementDescender - m_baselineOffset) / m_currentFontAsset.faceInfo.subscriptSize;
  1735. elementDescender = m_maxLineDescender;
  1736. m_maxLineDescender = baseDescender < m_maxLineDescender ? baseDescender : m_maxLineDescender;
  1737. }
  1738. if (m_lineNumber == 0 || m_isNewPage)
  1739. {
  1740. m_maxAscender = m_maxAscender > elementAscender ? m_maxAscender : elementAscender;
  1741. m_maxCapHeight = Mathf.Max(m_maxCapHeight, m_currentFontAsset.faceInfo.capLine * currentElementScale / smallCapsMultiplier);
  1742. }
  1743. if (m_lineOffset == 0) pageAscender = pageAscender > elementAscender ? pageAscender : elementAscender;
  1744. // Set Characters to not visible by default.
  1745. m_textInfo.characterInfo[m_characterCount].isVisible = false;
  1746. // Setup Mesh for visible text elements. ie. not a SPACE / LINEFEED / CARRIAGE RETURN.
  1747. #region Handle Visible Characters
  1748. //#if TMP_PROFILE_ON
  1749. //Profiler.BeginSample("Handle Visible Characters");
  1750. //#endif
  1751. if (charCode == 9 || charCode == 0xA0 || charCode == 0x2007 || (!char.IsWhiteSpace((char)charCode) && charCode != 0x200B) || m_textElementType == TMP_TextElementType.Sprite)
  1752. {
  1753. m_textInfo.characterInfo[m_characterCount].isVisible = true;
  1754. #region Experimental Margin Shaper
  1755. //Vector2 shapedMargins;
  1756. //if (marginShaper)
  1757. //{
  1758. // shapedMargins = m_marginShaper.GetShapedMargins(m_textInfo.characterInfo[m_characterCount].baseLine);
  1759. // if (shapedMargins.x < margins.x)
  1760. // {
  1761. // shapedMargins.x = m_marginLeft;
  1762. // }
  1763. // else
  1764. // {
  1765. // shapedMargins.x += m_marginLeft - margins.x;
  1766. // }
  1767. // if (shapedMargins.y < margins.z)
  1768. // {
  1769. // shapedMargins.y = m_marginRight;
  1770. // }
  1771. // else
  1772. // {
  1773. // shapedMargins.y += m_marginRight - margins.z;
  1774. // }
  1775. //}
  1776. //else
  1777. //{
  1778. // shapedMargins.x = m_marginLeft;
  1779. // shapedMargins.y = m_marginRight;
  1780. //}
  1781. //width = marginWidth + 0.0001f - shapedMargins.x - shapedMargins.y;
  1782. //if (m_width != -1 && m_width < width)
  1783. //{
  1784. // width = m_width;
  1785. //}
  1786. //m_textInfo.lineInfo[m_lineNumber].marginLeft = shapedMargins.x;
  1787. #endregion
  1788. width = m_width != -1 ? Mathf.Min(marginWidth + 0.0001f - m_marginLeft - m_marginRight, m_width) : marginWidth + 0.0001f - m_marginLeft - m_marginRight;
  1789. m_textInfo.lineInfo[m_lineNumber].marginLeft = m_marginLeft;
  1790. bool isJustifiedOrFlush = ((_HorizontalAlignmentOptions)m_lineJustification & _HorizontalAlignmentOptions.Flush) == _HorizontalAlignmentOptions.Flush || ((_HorizontalAlignmentOptions)m_lineJustification & _HorizontalAlignmentOptions.Justified) == _HorizontalAlignmentOptions.Justified;
  1791. // Calculate the line breaking width of the text.
  1792. linebreakingWidth = Mathf.Abs(m_xAdvance) + (!m_isRightToLeft ? m_cached_TextElement.glyph.metrics.horizontalAdvance : 0) * (1 - m_charWidthAdjDelta) * (charCode != 0xAD ? currentElementScale : old_scale);
  1793. // Check if Character exceeds the width of the Text Container
  1794. #region Handle Line Breaking, Text Auto-Sizing and Horizontal Overflow
  1795. if (linebreakingWidth > width * (isJustifiedOrFlush ? 1.05f : 1.0f))
  1796. {
  1797. ellipsisIndex = m_characterCount - 1; // Last safely rendered character
  1798. // Word Wrapping
  1799. #region Handle Word Wrapping
  1800. if (enableWordWrapping && m_characterCount != m_firstCharacterOfLine)
  1801. {
  1802. // Check if word wrapping is still possible
  1803. #region Line Breaking Check
  1804. if (wrappingIndex == m_SavedWordWrapState.previous_WordBreak || isFirstWord)
  1805. {
  1806. // Word wrapping is no longer possible. Shrink size of text if auto-sizing is enabled.
  1807. if (m_enableAutoSizing && m_fontSize > m_fontSizeMin)
  1808. {
  1809. // Handle Character Width Adjustments
  1810. #region Character Width Adjustments
  1811. if (m_charWidthAdjDelta < m_charWidthMaxAdj / 100)
  1812. {
  1813. loopCountA = 0;
  1814. m_charWidthAdjDelta += 0.01f;
  1815. GenerateTextMesh();
  1816. return;
  1817. }
  1818. #endregion
  1819. // Adjust Point Size
  1820. m_maxFontSize = m_fontSize;
  1821. m_fontSize -= Mathf.Max((m_fontSize - m_minFontSize) / 2, 0.05f);
  1822. m_fontSize = (int)(Mathf.Max(m_fontSize, m_fontSizeMin) * 20 + 0.5f) / 20f;
  1823. if (loopCountA > 20) return; // Added to debug
  1824. GenerateTextMesh();
  1825. return;
  1826. }
  1827. // Word wrapping is no longer possible, now breaking up individual words.
  1828. if (m_isCharacterWrappingEnabled == false)
  1829. {
  1830. if (ignoreNonBreakingSpace == false)
  1831. ignoreNonBreakingSpace = true;
  1832. else
  1833. m_isCharacterWrappingEnabled = true;
  1834. }
  1835. else
  1836. isLastBreakingChar = true;
  1837. //m_recursiveCount += 1;
  1838. //if (m_recursiveCount > 20)
  1839. //{
  1840. // Debug.Log("Recursive count exceeded!");
  1841. // continue;
  1842. //}
  1843. }
  1844. #endregion
  1845. // Restore to previously stored state of last valid (space character or linefeed)
  1846. i = RestoreWordWrappingState(ref m_SavedWordWrapState);
  1847. wrappingIndex = i; // Used to detect when line length can no longer be reduced.
  1848. // Handling for Soft Hyphen
  1849. if (m_TextParsingBuffer[i].unicode == 0xAD) // && !m_isCharacterWrappingEnabled) // && ellipsisIndex != i && !m_isCharacterWrappingEnabled)
  1850. {
  1851. m_isTextTruncated = true;
  1852. m_TextParsingBuffer[i].unicode = 0x2D;
  1853. GenerateTextMesh();
  1854. return;
  1855. }
  1856. //Debug.Log("Last Visible Character of line # " + m_lineNumber + " is [" + m_textInfo.characterInfo[m_lastVisibleCharacterOfLine].character + " Character Count: " + m_characterCount + " Last visible: " + m_lastVisibleCharacterOfLine);
  1857. // Check if Line Spacing of previous line needs to be adjusted.
  1858. if (m_lineNumber > 0 && !TMP_Math.Approximately(m_maxLineAscender, m_startOfLineAscender) && m_lineHeight == TMP_Math.FLOAT_UNSET && !m_isNewPage)
  1859. {
  1860. //Debug.Log("(Line Break - Adjusting Line Spacing on line #" + m_lineNumber);
  1861. float offsetDelta = m_maxLineAscender - m_startOfLineAscender;
  1862. AdjustLineOffset(m_firstCharacterOfLine, m_characterCount, offsetDelta);
  1863. m_lineOffset += offsetDelta;
  1864. m_SavedWordWrapState.lineOffset = m_lineOffset;
  1865. m_SavedWordWrapState.previousLineAscender = m_maxLineAscender;
  1866. // TODO - Add check for character exceeding vertical bounds
  1867. }
  1868. m_isNewPage = false;
  1869. // Calculate lineAscender & make sure if last character is superscript or subscript that we check that as well.
  1870. float lineAscender = m_maxLineAscender - m_lineOffset;
  1871. float lineDescender = m_maxLineDescender - m_lineOffset;
  1872. // Update maxDescender and maxVisibleDescender
  1873. m_maxDescender = m_maxDescender < lineDescender ? m_maxDescender : lineDescender;
  1874. if (!isMaxVisibleDescenderSet)
  1875. maxVisibleDescender = m_maxDescender;
  1876. if (m_useMaxVisibleDescender && (m_characterCount >= m_maxVisibleCharacters || m_lineNumber >= m_maxVisibleLines))
  1877. isMaxVisibleDescenderSet = true;
  1878. // Track & Store lineInfo for the new line
  1879. m_textInfo.lineInfo[m_lineNumber].firstCharacterIndex = m_firstCharacterOfLine;
  1880. m_textInfo.lineInfo[m_lineNumber].firstVisibleCharacterIndex = m_firstVisibleCharacterOfLine = m_firstCharacterOfLine > m_firstVisibleCharacterOfLine ? m_firstCharacterOfLine : m_firstVisibleCharacterOfLine;
  1881. m_textInfo.lineInfo[m_lineNumber].lastCharacterIndex = m_lastCharacterOfLine = m_characterCount - 1 > 0 ? m_characterCount - 1 : 0;
  1882. m_textInfo.lineInfo[m_lineNumber].lastVisibleCharacterIndex = m_lastVisibleCharacterOfLine = m_lastVisibleCharacterOfLine < m_firstVisibleCharacterOfLine ? m_firstVisibleCharacterOfLine : m_lastVisibleCharacterOfLine;
  1883. m_textInfo.lineInfo[m_lineNumber].characterCount = m_textInfo.lineInfo[m_lineNumber].lastCharacterIndex - m_textInfo.lineInfo[m_lineNumber].firstCharacterIndex + 1;
  1884. m_textInfo.lineInfo[m_lineNumber].visibleCharacterCount = m_lineVisibleCharacterCount;
  1885. m_textInfo.lineInfo[m_lineNumber].lineExtents.min = new Vector2(m_textInfo.characterInfo[m_firstVisibleCharacterOfLine].bottomLeft.x, lineDescender);
  1886. m_textInfo.lineInfo[m_lineNumber].lineExtents.max = new Vector2(m_textInfo.characterInfo[m_lastVisibleCharacterOfLine].topRight.x, lineAscender);
  1887. m_textInfo.lineInfo[m_lineNumber].length = m_textInfo.lineInfo[m_lineNumber].lineExtents.max.x;
  1888. m_textInfo.lineInfo[m_lineNumber].width = width;
  1889. //m_textInfo.lineInfo[m_lineNumber].alignment = m_lineJustification;
  1890. m_textInfo.lineInfo[m_lineNumber].maxAdvance = m_textInfo.characterInfo[m_lastVisibleCharacterOfLine].xAdvance - (characterSpacingAdjustment + m_currentFontAsset.normalSpacingOffset) * currentElementScale - m_cSpacing;
  1891. m_textInfo.lineInfo[m_lineNumber].baseline = 0 - m_lineOffset;
  1892. m_textInfo.lineInfo[m_lineNumber].ascender = lineAscender;
  1893. m_textInfo.lineInfo[m_lineNumber].descender = lineDescender;
  1894. m_textInfo.lineInfo[m_lineNumber].lineHeight = lineAscender - lineDescender + lineGap * baseScale;
  1895. m_firstCharacterOfLine = m_characterCount; // Store first character of the next line.
  1896. m_lineVisibleCharacterCount = 0;
  1897. // Store the state of the line before starting on the new line.
  1898. SaveWordWrappingState(ref m_SavedLineState, i, m_characterCount - 1);
  1899. m_lineNumber += 1;
  1900. isStartOfNewLine = true;
  1901. isFirstWord = true;
  1902. // Check to make sure Array is large enough to hold a new line.
  1903. if (m_lineNumber >= m_textInfo.lineInfo.Length)
  1904. ResizeLineExtents(m_lineNumber);
  1905. // Apply Line Spacing based on scale of the last character of the line.
  1906. if (m_lineHeight == TMP_Math.FLOAT_UNSET)
  1907. {
  1908. float ascender = m_textInfo.characterInfo[m_characterCount].ascender - m_textInfo.characterInfo[m_characterCount].baseLine;
  1909. lineOffsetDelta = 0 - m_maxLineDescender + ascender + (lineGap + m_lineSpacing + m_lineSpacingDelta) * baseScale;
  1910. m_lineOffset += lineOffsetDelta;
  1911. m_startOfLineAscender = ascender;
  1912. }
  1913. else
  1914. m_lineOffset += m_lineHeight + m_lineSpacing * baseScale;
  1915. m_maxLineAscender = k_LargeNegativeFloat;
  1916. m_maxLineDescender = k_LargePositiveFloat;
  1917. m_xAdvance = 0 + tag_Indent;
  1918. continue;
  1919. }
  1920. #endregion End Word Wrapping
  1921. // Text Auto-Sizing (text exceeding Width of container.
  1922. #region Handle Text Auto-Sizing
  1923. if (m_enableAutoSizing && m_fontSize > m_fontSizeMin)
  1924. {
  1925. // Handle Character Width Adjustments
  1926. #region Character Width Adjustments
  1927. if (m_charWidthAdjDelta < m_charWidthMaxAdj / 100)
  1928. {
  1929. loopCountA = 0;
  1930. m_charWidthAdjDelta += 0.01f;
  1931. GenerateTextMesh();
  1932. return;
  1933. }
  1934. #endregion
  1935. // Adjust Point Size
  1936. m_maxFontSize = m_fontSize;
  1937. m_fontSize -= Mathf.Max((m_fontSize - m_minFontSize) / 2, 0.05f);
  1938. m_fontSize = (int)(Mathf.Max(m_fontSize, m_fontSizeMin) * 20 + 0.5f) / 20f;
  1939. //m_recursiveCount = 0;
  1940. if (loopCountA > 20) return; // Added to debug
  1941. GenerateTextMesh();
  1942. return;
  1943. }
  1944. #endregion End Text Auto-Sizing
  1945. // Handle Text Overflow
  1946. #region Handle Text Overflow
  1947. switch (m_overflowMode)
  1948. {
  1949. case TextOverflowModes.Overflow:
  1950. if (m_isMaskingEnabled)
  1951. DisableMasking();
  1952. break;
  1953. case TextOverflowModes.Ellipsis:
  1954. if (m_isMaskingEnabled)
  1955. DisableMasking();
  1956. m_isTextTruncated = true;
  1957. if (m_characterCount < 1)
  1958. {
  1959. m_textInfo.characterInfo[m_characterCount].isVisible = false;
  1960. //m_visibleCharacterCount = 0;
  1961. break;
  1962. }
  1963. m_TextParsingBuffer[i - 1].unicode = 8230;
  1964. m_TextParsingBuffer[i].unicode = (char)0;
  1965. if (m_cached_Ellipsis_Character != null)
  1966. {
  1967. m_textInfo.characterInfo[ellipsisIndex].character = (char)8230;
  1968. m_textInfo.characterInfo[ellipsisIndex].textElement = m_cached_Ellipsis_Character;
  1969. m_textInfo.characterInfo[ellipsisIndex].fontAsset = m_materialReferences[0].fontAsset;
  1970. m_textInfo.characterInfo[ellipsisIndex].material = m_materialReferences[0].material;
  1971. m_textInfo.characterInfo[ellipsisIndex].materialReferenceIndex = 0;
  1972. }
  1973. else
  1974. {
  1975. Debug.LogWarning("Unable to use Ellipsis character since it wasn't found in the current Font Asset [" + m_fontAsset.name + "]. Consider regenerating this font asset to include the Ellipsis character (u+2026).\nNote: Warnings can be disabled in the TMP Settings file.", this);
  1976. }
  1977. m_totalCharacterCount = ellipsisIndex + 1;
  1978. GenerateTextMesh();
  1979. return;
  1980. //case TextOverflowModes.Masking:
  1981. // if (!m_isMaskingEnabled)
  1982. // EnableMasking();
  1983. // break;
  1984. //case TextOverflowModes.ScrollRect:
  1985. // if (!m_isMaskingEnabled)
  1986. // EnableMasking();
  1987. // break;
  1988. case TextOverflowModes.Truncate:
  1989. if (m_isMaskingEnabled)
  1990. DisableMasking();
  1991. m_textInfo.characterInfo[m_characterCount].isVisible = false;
  1992. break;
  1993. case TextOverflowModes.Linked:
  1994. //m_textInfo.characterInfo[m_characterCount].isVisible = false;
  1995. //if (m_linkedTextComponent != null)
  1996. //{
  1997. // m_linkedTextComponent.text = text;
  1998. // m_linkedTextComponent.firstVisibleCharacter = m_characterCount;
  1999. // m_linkedTextComponent.ForceMeshUpdate();
  2000. //}
  2001. break;
  2002. }
  2003. #endregion End Text Overflow
  2004. }
  2005. #endregion End Check for Characters Exceeding Width of Text Container
  2006. // Special handling of characters that are not ignored at the end of a line.
  2007. if (charCode == 9 || charCode == 0xA0 || charCode == 0x2007)
  2008. {
  2009. m_textInfo.characterInfo[m_characterCount].isVisible = false;
  2010. m_lastVisibleCharacterOfLine = m_characterCount;
  2011. m_textInfo.lineInfo[m_lineNumber].spaceCount += 1;
  2012. m_textInfo.spaceCount += 1;
  2013. if (charCode == 0xA0)
  2014. m_textInfo.lineInfo[m_lineNumber].controlCharacterCount += 1;
  2015. }
  2016. else
  2017. {
  2018. // Determine Vertex Color
  2019. if (m_overrideHtmlColors)
  2020. vertexColor = m_fontColor32;
  2021. else
  2022. vertexColor = m_htmlColor;
  2023. // Store Character & Sprite Vertex Information
  2024. if (m_textElementType == TMP_TextElementType.Character)
  2025. {
  2026. // Save Character Vertex Data
  2027. SaveGlyphVertexInfo(padding, style_padding, vertexColor);
  2028. }
  2029. else if (m_textElementType == TMP_TextElementType.Sprite)
  2030. {
  2031. SaveSpriteVertexInfo(vertexColor);
  2032. }
  2033. }
  2034. // Increase visible count for Characters.
  2035. if (m_textInfo.characterInfo[m_characterCount].isVisible && charCode != 0xAD)
  2036. {
  2037. if (isStartOfNewLine) { isStartOfNewLine = false; m_firstVisibleCharacterOfLine = m_characterCount; }
  2038. m_lineVisibleCharacterCount += 1;
  2039. m_lastVisibleCharacterOfLine = m_characterCount;
  2040. }
  2041. }
  2042. else
  2043. { // This is a Space, Tab, LineFeed or Carriage Return
  2044. // Track # of spaces per line which is used for line justification.
  2045. if ((charCode == 10 || char.IsSeparator((char)charCode)) && charCode != 0xAD && charCode != 0x200B && charCode != 0x2060)
  2046. {
  2047. m_textInfo.lineInfo[m_lineNumber].spaceCount += 1;
  2048. m_textInfo.spaceCount += 1;
  2049. }
  2050. }
  2051. //#if TMP_PROFILE_ON
  2052. //Profiler.EndSample();
  2053. //#endif
  2054. #endregion Handle Visible Characters
  2055. // Check if Line Spacing of previous line needs to be adjusted.
  2056. #region Adjust Line Spacing
  2057. #if TMP_PROFILE_ON
  2058. Profiler.BeginSample("Adjust Line Spacing");
  2059. #endif
  2060. if (m_lineNumber > 0 && !TMP_Math.Approximately(m_maxLineAscender, m_startOfLineAscender) && m_lineHeight == TMP_Math.FLOAT_UNSET && !m_isNewPage)
  2061. {
  2062. //Debug.Log("Inline - Adjusting Line Spacing on line #" + m_lineNumber);
  2063. //float gap = 0; // Compute gap.
  2064. float offsetDelta = m_maxLineAscender - m_startOfLineAscender;
  2065. AdjustLineOffset(m_firstCharacterOfLine, m_characterCount, offsetDelta);
  2066. elementDescenderII -= offsetDelta;
  2067. m_lineOffset += offsetDelta;
  2068. m_startOfLineAscender += offsetDelta;
  2069. m_SavedWordWrapState.lineOffset = m_lineOffset;
  2070. m_SavedWordWrapState.previousLineAscender = m_startOfLineAscender;
  2071. }
  2072. #if TMP_PROFILE_ON
  2073. Profiler.EndSample();
  2074. #endif
  2075. #endregion
  2076. // Store Rectangle positions for each Character.
  2077. #region Store Character Data
  2078. m_textInfo.characterInfo[m_characterCount].lineNumber = m_lineNumber;
  2079. m_textInfo.characterInfo[m_characterCount].pageNumber = m_pageNumber;
  2080. if (charCode != 10 && charCode != 13 && charCode != 8230 || m_textInfo.lineInfo[m_lineNumber].characterCount == 1)
  2081. m_textInfo.lineInfo[m_lineNumber].alignment = m_lineJustification;
  2082. #endregion Store Character Data
  2083. // Check if text Exceeds the vertical bounds of the margin area.
  2084. #region Check Vertical Bounds & Auto-Sizing
  2085. #if TMP_PROFILE_ON
  2086. Profiler.BeginSample("Check Vertical Bounds");
  2087. #endif
  2088. if (m_maxAscender - elementDescenderII > marginHeight + 0.0001f)
  2089. {
  2090. // Handle Line spacing adjustments
  2091. #region Line Spacing Adjustments
  2092. if (m_enableAutoSizing && m_lineSpacingDelta > m_lineSpacingMax && m_lineNumber > 0)
  2093. {
  2094. loopCountA = 0;
  2095. m_lineSpacingDelta -= 1;
  2096. GenerateTextMesh();
  2097. return;
  2098. }
  2099. #endregion
  2100. // Handle Text Auto-sizing resulting from text exceeding vertical bounds.
  2101. #region Text Auto-Sizing (Text greater than vertical bounds)
  2102. if (m_enableAutoSizing && m_fontSize > m_fontSizeMin)
  2103. {
  2104. m_maxFontSize = m_fontSize;
  2105. m_fontSize -= Mathf.Max((m_fontSize - m_minFontSize) / 2, 0.05f);
  2106. m_fontSize = (int)(Mathf.Max(m_fontSize, m_fontSizeMin) * 20 + 0.5f) / 20f;
  2107. //m_recursiveCount = 0;
  2108. if (loopCountA > 20) return; // Added to debug
  2109. GenerateTextMesh();
  2110. return;
  2111. }
  2112. #endregion Text Auto-Sizing
  2113. // Set isTextOverflowing and firstOverflowCharacterIndex
  2114. if (m_firstOverflowCharacterIndex == -1)
  2115. m_firstOverflowCharacterIndex = m_characterCount;
  2116. // Handle Text Overflow
  2117. #region Text Overflow
  2118. switch (m_overflowMode)
  2119. {
  2120. case TextOverflowModes.Overflow:
  2121. if (m_isMaskingEnabled)
  2122. DisableMasking();
  2123. break;
  2124. case TextOverflowModes.Ellipsis:
  2125. if (m_isMaskingEnabled)
  2126. DisableMasking();
  2127. if (m_lineNumber > 0)
  2128. {
  2129. m_TextParsingBuffer[m_textInfo.characterInfo[ellipsisIndex].index].unicode = 8230;
  2130. m_TextParsingBuffer[m_textInfo.characterInfo[ellipsisIndex].index + 1].unicode = (char)0;
  2131. if (m_cached_Ellipsis_Character != null)
  2132. {
  2133. m_textInfo.characterInfo[ellipsisIndex].character = (char)8230;
  2134. m_textInfo.characterInfo[ellipsisIndex].textElement = m_cached_Ellipsis_Character;
  2135. m_textInfo.characterInfo[ellipsisIndex].fontAsset = m_materialReferences[0].fontAsset;
  2136. m_textInfo.characterInfo[ellipsisIndex].material = m_materialReferences[0].material;
  2137. m_textInfo.characterInfo[ellipsisIndex].materialReferenceIndex = 0;
  2138. }
  2139. else
  2140. {
  2141. Debug.LogWarning("Unable to use Ellipsis character since it wasn't found in the current Font Asset [" + m_fontAsset.name + "]. Consider regenerating this font asset to include the Ellipsis character (u+2026).\nNote: Warnings can be disabled in the TMP Settings file.", this);
  2142. }
  2143. m_totalCharacterCount = ellipsisIndex + 1;
  2144. GenerateTextMesh();
  2145. m_isTextTruncated = true;
  2146. return;
  2147. }
  2148. else
  2149. {
  2150. ClearMesh();
  2151. return;
  2152. }
  2153. //case TextOverflowModes.Masking:
  2154. // if (!m_isMaskingEnabled)
  2155. // EnableMasking();
  2156. // break;
  2157. //case TextOverflowModes.ScrollRect:
  2158. // if (!m_isMaskingEnabled)
  2159. // EnableMasking();
  2160. // break;
  2161. case TextOverflowModes.Truncate:
  2162. if (m_isMaskingEnabled)
  2163. DisableMasking();
  2164. // TODO : Optimize
  2165. if (m_lineNumber > 0)
  2166. {
  2167. m_TextParsingBuffer[m_textInfo.characterInfo[ellipsisIndex].index + 1].unicode = (char)0;
  2168. m_totalCharacterCount = ellipsisIndex + 1;
  2169. GenerateTextMesh();
  2170. m_isTextTruncated = true;
  2171. return;
  2172. }
  2173. else
  2174. {
  2175. ClearMesh();
  2176. return;
  2177. }
  2178. case TextOverflowModes.Page:
  2179. if (m_isMaskingEnabled)
  2180. DisableMasking();
  2181. // Ignore Page Break, Linefeed or carriage return
  2182. if (charCode == 13 || charCode == 10)
  2183. break;
  2184. // Return if the first character doesn't fit.
  2185. if (i == 0)
  2186. {
  2187. ClearMesh();
  2188. return;
  2189. }
  2190. else if (previousPageOverflowChar == i)
  2191. {
  2192. m_TextParsingBuffer[i].unicode = 0;
  2193. m_isTextTruncated = true;
  2194. }
  2195. previousPageOverflowChar = i;
  2196. // Go back to previous line and re-layout
  2197. i = RestoreWordWrappingState(ref m_SavedLineState);
  2198. m_isNewPage = true;
  2199. m_xAdvance = 0 + tag_Indent;
  2200. m_lineOffset = 0;
  2201. m_maxAscender = 0;
  2202. pageAscender = 0;
  2203. m_lineNumber += 1;
  2204. m_pageNumber += 1;
  2205. continue;
  2206. case TextOverflowModes.Linked:
  2207. if (m_linkedTextComponent != null)
  2208. {
  2209. m_linkedTextComponent.text = text;
  2210. m_linkedTextComponent.firstVisibleCharacter = m_characterCount;
  2211. m_linkedTextComponent.ForceMeshUpdate();
  2212. }
  2213. // Truncate remaining text
  2214. if (m_lineNumber > 0)
  2215. {
  2216. m_TextParsingBuffer[i].unicode = (char)0;
  2217. m_totalCharacterCount = m_characterCount;
  2218. // TODO : Optimize as we should be able to end the layout phase here without having to do another pass.
  2219. GenerateTextMesh();
  2220. m_isTextTruncated = true;
  2221. return;
  2222. }
  2223. else
  2224. {
  2225. ClearMesh();
  2226. return;
  2227. }
  2228. }
  2229. #endregion End Text Overflow
  2230. }
  2231. #if TMP_PROFILE_ON
  2232. Profiler.EndSample();
  2233. #endif
  2234. #endregion Check Vertical Bounds
  2235. // Handle xAdvance & Tabulation Stops. Tab stops at every 25% of Font Size.
  2236. #region XAdvance, Tabulation & Stops
  2237. if (charCode == 9)
  2238. {
  2239. float tabSize = m_currentFontAsset.faceInfo.tabWidth * m_currentFontAsset.tabSize * currentElementScale;
  2240. float tabs = Mathf.Ceil(m_xAdvance / tabSize) * tabSize;
  2241. m_xAdvance = tabs > m_xAdvance ? tabs : m_xAdvance + tabSize;
  2242. }
  2243. else if (m_monoSpacing != 0)
  2244. {
  2245. m_xAdvance += (m_monoSpacing - monoAdvance + ((characterSpacingAdjustment + m_currentFontAsset.normalSpacingOffset) * currentElementScale) + m_cSpacing) * (1 - m_charWidthAdjDelta);
  2246. if (char.IsWhiteSpace((char)charCode) || charCode == 0x200B)
  2247. m_xAdvance += m_wordSpacing * currentElementScale;
  2248. }
  2249. else if (!m_isRightToLeft)
  2250. {
  2251. float scaleFXMultiplier = 1;
  2252. if (m_isFXMatrixSet) scaleFXMultiplier = m_FXMatrix.lossyScale.x;
  2253. m_xAdvance += ((m_cached_TextElement.glyph.metrics.horizontalAdvance * scaleFXMultiplier * bold_xAdvance_multiplier + characterSpacingAdjustment + m_currentFontAsset.normalSpacingOffset + glyphAdjustments.xAdvance) * currentElementScale + m_cSpacing) * (1 - m_charWidthAdjDelta);
  2254. if (char.IsWhiteSpace((char)charCode) || charCode == 0x200B)
  2255. m_xAdvance += m_wordSpacing * currentElementScale;
  2256. }
  2257. else
  2258. {
  2259. m_xAdvance -= glyphAdjustments.xAdvance * currentElementScale;
  2260. }
  2261. // Store xAdvance information
  2262. m_textInfo.characterInfo[m_characterCount].xAdvance = m_xAdvance;
  2263. #endregion Tabulation & Stops
  2264. // Handle Carriage Return
  2265. #region Carriage Return
  2266. if (charCode == 13)
  2267. {
  2268. m_xAdvance = 0 + tag_Indent;
  2269. }
  2270. #endregion Carriage Return
  2271. // Handle Line Spacing Adjustments + Word Wrapping & special case for last line.
  2272. #region Check for Line Feed and Last Character
  2273. #if TMP_PROFILE_ON
  2274. Profiler.BeginSample("Process Linefeed");
  2275. #endif
  2276. if (charCode == 10 || m_characterCount == totalCharacterCount - 1)
  2277. {
  2278. // Check if Line Spacing of previous line needs to be adjusted.
  2279. if (m_lineNumber > 0 && !TMP_Math.Approximately(m_maxLineAscender, m_startOfLineAscender) && m_lineHeight == TMP_Math.FLOAT_UNSET && !m_isNewPage)
  2280. {
  2281. //Debug.Log("Line Feed - Adjusting Line Spacing on line #" + m_lineNumber);
  2282. float offsetDelta = m_maxLineAscender - m_startOfLineAscender;
  2283. AdjustLineOffset(m_firstCharacterOfLine, m_characterCount, offsetDelta);
  2284. elementDescenderII -= offsetDelta;
  2285. m_lineOffset += offsetDelta;
  2286. }
  2287. m_isNewPage = false;
  2288. // Calculate lineAscender & make sure if last character is superscript or subscript that we check that as well.
  2289. float lineAscender = m_maxLineAscender - m_lineOffset;
  2290. float lineDescender = m_maxLineDescender - m_lineOffset;
  2291. // Update maxDescender and maxVisibleDescender
  2292. m_maxDescender = m_maxDescender < lineDescender ? m_maxDescender : lineDescender;
  2293. if (!isMaxVisibleDescenderSet)
  2294. maxVisibleDescender = m_maxDescender;
  2295. if (m_useMaxVisibleDescender && (m_characterCount >= m_maxVisibleCharacters || m_lineNumber >= m_maxVisibleLines))
  2296. isMaxVisibleDescenderSet = true;
  2297. // Save Line Information
  2298. m_textInfo.lineInfo[m_lineNumber].firstCharacterIndex = m_firstCharacterOfLine;
  2299. m_textInfo.lineInfo[m_lineNumber].firstVisibleCharacterIndex = m_firstVisibleCharacterOfLine = m_firstCharacterOfLine > m_firstVisibleCharacterOfLine ? m_firstCharacterOfLine : m_firstVisibleCharacterOfLine;
  2300. m_textInfo.lineInfo[m_lineNumber].lastCharacterIndex = m_lastCharacterOfLine = m_characterCount;
  2301. m_textInfo.lineInfo[m_lineNumber].lastVisibleCharacterIndex = m_lastVisibleCharacterOfLine = m_lastVisibleCharacterOfLine < m_firstVisibleCharacterOfLine ? m_firstVisibleCharacterOfLine : m_lastVisibleCharacterOfLine;
  2302. m_textInfo.lineInfo[m_lineNumber].characterCount = m_textInfo.lineInfo[m_lineNumber].lastCharacterIndex - m_textInfo.lineInfo[m_lineNumber].firstCharacterIndex + 1;
  2303. m_textInfo.lineInfo[m_lineNumber].visibleCharacterCount = m_lineVisibleCharacterCount;
  2304. m_textInfo.lineInfo[m_lineNumber].lineExtents.min = new Vector2(m_textInfo.characterInfo[m_firstVisibleCharacterOfLine].bottomLeft.x, lineDescender);
  2305. m_textInfo.lineInfo[m_lineNumber].lineExtents.max = new Vector2(m_textInfo.characterInfo[m_lastVisibleCharacterOfLine].topRight.x, lineAscender);
  2306. m_textInfo.lineInfo[m_lineNumber].length = m_textInfo.lineInfo[m_lineNumber].lineExtents.max.x - (padding * currentElementScale);
  2307. m_textInfo.lineInfo[m_lineNumber].width = width;
  2308. if (m_textInfo.lineInfo[m_lineNumber].characterCount == 1)
  2309. m_textInfo.lineInfo[m_lineNumber].alignment = m_lineJustification;
  2310. if (m_textInfo.characterInfo[m_lastVisibleCharacterOfLine].isVisible)
  2311. m_textInfo.lineInfo[m_lineNumber].maxAdvance = m_textInfo.characterInfo[m_lastVisibleCharacterOfLine].xAdvance - (characterSpacingAdjustment + m_currentFontAsset.normalSpacingOffset) * currentElementScale - m_cSpacing;
  2312. else
  2313. m_textInfo.lineInfo[m_lineNumber].maxAdvance = m_textInfo.characterInfo[m_lastCharacterOfLine].xAdvance - (characterSpacingAdjustment + m_currentFontAsset.normalSpacingOffset) * currentElementScale - m_cSpacing;
  2314. m_textInfo.lineInfo[m_lineNumber].baseline = 0 - m_lineOffset;
  2315. m_textInfo.lineInfo[m_lineNumber].ascender = lineAscender;
  2316. m_textInfo.lineInfo[m_lineNumber].descender = lineDescender;
  2317. m_textInfo.lineInfo[m_lineNumber].lineHeight = lineAscender - lineDescender + lineGap * baseScale;
  2318. m_firstCharacterOfLine = m_characterCount + 1;
  2319. m_lineVisibleCharacterCount = 0;
  2320. // Add new line if not last line or character.
  2321. if (charCode == 10)
  2322. {
  2323. // Store the state of the line before starting on the new line.
  2324. SaveWordWrappingState(ref m_SavedLineState, i, m_characterCount);
  2325. // Store the state of the last Character before the new line.
  2326. SaveWordWrappingState(ref m_SavedWordWrapState, i, m_characterCount);
  2327. m_lineNumber += 1;
  2328. isStartOfNewLine = true;
  2329. ignoreNonBreakingSpace = false;
  2330. isFirstWord = true;
  2331. // Check to make sure Array is large enough to hold a new line.
  2332. if (m_lineNumber >= m_textInfo.lineInfo.Length)
  2333. ResizeLineExtents(m_lineNumber);
  2334. // Apply Line Spacing
  2335. if (m_lineHeight == TMP_Math.FLOAT_UNSET)
  2336. {
  2337. lineOffsetDelta = 0 - m_maxLineDescender + elementAscender + (lineGap + m_lineSpacing + m_paragraphSpacing + m_lineSpacingDelta) * baseScale;
  2338. m_lineOffset += lineOffsetDelta;
  2339. }
  2340. else
  2341. m_lineOffset += m_lineHeight + (m_lineSpacing + m_paragraphSpacing) * baseScale;
  2342. m_maxLineAscender = k_LargeNegativeFloat;
  2343. m_maxLineDescender = k_LargePositiveFloat;
  2344. m_startOfLineAscender = elementAscender;
  2345. m_xAdvance = 0 + tag_LineIndent + tag_Indent;
  2346. ellipsisIndex = m_characterCount - 1;
  2347. m_characterCount += 1;
  2348. continue;
  2349. }
  2350. }
  2351. #if TMP_PROFILE_ON
  2352. Profiler.EndSample();
  2353. #endif
  2354. #endregion Check for Linefeed or Last Character
  2355. // Store Rectangle positions for each Character.
  2356. #region Save CharacterInfo for the current character.
  2357. #if TMP_PROFILE_ON
  2358. Profiler.BeginSample("Save CharacterInfo & Extents");
  2359. #endif
  2360. // Determine the bounds of the Mesh.
  2361. if (m_textInfo.characterInfo[m_characterCount].isVisible)
  2362. {
  2363. m_meshExtents.min.x = Mathf.Min(m_meshExtents.min.x, m_textInfo.characterInfo[m_characterCount].bottomLeft.x);
  2364. m_meshExtents.min.y = Mathf.Min(m_meshExtents.min.y, m_textInfo.characterInfo[m_characterCount].bottomLeft.y);
  2365. m_meshExtents.max.x = Mathf.Max(m_meshExtents.max.x, m_textInfo.characterInfo[m_characterCount].topRight.x);
  2366. m_meshExtents.max.y = Mathf.Max(m_meshExtents.max.y, m_textInfo.characterInfo[m_characterCount].topRight.y);
  2367. //m_meshExtents.min = new Vector2(Mathf.Min(m_meshExtents.min.x, m_textInfo.characterInfo[m_characterCount].bottomLeft.x), Mathf.Min(m_meshExtents.min.y, m_textInfo.characterInfo[m_characterCount].bottomLeft.y));
  2368. //m_meshExtents.max = new Vector2(Mathf.Max(m_meshExtents.max.x, m_textInfo.characterInfo[m_characterCount].topRight.x), Mathf.Max(m_meshExtents.max.y, m_textInfo.characterInfo[m_characterCount].topRight.y));
  2369. }
  2370. // Save pageInfo Data
  2371. if (m_overflowMode == TextOverflowModes.Page && charCode != 13 && charCode != 10) // && m_pageNumber < 16)
  2372. {
  2373. // Check if we need to increase allocations for the pageInfo array.
  2374. if (m_pageNumber + 1 > m_textInfo.pageInfo.Length)
  2375. TMP_TextInfo.Resize(ref m_textInfo.pageInfo, m_pageNumber + 1, true);
  2376. m_textInfo.pageInfo[m_pageNumber].ascender = pageAscender;
  2377. m_textInfo.pageInfo[m_pageNumber].descender = elementDescender < m_textInfo.pageInfo[m_pageNumber].descender ? elementDescender : m_textInfo.pageInfo[m_pageNumber].descender;
  2378. if (m_pageNumber == 0 && m_characterCount == 0)
  2379. m_textInfo.pageInfo[m_pageNumber].firstCharacterIndex = m_characterCount;
  2380. else if (m_characterCount > 0 && m_pageNumber != m_textInfo.characterInfo[m_characterCount - 1].pageNumber)
  2381. {
  2382. m_textInfo.pageInfo[m_pageNumber - 1].lastCharacterIndex = m_characterCount - 1;
  2383. m_textInfo.pageInfo[m_pageNumber].firstCharacterIndex = m_characterCount;
  2384. }
  2385. else if (m_characterCount == totalCharacterCount - 1)
  2386. m_textInfo.pageInfo[m_pageNumber].lastCharacterIndex = m_characterCount;
  2387. }
  2388. #if TMP_PROFILE_ON
  2389. Profiler.EndSample();
  2390. #endif
  2391. #endregion Saving CharacterInfo
  2392. // Save State of Mesh Creation for handling of Word Wrapping
  2393. #region Save Word Wrapping State
  2394. #if TMP_PROFILE_ON
  2395. Profiler.BeginSample("Save Word Wrapping State");
  2396. #endif
  2397. if (m_enableWordWrapping || m_overflowMode == TextOverflowModes.Truncate || m_overflowMode == TextOverflowModes.Ellipsis)
  2398. {
  2399. if ((char.IsWhiteSpace((char)charCode) || charCode == 0x200B || charCode == 0x2D || charCode == 0xAD) && (!m_isNonBreakingSpace || ignoreNonBreakingSpace) && charCode != 0xA0 && charCode != 0x2007 && charCode != 0x2011 && charCode != 0x202F && charCode != 0x2060)
  2400. {
  2401. // We store the state of numerous variables for the most recent Space, LineFeed or Carriage Return to enable them to be restored
  2402. // for Word Wrapping.
  2403. SaveWordWrappingState(ref m_SavedWordWrapState, i, m_characterCount);
  2404. m_isCharacterWrappingEnabled = false;
  2405. isFirstWord = false;
  2406. }
  2407. // Handling for East Asian languages
  2408. else if (( charCode > 0x1100 && charCode < 0x11ff || /* Hangul Jamo */
  2409. charCode > 0x2E80 && charCode < 0x9FFF || /* CJK */
  2410. charCode > 0xA960 && charCode < 0xA97F || /* Hangul Jame Extended-A */
  2411. charCode > 0xAC00 && charCode < 0xD7FF || /* Hangul Syllables */
  2412. charCode > 0xF900 && charCode < 0xFAFF || /* CJK Compatibility Ideographs */
  2413. charCode > 0xFE30 && charCode < 0xFE4F || /* CJK Compatibility Forms */
  2414. charCode > 0xFF00 && charCode < 0xFFEF) /* CJK Halfwidth */
  2415. && !m_isNonBreakingSpace)
  2416. {
  2417. if (isFirstWord || isLastBreakingChar || TMP_Settings.linebreakingRules.leadingCharacters.ContainsKey(charCode) == false &&
  2418. (m_characterCount < totalCharacterCount - 1 &&
  2419. TMP_Settings.linebreakingRules.followingCharacters.ContainsKey(m_textInfo.characterInfo[m_characterCount + 1].character) == false))
  2420. {
  2421. SaveWordWrappingState(ref m_SavedWordWrapState, i, m_characterCount);
  2422. m_isCharacterWrappingEnabled = false;
  2423. isFirstWord = false;
  2424. }
  2425. }
  2426. else if ((isFirstWord || m_isCharacterWrappingEnabled == true || isLastBreakingChar))
  2427. SaveWordWrappingState(ref m_SavedWordWrapState, i, m_characterCount);
  2428. }
  2429. #if TMP_PROFILE_ON
  2430. Profiler.EndSample();
  2431. #endif
  2432. #endregion Save Word Wrapping State
  2433. m_characterCount += 1;
  2434. }
  2435. // Check Auto Sizing and increase font size to fill text container.
  2436. #region Check Auto-Sizing (Upper Font Size Bounds)
  2437. fontSizeDelta = m_maxFontSize - m_minFontSize;
  2438. if (!m_isCharacterWrappingEnabled && m_enableAutoSizing && fontSizeDelta > 0.051f && m_fontSize < m_fontSizeMax)
  2439. {
  2440. m_minFontSize = m_fontSize;
  2441. m_fontSize += Mathf.Max((m_maxFontSize - m_fontSize) / 2, 0.05f);
  2442. m_fontSize = (int)(Mathf.Min(m_fontSize, m_fontSizeMax) * 20 + 0.5f) / 20f;
  2443. //Debug.Log(m_fontSize);
  2444. if (loopCountA > 20) return; // Added to debug
  2445. GenerateTextMesh();
  2446. return;
  2447. }
  2448. #endregion End Auto-sizing Check
  2449. m_isCharacterWrappingEnabled = false;
  2450. #if TMP_PROFILE_PHASES_ON
  2451. Profiler.EndSample();
  2452. #endif
  2453. //Debug.Log("Iteration Count: " + loopCountA + ". Final Point Size: " + m_fontSize); // + " B: " + loopCountB + " C: " + loopCountC + " D: " + loopCountD);
  2454. // *** PHASE II of Text Generation ***
  2455. #if TMP_PROFILE_PHASES_ON
  2456. Profiler.BeginSample("TMP Generate Text - Phase II");
  2457. #endif
  2458. // If there are no visible characters... no need to continue
  2459. if (m_characterCount == 0) // && m_visibleSpriteCount == 0)
  2460. {
  2461. ClearMesh();
  2462. // Event indicating the text has been regenerated.
  2463. TMPro_EventManager.ON_TEXT_CHANGED(this);
  2464. return;
  2465. }
  2466. // *** PHASE II of Text Generation ***
  2467. int last_vert_index = m_materialReferences[0].referenceCount * 4;
  2468. // Partial clear of the vertices array to mark unused vertices as degenerate.
  2469. m_textInfo.meshInfo[0].Clear(false);
  2470. // Handle Text Alignment
  2471. #region Text Vertical Alignment
  2472. #if TMP_PROFILE_ON
  2473. Profiler.BeginSample("Vertical Text Alignment");
  2474. #endif
  2475. Vector3 anchorOffset = Vector3.zero;
  2476. Vector3[] corners = m_RectTransformCorners; // GetTextContainerLocalCorners();
  2477. switch (m_textAlignment)
  2478. {
  2479. // Top Vertically
  2480. case TextAlignmentOptions.Top:
  2481. case TextAlignmentOptions.TopLeft:
  2482. case TextAlignmentOptions.TopRight:
  2483. case TextAlignmentOptions.TopJustified:
  2484. case TextAlignmentOptions.TopFlush:
  2485. case TextAlignmentOptions.TopGeoAligned:
  2486. if (m_overflowMode != TextOverflowModes.Page)
  2487. anchorOffset = corners[1] + new Vector3(0 + margins.x, 0 - m_maxAscender - margins.y, 0);
  2488. else
  2489. anchorOffset = corners[1] + new Vector3(0 + margins.x, 0 - m_textInfo.pageInfo[pageToDisplay].ascender - margins.y, 0);
  2490. break;
  2491. // Middle Vertically
  2492. case TextAlignmentOptions.Left:
  2493. case TextAlignmentOptions.Right:
  2494. case TextAlignmentOptions.Center:
  2495. case TextAlignmentOptions.Justified:
  2496. case TextAlignmentOptions.Flush:
  2497. case TextAlignmentOptions.CenterGeoAligned:
  2498. if (m_overflowMode != TextOverflowModes.Page)
  2499. anchorOffset = (corners[0] + corners[1]) / 2 + new Vector3(0 + margins.x, 0 - (m_maxAscender + margins.y + maxVisibleDescender - margins.w) / 2, 0);
  2500. else
  2501. anchorOffset = (corners[0] + corners[1]) / 2 + new Vector3(0 + margins.x, 0 - (m_textInfo.pageInfo[pageToDisplay].ascender + margins.y + m_textInfo.pageInfo[pageToDisplay].descender - margins.w) / 2, 0);
  2502. break;
  2503. // Bottom Vertically
  2504. case TextAlignmentOptions.Bottom:
  2505. case TextAlignmentOptions.BottomLeft:
  2506. case TextAlignmentOptions.BottomRight:
  2507. case TextAlignmentOptions.BottomJustified:
  2508. case TextAlignmentOptions.BottomFlush:
  2509. case TextAlignmentOptions.BottomGeoAligned:
  2510. if (m_overflowMode != TextOverflowModes.Page)
  2511. anchorOffset = corners[0] + new Vector3(0 + margins.x, 0 - maxVisibleDescender + margins.w, 0);
  2512. else
  2513. anchorOffset = corners[0] + new Vector3(0 + margins.x, 0 - m_textInfo.pageInfo[pageToDisplay].descender + margins.w, 0);
  2514. break;
  2515. // Baseline Vertically
  2516. case TextAlignmentOptions.Baseline:
  2517. case TextAlignmentOptions.BaselineLeft:
  2518. case TextAlignmentOptions.BaselineRight:
  2519. case TextAlignmentOptions.BaselineJustified:
  2520. case TextAlignmentOptions.BaselineFlush:
  2521. case TextAlignmentOptions.BaselineGeoAligned:
  2522. anchorOffset = (corners[0] + corners[1]) / 2 + new Vector3(0 + margins.x, 0, 0);
  2523. break;
  2524. // Midline Vertically
  2525. case TextAlignmentOptions.MidlineLeft:
  2526. case TextAlignmentOptions.Midline:
  2527. case TextAlignmentOptions.MidlineRight:
  2528. case TextAlignmentOptions.MidlineJustified:
  2529. case TextAlignmentOptions.MidlineFlush:
  2530. case TextAlignmentOptions.MidlineGeoAligned:
  2531. anchorOffset = (corners[0] + corners[1]) / 2 + new Vector3(0 + margins.x, 0 - (m_meshExtents.max.y + margins.y + m_meshExtents.min.y - margins.w) / 2, 0);
  2532. break;
  2533. // Capline Vertically
  2534. case TextAlignmentOptions.CaplineLeft:
  2535. case TextAlignmentOptions.Capline:
  2536. case TextAlignmentOptions.CaplineRight:
  2537. case TextAlignmentOptions.CaplineJustified:
  2538. case TextAlignmentOptions.CaplineFlush:
  2539. case TextAlignmentOptions.CaplineGeoAligned:
  2540. anchorOffset = (corners[0] + corners[1]) / 2 + new Vector3(0 + margins.x, 0 - (m_maxCapHeight - margins.y - margins.w) / 2, 0);
  2541. break;
  2542. }
  2543. #if TMP_PROFILE_ON
  2544. Profiler.EndSample();
  2545. #endif
  2546. #endregion
  2547. // Initialization for Second Pass
  2548. Vector3 justificationOffset = Vector3.zero;
  2549. Vector3 offset = Vector3.zero;
  2550. int vert_index_X4 = 0;
  2551. int sprite_index_X4 = 0;
  2552. int wordCount = 0;
  2553. int lineCount = 0;
  2554. int lastLine = 0;
  2555. bool isFirstSeperator = false;
  2556. bool isStartOfWord = false;
  2557. int wordFirstChar = 0;
  2558. int wordLastChar = 0;
  2559. // Second Pass : Line Justification, UV Mapping, Character & Line Visibility & more.
  2560. #region Handle Line Justification & UV Mapping & Character Visibility & More
  2561. // Variables used to handle Canvas Render Modes and SDF Scaling
  2562. bool isCameraAssigned = m_canvas.worldCamera == null ? false : true;
  2563. float lossyScale = m_previousLossyScaleY = this.transform.lossyScale.y;
  2564. RenderMode canvasRenderMode = m_canvas.renderMode;
  2565. float canvasScaleFactor = m_canvas.scaleFactor;
  2566. Color32 underlineColor = Color.white;
  2567. Color32 strikethroughColor = Color.white;
  2568. Color32 highlightColor = new Color32(255, 255, 0, 64);
  2569. float xScale = 0;
  2570. float xScaleMax = 0;
  2571. float underlineStartScale = 0;
  2572. float underlineEndScale = 0;
  2573. float underlineMaxScale = 0;
  2574. float underlineBaseLine = k_LargePositiveFloat;
  2575. int lastPage = 0;
  2576. float strikethroughPointSize = 0;
  2577. float strikethroughScale = 0;
  2578. float strikethroughBaseline = 0;
  2579. TMP_CharacterInfo[] characterInfos = m_textInfo.characterInfo;
  2580. #region Handle Line Justification & UV Mapping & Character Visibility & More
  2581. for (int i = 0; i < m_characterCount; i++)
  2582. {
  2583. TMP_FontAsset currentFontAsset = characterInfos[i].fontAsset;
  2584. char currentCharacter = characterInfos[i].character;
  2585. int currentLine = characterInfos[i].lineNumber;
  2586. TMP_LineInfo lineInfo = m_textInfo.lineInfo[currentLine];
  2587. lineCount = currentLine + 1;
  2588. TextAlignmentOptions lineAlignment = lineInfo.alignment;
  2589. // Process Line Justification
  2590. #region Handle Line Justification
  2591. #if TMP_PROFILE_ON
  2592. Profiler.BeginSample("Horizontal Text Alignment");
  2593. #endif
  2594. //if (!characterInfos[i].isIgnoringAlignment)
  2595. //{
  2596. switch (lineAlignment)
  2597. {
  2598. case TextAlignmentOptions.TopLeft:
  2599. case TextAlignmentOptions.Left:
  2600. case TextAlignmentOptions.BottomLeft:
  2601. case TextAlignmentOptions.BaselineLeft:
  2602. case TextAlignmentOptions.MidlineLeft:
  2603. case TextAlignmentOptions.CaplineLeft:
  2604. if (!m_isRightToLeft)
  2605. justificationOffset = new Vector3(0 + lineInfo.marginLeft, 0, 0);
  2606. else
  2607. justificationOffset = new Vector3(0 - lineInfo.maxAdvance, 0, 0);
  2608. break;
  2609. case TextAlignmentOptions.Top:
  2610. case TextAlignmentOptions.Center:
  2611. case TextAlignmentOptions.Bottom:
  2612. case TextAlignmentOptions.Baseline:
  2613. case TextAlignmentOptions.Midline:
  2614. case TextAlignmentOptions.Capline:
  2615. justificationOffset = new Vector3(lineInfo.marginLeft + lineInfo.width / 2 - lineInfo.maxAdvance / 2, 0, 0);
  2616. break;
  2617. case TextAlignmentOptions.TopGeoAligned:
  2618. case TextAlignmentOptions.CenterGeoAligned:
  2619. case TextAlignmentOptions.BottomGeoAligned:
  2620. case TextAlignmentOptions.BaselineGeoAligned:
  2621. case TextAlignmentOptions.MidlineGeoAligned:
  2622. case TextAlignmentOptions.CaplineGeoAligned:
  2623. justificationOffset = new Vector3(lineInfo.marginLeft + lineInfo.width / 2 - (lineInfo.lineExtents.min.x + lineInfo.lineExtents.max.x) / 2, 0, 0);
  2624. break;
  2625. case TextAlignmentOptions.TopRight:
  2626. case TextAlignmentOptions.Right:
  2627. case TextAlignmentOptions.BottomRight:
  2628. case TextAlignmentOptions.BaselineRight:
  2629. case TextAlignmentOptions.MidlineRight:
  2630. case TextAlignmentOptions.CaplineRight:
  2631. if (!m_isRightToLeft)
  2632. justificationOffset = new Vector3(lineInfo.marginLeft + lineInfo.width - lineInfo.maxAdvance, 0, 0);
  2633. else
  2634. justificationOffset = new Vector3(lineInfo.marginLeft + lineInfo.width, 0, 0);
  2635. break;
  2636. case TextAlignmentOptions.TopJustified:
  2637. case TextAlignmentOptions.Justified:
  2638. case TextAlignmentOptions.BottomJustified:
  2639. case TextAlignmentOptions.BaselineJustified:
  2640. case TextAlignmentOptions.MidlineJustified:
  2641. case TextAlignmentOptions.CaplineJustified:
  2642. case TextAlignmentOptions.TopFlush:
  2643. case TextAlignmentOptions.Flush:
  2644. case TextAlignmentOptions.BottomFlush:
  2645. case TextAlignmentOptions.BaselineFlush:
  2646. case TextAlignmentOptions.MidlineFlush:
  2647. case TextAlignmentOptions.CaplineFlush:
  2648. // Skip Zero Width Characters
  2649. if (currentCharacter == 0xAD || currentCharacter == 0x200B || currentCharacter == 0x2060) break;
  2650. char lastCharOfCurrentLine = characterInfos[lineInfo.lastCharacterIndex].character;
  2651. bool isFlush = ((_HorizontalAlignmentOptions)lineAlignment & _HorizontalAlignmentOptions.Flush) == _HorizontalAlignmentOptions.Flush;
  2652. // In Justified mode, all lines are justified except the last one.
  2653. // In Flush mode, all lines are justified.
  2654. if (char.IsControl(lastCharOfCurrentLine) == false && currentLine < m_lineNumber || isFlush || lineInfo.maxAdvance > lineInfo.width)
  2655. {
  2656. // First character of each line.
  2657. if (currentLine != lastLine || i == 0 || i == m_firstVisibleCharacter)
  2658. {
  2659. if (!m_isRightToLeft)
  2660. justificationOffset = new Vector3(lineInfo.marginLeft, 0, 0);
  2661. else
  2662. justificationOffset = new Vector3(lineInfo.marginLeft + lineInfo.width, 0, 0);
  2663. if (char.IsSeparator(currentCharacter))
  2664. isFirstSeperator = true;
  2665. else
  2666. isFirstSeperator = false;
  2667. }
  2668. else
  2669. {
  2670. float gap = !m_isRightToLeft ? lineInfo.width - lineInfo.maxAdvance : lineInfo.width + lineInfo.maxAdvance;
  2671. int visibleCount = lineInfo.visibleCharacterCount - 1 + lineInfo.controlCharacterCount;
  2672. // Get the number of spaces for each line ignoring the last character if it is not visible (ie. a space or linefeed).
  2673. int spaces = (characterInfos[lineInfo.lastCharacterIndex].isVisible ? lineInfo.spaceCount : lineInfo.spaceCount - 1) - lineInfo.controlCharacterCount;
  2674. if (isFirstSeperator) { spaces -= 1; visibleCount += 1; }
  2675. float ratio = spaces > 0 ? m_wordWrappingRatios : 1;
  2676. if (spaces < 1) spaces = 1;
  2677. if (currentCharacter != 0xA0 && (currentCharacter == 9 || char.IsSeparator((char)currentCharacter)))
  2678. {
  2679. if (!m_isRightToLeft)
  2680. justificationOffset += new Vector3(gap * (1 - ratio) / spaces, 0, 0);
  2681. else
  2682. justificationOffset -= new Vector3(gap * (1 - ratio) / spaces, 0, 0);
  2683. }
  2684. else
  2685. {
  2686. if (!m_isRightToLeft)
  2687. justificationOffset += new Vector3(gap * ratio / visibleCount, 0, 0);
  2688. else
  2689. justificationOffset -= new Vector3(gap * ratio / visibleCount, 0, 0);
  2690. }
  2691. }
  2692. }
  2693. else
  2694. {
  2695. if (!m_isRightToLeft)
  2696. justificationOffset = new Vector3(lineInfo.marginLeft, 0, 0); // Keep last line left justified.
  2697. else
  2698. justificationOffset = new Vector3(lineInfo.marginLeft + lineInfo.width, 0, 0); // Keep last line right justified.
  2699. }
  2700. //Debug.Log("Char [" + (char)charCode + "] Code:" + charCode + " Line # " + currentLine + " Offset:" + justificationOffset + " # Spaces:" + lineInfo.spaceCount + " # Characters:" + lineInfo.characterCount);
  2701. break;
  2702. }
  2703. //}
  2704. #if TMP_PROFILE_ON
  2705. Profiler.EndSample();
  2706. #endif
  2707. #endregion End Text Justification
  2708. offset = anchorOffset + justificationOffset;
  2709. // Handle UV2 mapping options and packing of scale information into UV2.
  2710. #region Handling of UV2 mapping & Scale packing
  2711. bool isCharacterVisible = characterInfos[i].isVisible;
  2712. if (isCharacterVisible)
  2713. {
  2714. TMP_TextElementType elementType = characterInfos[i].elementType;
  2715. switch (elementType)
  2716. {
  2717. // CHARACTERS
  2718. case TMP_TextElementType.Character:
  2719. Extents lineExtents = lineInfo.lineExtents;
  2720. float uvOffset = (m_uvLineOffset * currentLine) % 1; // + m_uvOffset.x;
  2721. // Setup UV2 based on Character Mapping Options Selected
  2722. #region Handle UV Mapping Options
  2723. #if TMP_PROFILE_ON
  2724. Profiler.BeginSample("UV MAPPING");
  2725. #endif
  2726. switch (m_horizontalMapping)
  2727. {
  2728. case TextureMappingOptions.Character:
  2729. characterInfos[i].vertex_BL.uv2.x = 0; //+ m_uvOffset.x;
  2730. characterInfos[i].vertex_TL.uv2.x = 0; //+ m_uvOffset.x;
  2731. characterInfos[i].vertex_TR.uv2.x = 1; //+ m_uvOffset.x;
  2732. characterInfos[i].vertex_BR.uv2.x = 1; //+ m_uvOffset.x;
  2733. break;
  2734. case TextureMappingOptions.Line:
  2735. if (m_textAlignment != TextAlignmentOptions.Justified)
  2736. {
  2737. characterInfos[i].vertex_BL.uv2.x = (characterInfos[i].vertex_BL.position.x - lineExtents.min.x) / (lineExtents.max.x - lineExtents.min.x) + uvOffset;
  2738. characterInfos[i].vertex_TL.uv2.x = (characterInfos[i].vertex_TL.position.x - lineExtents.min.x) / (lineExtents.max.x - lineExtents.min.x) + uvOffset;
  2739. characterInfos[i].vertex_TR.uv2.x = (characterInfos[i].vertex_TR.position.x - lineExtents.min.x) / (lineExtents.max.x - lineExtents.min.x) + uvOffset;
  2740. characterInfos[i].vertex_BR.uv2.x = (characterInfos[i].vertex_BR.position.x - lineExtents.min.x) / (lineExtents.max.x - lineExtents.min.x) + uvOffset;
  2741. break;
  2742. }
  2743. else // Special Case if Justified is used in Line Mode.
  2744. {
  2745. characterInfos[i].vertex_BL.uv2.x = (characterInfos[i].vertex_BL.position.x + justificationOffset.x - m_meshExtents.min.x) / (m_meshExtents.max.x - m_meshExtents.min.x) + uvOffset;
  2746. characterInfos[i].vertex_TL.uv2.x = (characterInfos[i].vertex_TL.position.x + justificationOffset.x - m_meshExtents.min.x) / (m_meshExtents.max.x - m_meshExtents.min.x) + uvOffset;
  2747. characterInfos[i].vertex_TR.uv2.x = (characterInfos[i].vertex_TR.position.x + justificationOffset.x - m_meshExtents.min.x) / (m_meshExtents.max.x - m_meshExtents.min.x) + uvOffset;
  2748. characterInfos[i].vertex_BR.uv2.x = (characterInfos[i].vertex_BR.position.x + justificationOffset.x - m_meshExtents.min.x) / (m_meshExtents.max.x - m_meshExtents.min.x) + uvOffset;
  2749. break;
  2750. }
  2751. case TextureMappingOptions.Paragraph:
  2752. characterInfos[i].vertex_BL.uv2.x = (characterInfos[i].vertex_BL.position.x + justificationOffset.x - m_meshExtents.min.x) / (m_meshExtents.max.x - m_meshExtents.min.x) + uvOffset;
  2753. characterInfos[i].vertex_TL.uv2.x = (characterInfos[i].vertex_TL.position.x + justificationOffset.x - m_meshExtents.min.x) / (m_meshExtents.max.x - m_meshExtents.min.x) + uvOffset;
  2754. characterInfos[i].vertex_TR.uv2.x = (characterInfos[i].vertex_TR.position.x + justificationOffset.x - m_meshExtents.min.x) / (m_meshExtents.max.x - m_meshExtents.min.x) + uvOffset;
  2755. characterInfos[i].vertex_BR.uv2.x = (characterInfos[i].vertex_BR.position.x + justificationOffset.x - m_meshExtents.min.x) / (m_meshExtents.max.x - m_meshExtents.min.x) + uvOffset;
  2756. break;
  2757. case TextureMappingOptions.MatchAspect:
  2758. switch (m_verticalMapping)
  2759. {
  2760. case TextureMappingOptions.Character:
  2761. characterInfos[i].vertex_BL.uv2.y = 0; // + m_uvOffset.y;
  2762. characterInfos[i].vertex_TL.uv2.y = 1; // + m_uvOffset.y;
  2763. characterInfos[i].vertex_TR.uv2.y = 0; // + m_uvOffset.y;
  2764. characterInfos[i].vertex_BR.uv2.y = 1; // + m_uvOffset.y;
  2765. break;
  2766. case TextureMappingOptions.Line:
  2767. characterInfos[i].vertex_BL.uv2.y = (characterInfos[i].vertex_BL.position.y - lineExtents.min.y) / (lineExtents.max.y - lineExtents.min.y) + uvOffset;
  2768. characterInfos[i].vertex_TL.uv2.y = (characterInfos[i].vertex_TL.position.y - lineExtents.min.y) / (lineExtents.max.y - lineExtents.min.y) + uvOffset;
  2769. characterInfos[i].vertex_TR.uv2.y = characterInfos[i].vertex_BL.uv2.y;
  2770. characterInfos[i].vertex_BR.uv2.y = characterInfos[i].vertex_TL.uv2.y;
  2771. break;
  2772. case TextureMappingOptions.Paragraph:
  2773. characterInfos[i].vertex_BL.uv2.y = (characterInfos[i].vertex_BL.position.y - m_meshExtents.min.y) / (m_meshExtents.max.y - m_meshExtents.min.y) + uvOffset;
  2774. characterInfos[i].vertex_TL.uv2.y = (characterInfos[i].vertex_TL.position.y - m_meshExtents.min.y) / (m_meshExtents.max.y - m_meshExtents.min.y) + uvOffset;
  2775. characterInfos[i].vertex_TR.uv2.y = characterInfos[i].vertex_BL.uv2.y;
  2776. characterInfos[i].vertex_BR.uv2.y = characterInfos[i].vertex_TL.uv2.y;
  2777. break;
  2778. case TextureMappingOptions.MatchAspect:
  2779. Debug.Log("ERROR: Cannot Match both Vertical & Horizontal.");
  2780. break;
  2781. }
  2782. //float xDelta = 1 - (_uv2s[vert_index + 0].y * textMeshCharacterInfo[i].AspectRatio); // Left aligned
  2783. float xDelta = (1 - ((characterInfos[i].vertex_BL.uv2.y + characterInfos[i].vertex_TL.uv2.y) * characterInfos[i].aspectRatio)) / 2; // Center of Rectangle
  2784. characterInfos[i].vertex_BL.uv2.x = (characterInfos[i].vertex_BL.uv2.y * characterInfos[i].aspectRatio) + xDelta + uvOffset;
  2785. characterInfos[i].vertex_TL.uv2.x = characterInfos[i].vertex_BL.uv2.x;
  2786. characterInfos[i].vertex_TR.uv2.x = (characterInfos[i].vertex_TL.uv2.y * characterInfos[i].aspectRatio) + xDelta + uvOffset;
  2787. characterInfos[i].vertex_BR.uv2.x = characterInfos[i].vertex_TR.uv2.x;
  2788. break;
  2789. }
  2790. switch (m_verticalMapping)
  2791. {
  2792. case TextureMappingOptions.Character:
  2793. characterInfos[i].vertex_BL.uv2.y = 0; // + m_uvOffset.y;
  2794. characterInfos[i].vertex_TL.uv2.y = 1; // + m_uvOffset.y;
  2795. characterInfos[i].vertex_TR.uv2.y = 1; // + m_uvOffset.y;
  2796. characterInfos[i].vertex_BR.uv2.y = 0; // + m_uvOffset.y;
  2797. break;
  2798. case TextureMappingOptions.Line:
  2799. characterInfos[i].vertex_BL.uv2.y = (characterInfos[i].vertex_BL.position.y - lineInfo.descender) / (lineInfo.ascender - lineInfo.descender); // + m_uvOffset.y;
  2800. characterInfos[i].vertex_TL.uv2.y = (characterInfos[i].vertex_TL.position.y - lineInfo.descender) / (lineInfo.ascender - lineInfo.descender); // + m_uvOffset.y;
  2801. characterInfos[i].vertex_TR.uv2.y = characterInfos[i].vertex_TL.uv2.y;
  2802. characterInfos[i].vertex_BR.uv2.y = characterInfos[i].vertex_BL.uv2.y;
  2803. break;
  2804. case TextureMappingOptions.Paragraph:
  2805. characterInfos[i].vertex_BL.uv2.y = (characterInfos[i].vertex_BL.position.y - m_meshExtents.min.y) / (m_meshExtents.max.y - m_meshExtents.min.y); // + m_uvOffset.y;
  2806. characterInfos[i].vertex_TL.uv2.y = (characterInfos[i].vertex_TL.position.y - m_meshExtents.min.y) / (m_meshExtents.max.y - m_meshExtents.min.y); // + m_uvOffset.y;
  2807. characterInfos[i].vertex_TR.uv2.y = characterInfos[i].vertex_TL.uv2.y;
  2808. characterInfos[i].vertex_BR.uv2.y = characterInfos[i].vertex_BL.uv2.y;
  2809. break;
  2810. case TextureMappingOptions.MatchAspect:
  2811. float yDelta = (1 - ((characterInfos[i].vertex_BL.uv2.x + characterInfos[i].vertex_TR.uv2.x) / characterInfos[i].aspectRatio)) / 2; // Center of Rectangle
  2812. characterInfos[i].vertex_BL.uv2.y = yDelta + (characterInfos[i].vertex_BL.uv2.x / characterInfos[i].aspectRatio); // + m_uvOffset.y;
  2813. characterInfos[i].vertex_TL.uv2.y = yDelta + (characterInfos[i].vertex_TR.uv2.x / characterInfos[i].aspectRatio); // + m_uvOffset.y;
  2814. characterInfos[i].vertex_BR.uv2.y = characterInfos[i].vertex_BL.uv2.y;
  2815. characterInfos[i].vertex_TR.uv2.y = characterInfos[i].vertex_TL.uv2.y;
  2816. break;
  2817. }
  2818. #if TMP_PROFILE_ON
  2819. Profiler.EndSample();
  2820. #endif
  2821. #endregion End UV Mapping Options
  2822. // Pack UV's so that we can pass Xscale needed for Shader to maintain 1:1 ratio.
  2823. #region Pack Scale into UV2
  2824. #if TMP_PROFILE_ON
  2825. Profiler.BeginSample("Pack UV");
  2826. #endif
  2827. xScale = characterInfos[i].scale * (1 - m_charWidthAdjDelta);
  2828. if (!characterInfos[i].isUsingAlternateTypeface && (characterInfos[i].style & FontStyles.Bold) == FontStyles.Bold) xScale *= -1;
  2829. switch (canvasRenderMode)
  2830. {
  2831. case RenderMode.ScreenSpaceOverlay:
  2832. xScale *= Mathf.Abs(lossyScale) / canvasScaleFactor;
  2833. break;
  2834. case RenderMode.ScreenSpaceCamera:
  2835. xScale *= isCameraAssigned ? Mathf.Abs(lossyScale) : 1;
  2836. break;
  2837. case RenderMode.WorldSpace:
  2838. xScale *= Mathf.Abs(lossyScale);
  2839. break;
  2840. }
  2841. // isBold is encoded in the X value and SDF Scale in Y.
  2842. //Vector2 vertexData = new Vector2((characterInfos[i].style & FontStyles.Bold) == FontStyles.Bold ? 1 : 0, xScale);
  2843. //characterInfos[i].vertex_BL.uv2 = vertexData;
  2844. //characterInfos[i].vertex_TL.uv2 = vertexData;
  2845. //characterInfos[i].vertex_TR.uv2 = vertexData;
  2846. //characterInfos[i].vertex_BR.uv2 = vertexData;
  2847. float x0 = characterInfos[i].vertex_BL.uv2.x;
  2848. float y0 = characterInfos[i].vertex_BL.uv2.y;
  2849. float x1 = characterInfos[i].vertex_TR.uv2.x;
  2850. float y1 = characterInfos[i].vertex_TR.uv2.y;
  2851. float dx = (int)x0;
  2852. float dy = (int)y0;
  2853. x0 = x0 - dx;
  2854. x1 = x1 - dx;
  2855. y0 = y0 - dy;
  2856. y1 = y1 - dy;
  2857. // Optimization to avoid having a vector2 returned from the Pack UV function.
  2858. characterInfos[i].vertex_BL.uv2.x = PackUV(x0, y0); characterInfos[i].vertex_BL.uv2.y = xScale;
  2859. characterInfos[i].vertex_TL.uv2.x = PackUV(x0, y1); characterInfos[i].vertex_TL.uv2.y = xScale;
  2860. characterInfos[i].vertex_TR.uv2.x = PackUV(x1, y1); characterInfos[i].vertex_TR.uv2.y = xScale;
  2861. characterInfos[i].vertex_BR.uv2.x = PackUV(x1, y0); characterInfos[i].vertex_BR.uv2.y = xScale;
  2862. #if TMP_PROFILE_ON
  2863. Profiler.EndSample();
  2864. #endif
  2865. #endregion
  2866. break;
  2867. // SPRITES
  2868. case TMP_TextElementType.Sprite:
  2869. // Nothing right now
  2870. break;
  2871. }
  2872. // Handle maxVisibleCharacters, maxVisibleLines and Overflow Page Mode.
  2873. #region Handle maxVisibleCharacters / maxVisibleLines / Page Mode
  2874. #if TMP_PROFILE_ON
  2875. Profiler.BeginSample("Process MaxVisible Characters & Lines");
  2876. #endif
  2877. if (i < m_maxVisibleCharacters && wordCount < m_maxVisibleWords && currentLine < m_maxVisibleLines && m_overflowMode != TextOverflowModes.Page)
  2878. {
  2879. characterInfos[i].vertex_BL.position += offset;
  2880. characterInfos[i].vertex_TL.position += offset;
  2881. characterInfos[i].vertex_TR.position += offset;
  2882. characterInfos[i].vertex_BR.position += offset;
  2883. }
  2884. else if (i < m_maxVisibleCharacters && wordCount < m_maxVisibleWords && currentLine < m_maxVisibleLines && m_overflowMode == TextOverflowModes.Page && characterInfos[i].pageNumber == pageToDisplay)
  2885. {
  2886. characterInfos[i].vertex_BL.position += offset;
  2887. characterInfos[i].vertex_TL.position += offset;
  2888. characterInfos[i].vertex_TR.position += offset;
  2889. characterInfos[i].vertex_BR.position += offset;
  2890. }
  2891. else
  2892. {
  2893. characterInfos[i].vertex_BL.position = Vector3.zero;
  2894. characterInfos[i].vertex_TL.position = Vector3.zero;
  2895. characterInfos[i].vertex_TR.position = Vector3.zero;
  2896. characterInfos[i].vertex_BR.position = Vector3.zero;
  2897. characterInfos[i].isVisible = false;
  2898. }
  2899. #if TMP_PROFILE_ON
  2900. Profiler.EndSample();
  2901. #endif
  2902. #endregion
  2903. // Fill Vertex Buffers for the various types of element
  2904. if (elementType == TMP_TextElementType.Character)
  2905. {
  2906. FillCharacterVertexBuffers(i, vert_index_X4);
  2907. }
  2908. else if (elementType == TMP_TextElementType.Sprite)
  2909. {
  2910. FillSpriteVertexBuffers(i, sprite_index_X4);
  2911. }
  2912. }
  2913. #endregion
  2914. // Apply Alignment and Justification Offset
  2915. m_textInfo.characterInfo[i].bottomLeft += offset;
  2916. m_textInfo.characterInfo[i].topLeft += offset;
  2917. m_textInfo.characterInfo[i].topRight += offset;
  2918. m_textInfo.characterInfo[i].bottomRight += offset;
  2919. m_textInfo.characterInfo[i].origin += offset.x;
  2920. m_textInfo.characterInfo[i].xAdvance += offset.x;
  2921. m_textInfo.characterInfo[i].ascender += offset.y;
  2922. m_textInfo.characterInfo[i].descender += offset.y;
  2923. m_textInfo.characterInfo[i].baseLine += offset.y;
  2924. // Update MeshExtents
  2925. if (isCharacterVisible)
  2926. {
  2927. //m_meshExtents.min = new Vector2(Mathf.Min(m_meshExtents.min.x, m_textInfo.characterInfo[i].bottomLeft.x), Mathf.Min(m_meshExtents.min.y, m_textInfo.characterInfo[i].bottomLeft.y));
  2928. //m_meshExtents.max = new Vector2(Mathf.Max(m_meshExtents.max.x, m_textInfo.characterInfo[i].topRight.x), Mathf.Max(m_meshExtents.max.y, m_textInfo.characterInfo[i].topLeft.y));
  2929. }
  2930. // Need to recompute lineExtent to account for the offset from justification.
  2931. #region Adjust lineExtents resulting from alignment offset
  2932. #if TMP_PROFILE_ON
  2933. Profiler.BeginSample("Adjust LineExtents");
  2934. #endif
  2935. if (currentLine != lastLine || i == m_characterCount - 1)
  2936. {
  2937. // Update the previous line's extents
  2938. if (currentLine != lastLine)
  2939. {
  2940. m_textInfo.lineInfo[lastLine].baseline += offset.y;
  2941. m_textInfo.lineInfo[lastLine].ascender += offset.y;
  2942. m_textInfo.lineInfo[lastLine].descender += offset.y;
  2943. m_textInfo.lineInfo[lastLine].lineExtents.min = new Vector2(m_textInfo.characterInfo[m_textInfo.lineInfo[lastLine].firstCharacterIndex].bottomLeft.x, m_textInfo.lineInfo[lastLine].descender);
  2944. m_textInfo.lineInfo[lastLine].lineExtents.max = new Vector2(m_textInfo.characterInfo[m_textInfo.lineInfo[lastLine].lastVisibleCharacterIndex].topRight.x, m_textInfo.lineInfo[lastLine].ascender);
  2945. }
  2946. // Update the current line's extents
  2947. if (i == m_characterCount - 1)
  2948. {
  2949. m_textInfo.lineInfo[currentLine].baseline += offset.y;
  2950. m_textInfo.lineInfo[currentLine].ascender += offset.y;
  2951. m_textInfo.lineInfo[currentLine].descender += offset.y;
  2952. m_textInfo.lineInfo[currentLine].lineExtents.min = new Vector2(m_textInfo.characterInfo[m_textInfo.lineInfo[currentLine].firstCharacterIndex].bottomLeft.x, m_textInfo.lineInfo[currentLine].descender);
  2953. m_textInfo.lineInfo[currentLine].lineExtents.max = new Vector2(m_textInfo.characterInfo[m_textInfo.lineInfo[currentLine].lastVisibleCharacterIndex].topRight.x, m_textInfo.lineInfo[currentLine].ascender);
  2954. }
  2955. }
  2956. #if TMP_PROFILE_ON
  2957. Profiler.EndSample();
  2958. #endif
  2959. #endregion
  2960. // Track Word Count per line and for the object
  2961. #region Track Word Count
  2962. #if TMP_PROFILE_ON
  2963. Profiler.BeginSample("Track Word Count");
  2964. #endif
  2965. if (char.IsLetterOrDigit(currentCharacter) || currentCharacter == 0x2D || currentCharacter == 0xAD || currentCharacter == 0x2010 || currentCharacter == 0x2011)
  2966. {
  2967. if (isStartOfWord == false)
  2968. {
  2969. isStartOfWord = true;
  2970. wordFirstChar = i;
  2971. }
  2972. // If last character is a word
  2973. if (isStartOfWord && i == m_characterCount - 1)
  2974. {
  2975. int size = m_textInfo.wordInfo.Length;
  2976. int index = m_textInfo.wordCount;
  2977. if (m_textInfo.wordCount + 1 > size)
  2978. TMP_TextInfo.Resize(ref m_textInfo.wordInfo, size + 1);
  2979. wordLastChar = i;
  2980. m_textInfo.wordInfo[index].firstCharacterIndex = wordFirstChar;
  2981. m_textInfo.wordInfo[index].lastCharacterIndex = wordLastChar;
  2982. m_textInfo.wordInfo[index].characterCount = wordLastChar - wordFirstChar + 1;
  2983. m_textInfo.wordInfo[index].textComponent = this;
  2984. wordCount += 1;
  2985. m_textInfo.wordCount += 1;
  2986. m_textInfo.lineInfo[currentLine].wordCount += 1;
  2987. }
  2988. }
  2989. else if (isStartOfWord || i == 0 && (!char.IsPunctuation(currentCharacter) || char.IsWhiteSpace(currentCharacter) || currentCharacter == 0x200B || i == m_characterCount - 1))
  2990. {
  2991. if (i > 0 && i < characterInfos.Length - 1 && i < m_characterCount && (currentCharacter == 39 || currentCharacter == 8217) && char.IsLetterOrDigit(characterInfos[i - 1].character) && char.IsLetterOrDigit(characterInfos[i + 1].character))
  2992. {
  2993. }
  2994. else
  2995. {
  2996. wordLastChar = i == m_characterCount - 1 && char.IsLetterOrDigit(currentCharacter) ? i : i - 1;
  2997. isStartOfWord = false;
  2998. int size = m_textInfo.wordInfo.Length;
  2999. int index = m_textInfo.wordCount;
  3000. if (m_textInfo.wordCount + 1 > size)
  3001. TMP_TextInfo.Resize(ref m_textInfo.wordInfo, size + 1);
  3002. m_textInfo.wordInfo[index].firstCharacterIndex = wordFirstChar;
  3003. m_textInfo.wordInfo[index].lastCharacterIndex = wordLastChar;
  3004. m_textInfo.wordInfo[index].characterCount = wordLastChar - wordFirstChar + 1;
  3005. m_textInfo.wordInfo[index].textComponent = this;
  3006. wordCount += 1;
  3007. m_textInfo.wordCount += 1;
  3008. m_textInfo.lineInfo[currentLine].wordCount += 1;
  3009. }
  3010. }
  3011. #if TMP_PROFILE_ON
  3012. Profiler.EndSample();
  3013. #endif
  3014. #endregion
  3015. // Setup & Handle Underline
  3016. #region Underline
  3017. #if TMP_PROFILE_ON
  3018. Profiler.BeginSample("Process Underline & Strikethrough");
  3019. #endif
  3020. // NOTE: Need to figure out how underline will be handled with multiple fonts and which font will be used for the underline.
  3021. bool isUnderline = (m_textInfo.characterInfo[i].style & FontStyles.Underline) == FontStyles.Underline;
  3022. if (isUnderline)
  3023. {
  3024. bool isUnderlineVisible = true;
  3025. int currentPage = m_textInfo.characterInfo[i].pageNumber;
  3026. if (i > m_maxVisibleCharacters || currentLine > m_maxVisibleLines || (m_overflowMode == TextOverflowModes.Page && currentPage + 1 != m_pageToDisplay))
  3027. isUnderlineVisible = false;
  3028. // We only use the scale of visible characters.
  3029. if (!char.IsWhiteSpace(currentCharacter) && currentCharacter != 0x200B)
  3030. {
  3031. underlineMaxScale = Mathf.Max(underlineMaxScale, m_textInfo.characterInfo[i].scale);
  3032. xScaleMax = Mathf.Max(xScaleMax, Mathf.Abs(xScale));
  3033. underlineBaseLine = Mathf.Min(currentPage == lastPage ? underlineBaseLine : k_LargePositiveFloat, m_textInfo.characterInfo[i].baseLine + font.faceInfo.underlineOffset * underlineMaxScale);
  3034. lastPage = currentPage; // Need to track pages to ensure we reset baseline for the new pages.
  3035. }
  3036. if (beginUnderline == false && isUnderlineVisible == true && i <= lineInfo.lastVisibleCharacterIndex && currentCharacter != 10 && currentCharacter != 13)
  3037. {
  3038. if (i == lineInfo.lastVisibleCharacterIndex && char.IsSeparator(currentCharacter))
  3039. { }
  3040. else
  3041. {
  3042. beginUnderline = true;
  3043. underlineStartScale = m_textInfo.characterInfo[i].scale;
  3044. if (underlineMaxScale == 0)
  3045. {
  3046. underlineMaxScale = underlineStartScale;
  3047. xScaleMax = xScale;
  3048. }
  3049. underline_start = new Vector3(m_textInfo.characterInfo[i].bottomLeft.x, underlineBaseLine, 0);
  3050. underlineColor = m_textInfo.characterInfo[i].underlineColor;
  3051. }
  3052. }
  3053. // End Underline if text only contains one character.
  3054. if (beginUnderline && m_characterCount == 1)
  3055. {
  3056. beginUnderline = false;
  3057. underline_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, underlineBaseLine, 0);
  3058. underlineEndScale = m_textInfo.characterInfo[i].scale;
  3059. DrawUnderlineMesh(underline_start, underline_end, ref last_vert_index, underlineStartScale, underlineEndScale, underlineMaxScale, xScaleMax, underlineColor);
  3060. underlineMaxScale = 0;
  3061. xScaleMax = 0;
  3062. underlineBaseLine = k_LargePositiveFloat;
  3063. }
  3064. else if (beginUnderline && (i == lineInfo.lastCharacterIndex || i >= lineInfo.lastVisibleCharacterIndex))
  3065. {
  3066. // Terminate underline at previous visible character if space or carriage return.
  3067. if (char.IsWhiteSpace(currentCharacter) || currentCharacter == 0x200B)
  3068. {
  3069. int lastVisibleCharacterIndex = lineInfo.lastVisibleCharacterIndex;
  3070. underline_end = new Vector3(m_textInfo.characterInfo[lastVisibleCharacterIndex].topRight.x, underlineBaseLine, 0);
  3071. underlineEndScale = m_textInfo.characterInfo[lastVisibleCharacterIndex].scale;
  3072. }
  3073. else
  3074. { // End underline if last character of the line.
  3075. underline_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, underlineBaseLine, 0);
  3076. underlineEndScale = m_textInfo.characterInfo[i].scale;
  3077. }
  3078. beginUnderline = false;
  3079. DrawUnderlineMesh(underline_start, underline_end, ref last_vert_index, underlineStartScale, underlineEndScale, underlineMaxScale, xScaleMax, underlineColor);
  3080. underlineMaxScale = 0;
  3081. xScaleMax = 0;
  3082. underlineBaseLine = k_LargePositiveFloat;
  3083. }
  3084. else if (beginUnderline && !isUnderlineVisible)
  3085. {
  3086. beginUnderline = false;
  3087. underline_end = new Vector3(m_textInfo.characterInfo[i - 1].topRight.x, underlineBaseLine, 0);
  3088. underlineEndScale = m_textInfo.characterInfo[i - 1].scale;
  3089. DrawUnderlineMesh(underline_start, underline_end, ref last_vert_index, underlineStartScale, underlineEndScale, underlineMaxScale, xScaleMax, underlineColor);
  3090. underlineMaxScale = 0;
  3091. xScaleMax = 0;
  3092. underlineBaseLine = k_LargePositiveFloat;
  3093. }
  3094. else if (beginUnderline && i < m_characterCount - 1 && !underlineColor.Compare(m_textInfo.characterInfo[i + 1].underlineColor))
  3095. {
  3096. // End underline if underline color has changed.
  3097. beginUnderline = false;
  3098. underline_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, underlineBaseLine, 0);
  3099. underlineEndScale = m_textInfo.characterInfo[i].scale;
  3100. DrawUnderlineMesh(underline_start, underline_end, ref last_vert_index, underlineStartScale, underlineEndScale, underlineMaxScale, xScaleMax, underlineColor);
  3101. underlineMaxScale = 0;
  3102. xScaleMax = 0;
  3103. underlineBaseLine = k_LargePositiveFloat;
  3104. }
  3105. }
  3106. else
  3107. {
  3108. // End Underline
  3109. if (beginUnderline == true)
  3110. {
  3111. beginUnderline = false;
  3112. underline_end = new Vector3(m_textInfo.characterInfo[i - 1].topRight.x, underlineBaseLine, 0);
  3113. underlineEndScale = m_textInfo.characterInfo[i - 1].scale;
  3114. DrawUnderlineMesh(underline_start, underline_end, ref last_vert_index, underlineStartScale, underlineEndScale, underlineMaxScale, xScaleMax, underlineColor);
  3115. underlineMaxScale = 0;
  3116. xScaleMax = 0;
  3117. underlineBaseLine = k_LargePositiveFloat;
  3118. }
  3119. }
  3120. #endregion
  3121. // Setup & Handle Strikethrough
  3122. #region Strikethrough
  3123. // NOTE: Need to figure out how underline will be handled with multiple fonts and which font will be used for the underline.
  3124. bool isStrikethrough = (m_textInfo.characterInfo[i].style & FontStyles.Strikethrough) == FontStyles.Strikethrough;
  3125. float strikethroughOffset = currentFontAsset.faceInfo.strikethroughOffset;
  3126. if (isStrikethrough)
  3127. {
  3128. bool isStrikeThroughVisible = true;
  3129. if (i > m_maxVisibleCharacters || currentLine > m_maxVisibleLines || (m_overflowMode == TextOverflowModes.Page && m_textInfo.characterInfo[i].pageNumber + 1 != m_pageToDisplay))
  3130. isStrikeThroughVisible = false;
  3131. if (beginStrikethrough == false && isStrikeThroughVisible && i <= lineInfo.lastVisibleCharacterIndex && currentCharacter != 10 && currentCharacter != 13)
  3132. {
  3133. if (i == lineInfo.lastVisibleCharacterIndex && char.IsSeparator(currentCharacter))
  3134. { }
  3135. else
  3136. {
  3137. beginStrikethrough = true;
  3138. strikethroughPointSize = m_textInfo.characterInfo[i].pointSize;
  3139. strikethroughScale = m_textInfo.characterInfo[i].scale;
  3140. strikethrough_start = new Vector3(m_textInfo.characterInfo[i].bottomLeft.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
  3141. strikethroughColor = m_textInfo.characterInfo[i].strikethroughColor;
  3142. strikethroughBaseline = m_textInfo.characterInfo[i].baseLine;
  3143. //Debug.Log("Char [" + currentCharacter + "] Start Strikethrough POS: " + strikethrough_start);
  3144. }
  3145. }
  3146. // End Strikethrough if text only contains one character.
  3147. if (beginStrikethrough && m_characterCount == 1)
  3148. {
  3149. beginStrikethrough = false;
  3150. strikethrough_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
  3151. DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);
  3152. }
  3153. else if (beginStrikethrough && i == lineInfo.lastCharacterIndex)
  3154. {
  3155. // Terminate Strikethrough at previous visible character if space or carriage return.
  3156. if (char.IsWhiteSpace(currentCharacter) || currentCharacter == 0x200B)
  3157. {
  3158. int lastVisibleCharacterIndex = lineInfo.lastVisibleCharacterIndex;
  3159. strikethrough_end = new Vector3(m_textInfo.characterInfo[lastVisibleCharacterIndex].topRight.x, m_textInfo.characterInfo[lastVisibleCharacterIndex].baseLine + strikethroughOffset * strikethroughScale, 0);
  3160. }
  3161. else
  3162. {
  3163. // Terminate Strikethrough at last character of line.
  3164. strikethrough_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
  3165. }
  3166. beginStrikethrough = false;
  3167. DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);
  3168. }
  3169. else if (beginStrikethrough && i < m_characterCount && (m_textInfo.characterInfo[i + 1].pointSize != strikethroughPointSize || !TMP_Math.Approximately(m_textInfo.characterInfo[i + 1].baseLine + offset.y, strikethroughBaseline)))
  3170. {
  3171. // Terminate Strikethrough if scale changes.
  3172. beginStrikethrough = false;
  3173. int lastVisibleCharacterIndex = lineInfo.lastVisibleCharacterIndex;
  3174. if (i > lastVisibleCharacterIndex)
  3175. strikethrough_end = new Vector3(m_textInfo.characterInfo[lastVisibleCharacterIndex].topRight.x, m_textInfo.characterInfo[lastVisibleCharacterIndex].baseLine + strikethroughOffset * strikethroughScale, 0);
  3176. else
  3177. strikethrough_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
  3178. DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);
  3179. //Debug.Log("Char [" + currentCharacter + "] at Index: " + i + " End Strikethrough POS: " + strikethrough_end + " Baseline: " + m_textInfo.characterInfo[i].baseLine.ToString("f3"));
  3180. }
  3181. else if (beginStrikethrough && i < m_characterCount && currentFontAsset.GetInstanceID() != characterInfos[i + 1].fontAsset.GetInstanceID())
  3182. {
  3183. // Terminate Strikethrough if font asset changes.
  3184. beginStrikethrough = false;
  3185. strikethrough_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
  3186. DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);
  3187. }
  3188. else if (beginStrikethrough && !isStrikeThroughVisible)
  3189. {
  3190. // Terminate Strikethrough if character is not visible.
  3191. beginStrikethrough = false;
  3192. strikethrough_end = new Vector3(m_textInfo.characterInfo[i - 1].topRight.x, m_textInfo.characterInfo[i - 1].baseLine + strikethroughOffset * strikethroughScale, 0);
  3193. DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);
  3194. }
  3195. }
  3196. else
  3197. {
  3198. // End Strikethrough
  3199. if (beginStrikethrough == true)
  3200. {
  3201. beginStrikethrough = false;
  3202. strikethrough_end = new Vector3(m_textInfo.characterInfo[i - 1].topRight.x, m_textInfo.characterInfo[i - 1].baseLine + strikethroughOffset * strikethroughScale, 0);
  3203. DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);
  3204. }
  3205. }
  3206. #endregion
  3207. // HANDLE TEXT HIGHLIGHTING
  3208. #region Text Highlighting
  3209. bool isHighlight = (m_textInfo.characterInfo[i].style & FontStyles.Highlight) == FontStyles.Highlight;
  3210. if (isHighlight)
  3211. {
  3212. bool isHighlightVisible = true;
  3213. int currentPage = m_textInfo.characterInfo[i].pageNumber;
  3214. if (i > m_maxVisibleCharacters || currentLine > m_maxVisibleLines || (m_overflowMode == TextOverflowModes.Page && currentPage + 1 != m_pageToDisplay))
  3215. isHighlightVisible = false;
  3216. if (beginHighlight == false && isHighlightVisible == true && i <= lineInfo.lastVisibleCharacterIndex && currentCharacter != 10 && currentCharacter != 13)
  3217. {
  3218. if (i == lineInfo.lastVisibleCharacterIndex && char.IsSeparator(currentCharacter))
  3219. { }
  3220. else
  3221. {
  3222. beginHighlight = true;
  3223. highlight_start = k_LargePositiveVector2;
  3224. highlight_end = k_LargeNegativeVector2;
  3225. highlightColor = m_textInfo.characterInfo[i].highlightColor;
  3226. }
  3227. }
  3228. if (beginHighlight)
  3229. {
  3230. Color32 currentHighlightColor = m_textInfo.characterInfo[i].highlightColor;
  3231. bool isColorTransition = false;
  3232. // Handle Highlight color changes
  3233. if (!highlightColor.Compare(currentHighlightColor))
  3234. {
  3235. // End drawing at the start of new highlight color to prevent a gap between highlight sections.
  3236. highlight_end.x = (highlight_end.x + m_textInfo.characterInfo[i].bottomLeft.x) / 2;
  3237. highlight_start.y = Mathf.Min(highlight_start.y, m_textInfo.characterInfo[i].descender);
  3238. highlight_end.y = Mathf.Max(highlight_end.y, m_textInfo.characterInfo[i].ascender);
  3239. DrawTextHighlight(highlight_start, highlight_end, ref last_vert_index, highlightColor);
  3240. beginHighlight = true;
  3241. highlight_start = highlight_end;
  3242. highlight_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].descender, 0);
  3243. highlightColor = m_textInfo.characterInfo[i].highlightColor;
  3244. isColorTransition = true;
  3245. }
  3246. if (!isColorTransition)
  3247. {
  3248. // Use the Min / Max Extents of the Highlight area to handle different character sizes and fonts.
  3249. highlight_start.x = Mathf.Min(highlight_start.x, m_textInfo.characterInfo[i].bottomLeft.x);
  3250. highlight_start.y = Mathf.Min(highlight_start.y, m_textInfo.characterInfo[i].descender);
  3251. highlight_end.x = Mathf.Max(highlight_end.x, m_textInfo.characterInfo[i].topRight.x);
  3252. highlight_end.y = Mathf.Max(highlight_end.y, m_textInfo.characterInfo[i].ascender);
  3253. }
  3254. }
  3255. // End Highlight if text only contains one character.
  3256. if (beginHighlight && m_characterCount == 1)
  3257. {
  3258. beginHighlight = false;
  3259. DrawTextHighlight(highlight_start, highlight_end, ref last_vert_index, highlightColor);
  3260. }
  3261. else if (beginHighlight && (i == lineInfo.lastCharacterIndex || i >= lineInfo.lastVisibleCharacterIndex))
  3262. {
  3263. beginHighlight = false;
  3264. DrawTextHighlight(highlight_start, highlight_end, ref last_vert_index, highlightColor);
  3265. }
  3266. else if (beginHighlight && !isHighlightVisible)
  3267. {
  3268. beginHighlight = false;
  3269. DrawTextHighlight(highlight_start, highlight_end, ref last_vert_index, highlightColor);
  3270. }
  3271. }
  3272. else
  3273. {
  3274. // End Highlight
  3275. if (beginHighlight == true)
  3276. {
  3277. beginHighlight = false;
  3278. DrawTextHighlight(highlight_start, highlight_end, ref last_vert_index, highlightColor);
  3279. }
  3280. }
  3281. #endregion
  3282. #if TMP_PROFILE_ON
  3283. Profiler.EndSample();
  3284. #endif
  3285. #endregion
  3286. lastLine = currentLine;
  3287. }
  3288. #endregion
  3289. // METRICS ABOUT THE TEXT OBJECT
  3290. m_textInfo.characterCount = m_characterCount;
  3291. m_textInfo.spriteCount = m_spriteCount;
  3292. m_textInfo.lineCount = lineCount;
  3293. m_textInfo.wordCount = wordCount != 0 && m_characterCount > 0 ? wordCount : 1;
  3294. m_textInfo.pageCount = m_pageNumber + 1;
  3295. #if TMP_PROFILE_PHASES_ON
  3296. Profiler.EndSample();
  3297. #endif
  3298. // *** UPLOAD MESH DATA ***
  3299. #if TMP_PROFILE_PHASES_ON
  3300. Profiler.BeginSample("TMP Generate Text - Phase III");
  3301. #endif
  3302. if (m_renderMode == TextRenderFlags.Render && IsActive())
  3303. {
  3304. // Clear unused vertices
  3305. //m_textInfo.meshInfo[0].ClearUnusedVertices();
  3306. // Must ensure the Canvas support the additon vertex attributes used by TMP.
  3307. if (m_canvas.additionalShaderChannels != (AdditionalCanvasShaderChannels)25)
  3308. m_canvas.additionalShaderChannels |= (AdditionalCanvasShaderChannels)25;
  3309. // Sort the geometry of the text object if needed.
  3310. if (m_geometrySortingOrder != VertexSortingOrder.Normal)
  3311. m_textInfo.meshInfo[0].SortGeometry(VertexSortingOrder.Reverse);
  3312. // Upload Mesh Data
  3313. m_mesh.MarkDynamic();
  3314. m_mesh.vertices = m_textInfo.meshInfo[0].vertices;
  3315. m_mesh.uv = m_textInfo.meshInfo[0].uvs0;
  3316. m_mesh.uv2 = m_textInfo.meshInfo[0].uvs2;
  3317. //m_mesh.uv4 = m_textInfo.meshInfo[0].uvs4;
  3318. m_mesh.colors32 = m_textInfo.meshInfo[0].colors32;
  3319. // Compute Bounds for the mesh. Manual computation is more efficient then using Mesh.recalcualteBounds.
  3320. m_mesh.RecalculateBounds();
  3321. //m_mesh.bounds = new Bounds(new Vector3((m_meshExtents.max.x + m_meshExtents.min.x) / 2, (m_meshExtents.max.y + m_meshExtents.min.y) / 2, 0) + offset, new Vector3(m_meshExtents.max.x - m_meshExtents.min.x, m_meshExtents.max.y - m_meshExtents.min.y, 0));
  3322. m_canvasRenderer.SetMesh(m_mesh);
  3323. // Cache CanvasRenderer color of the parent text object.
  3324. Color parentBaseColor = m_canvasRenderer.GetColor();
  3325. for (int i = 1; i < m_textInfo.materialCount; i++)
  3326. {
  3327. // Clear unused vertices
  3328. m_textInfo.meshInfo[i].ClearUnusedVertices();
  3329. if (m_subTextObjects[i] == null) continue;
  3330. // Sort the geometry of the sub-text objects if needed.
  3331. if (m_geometrySortingOrder != VertexSortingOrder.Normal)
  3332. m_textInfo.meshInfo[i].SortGeometry(VertexSortingOrder.Reverse);
  3333. //m_subTextObjects[i].mesh.MarkDynamic();
  3334. m_subTextObjects[i].mesh.vertices = m_textInfo.meshInfo[i].vertices;
  3335. m_subTextObjects[i].mesh.uv = m_textInfo.meshInfo[i].uvs0;
  3336. m_subTextObjects[i].mesh.uv2 = m_textInfo.meshInfo[i].uvs2;
  3337. //m_subTextObjects[i].mesh.uv4 = m_textInfo.meshInfo[i].uvs4;
  3338. m_subTextObjects[i].mesh.colors32 = m_textInfo.meshInfo[i].colors32;
  3339. m_subTextObjects[i].mesh.RecalculateBounds();
  3340. m_subTextObjects[i].canvasRenderer.SetMesh(m_subTextObjects[i].mesh);
  3341. // Set CanvasRenderer color to match the parent text object.
  3342. m_subTextObjects[i].canvasRenderer.SetColor(parentBaseColor);
  3343. }
  3344. }
  3345. // Event indicating the text has been regenerated.
  3346. TMPro_EventManager.ON_TEXT_CHANGED(this);
  3347. //SendOnTextChanged();
  3348. #if TMP_PROFILE_PHASES_ON
  3349. Profiler.EndSample();
  3350. #endif
  3351. //Debug.Log("Done Rendering Text.");
  3352. }
  3353. /// <summary>
  3354. /// Method to return the local corners of the Text Container or RectTransform.
  3355. /// </summary>
  3356. /// <returns></returns>
  3357. protected override Vector3[] GetTextContainerLocalCorners()
  3358. {
  3359. if (m_rectTransform == null) m_rectTransform = this.rectTransform;
  3360. m_rectTransform.GetLocalCorners(m_RectTransformCorners);
  3361. return m_RectTransformCorners;
  3362. }
  3363. /// <summary>
  3364. /// Method to Enable or Disable child SubMesh objects.
  3365. /// </summary>
  3366. /// <param name="state"></param>
  3367. protected override void SetActiveSubMeshes(bool state)
  3368. {
  3369. for (int i = 1; i < m_subTextObjects.Length && m_subTextObjects[i] != null; i++)
  3370. {
  3371. if (m_subTextObjects[i].enabled != state)
  3372. m_subTextObjects[i].enabled = state;
  3373. }
  3374. }
  3375. /// <summary>
  3376. /// Method returning the compound bounds of the text object and child sub objects.
  3377. /// </summary>
  3378. /// <returns></returns>
  3379. protected override Bounds GetCompoundBounds()
  3380. {
  3381. Bounds mainBounds = m_mesh.bounds;
  3382. Vector3 min = mainBounds.min;
  3383. Vector3 max = mainBounds.max;
  3384. for (int i = 1; i < m_subTextObjects.Length && m_subTextObjects[i] != null; i++)
  3385. {
  3386. Bounds subBounds = m_subTextObjects[i].mesh.bounds;
  3387. min.x = min.x < subBounds.min.x ? min.x : subBounds.min.x;
  3388. min.y = min.y < subBounds.min.y ? min.y : subBounds.min.y;
  3389. max.x = max.x > subBounds.max.x ? max.x : subBounds.max.x;
  3390. max.y = max.y > subBounds.max.y ? max.y : subBounds.max.y;
  3391. }
  3392. Vector3 center = (min + max) / 2;
  3393. Vector2 size = max - min;
  3394. return new Bounds(center, size);
  3395. }
  3396. //public override void UpdateGeometry()
  3397. //{
  3398. //}
  3399. /// <summary>
  3400. /// Method to Update Scale in UV2
  3401. /// </summary>
  3402. //void UpdateSDFScale(float lossyScale)
  3403. //{
  3404. // // TODO: Resolve - Underline / Strikethrough segments not getting their SDF Scale adjusted.
  3405. // //Debug.Log("Updating SDF Scale.");
  3406. // // Return if we don't have a valid reference to a Canvas.
  3407. // if (m_canvas == null)
  3408. // {
  3409. // m_canvas = GetCanvas();
  3410. // if (m_canvas == null) return;
  3411. // }
  3412. // lossyScale = lossyScale == 0 ? 1 : lossyScale;
  3413. // float xScale = 0;
  3414. // float canvasScaleFactor = m_canvas.scaleFactor;
  3415. // if (m_canvas.renderMode == RenderMode.ScreenSpaceOverlay)
  3416. // xScale = lossyScale / canvasScaleFactor;
  3417. // else if (m_canvas.renderMode == RenderMode.ScreenSpaceCamera)
  3418. // xScale = m_canvas.worldCamera != null ? lossyScale : 1;
  3419. // else
  3420. // xScale = lossyScale;
  3421. // // Iterate through each of the characters.
  3422. // for (int i = 0; i < m_textInfo.characterCount; i++)
  3423. // {
  3424. // // Only update scale for visible characters.
  3425. // if (m_textInfo.characterInfo[i].isVisible && m_textInfo.characterInfo[i].elementType == TMP_TextElementType.Character)
  3426. // {
  3427. // float scale = xScale * m_textInfo.characterInfo[i].scale * (1 - m_charWidthAdjDelta);
  3428. // if (!m_textInfo.characterInfo[i].isUsingAlternateTypeface && (m_textInfo.characterInfo[i].style & FontStyles.Bold) == FontStyles.Bold) scale *= -1;
  3429. // int index = m_textInfo.characterInfo[i].materialReferenceIndex;
  3430. // int vertexIndex = m_textInfo.characterInfo[i].vertexIndex;
  3431. // m_textInfo.meshInfo[index].uvs2[vertexIndex + 0].y = scale;
  3432. // m_textInfo.meshInfo[index].uvs2[vertexIndex + 1].y = scale;
  3433. // m_textInfo.meshInfo[index].uvs2[vertexIndex + 2].y = scale;
  3434. // m_textInfo.meshInfo[index].uvs2[vertexIndex + 3].y = scale;
  3435. // }
  3436. // }
  3437. // // Push the updated uv2 scale information to the meshes.
  3438. // for (int i = 0; i < m_textInfo.materialCount; i++)
  3439. // {
  3440. // if (i == 0)
  3441. // {
  3442. // m_mesh.uv2 = m_textInfo.meshInfo[0].uvs2;
  3443. // m_canvasRenderer.SetMesh(m_mesh);
  3444. // }
  3445. // else
  3446. // {
  3447. // m_subTextObjects[i].mesh.uv2 = m_textInfo.meshInfo[i].uvs2;
  3448. // m_subTextObjects[i].canvasRenderer.SetMesh(m_subTextObjects[i].mesh);
  3449. // }
  3450. // }
  3451. //}
  3452. /// <summary>
  3453. /// Method to update the SDF Scale in UV2.
  3454. /// </summary>
  3455. /// <param name="scaleDelta"></param>
  3456. void UpdateSDFScale(float scaleDelta)
  3457. {
  3458. if (scaleDelta == 0 || scaleDelta == float.PositiveInfinity)
  3459. {
  3460. m_havePropertiesChanged = true;
  3461. OnPreRenderCanvas();
  3462. return;
  3463. }
  3464. for (int materialIndex = 0; materialIndex < m_textInfo.materialCount; materialIndex ++)
  3465. {
  3466. TMP_MeshInfo meshInfo = m_textInfo.meshInfo[materialIndex];
  3467. for (int i = 0; i < meshInfo.uvs2.Length; i++)
  3468. {
  3469. meshInfo.uvs2[i].y *= Mathf.Abs(scaleDelta);
  3470. }
  3471. }
  3472. // Push the updated uv2 scale information to the meshes.
  3473. for (int i = 0; i < m_textInfo.materialCount; i++)
  3474. {
  3475. if (i == 0)
  3476. {
  3477. m_mesh.uv2 = m_textInfo.meshInfo[0].uvs2;
  3478. m_canvasRenderer.SetMesh(m_mesh);
  3479. }
  3480. else
  3481. {
  3482. m_subTextObjects[i].mesh.uv2 = m_textInfo.meshInfo[i].uvs2;
  3483. m_subTextObjects[i].canvasRenderer.SetMesh(m_subTextObjects[i].mesh);
  3484. }
  3485. }
  3486. }
  3487. // Function to offset vertices position to account for line spacing changes.
  3488. protected override void AdjustLineOffset(int startIndex, int endIndex, float offset)
  3489. {
  3490. Vector3 vertexOffset = new Vector3(0, offset, 0);
  3491. for (int i = startIndex; i <= endIndex; i++)
  3492. {
  3493. m_textInfo.characterInfo[i].bottomLeft -= vertexOffset;
  3494. m_textInfo.characterInfo[i].topLeft -= vertexOffset;
  3495. m_textInfo.characterInfo[i].topRight -= vertexOffset;
  3496. m_textInfo.characterInfo[i].bottomRight -= vertexOffset;
  3497. m_textInfo.characterInfo[i].ascender -= vertexOffset.y;
  3498. m_textInfo.characterInfo[i].baseLine -= vertexOffset.y;
  3499. m_textInfo.characterInfo[i].descender -= vertexOffset.y;
  3500. if (m_textInfo.characterInfo[i].isVisible)
  3501. {
  3502. m_textInfo.characterInfo[i].vertex_BL.position -= vertexOffset;
  3503. m_textInfo.characterInfo[i].vertex_TL.position -= vertexOffset;
  3504. m_textInfo.characterInfo[i].vertex_TR.position -= vertexOffset;
  3505. m_textInfo.characterInfo[i].vertex_BR.position -= vertexOffset;
  3506. }
  3507. }
  3508. }
  3509. }
  3510. }