TMPro_MeshUtilities.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. }
  113. /// <summary>
  114. /// Function which returns the text contained in a link.
  115. /// </summary>
  116. /// <param name="textInfo"></param>
  117. /// <returns></returns>
  118. public string GetLinkText()
  119. {
  120. string text = string.Empty;
  121. TMP_TextInfo textInfo = textComponent.textInfo;
  122. for (int i = linkTextfirstCharacterIndex; i < linkTextfirstCharacterIndex + linkTextLength; i++)
  123. text += textInfo.characterInfo[i].character;
  124. return text;
  125. }
  126. /// <summary>
  127. /// Function which returns the link ID as a string.
  128. /// </summary>
  129. /// <param name="text">The source input text.</param>
  130. /// <returns></returns>
  131. public string GetLinkID()
  132. {
  133. if (textComponent == null)
  134. return string.Empty;
  135. return new string(linkID, 0, linkIdLength);
  136. //return textComponent.text.Substring(linkIdFirstCharacterIndex, linkIdLength);
  137. }
  138. }
  139. /// <summary>
  140. /// Structure containing information about the individual words contained in the text object.
  141. /// </summary>
  142. public struct TMP_WordInfo
  143. {
  144. // NOTE: Structure could be simplified by only including the firstCharacterIndex and length.
  145. public TMP_Text textComponent;
  146. public int firstCharacterIndex;
  147. public int lastCharacterIndex;
  148. public int characterCount;
  149. //public float length;
  150. /// <summary>
  151. /// Returns the word as a string.
  152. /// </summary>
  153. /// <returns></returns>
  154. public string GetWord()
  155. {
  156. string word = string.Empty;
  157. TMP_CharacterInfo[] charInfo = textComponent.textInfo.characterInfo;
  158. for (int i = firstCharacterIndex; i < lastCharacterIndex + 1; i++)
  159. {
  160. word += charInfo[i].character;
  161. }
  162. return word;
  163. }
  164. }
  165. public struct TMP_SpriteInfo
  166. {
  167. public int spriteIndex; // Index of the sprite in the sprite atlas.
  168. public int characterIndex; // The characterInfo index which holds the key information about this sprite.
  169. public int vertexIndex;
  170. }
  171. //public struct SpriteInfo
  172. //{
  173. //
  174. //}
  175. public struct Extents
  176. {
  177. internal static Extents zero = new Extents(Vector2.zero, Vector2.zero);
  178. internal static Extents uninitialized = new Extents(new Vector2(32767, 32767), new Vector2(-32767, -32767));
  179. public Vector2 min;
  180. public Vector2 max;
  181. public Extents(Vector2 min, Vector2 max)
  182. {
  183. this.min = min;
  184. this.max = max;
  185. }
  186. public override string ToString()
  187. {
  188. string s = "Min (" + min.x.ToString("f2") + ", " + min.y.ToString("f2") + ") Max (" + max.x.ToString("f2") + ", " + max.y.ToString("f2") + ")";
  189. return s;
  190. }
  191. }
  192. [Serializable]
  193. public struct Mesh_Extents
  194. {
  195. public Vector2 min;
  196. public Vector2 max;
  197. public Mesh_Extents(Vector2 min, Vector2 max)
  198. {
  199. this.min = min;
  200. this.max = max;
  201. }
  202. public override string ToString()
  203. {
  204. string s = "Min (" + min.x.ToString("f2") + ", " + min.y.ToString("f2") + ") Max (" + max.x.ToString("f2") + ", " + max.y.ToString("f2") + ")";
  205. //string s = "Center: (" + ")" + " Extents: (" + ((max.x - min.x) / 2).ToString("f2") + "," + ((max.y - min.y) / 2).ToString("f2") + ").";
  206. return s;
  207. }
  208. }
  209. // internal struct TMP_TextProcessingState
  210. // {
  211. // // Multi Font & Material support related
  212. // public TMP_FontAsset CurrentFontAsset;
  213. // public TMP_SpriteAsset CurrentSpriteAsset;
  214. // public Material CurrentMaterial;
  215. // public int CurrentMaterialIndex;
  216. //
  217. // public float CurrentFontSize;
  218. // public float FontScale;
  219. // public float FontScaleMultiplier;
  220. // public FontStyles FontStyle;
  221. // public int ItalicAngle;
  222. //
  223. // public float CharacterSpacing;
  224. // public float CharacterMonoSpacing;
  225. // public bool TagNoParsing;
  226. //
  227. // public float HorizontalAdvance;
  228. // public float MaxCapHeight;
  229. // public float MaxTextAscender;
  230. // public float MaxTextDescender;
  231. // public float MaxElementAscender;
  232. // public float MaxElementDescender;
  233. // public float StartOfLineAscender;
  234. // public float MaxLineAscender;
  235. // public float MaxLineDescender;
  236. // public float PageAscender;
  237. //
  238. // public int PreviousWordBreak;
  239. // public int TotalCharacterCount;
  240. // //public int VisibleCharacterCount;
  241. // //public int VisibleSpriteCount;
  242. // public int VisibleLinkCount;
  243. // public int FirstCharacterIndex;
  244. // public int FirstVisibleCharacterIndex;
  245. // public int LastCharacterIndex;
  246. // public int LastVisibleCharIndex;
  247. //
  248. // public int LineNumber;
  249. // public float baselineOffset;
  250. // public float lineOffset;
  251. // public bool isDrivenLineSpacing;
  252. // public bool IsNonBreakingSpace;
  253. //
  254. // public HorizontalAlignmentOptions HorizontalAlignment;
  255. // public float MarginLeft;
  256. // public float MarginRight;
  257. //
  258. // public float PreferredWidth;
  259. // public float PreferredHeight;
  260. //
  261. // public Color32 VertexColor;
  262. // public Color32 UnderlineColor;
  263. // public Color32 StrikethroughColor;
  264. // //public Color32 HighlightColor;
  265. //
  266. // public Extents MeshExtents;
  267. // public TMP_LineInfo lineInfo;
  268. //
  269. // public int spriteAnimationID;
  270. //
  271. // public TMP_FontStyleStack BasicStyleStack;
  272. // public TMP_RichTextTagStack<int> ItalicAngleStack;
  273. // public TMP_RichTextTagStack<Color32> ColorStack;
  274. // public TMP_RichTextTagStack<Color32> UnderlineColorStack;
  275. // public TMP_RichTextTagStack<Color32> StrikethroughColorStack;
  276. // public TMP_RichTextTagStack<Color32> HighlightColorStack;
  277. // public TMP_RichTextTagStack<HighlightState> HighlightStateStack;
  278. // public TMP_RichTextTagStack<TMP_ColorGradient> ColorGradientStack;
  279. // public TMP_RichTextTagStack<float> SizeStack;
  280. // public TMP_RichTextTagStack<float> IndentStack;
  281. // public TMP_RichTextTagStack<FontWeight> FontWeightStack;
  282. //
  283. // public TMP_RichTextTagStack<float> BaselineStack;
  284. // //public TMP_RichTextTagStack<int> ActionStack;
  285. // public TMP_RichTextTagStack<MaterialReference> MaterialReferenceStack;
  286. // public TMP_RichTextTagStack<HorizontalAlignmentOptions> LineJustificationStack;
  287. // }
  288. // Structure used for Word Wrapping which tracks the state of execution when the last space or carriage return character was encountered.
  289. public struct WordWrapState
  290. {
  291. public int previous_WordBreak;
  292. public int total_CharacterCount;
  293. public int visible_CharacterCount;
  294. public int visible_SpriteCount;
  295. public int visible_LinkCount;
  296. public int firstCharacterIndex;
  297. public int firstVisibleCharacterIndex;
  298. public int lastCharacterIndex;
  299. public int lastVisibleCharIndex;
  300. public int lineNumber;
  301. public float maxCapHeight;
  302. public float maxAscender;
  303. public float maxDescender;
  304. public float startOfLineAscender;
  305. public float maxLineAscender;
  306. public float maxLineDescender;
  307. public float pageAscender;
  308. public HorizontalAlignmentOptions horizontalAlignment;
  309. public float marginLeft;
  310. public float marginRight;
  311. public float xAdvance;
  312. public float preferredWidth;
  313. public float preferredHeight;
  314. //public float maxFontScale;
  315. public float previousLineScale;
  316. public int wordCount;
  317. public FontStyles fontStyle;
  318. public int italicAngle;
  319. public float fontScale;
  320. public float fontScaleMultiplier;
  321. public float currentFontSize;
  322. public float baselineOffset;
  323. public float lineOffset;
  324. public bool isDrivenLineSpacing;
  325. public float glyphHorizontalAdvanceAdjustment;
  326. public float cSpace;
  327. public float mSpace;
  328. public TMP_TextInfo textInfo;
  329. public TMP_LineInfo lineInfo;
  330. public Color32 vertexColor;
  331. public Color32 underlineColor;
  332. public Color32 strikethroughColor;
  333. public Color32 highlightColor;
  334. public TMP_FontStyleStack basicStyleStack;
  335. public TMP_TextProcessingStack<int> italicAngleStack;
  336. public TMP_TextProcessingStack<Color32> colorStack;
  337. public TMP_TextProcessingStack<Color32> underlineColorStack;
  338. public TMP_TextProcessingStack<Color32> strikethroughColorStack;
  339. public TMP_TextProcessingStack<Color32> highlightColorStack;
  340. public TMP_TextProcessingStack<HighlightState> highlightStateStack;
  341. public TMP_TextProcessingStack<TMP_ColorGradient> colorGradientStack;
  342. public TMP_TextProcessingStack<float> sizeStack;
  343. public TMP_TextProcessingStack<float> indentStack;
  344. public TMP_TextProcessingStack<FontWeight> fontWeightStack;
  345. public TMP_TextProcessingStack<int> styleStack;
  346. public TMP_TextProcessingStack<float> baselineStack;
  347. public TMP_TextProcessingStack<int> actionStack;
  348. public TMP_TextProcessingStack<MaterialReference> materialReferenceStack;
  349. public TMP_TextProcessingStack<HorizontalAlignmentOptions> lineJustificationStack;
  350. public int spriteAnimationID;
  351. public TMP_FontAsset currentFontAsset;
  352. public TMP_SpriteAsset currentSpriteAsset;
  353. public Material currentMaterial;
  354. public int currentMaterialIndex;
  355. public Extents meshExtents;
  356. public bool tagNoParsing;
  357. public bool isNonBreakingSpace;
  358. }
  359. /// <summary>
  360. /// Structure used to store retrieve the name and hashcode of the font and material
  361. /// </summary>
  362. public struct TagAttribute
  363. {
  364. public int startIndex;
  365. public int length;
  366. public int hashCode;
  367. }
  368. public struct RichTextTagAttribute
  369. {
  370. public int nameHashCode;
  371. public int valueHashCode;
  372. public TagValueType valueType;
  373. public int valueStartIndex;
  374. public int valueLength;
  375. public TagUnitType unitType;
  376. }
  377. }