暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TMP_TextProcessingStack.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4. namespace TMPro
  5. {
  6. /// <summary>
  7. /// Structure used to track basic XML tags which are binary (on / off)
  8. /// </summary>
  9. public struct TMP_FontStyleStack
  10. {
  11. public byte bold;
  12. public byte italic;
  13. public byte underline;
  14. public byte strikethrough;
  15. public byte highlight;
  16. public byte superscript;
  17. public byte subscript;
  18. public byte uppercase;
  19. public byte lowercase;
  20. public byte smallcaps;
  21. /// <summary>
  22. /// Clear the basic XML tag stack.
  23. /// </summary>
  24. public void Clear()
  25. {
  26. bold = 0;
  27. italic = 0;
  28. underline = 0;
  29. strikethrough = 0;
  30. highlight = 0;
  31. superscript = 0;
  32. subscript = 0;
  33. uppercase = 0;
  34. lowercase = 0;
  35. smallcaps = 0;
  36. }
  37. public byte Add(FontStyles style)
  38. {
  39. switch (style)
  40. {
  41. case FontStyles.Bold:
  42. bold++;
  43. return bold;
  44. case FontStyles.Italic:
  45. italic++;
  46. return italic;
  47. case FontStyles.Underline:
  48. underline++;
  49. return underline;
  50. case FontStyles.UpperCase:
  51. uppercase++;
  52. return uppercase;
  53. case FontStyles.LowerCase:
  54. lowercase++;
  55. return lowercase;
  56. case FontStyles.Strikethrough:
  57. strikethrough++;
  58. return strikethrough;
  59. case FontStyles.Superscript:
  60. superscript++;
  61. return superscript;
  62. case FontStyles.Subscript:
  63. subscript++;
  64. return subscript;
  65. case FontStyles.Highlight:
  66. highlight++;
  67. return highlight;
  68. }
  69. return 0;
  70. }
  71. public byte Remove(FontStyles style)
  72. {
  73. switch (style)
  74. {
  75. case FontStyles.Bold:
  76. if (bold > 1)
  77. bold--;
  78. else
  79. bold = 0;
  80. return bold;
  81. case FontStyles.Italic:
  82. if (italic > 1)
  83. italic--;
  84. else
  85. italic = 0;
  86. return italic;
  87. case FontStyles.Underline:
  88. if (underline > 1)
  89. underline--;
  90. else
  91. underline = 0;
  92. return underline;
  93. case FontStyles.UpperCase:
  94. if (uppercase > 1)
  95. uppercase--;
  96. else
  97. uppercase = 0;
  98. return uppercase;
  99. case FontStyles.LowerCase:
  100. if (lowercase > 1)
  101. lowercase--;
  102. else
  103. lowercase = 0;
  104. return lowercase;
  105. case FontStyles.Strikethrough:
  106. if (strikethrough > 1)
  107. strikethrough--;
  108. else
  109. strikethrough = 0;
  110. return strikethrough;
  111. case FontStyles.Highlight:
  112. if (highlight > 1)
  113. highlight--;
  114. else
  115. highlight = 0;
  116. return highlight;
  117. case FontStyles.Superscript:
  118. if (superscript > 1)
  119. superscript--;
  120. else
  121. superscript = 0;
  122. return superscript;
  123. case FontStyles.Subscript:
  124. if (subscript > 1)
  125. subscript--;
  126. else
  127. subscript = 0;
  128. return subscript;
  129. }
  130. return 0;
  131. }
  132. }
  133. /// <summary>
  134. /// Structure used to track XML tags of various types.
  135. /// </summary>
  136. /// <typeparam name="T"></typeparam>
  137. [DebuggerDisplay("Item count = {m_Count}")]
  138. public struct TMP_TextProcessingStack<T>
  139. {
  140. public T[] itemStack;
  141. public int index;
  142. T m_DefaultItem;
  143. int m_Capacity;
  144. int m_RolloverSize;
  145. int m_Count;
  146. const int k_DefaultCapacity = 4;
  147. /// <summary>
  148. /// Constructor to create a new item stack.
  149. /// </summary>
  150. /// <param name="stack"></param>
  151. public TMP_TextProcessingStack(T[] stack)
  152. {
  153. itemStack = stack;
  154. m_Capacity = stack.Length;
  155. index = 0;
  156. m_RolloverSize = 0;
  157. m_DefaultItem = default(T);
  158. m_Count = 0;
  159. }
  160. /// <summary>
  161. /// Constructor for a new item stack with the given capacity.
  162. /// </summary>
  163. /// <param name="capacity"></param>
  164. public TMP_TextProcessingStack(int capacity)
  165. {
  166. itemStack = new T[capacity];
  167. m_Capacity = capacity;
  168. index = 0;
  169. m_RolloverSize = 0;
  170. m_DefaultItem = default(T);
  171. m_Count = 0;
  172. }
  173. public TMP_TextProcessingStack(int capacity, int rolloverSize)
  174. {
  175. itemStack = new T[capacity];
  176. m_Capacity = capacity;
  177. index = 0;
  178. m_RolloverSize = rolloverSize;
  179. m_DefaultItem = default(T);
  180. m_Count = 0;
  181. }
  182. /// <summary>
  183. ///
  184. /// </summary>
  185. public int Count
  186. {
  187. get { return m_Count; }
  188. }
  189. /// <summary>
  190. /// Returns the current item on the stack.
  191. /// </summary>
  192. public T current
  193. {
  194. get
  195. {
  196. if (index > 0)
  197. return itemStack[index - 1];
  198. return itemStack[0];
  199. }
  200. }
  201. /// <summary>
  202. ///
  203. /// </summary>
  204. public int rolloverSize
  205. {
  206. get { return m_RolloverSize; }
  207. set
  208. {
  209. m_RolloverSize = value;
  210. //if (m_Capacity < m_RolloverSize)
  211. // Array.Resize(ref itemStack, m_RolloverSize);
  212. }
  213. }
  214. /// <summary>
  215. /// Set stack elements to default item.
  216. /// </summary>
  217. /// <param name="stack">The stack of elements.</param>
  218. /// <param name="item"></param>
  219. internal static void SetDefault(TMP_TextProcessingStack<T>[] stack, T item)
  220. {
  221. for (int i = 0; i < stack.Length; i++)
  222. stack[i].SetDefault(item);
  223. }
  224. /// <summary>
  225. /// Function to clear and reset stack to first item.
  226. /// </summary>
  227. public void Clear()
  228. {
  229. index = 0;
  230. m_Count = 0;
  231. }
  232. /// <summary>
  233. /// Function to set the first item on the stack and reset index.
  234. /// </summary>
  235. /// <param name="item"></param>
  236. public void SetDefault(T item)
  237. {
  238. if (itemStack == null)
  239. {
  240. m_Capacity = k_DefaultCapacity;
  241. itemStack = new T[m_Capacity];
  242. m_DefaultItem = default(T);
  243. }
  244. itemStack[0] = item;
  245. index = 1;
  246. m_Count = 1;
  247. }
  248. /// <summary>
  249. /// Function to add a new item to the stack.
  250. /// </summary>
  251. /// <param name="item"></param>
  252. public void Add(T item)
  253. {
  254. if (index < itemStack.Length)
  255. {
  256. itemStack[index] = item;
  257. index += 1;
  258. }
  259. }
  260. /// <summary>
  261. /// Function to retrieve an item from the stack.
  262. /// </summary>
  263. /// <returns></returns>
  264. public T Remove()
  265. {
  266. index -= 1;
  267. m_Count -= 1;
  268. if (index <= 0)
  269. {
  270. m_Count = 0;
  271. index = 1;
  272. return itemStack[0];
  273. }
  274. return itemStack[index - 1];
  275. }
  276. public void Push(T item)
  277. {
  278. if (index == m_Capacity)
  279. {
  280. m_Capacity *= 2;
  281. if (m_Capacity == 0)
  282. m_Capacity = k_DefaultCapacity;
  283. Array.Resize(ref itemStack, m_Capacity);
  284. }
  285. itemStack[index] = item;
  286. if (m_RolloverSize == 0)
  287. {
  288. index += 1;
  289. m_Count += 1;
  290. }
  291. else
  292. {
  293. index = (index + 1) % m_RolloverSize;
  294. m_Count = m_Count < m_RolloverSize ? m_Count + 1 : m_RolloverSize;
  295. }
  296. }
  297. public T Pop()
  298. {
  299. if (index == 0 && m_RolloverSize == 0)
  300. return default(T);
  301. if (m_RolloverSize == 0)
  302. index -= 1;
  303. else
  304. {
  305. index = (index - 1) % m_RolloverSize;
  306. index = index < 0 ? index + m_RolloverSize : index;
  307. }
  308. T item = itemStack[index];
  309. itemStack[index] = m_DefaultItem;
  310. m_Count = m_Count > 0 ? m_Count - 1 : 0;
  311. return item;
  312. }
  313. /// <summary>
  314. ///
  315. /// </summary>
  316. /// <returns></returns>
  317. public T Peek()
  318. {
  319. if (index == 0)
  320. return m_DefaultItem;
  321. return itemStack[index - 1];
  322. }
  323. /// <summary>
  324. /// Function to retrieve the current item from the stack.
  325. /// </summary>
  326. /// <returns>itemStack <T></returns>
  327. public T CurrentItem()
  328. {
  329. if (index > 0)
  330. return itemStack[index - 1];
  331. return itemStack[0];
  332. }
  333. /// <summary>
  334. /// Function to retrieve the previous item without affecting the stack.
  335. /// </summary>
  336. /// <returns></returns>
  337. public T PreviousItem()
  338. {
  339. if (index > 1)
  340. return itemStack[index - 2];
  341. return itemStack[0];
  342. }
  343. }
  344. }