No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TMPro_MeshUtilities.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using UnityEngine;
  2. using UnityEngine.TextCore;
  3. using System;
  4. namespace TMPro
  5. {
  6. /// <summary>
  7. /// Flags to control what vertex data is pushed to the mesh and renderer.
  8. /// </summary>
  9. public enum TMP_VertexDataUpdateFlags
  10. {
  11. None = 0x0,
  12. Vertices = 0x1,
  13. Uv0 = 0x2,
  14. Uv2 = 0x4,
  15. Uv4 = 0x8,
  16. Colors32 = 0x10,
  17. All = 0xFF
  18. };
  19. /// <summary>
  20. /// TMP custom data type to represent 32 bit characters.
  21. /// </summary>
  22. //public struct TMP_Char
  23. //{
  24. // private int m_value;
  25. // private TMP_Char(int value)
  26. // {
  27. // this.m_value = value;
  28. // }
  29. // private TMP_Char(TMP_Char value)
  30. // {
  31. // this.m_value = (int)value;
  32. // }
  33. // public static implicit operator TMP_Char(int value)
  34. // {
  35. // return new TMP_Char(value);
  36. // }
  37. // public static implicit operator TMP_Char(char c)
  38. // {
  39. // return new TMP_Char(c);
  40. // }
  41. // public static explicit operator int(TMP_Char value)
  42. // {
  43. // return value.m_value;
  44. // }
  45. // public override string ToString()
  46. // {
  47. // return m_value.ToString();
  48. // }
  49. //}
  50. //public struct TMP_VertexInfo
  51. //{
  52. // public TMP_Vertex topLeft;
  53. // public TMP_Vertex bottomLeft;
  54. // public TMP_Vertex topRight;
  55. // public TMP_Vertex bottomRight;
  56. //}
  57. [Serializable]
  58. public struct VertexGradient
  59. {
  60. public Color topLeft;
  61. public Color topRight;
  62. public Color bottomLeft;
  63. public Color bottomRight;
  64. public VertexGradient (Color color)
  65. {
  66. this.topLeft = color;
  67. this.topRight = color;
  68. this.bottomLeft = color;
  69. this.bottomRight = color;
  70. }
  71. /// <summary>
  72. /// The vertex colors at the corners of the characters.
  73. /// </summary>
  74. /// <param name="color0">Top left color.</param>
  75. /// <param name="color1">Top right color.</param>
  76. /// <param name="color2">Bottom left color.</param>
  77. /// <param name="color3">Bottom right color.</param>
  78. public VertexGradient(Color color0, Color color1, Color color2, Color color3)
  79. {
  80. this.topLeft = color0;
  81. this.topRight = color1;
  82. this.bottomLeft = color2;
  83. this.bottomRight = color3;
  84. }
  85. }
  86. public struct TMP_PageInfo
  87. {
  88. public int firstCharacterIndex;
  89. public int lastCharacterIndex;
  90. public float ascender;
  91. public float baseLine;
  92. public float descender;
  93. // public float extents;
  94. }
  95. /// <summary>
  96. /// Structure containing information about individual links contained in the text object.
  97. /// </summary>
  98. public struct TMP_LinkInfo
  99. {
  100. public TMP_Text textComponent;
  101. public int hashCode;
  102. public int linkIdFirstCharacterIndex;
  103. public int linkIdLength;
  104. public int linkTextfirstCharacterIndex;
  105. public int linkTextLength;
  106. internal char[] linkID;
  107. internal void SetLinkID(char[] text, int startIndex, int length)
  108. {
  109. if (linkID == null || linkID.Length < length) linkID = new char[length];
  110. for (int i = 0; i < length; i++)
  111. linkID[i] = text[startIndex + i];
  112. linkIdLength = length;
  113. }
  114. /// <summary>
  115. /// Function which returns the text contained in a link.
  116. /// </summary>
  117. /// <param name="textInfo"></param>
  118. /// <returns></returns>
  119. public string GetLinkText()
  120. {
  121. string text = string.Empty;
  122. TMP_TextInfo textInfo = textComponent.textInfo;
  123. for (int i = linkTextfirstCharacterIndex; i < linkTextfirstCharacterIndex + linkTextLength; i++)
  124. text += textInfo.characterInfo[i].character;
  125. return text;
  126. }
  127. /// <summary>
  128. /// Function which returns the link as a string.
  129. /// </summary>
  130. /// <returns></returns>
  131. public string GetLink()
  132. {
  133. return GetLinkID();
  134. }
  135. /// <summary>
  136. /// Function which returns the link ID as a string.
  137. /// </summary>
  138. /// <param name="text">The source input text.</param>
  139. /// <returns></returns>
  140. public string GetLinkID()
  141. {
  142. if (textComponent == null)
  143. return string.Empty;
  144. return new string(linkID, 0, linkIdLength);
  145. }
  146. }
  147. /// <summary>
  148. /// Structure containing information about the individual words contained in the text object.
  149. /// </summary>
  150. public struct TMP_WordInfo
  151. {
  152. // NOTE: Structure could be simplified by only including the firstCharacterIndex and length.
  153. public TMP_Text textComponent;
  154. public int firstCharacterIndex;
  155. public int lastCharacterIndex;
  156. public int characterCount;
  157. //public float length;
  158. /// <summary>
  159. /// Returns the word as a string.
  160. /// </summary>
  161. /// <returns></returns>
  162. public string GetWord()
  163. {
  164. string word = string.Empty;
  165. TMP_CharacterInfo[] charInfo = textComponent.textInfo.characterInfo;
  166. for (int i = firstCharacterIndex; i < lastCharacterIndex + 1; i++)
  167. {
  168. word += charInfo[i].character;
  169. }
  170. return word;
  171. }
  172. }
  173. public struct TMP_SpriteInfo
  174. {
  175. public int spriteIndex; // Index of the sprite in the sprite atlas.
  176. public int characterIndex; // The characterInfo index which holds the key information about this sprite.
  177. public int vertexIndex;
  178. }
  179. //public struct SpriteInfo
  180. //{
  181. //
  182. //}
  183. public struct Extents
  184. {
  185. internal static Extents zero = new Extents(Vector2.zero, Vector2.zero);
  186. internal static Extents uninitialized = new Extents(new Vector2(32767, 32767), new Vector2(-32767, -32767));
  187. public Vector2 min;
  188. public Vector2 max;
  189. public Extents(Vector2 min, Vector2 max)
  190. {
  191. this.min = min;
  192. this.max = max;
  193. }
  194. public override string ToString()
  195. {
  196. string s = "Min (" + min.x.ToString("f2") + ", " + min.y.ToString("f2") + ") Max (" + max.x.ToString("f2") + ", " + max.y.ToString("f2") + ")";
  197. return s;
  198. }
  199. }
  200. [Serializable]
  201. public struct Mesh_Extents
  202. {
  203. public Vector2 min;
  204. public Vector2 max;
  205. public Mesh_Extents(Vector2 min, Vector2 max)
  206. {
  207. this.min = min;
  208. this.max = max;
  209. }
  210. public override string ToString()
  211. {
  212. string s = "Min (" + min.x.ToString("f2") + ", " + min.y.ToString("f2") + ") Max (" + max.x.ToString("f2") + ", " + max.y.ToString("f2") + ")";
  213. //string s = "Center: (" + ")" + " Extents: (" + ((max.x - min.x) / 2).ToString("f2") + "," + ((max.y - min.y) / 2).ToString("f2") + ").";
  214. return s;
  215. }
  216. }
  217. // internal struct TMP_TextProcessingState
  218. // {
  219. // // Multi Font & Material support related
  220. // public TMP_FontAsset CurrentFontAsset;
  221. // public TMP_SpriteAsset CurrentSpriteAsset;
  222. // public Material CurrentMaterial;
  223. // public int CurrentMaterialIndex;
  224. //
  225. // public float CurrentFontSize;
  226. // public float FontScale;
  227. // public float FontScaleMultiplier;
  228. // public FontStyles FontStyle;
  229. // public int ItalicAngle;
  230. //
  231. // public float CharacterSpacing;
  232. // public float CharacterMonoSpacing;
  233. // public bool TagNoParsing;
  234. //
  235. // public float HorizontalAdvance;
  236. // public float MaxCapHeight;
  237. // public float MaxTextAscender;
  238. // public float MaxTextDescender;
  239. // public float MaxElementAscender;
  240. // public float MaxElementDescender;
  241. // public float StartOfLineAscender;
  242. // public float MaxLineAscender;
  243. // public float MaxLineDescender;
  244. // public float PageAscender;
  245. //
  246. // public int PreviousWordBreak;
  247. // public int TotalCharacterCount;
  248. // //public int VisibleCharacterCount;
  249. // //public int VisibleSpriteCount;
  250. // public int VisibleLinkCount;
  251. // public int FirstCharacterIndex;
  252. // public int FirstVisibleCharacterIndex;
  253. // public int LastCharacterIndex;
  254. // public int LastVisibleCharIndex;
  255. //
  256. // public int LineNumber;
  257. // public float baselineOffset;
  258. // public float lineOffset;
  259. // public bool isDrivenLineSpacing;
  260. // public bool IsNonBreakingSpace;
  261. //
  262. // public HorizontalAlignmentOptions HorizontalAlignment;
  263. // public float MarginLeft;
  264. // public float MarginRight;
  265. //
  266. // public float PreferredWidth;
  267. // public float PreferredHeight;
  268. //
  269. // public Color32 VertexColor;
  270. // public Color32 UnderlineColor;
  271. // public Color32 StrikethroughColor;
  272. // //public Color32 HighlightColor;
  273. //
  274. // public Extents MeshExtents;
  275. // public TMP_LineInfo lineInfo;
  276. //
  277. // public int spriteAnimationID;
  278. //
  279. // public TMP_FontStyleStack BasicStyleStack;
  280. // public TMP_RichTextTagStack<int> ItalicAngleStack;
  281. // public TMP_RichTextTagStack<Color32> ColorStack;
  282. // public TMP_RichTextTagStack<Color32> UnderlineColorStack;
  283. // public TMP_RichTextTagStack<Color32> StrikethroughColorStack;
  284. // public TMP_RichTextTagStack<Color32> HighlightColorStack;
  285. // public TMP_RichTextTagStack<HighlightState> HighlightStateStack;
  286. // public TMP_RichTextTagStack<TMP_ColorGradient> ColorGradientStack;
  287. // public TMP_RichTextTagStack<float> SizeStack;
  288. // public TMP_RichTextTagStack<float> IndentStack;
  289. // public TMP_RichTextTagStack<FontWeight> FontWeightStack;
  290. //
  291. // public TMP_RichTextTagStack<float> BaselineStack;
  292. // //public TMP_RichTextTagStack<int> ActionStack;
  293. // public TMP_RichTextTagStack<MaterialReference> MaterialReferenceStack;
  294. // public TMP_RichTextTagStack<HorizontalAlignmentOptions> LineJustificationStack;
  295. // }
  296. // Structure used for Word Wrapping which tracks the state of execution when the last space or carriage return character was encountered.
  297. internal struct WordWrapState
  298. {
  299. public int previous_WordBreak;
  300. public int total_CharacterCount;
  301. public int visible_CharacterCount;
  302. public int visibleSpaceCount;
  303. public int visible_SpriteCount;
  304. public int visible_LinkCount;
  305. public int firstCharacterIndex;
  306. public int firstVisibleCharacterIndex;
  307. public int lastCharacterIndex;
  308. public int lastVisibleCharIndex;
  309. public int lineNumber;
  310. public float maxCapHeight;
  311. public float maxAscender;
  312. public float maxDescender;
  313. public float startOfLineAscender;
  314. public float maxLineAscender;
  315. public float maxLineDescender;
  316. public float pageAscender;
  317. public HorizontalAlignmentOptions horizontalAlignment;
  318. public float marginLeft;
  319. public float marginRight;
  320. public float xAdvance;
  321. public float preferredWidth;
  322. public float preferredHeight;
  323. public float renderedWidth;
  324. public float renderedHeight;
  325. public float previousLineScale;
  326. public int wordCount;
  327. public FontStyles fontStyle;
  328. public int italicAngle;
  329. public float fontScaleMultiplier;
  330. public float currentFontSize;
  331. public float baselineOffset;
  332. public float lineOffset;
  333. public bool isDrivenLineSpacing;
  334. public int lastBaseGlyphIndex;
  335. public float cSpace;
  336. public float mSpace;
  337. public TMP_TextInfo textInfo;
  338. public TMP_LineInfo lineInfo;
  339. public Color32 vertexColor;
  340. public Color32 underlineColor;
  341. public Color32 strikethroughColor;
  342. public HighlightState highlightState;
  343. public TMP_FontStyleStack basicStyleStack;
  344. public TMP_TextProcessingStack<int> italicAngleStack;
  345. public TMP_TextProcessingStack<Color32> colorStack;
  346. public TMP_TextProcessingStack<Color32> underlineColorStack;
  347. public TMP_TextProcessingStack<Color32> strikethroughColorStack;
  348. public TMP_TextProcessingStack<Color32> highlightColorStack;
  349. public TMP_TextProcessingStack<HighlightState> highlightStateStack;
  350. public TMP_TextProcessingStack<TMP_ColorGradient> colorGradientStack;
  351. public TMP_TextProcessingStack<float> sizeStack;
  352. public TMP_TextProcessingStack<float> indentStack;
  353. public TMP_TextProcessingStack<FontWeight> fontWeightStack;
  354. public TMP_TextProcessingStack<int> styleStack;
  355. public TMP_TextProcessingStack<float> baselineStack;
  356. public TMP_TextProcessingStack<int> actionStack;
  357. public TMP_TextProcessingStack<MaterialReference> materialReferenceStack;
  358. public TMP_TextProcessingStack<HorizontalAlignmentOptions> lineJustificationStack;
  359. public int spriteAnimationID;
  360. public TMP_FontAsset currentFontAsset;
  361. public TMP_SpriteAsset currentSpriteAsset;
  362. public Material currentMaterial;
  363. public int currentMaterialIndex;
  364. public Extents meshExtents;
  365. public bool tagNoParsing;
  366. public bool isNonBreakingSpace;
  367. public Quaternion fxRotation;
  368. public Vector3 fxScale;
  369. }
  370. /// <summary>
  371. /// Structure used to store retrieve the name and hashcode of the font and material
  372. /// </summary>
  373. internal struct TagAttribute
  374. {
  375. public int startIndex;
  376. public int length;
  377. public int hashCode;
  378. }
  379. internal struct RichTextTagAttribute
  380. {
  381. public int nameHashCode;
  382. public int valueHashCode;
  383. public TagValueType valueType;
  384. public int valueStartIndex;
  385. public int valueLength;
  386. public TagUnitType unitType;
  387. }
  388. }