TMPro_Private.cs 205 KB

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