TMP_TextProcessingStack.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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.Strikethrough:
  51. strikethrough++;
  52. return strikethrough;
  53. case FontStyles.Superscript:
  54. superscript++;
  55. return superscript;
  56. case FontStyles.Subscript:
  57. subscript++;
  58. return subscript;
  59. case FontStyles.Highlight:
  60. highlight++;
  61. return highlight;
  62. }
  63. return 0;
  64. }
  65. public byte Remove(FontStyles style)
  66. {
  67. switch (style)
  68. {
  69. case FontStyles.Bold:
  70. if (bold > 1)
  71. bold--;
  72. else
  73. bold = 0;
  74. return bold;
  75. case FontStyles.Italic:
  76. if (italic > 1)
  77. italic--;
  78. else
  79. italic = 0;
  80. return italic;
  81. case FontStyles.Underline:
  82. if (underline > 1)
  83. underline--;
  84. else
  85. underline = 0;
  86. return underline;
  87. case FontStyles.Strikethrough:
  88. if (strikethrough > 1)
  89. strikethrough--;
  90. else
  91. strikethrough = 0;
  92. return strikethrough;
  93. case FontStyles.Highlight:
  94. if (highlight > 1)
  95. highlight--;
  96. else
  97. highlight = 0;
  98. return highlight;
  99. case FontStyles.Superscript:
  100. if (superscript > 1)
  101. superscript--;
  102. else
  103. superscript = 0;
  104. return superscript;
  105. case FontStyles.Subscript:
  106. if (subscript > 1)
  107. subscript--;
  108. else
  109. subscript = 0;
  110. return subscript;
  111. }
  112. return 0;
  113. }
  114. }
  115. /// <summary>
  116. /// Structure used to track XML tags of various types.
  117. /// </summary>
  118. /// <typeparam name="T"></typeparam>
  119. [DebuggerDisplay("Item count = {m_Count}")]
  120. public struct TMP_TextProcessingStack<T>
  121. {
  122. public T[] itemStack;
  123. public int index;
  124. T m_DefaultItem;
  125. int m_Capacity;
  126. int m_RolloverSize;
  127. int m_Count;
  128. const int k_DefaultCapacity = 4;
  129. /// <summary>
  130. /// Constructor to create a new item stack.
  131. /// </summary>
  132. /// <param name="stack"></param>
  133. public TMP_TextProcessingStack(T[] stack)
  134. {
  135. itemStack = stack;
  136. m_Capacity = stack.Length;
  137. index = 0;
  138. m_RolloverSize = 0;
  139. m_DefaultItem = default;
  140. m_Count = 0;
  141. }
  142. /// <summary>
  143. /// Constructor for a new item stack with the given capacity.
  144. /// </summary>
  145. /// <param name="capacity"></param>
  146. public TMP_TextProcessingStack(int capacity)
  147. {
  148. itemStack = new T[capacity];
  149. m_Capacity = capacity;
  150. index = 0;
  151. m_RolloverSize = 0;
  152. m_DefaultItem = default;
  153. m_Count = 0;
  154. }
  155. public TMP_TextProcessingStack(int capacity, int rolloverSize)
  156. {
  157. itemStack = new T[capacity];
  158. m_Capacity = capacity;
  159. index = 0;
  160. m_RolloverSize = rolloverSize;
  161. m_DefaultItem = default;
  162. m_Count = 0;
  163. }
  164. /// <summary>
  165. ///
  166. /// </summary>
  167. public int Count
  168. {
  169. get { return m_Count; }
  170. }
  171. /// <summary>
  172. /// Returns the current item on the stack.
  173. /// </summary>
  174. public T current
  175. {
  176. get
  177. {
  178. if (index > 0)
  179. return itemStack[index - 1];
  180. return itemStack[0];
  181. }
  182. }
  183. /// <summary>
  184. ///
  185. /// </summary>
  186. public int rolloverSize
  187. {
  188. get { return m_RolloverSize; }
  189. set
  190. {
  191. m_RolloverSize = value;
  192. //if (m_Capacity < m_RolloverSize)
  193. // Array.Resize(ref itemStack, m_RolloverSize);
  194. }
  195. }
  196. /// <summary>
  197. /// Function to clear and reset stack to first item.
  198. /// </summary>
  199. public void Clear()
  200. {
  201. index = 0;
  202. m_Count = 0;
  203. }
  204. /// <summary>
  205. /// Function to set the first item on the stack and reset index.
  206. /// </summary>
  207. /// <param name="item"></param>
  208. public void SetDefault(T item)
  209. {
  210. if (itemStack == null)
  211. {
  212. m_Capacity = k_DefaultCapacity;
  213. itemStack = new T[m_Capacity];
  214. m_DefaultItem = default;
  215. }
  216. itemStack[0] = item;
  217. index = 1;
  218. }
  219. /// <summary>
  220. /// Function to add a new item to the stack.
  221. /// </summary>
  222. /// <param name="item"></param>
  223. public void Add(T item)
  224. {
  225. if (index < itemStack.Length)
  226. {
  227. itemStack[index] = item;
  228. index += 1;
  229. }
  230. }
  231. /// <summary>
  232. /// Function to retrieve an item from the stack.
  233. /// </summary>
  234. /// <returns></returns>
  235. public T Remove()
  236. {
  237. index -= 1;
  238. if (index <= 0)
  239. {
  240. index = 1;
  241. return itemStack[0];
  242. }
  243. return itemStack[index - 1];
  244. }
  245. public void Push(T item)
  246. {
  247. if (index == m_Capacity)
  248. {
  249. m_Capacity *= 2;
  250. if (m_Capacity == 0)
  251. m_Capacity = k_DefaultCapacity;
  252. Array.Resize(ref itemStack, m_Capacity);
  253. }
  254. itemStack[index] = item;
  255. if (m_RolloverSize == 0)
  256. {
  257. index += 1;
  258. m_Count += 1;
  259. }
  260. else
  261. {
  262. index = (index + 1) % m_RolloverSize;
  263. m_Count = m_Count < m_RolloverSize ? m_Count + 1 : m_RolloverSize;
  264. }
  265. }
  266. public T Pop()
  267. {
  268. if (index == 0 && m_RolloverSize == 0)
  269. return default;
  270. if (m_RolloverSize == 0)
  271. index -= 1;
  272. else
  273. {
  274. index = (index - 1) % m_RolloverSize;
  275. index = index < 0 ? index + m_RolloverSize : index;
  276. }
  277. T item = itemStack[index];
  278. itemStack[index] = m_DefaultItem;
  279. m_Count = m_Count > 0 ? m_Count - 1 : 0;
  280. return item;
  281. }
  282. /// <summary>
  283. ///
  284. /// </summary>
  285. /// <returns></returns>
  286. public T Peek()
  287. {
  288. if (index == 0)
  289. return m_DefaultItem;
  290. return itemStack[index - 1];
  291. }
  292. /// <summary>
  293. /// Function to retrieve the current item from the stack.
  294. /// </summary>
  295. /// <returns>itemStack <T></returns>
  296. public T CurrentItem()
  297. {
  298. if (index > 0)
  299. return itemStack[index - 1];
  300. return itemStack[0];
  301. }
  302. /// <summary>
  303. /// Function to retrieve the previous item without affecting the stack.
  304. /// </summary>
  305. /// <returns></returns>
  306. public T PreviousItem()
  307. {
  308. if (index > 1)
  309. return itemStack[index - 2];
  310. return itemStack[0];
  311. }
  312. }
  313. }