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.

253 lines
6.6 KiB

  1. namespace TMPro
  2. {
  3. /// <summary>
  4. /// Structure used to track basic XML tags which are binary (on / off)
  5. /// </summary>
  6. public struct TMP_BasicXmlTagStack
  7. {
  8. public byte bold;
  9. public byte italic;
  10. public byte underline;
  11. public byte strikethrough;
  12. public byte highlight;
  13. public byte superscript;
  14. public byte subscript;
  15. public byte uppercase;
  16. public byte lowercase;
  17. public byte smallcaps;
  18. /// <summary>
  19. /// Clear the basic XML tag stack.
  20. /// </summary>
  21. public void Clear()
  22. {
  23. bold = 0;
  24. italic = 0;
  25. underline = 0;
  26. strikethrough = 0;
  27. highlight = 0;
  28. superscript = 0;
  29. subscript = 0;
  30. uppercase = 0;
  31. lowercase = 0;
  32. smallcaps = 0;
  33. }
  34. public byte Add(FontStyles style)
  35. {
  36. switch (style)
  37. {
  38. case FontStyles.Bold:
  39. bold += 1;
  40. return bold;
  41. case FontStyles.Italic:
  42. italic += 1;
  43. return italic;
  44. case FontStyles.Underline:
  45. underline += 1;
  46. return underline;
  47. case FontStyles.Strikethrough:
  48. strikethrough += 1;
  49. return strikethrough;
  50. case FontStyles.Superscript:
  51. superscript += 1;
  52. return superscript;
  53. case FontStyles.Subscript:
  54. subscript += 1;
  55. return subscript;
  56. case FontStyles.Highlight:
  57. highlight += 1;
  58. return highlight;
  59. }
  60. return 0;
  61. }
  62. public byte Remove(FontStyles style)
  63. {
  64. switch (style)
  65. {
  66. case FontStyles.Bold:
  67. if (bold > 1)
  68. bold -= 1;
  69. else
  70. bold = 0;
  71. return bold;
  72. case FontStyles.Italic:
  73. if (italic > 1)
  74. italic -= 1;
  75. else
  76. italic = 0;
  77. return italic;
  78. case FontStyles.Underline:
  79. if (underline > 1)
  80. underline -= 1;
  81. else
  82. underline = 0;
  83. return underline;
  84. case FontStyles.Strikethrough:
  85. if (strikethrough > 1)
  86. strikethrough -= 1;
  87. else
  88. strikethrough = 0;
  89. return strikethrough;
  90. case FontStyles.Highlight:
  91. if (highlight > 1)
  92. highlight -= 1;
  93. else
  94. highlight = 0;
  95. return highlight;
  96. case FontStyles.Superscript:
  97. if (superscript > 1)
  98. superscript -= 1;
  99. else
  100. superscript = 0;
  101. return superscript;
  102. case FontStyles.Subscript:
  103. if (subscript > 1)
  104. subscript -= 1;
  105. else
  106. subscript = 0;
  107. return subscript;
  108. }
  109. return 0;
  110. }
  111. }
  112. /// <summary>
  113. /// Structure used to track XML tags of various types.
  114. /// </summary>
  115. /// <typeparam name="T"></typeparam>
  116. public struct TMP_XmlTagStack<T>
  117. {
  118. public T[] itemStack;
  119. public int index;
  120. private int m_capacity;
  121. private T m_defaultItem;
  122. private const int k_defaultCapacity = 4;
  123. //static readonly T[] m_emptyStack = new T[0];
  124. /// <summary>
  125. /// Constructor to create a new item stack.
  126. /// </summary>
  127. /// <param name="tagStack"></param>
  128. public TMP_XmlTagStack(T[] tagStack)
  129. {
  130. itemStack = tagStack;
  131. m_capacity = tagStack.Length;
  132. index = 0;
  133. m_defaultItem = default(T);
  134. }
  135. /// <summary>
  136. /// Function to clear and reset stack to first item.
  137. /// </summary>
  138. public void Clear()
  139. {
  140. index = 0;
  141. }
  142. /// <summary>
  143. /// Function to set the first item on the stack and reset index.
  144. /// </summary>
  145. /// <param name="item"></param>
  146. public void SetDefault(T item)
  147. {
  148. itemStack[0] = item;
  149. index = 1;
  150. }
  151. /// <summary>
  152. /// Function to add a new item to the stack.
  153. /// </summary>
  154. /// <param name="item"></param>
  155. public void Add(T item)
  156. {
  157. if (index < itemStack.Length)
  158. {
  159. itemStack[index] = item;
  160. index += 1;
  161. }
  162. }
  163. /// <summary>
  164. /// Function to retrieve an item from the stack.
  165. /// </summary>
  166. /// <returns></returns>
  167. public T Remove()
  168. {
  169. index -= 1;
  170. if (index <= 0)
  171. {
  172. index = 1;
  173. return itemStack[0];
  174. }
  175. return itemStack[index - 1];
  176. }
  177. public void Push(T item)
  178. {
  179. if (index == m_capacity)
  180. {
  181. m_capacity *= 2;
  182. if (m_capacity == 0)
  183. m_capacity = k_defaultCapacity;
  184. System.Array.Resize(ref itemStack, m_capacity);
  185. }
  186. itemStack[index] = item;
  187. index += 1;
  188. }
  189. public T Pop()
  190. {
  191. if (index == 0)
  192. return default(T);
  193. index -= 1;
  194. T item = itemStack[index];
  195. itemStack[index] = m_defaultItem;
  196. return item;
  197. }
  198. /// <summary>
  199. /// Function to retrieve the current item from the stack.
  200. /// </summary>
  201. /// <returns>itemStack <T></returns>
  202. public T CurrentItem()
  203. {
  204. if (index > 0)
  205. return itemStack[index - 1];
  206. return itemStack[0];
  207. }
  208. /// <summary>
  209. /// Function to retrieve the previous item without affecting the stack.
  210. /// </summary>
  211. /// <returns></returns>
  212. public T PreviousItem()
  213. {
  214. if (index > 1)
  215. return itemStack[index - 2];
  216. return itemStack[0];
  217. }
  218. }
  219. }