No Description
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.

TMP_TextProcessingCommon.cs 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System.Diagnostics;
  2. using UnityEngine;
  3. namespace TMPro
  4. {
  5. internal enum TextProcessingElementType
  6. {
  7. Undefined = 0x0,
  8. TextCharacterElement = 0x1,
  9. TextMarkupElement = 0x2
  10. }
  11. internal struct CharacterElement
  12. {
  13. public uint Unicode
  14. {
  15. get { return m_Unicode; }
  16. set { m_Unicode = value; }
  17. }
  18. public CharacterElement(TMP_TextElement textElement)
  19. {
  20. m_Unicode = textElement.unicode;
  21. m_TextElement = textElement;
  22. }
  23. // =============================================
  24. // Private backing fields for public properties.
  25. // =============================================
  26. uint m_Unicode;
  27. TMP_TextElement m_TextElement;
  28. }
  29. internal struct MarkupAttribute
  30. {
  31. /// <summary>
  32. /// The hash code of the name of the Markup attribute.
  33. /// </summary>
  34. public int NameHashCode
  35. {
  36. get { return m_NameHashCode; }
  37. set { m_NameHashCode = value; }
  38. }
  39. /// <summary>
  40. /// The hash code of the value of the Markup attribute.
  41. /// </summary>
  42. public int ValueHashCode
  43. {
  44. get { return m_ValueHashCode; }
  45. set { m_ValueHashCode = value; }
  46. }
  47. /// <summary>
  48. /// The index of the value of the Markup attribute in the text backing buffer.
  49. /// </summary>
  50. public int ValueStartIndex
  51. {
  52. get { return m_ValueStartIndex; }
  53. set { m_ValueStartIndex = value; }
  54. }
  55. /// <summary>
  56. /// The length of the value of the Markup attribute in the text backing buffer.
  57. /// </summary>
  58. public int ValueLength
  59. {
  60. get { return m_ValueLength; }
  61. set { m_ValueLength = value; }
  62. }
  63. // =============================================
  64. // Private backing fields for public properties.
  65. // =============================================
  66. int m_NameHashCode;
  67. int m_ValueHashCode;
  68. int m_ValueStartIndex;
  69. int m_ValueLength;
  70. }
  71. internal struct MarkupElement
  72. {
  73. /// <summary>
  74. /// The hash code of the name of the markup element.
  75. /// </summary>
  76. public int NameHashCode
  77. {
  78. get
  79. {
  80. return m_Attributes == null ? 0 : m_Attributes[0].NameHashCode;
  81. }
  82. set
  83. {
  84. if (m_Attributes == null)
  85. m_Attributes = new MarkupAttribute[8];
  86. m_Attributes[0].NameHashCode = value;
  87. }
  88. }
  89. /// <summary>
  90. /// The hash code of the value of the markup element.
  91. /// </summary>
  92. public int ValueHashCode
  93. {
  94. get { return m_Attributes == null ? 0 : m_Attributes[0].ValueHashCode; }
  95. set { m_Attributes[0].ValueHashCode = value; }
  96. }
  97. /// <summary>
  98. /// The index of the value of the markup element in the text backing buffer.
  99. /// </summary>
  100. public int ValueStartIndex
  101. {
  102. get { return m_Attributes == null ? 0 : m_Attributes[0].ValueStartIndex; }
  103. set { m_Attributes[0].ValueStartIndex = value; }
  104. }
  105. /// <summary>
  106. /// The length of the value of the markup element in the text backing buffer.
  107. /// </summary>
  108. public int ValueLength
  109. {
  110. get { return m_Attributes == null ? 0 : m_Attributes[0].ValueLength; }
  111. set { m_Attributes[0].ValueLength = value; }
  112. }
  113. /// <summary>
  114. ///
  115. /// </summary>
  116. public MarkupAttribute[] Attributes
  117. {
  118. get { return m_Attributes; }
  119. set { m_Attributes = value; }
  120. }
  121. /// <summary>
  122. /// Constructor for a new Markup Element
  123. /// </summary>
  124. /// <param name="nameHashCode"></param>
  125. public MarkupElement(int nameHashCode, int startIndex, int length)
  126. {
  127. m_Attributes = new MarkupAttribute[8];
  128. m_Attributes[0].NameHashCode = nameHashCode;
  129. m_Attributes[0].ValueStartIndex = startIndex;
  130. m_Attributes[0].ValueLength = length;
  131. }
  132. // =============================================
  133. // Private backing fields for public properties.
  134. // =============================================
  135. private MarkupAttribute[] m_Attributes;
  136. }
  137. [DebuggerDisplay("{DebuggerDisplay()}")]
  138. internal struct TextProcessingElement
  139. {
  140. public TextProcessingElementType ElementType
  141. {
  142. get { return m_ElementType; }
  143. set { m_ElementType = value; }
  144. }
  145. public int StartIndex
  146. {
  147. get { return m_StartIndex; }
  148. set { m_StartIndex = value; }
  149. }
  150. public int Length
  151. {
  152. get { return m_Length; }
  153. set { m_Length = value; }
  154. }
  155. public CharacterElement CharacterElement
  156. {
  157. get { return m_CharacterElement; }
  158. }
  159. public MarkupElement MarkupElement
  160. {
  161. get { return m_MarkupElement; }
  162. set { m_MarkupElement = value; }
  163. }
  164. public TextProcessingElement(TextProcessingElementType elementType, int startIndex, int length)
  165. {
  166. m_ElementType = elementType;
  167. m_StartIndex = startIndex;
  168. m_Length = length;
  169. m_CharacterElement = new CharacterElement();
  170. m_MarkupElement = new MarkupElement();
  171. }
  172. public TextProcessingElement(TMP_TextElement textElement, int startIndex, int length)
  173. {
  174. m_ElementType = TextProcessingElementType.TextCharacterElement;
  175. m_StartIndex = startIndex;
  176. m_Length = length;
  177. m_CharacterElement = new CharacterElement(textElement);
  178. m_MarkupElement = new MarkupElement();
  179. }
  180. public TextProcessingElement(CharacterElement characterElement, int startIndex, int length)
  181. {
  182. m_ElementType = TextProcessingElementType.TextCharacterElement;
  183. m_StartIndex = startIndex;
  184. m_Length = length;
  185. m_CharacterElement = characterElement;
  186. m_MarkupElement = new MarkupElement();
  187. }
  188. public TextProcessingElement(MarkupElement markupElement)
  189. {
  190. m_ElementType = TextProcessingElementType.TextMarkupElement;
  191. m_StartIndex = markupElement.ValueStartIndex;
  192. m_Length = markupElement.ValueLength;
  193. m_CharacterElement = new CharacterElement();
  194. m_MarkupElement = markupElement;
  195. }
  196. public static TextProcessingElement Undefined => new TextProcessingElement() { ElementType = TextProcessingElementType.Undefined };
  197. private string DebuggerDisplay()
  198. {
  199. return m_ElementType == TextProcessingElementType.TextCharacterElement ? $"Unicode ({m_CharacterElement.Unicode}) '{(char)m_CharacterElement.Unicode}' " : $"Markup = {(MarkupTag)m_MarkupElement.NameHashCode}";
  200. }
  201. // =============================================
  202. // Private backing fields for public properties.
  203. // =============================================
  204. TextProcessingElementType m_ElementType;
  205. int m_StartIndex;
  206. int m_Length;
  207. CharacterElement m_CharacterElement;
  208. MarkupElement m_MarkupElement;
  209. }
  210. }