HyphenationJP.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using UnityEngine.EventSystems;
  6. namespace UnityEngine.UI
  7. {
  8. [ExecuteInEditMode]
  9. public class HyphenationJP : UIBehaviour
  10. {
  11. private string text
  12. {
  13. get
  14. {
  15. return this._Text.text;
  16. }
  17. }
  18. private RectTransform _RectTransform
  19. {
  20. get
  21. {
  22. if (this._rectTransform == null)
  23. {
  24. this._rectTransform = base.GetComponent<RectTransform>();
  25. }
  26. return this._rectTransform;
  27. }
  28. }
  29. private Text _Text
  30. {
  31. get
  32. {
  33. if (this._text == null)
  34. {
  35. this._text = base.GetComponent<Text>();
  36. }
  37. return this._text;
  38. }
  39. }
  40. protected override void OnRectTransformDimensionsChange()
  41. {
  42. base.OnRectTransformDimensionsChange();
  43. if (this._Text == null)
  44. {
  45. return;
  46. }
  47. this.UpdateText(this.text);
  48. }
  49. public void UpdateText()
  50. {
  51. this.UpdateText(this.text);
  52. }
  53. private void UpdateText(string str)
  54. {
  55. this._Text.text = this.GetFormatedText(this._Text, str);
  56. }
  57. private float GetTextWidth(Text textComp, string message)
  58. {
  59. if (this._Text.supportRichText)
  60. {
  61. message = HyphenationJP.REG_RICH_TEXT.Replace(message, string.Empty);
  62. }
  63. textComp.text = message;
  64. return textComp.preferredWidth;
  65. }
  66. private string GetFormatedText(Text textComp, string msg)
  67. {
  68. if (!base.enabled)
  69. {
  70. return msg;
  71. }
  72. if (string.IsNullOrEmpty(msg))
  73. {
  74. return string.Empty;
  75. }
  76. float width = this._RectTransform.rect.width;
  77. float textWidth = this.GetTextWidth(textComp, " ");
  78. textComp.horizontalOverflow = HorizontalWrapMode.Overflow;
  79. StringBuilder stringBuilder = new StringBuilder();
  80. float num = 0f;
  81. foreach (string text in this.GetWordList(msg))
  82. {
  83. num += this.GetTextWidth(textComp, text);
  84. if (HyphenationJP.IsNewLine(text))
  85. {
  86. num = 0f;
  87. }
  88. else
  89. {
  90. if (text == " ")
  91. {
  92. num += textWidth;
  93. }
  94. if (num > width)
  95. {
  96. stringBuilder.Append(Environment.NewLine);
  97. num = this.GetTextWidth(textComp, text);
  98. }
  99. }
  100. stringBuilder.Append(text);
  101. }
  102. return stringBuilder.ToString();
  103. }
  104. private List<string> GetWordList(string tmpText)
  105. {
  106. List<string> list = new List<string>();
  107. StringBuilder stringBuilder = new StringBuilder();
  108. char c = '\0';
  109. bool flag = false;
  110. for (int i = 0; i < tmpText.Length; i++)
  111. {
  112. char c2 = tmpText[i];
  113. char c3 = (i >= tmpText.Length - 1) ? c : tmpText[i + 1];
  114. char c4 = (i <= 0) ? c : tmpText[i - 1];
  115. stringBuilder.Append(c2);
  116. if (c2 == '<')
  117. {
  118. flag = true;
  119. }
  120. else if (c2 == '>')
  121. {
  122. flag = false;
  123. }
  124. if (!flag && (HyphenationJP.IsNewLine(c2.ToString()) || (HyphenationJP.IsLatin(c2) && HyphenationJP.IsLatin(c4) && HyphenationJP.IsLatin(c2) && !HyphenationJP.IsLatin(c4)) || (!HyphenationJP.IsLatin(c2) && HyphenationJP.CHECK_HYP_BACK(c4)) || (!HyphenationJP.IsLatin(c3) && !HyphenationJP.CHECK_HYP_FRONT(c3) && !HyphenationJP.CHECK_HYP_BACK(c2)) || i == tmpText.Length - 1))
  125. {
  126. list.Add(stringBuilder.ToString());
  127. stringBuilder = new StringBuilder();
  128. }
  129. }
  130. return list;
  131. }
  132. public float textWidth
  133. {
  134. get
  135. {
  136. return this._RectTransform.rect.width;
  137. }
  138. set
  139. {
  140. this._RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, value);
  141. }
  142. }
  143. public int fontSize
  144. {
  145. get
  146. {
  147. return this._Text.fontSize;
  148. }
  149. set
  150. {
  151. this._Text.fontSize = value;
  152. }
  153. }
  154. private static bool CHECK_HYP_FRONT(char c)
  155. {
  156. return HyphenationJP.HYP_FRONT_.Contains(c);
  157. }
  158. private static bool CHECK_HYP_BACK(char c)
  159. {
  160. return HyphenationJP.HYP_BACK_.Contains(c);
  161. }
  162. private static bool IsLatin(char c)
  163. {
  164. return HyphenationJP.HYP_LATIN_.Contains(c);
  165. }
  166. private static bool IsNewLine(string s)
  167. {
  168. return HyphenationJP.REG_NEW_LINE.IsMatch(s);
  169. }
  170. private RectTransform _rectTransform;
  171. private Text _text;
  172. private static readonly string RICH_TEXT_REPLACE = "(<color=.*?>|</color>|<size=.*?>|</size>|<b>|</b>|<i>|</i>|<a href=.*?>|</a>)";
  173. private static readonly HashSet<char> HYP_FRONT_ = new HashSet<char>(",)]}、。)〕〉》」』】〙〗〟’”⦆»ァィゥェォッャュョヮヵヶっぁぃぅぇぉっゃゅょゎ‐゠–〜ー?!!?‼⁇⁈⁉・:;。.".ToCharArray());
  174. private static readonly HashSet<char> HYP_BACK_ = new HashSet<char>("(([{〔〈《「『【〘〖〝‘“⦅«".ToCharArray());
  175. private static readonly HashSet<char> HYP_LATIN_ = new HashSet<char>("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<>=/().,#\"'".ToCharArray());
  176. private static readonly Regex REG_NEW_LINE = new Regex("\r\n|\r|\n");
  177. private static readonly Regex REG_RICH_TEXT = new Regex(HyphenationJP.RICH_TEXT_REPLACE, RegexOptions.IgnoreCase);
  178. }
  179. }