TMPro_MeshUtilities.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. public Vector2 min;
  178. public Vector2 max;
  179. public Extents(Vector2 min, Vector2 max)
  180. {
  181. this.min = min;
  182. this.max = max;
  183. }
  184. public override string ToString()
  185. {
  186. string s = "Min (" + min.x.ToString("f2") + ", " + min.y.ToString("f2") + ") Max (" + max.x.ToString("f2") + ", " + max.y.ToString("f2") + ")";
  187. return s;
  188. }
  189. }
  190. [Serializable]
  191. public struct Mesh_Extents
  192. {
  193. public Vector2 min;
  194. public Vector2 max;
  195. public Mesh_Extents(Vector2 min, Vector2 max)
  196. {
  197. this.min = min;
  198. this.max = max;
  199. }
  200. public override string ToString()
  201. {
  202. string s = "Min (" + min.x.ToString("f2") + ", " + min.y.ToString("f2") + ") Max (" + max.x.ToString("f2") + ", " + max.y.ToString("f2") + ")";
  203. //string s = "Center: (" + ")" + " Extents: (" + ((max.x - min.x) / 2).ToString("f2") + "," + ((max.y - min.y) / 2).ToString("f2") + ").";
  204. return s;
  205. }
  206. }
  207. // Structure used for Word Wrapping which tracks the state of execution when the last space or carriage return character was encountered.
  208. public struct WordWrapState
  209. {
  210. public int previous_WordBreak;
  211. public int total_CharacterCount;
  212. public int visible_CharacterCount;
  213. public int visible_SpriteCount;
  214. public int visible_LinkCount;
  215. public int firstCharacterIndex;
  216. public int firstVisibleCharacterIndex;
  217. public int lastCharacterIndex;
  218. public int lastVisibleCharIndex;
  219. public int lineNumber;
  220. public float maxCapHeight;
  221. public float maxAscender;
  222. public float maxDescender;
  223. public float maxLineAscender;
  224. public float maxLineDescender;
  225. public float previousLineAscender;
  226. public float xAdvance;
  227. public float preferredWidth;
  228. public float preferredHeight;
  229. //public float maxFontScale;
  230. public float previousLineScale;
  231. public int wordCount;
  232. public FontStyles fontStyle;
  233. public float fontScale;
  234. public float fontScaleMultiplier;
  235. public float currentFontSize;
  236. public float baselineOffset;
  237. public float lineOffset;
  238. public TMP_TextInfo textInfo;
  239. //public TMPro_CharacterInfo[] characterInfo;
  240. public TMP_LineInfo lineInfo;
  241. public Color32 vertexColor;
  242. public Color32 underlineColor;
  243. public Color32 strikethroughColor;
  244. public Color32 highlightColor;
  245. public TMP_FontStyleStack basicStyleStack;
  246. public TMP_RichTextTagStack<Color32> colorStack;
  247. public TMP_RichTextTagStack<Color32> underlineColorStack;
  248. public TMP_RichTextTagStack<Color32> strikethroughColorStack;
  249. public TMP_RichTextTagStack<Color32> highlightColorStack;
  250. public TMP_RichTextTagStack<TMP_ColorGradient> colorGradientStack;
  251. public TMP_RichTextTagStack<float> sizeStack;
  252. public TMP_RichTextTagStack<float> indentStack;
  253. public TMP_RichTextTagStack<FontWeight> fontWeightStack;
  254. public TMP_RichTextTagStack<int> styleStack;
  255. public TMP_RichTextTagStack<float> baselineStack;
  256. public TMP_RichTextTagStack<int> actionStack;
  257. public TMP_RichTextTagStack<MaterialReference> materialReferenceStack;
  258. public TMP_RichTextTagStack<TextAlignmentOptions> lineJustificationStack;
  259. //public TMP_XmlTagStack<int> spriteAnimationStack;
  260. public int spriteAnimationID;
  261. public TMP_FontAsset currentFontAsset;
  262. public TMP_SpriteAsset currentSpriteAsset;
  263. public Material currentMaterial;
  264. public int currentMaterialIndex;
  265. public Extents meshExtents;
  266. public bool tagNoParsing;
  267. public bool isNonBreakingSpace;
  268. //public Mesh_Extents lineExtents;
  269. }
  270. /// <summary>
  271. /// Structure used to store retrieve the name and hashcode of the font and material
  272. /// </summary>
  273. public struct TagAttribute
  274. {
  275. public int startIndex;
  276. public int length;
  277. public int hashCode;
  278. }
  279. public struct RichTextTagAttribute
  280. {
  281. public int nameHashCode;
  282. public int valueHashCode;
  283. public TagValueType valueType;
  284. public int valueStartIndex;
  285. public int valueLength;
  286. public TagUnitType unitType;
  287. }
  288. }