ElementSegment.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. namespace WXB
  5. {
  6. // 一句语句,可能会分多行显示,此接口为分割字符串的接口,把希望在同一行显示的部分组合成一个Element
  7. public interface ElementSegment
  8. {
  9. void Segment(string text, List<NodeBase.Element> widths, Func<char, float> fontwidth);
  10. }
  11. // 默认的单词分割算法
  12. internal class DefaultES : ElementSegment
  13. {
  14. enum CharType
  15. {
  16. Null,
  17. English, // 英文单词
  18. Chinese, // 中文符号
  19. Punctuation, // 标点符号
  20. }
  21. static CharType GetCharType(char c)
  22. {
  23. switch (c)
  24. {
  25. case ',':
  26. case ' ':
  27. case '.':
  28. case ':':
  29. case ';':
  30. case '?':
  31. case ';':
  32. case '。':
  33. case '"':
  34. case '“':
  35. case '”':
  36. case ',':
  37. return CharType.Punctuation;
  38. }
  39. if (c > 128)
  40. {
  41. return CharType.Chinese; // 中文都算是隔断符号
  42. }
  43. return CharType.English;
  44. }
  45. static NodeBase.Element Create(StringBuilder sb, Func<char, float> fontwidth)
  46. {
  47. if (sb.Length == 1)
  48. {
  49. var e = new NodeBase.Element(fontwidth(sb[0]));
  50. #if UNITY_EDITOR
  51. e.text = sb.ToString();
  52. #endif
  53. sb.Length = 0;
  54. return e;
  55. }
  56. else
  57. {
  58. List<float> widths = new List<float>();
  59. for (int i = 0; i < sb.Length; ++i)
  60. widths.Add(fontwidth(sb[i]));
  61. var e = new NodeBase.Element(widths);
  62. #if UNITY_EDITOR
  63. e.text = sb.ToString();
  64. #endif
  65. sb.Length = 0;
  66. return e;
  67. }
  68. }
  69. public void Segment(string text, List<NodeBase.Element> widths, Func<char, float> fontwidth)
  70. {
  71. // 判断标准,如果全英文的,那么都尽量在同一行显示
  72. using (PD<StringBuilder> psb = Pool.GetSB())
  73. {
  74. StringBuilder sb = psb.value;
  75. char current;
  76. CharType lasttype = CharType.Null;
  77. for (int i = 0; i < text.Length; ++i)
  78. {
  79. current = text[i];
  80. CharType ct = GetCharType(current);
  81. switch (ct)
  82. {
  83. case CharType.English:
  84. {
  85. switch (lasttype)
  86. {
  87. case CharType.Chinese: // 中文接英文,那么先把中文的保存起来
  88. widths.Add(Create(sb, fontwidth));
  89. break;
  90. case CharType.Punctuation: // 英文接标点符号的
  91. widths.Add(Create(sb, fontwidth));
  92. break;
  93. case CharType.English:
  94. break;
  95. }
  96. sb.Append(current);
  97. }
  98. break;
  99. case CharType.Chinese:
  100. {
  101. switch (lasttype)
  102. {
  103. case CharType.Chinese: // 中文接英文,那么先把中文的保存起来
  104. widths.Add(Create(sb, fontwidth));
  105. break;
  106. case CharType.Punctuation:
  107. case CharType.English:
  108. break;
  109. }
  110. sb.Append(current);
  111. }
  112. break;
  113. case CharType.Punctuation: // 标点符号
  114. {
  115. sb.Append(current);
  116. widths.Add(Create(sb, fontwidth));
  117. }
  118. break;
  119. }
  120. lasttype = ct;
  121. }
  122. if (sb.Length != 0)
  123. widths.Add(Create(sb, fontwidth));
  124. }
  125. }
  126. }
  127. public class ESFactory
  128. {
  129. // 类型列表
  130. static Dictionary<string, ElementSegment> TypeList = new Dictionary<string, ElementSegment>();
  131. static ESFactory()
  132. {
  133. TypeList.Add("Default", new DefaultES());
  134. }
  135. static public void Add(string name, ElementSegment es)
  136. {
  137. TypeList.Add(name, es);
  138. }
  139. static public bool Remove(string name)
  140. {
  141. return TypeList.Remove(name);
  142. }
  143. public static ElementSegment Get(string name)
  144. {
  145. ElementSegment es = null;
  146. if (TypeList.TryGetValue(name, out es))
  147. return es;
  148. return null;
  149. }
  150. public static List<string> GetAllName()
  151. {
  152. List<string> names = new List<string>();
  153. foreach (var itor in TypeList)
  154. names.Add(itor.Key);
  155. return names;
  156. }
  157. }
  158. }