TMPro_Private.cs 205 KB

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