TMPro_UGUI_Private.cs 215 KB

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