TextParser.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. using System.Text;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. // 文本解析
  5. namespace WXB
  6. {
  7. public enum Anchor
  8. {
  9. UpperLeft = 0,
  10. UpperCenter = 1,
  11. UpperRight = 2,
  12. MiddleLeft = 3,
  13. MiddleCenter = 4,
  14. MiddleRight = 5,
  15. LowerLeft = 6,
  16. LowerCenter = 7,
  17. LowerRight = 8,
  18. Null,
  19. }
  20. public partial class TextParser
  21. {
  22. public T CreateNode<T>() where T : NodeBase, new()
  23. {
  24. T t = new T();
  25. t.Reset(mOwner, currentConfig.anchor);
  26. return t;
  27. }
  28. static bool Get(char c, out Anchor a)
  29. {
  30. switch (c)
  31. {
  32. case '1': a = Anchor.MiddleLeft; return true;
  33. case '2': a = Anchor.MiddleCenter; return true;
  34. case '3': a = Anchor.MiddleRight; return true;
  35. }
  36. a = Anchor.MiddleCenter;
  37. return false;
  38. }
  39. static bool Get(char c, out LineAlignment a)
  40. {
  41. switch (c)
  42. {
  43. case '1': a = LineAlignment.Top; return true;
  44. case '2': a = LineAlignment.Center; return true;
  45. case '3': a = LineAlignment.Bottom; return true;
  46. }
  47. a = LineAlignment.Default;
  48. return false;
  49. }
  50. public TextParser()
  51. {
  52. clear();
  53. Reg();
  54. RegTag();
  55. }
  56. Owner mOwner;
  57. static bool ParserInt(ref int d_curPos, string text, ref int value, int num = 3)
  58. {
  59. using (PD<StringBuilder> psb = Pool.GetSB())
  60. {
  61. StringBuilder sb = psb.value;
  62. d_curPos++;
  63. while (text.Length > d_curPos && ((text[d_curPos] >= '0' && text[d_curPos] <= '9')))
  64. {
  65. sb.Append(text[d_curPos]);
  66. d_curPos++;
  67. if (sb.Length >= num)
  68. break;
  69. }
  70. value = Tools.stringToInt(sb.ToString(), -1);
  71. if (sb.Length == 0)
  72. {
  73. d_curPos--;
  74. return false;
  75. }
  76. return true;
  77. }
  78. }
  79. static bool ParserFloat(ref int d_curPos, string text, ref float value, int num = 3)
  80. {
  81. using (PD<StringBuilder> psb = Pool.GetSB())
  82. {
  83. var sb = psb.value;
  84. d_curPos++;
  85. bool bInPoint = false;
  86. while (text.Length > d_curPos && ((text[d_curPos] >= '0' && text[d_curPos] <= '9') || (text[d_curPos] == '.')))
  87. {
  88. if (text[d_curPos] == '.')
  89. bInPoint = true;
  90. sb.Append(text[d_curPos]);
  91. d_curPos++;
  92. int size = (bInPoint == true ? (num + 1) : num);
  93. if (sb.Length >= size)
  94. break;
  95. }
  96. value = Tools.stringToFloat(sb.ToString(), 0);
  97. if (sb.Length == 0)
  98. {
  99. d_curPos--;
  100. return false;
  101. }
  102. return true;
  103. }
  104. }
  105. public struct Config
  106. {
  107. public Anchor anchor;
  108. public Font font;
  109. public FontStyle fontStyle;
  110. public int fontSize;
  111. public Color fontColor;
  112. public bool isUnderline;
  113. public bool isStrickout;
  114. public bool isBlink;
  115. public bool isDyncUnderline; // 动态下划线
  116. public bool isDyncStrickout; // 动态删除线
  117. public int dyncSpeed;
  118. public bool isOffset;
  119. public Rect offsetRect;
  120. public EffectType effectType;
  121. public Color effectColor;
  122. public Vector2 effectDistance;
  123. public LineAlignment lineAlignment;
  124. public int nextLineX; // 下一行的起始偏移量
  125. public void Clear()
  126. {
  127. anchor = Anchor.Null;
  128. font = null;
  129. fontStyle = FontStyle.Normal;
  130. fontSize = 0;
  131. fontColor = Color.white;
  132. isUnderline = false;
  133. isStrickout = false;
  134. isBlink = false;
  135. isDyncUnderline = false;
  136. isDyncStrickout = false;
  137. dyncSpeed = 0;
  138. isOffset = false;
  139. offsetRect.Set(0, 0, 0, 0);
  140. effectType = EffectType.Null;
  141. effectColor = Color.black;
  142. effectDistance = Vector2.zero;
  143. lineAlignment = LineAlignment.Default;
  144. nextLineX = 0;
  145. }
  146. public void Set(Config c)
  147. {
  148. anchor = c.anchor;
  149. font = c.font;
  150. fontStyle = c.fontStyle;
  151. fontSize = c.fontSize;
  152. fontColor = c.fontColor;
  153. isUnderline = c.isUnderline;
  154. isStrickout = c.isStrickout;
  155. isBlink = c.isBlink;
  156. dyncSpeed = c.dyncSpeed;
  157. isOffset = c.isOffset;
  158. offsetRect = c.offsetRect;
  159. effectType = c.effectType;
  160. effectColor = c.effectColor;
  161. effectDistance = c.effectDistance;
  162. isDyncUnderline = c.isDyncUnderline;
  163. isDyncStrickout = c.isDyncStrickout;
  164. lineAlignment = c.lineAlignment;
  165. nextLineX = c.nextLineX;
  166. }
  167. public bool isSame(Config c)
  168. {
  169. return anchor == c.anchor &&
  170. font == c.font &&
  171. fontStyle == c.fontStyle &&
  172. isUnderline == c.isUnderline &&
  173. fontColor == c.fontColor &&
  174. isStrickout == c.isStrickout &&
  175. isBlink == c.isBlink &&
  176. fontSize == c.fontSize &&
  177. lineAlignment == c.lineAlignment &&
  178. isDyncUnderline == c.isDyncUnderline &&
  179. isDyncStrickout == c.isDyncStrickout &&
  180. nextLineX == c.nextLineX &&
  181. dyncSpeed == c.dyncSpeed &&
  182. (
  183. (effectType == EffectType.Null && c.effectType == EffectType.Null) ||
  184. (effectType == c.effectType && effectColor == c.effectColor && effectDistance == c.effectDistance)
  185. ) &&
  186. (
  187. (isOffset == false && c.isOffset == false) ||
  188. (isOffset == c.isOffset && offsetRect == c.offsetRect)
  189. );
  190. }
  191. }
  192. System.Func<TagAttributes, IExternalNode> getExternalNode = null;
  193. public void parser(Owner owner, string text, Config config, List<NodeBase> vList, System.Func<TagAttributes, IExternalNode> getExternalNode)
  194. {
  195. clear();
  196. mOwner = owner;
  197. this.getExternalNode = getExternalNode;
  198. d_nodeList = vList;
  199. startConfig.Set(config);
  200. currentConfig.Set(config);
  201. if (currentConfig.font == null)
  202. {
  203. Debug.LogError("TextParser pFont == null");
  204. return;
  205. }
  206. if (string.IsNullOrEmpty(text))
  207. {
  208. return;
  209. }
  210. int lenght = text.Length;
  211. while (lenght > d_curPos)
  212. {
  213. if (d_bBegin == false)
  214. {
  215. switch (text[d_curPos])
  216. {
  217. case '#':
  218. {
  219. // 未遇到功能字符,开始功能字符的解析
  220. d_bBegin = true;
  221. ++d_curPos;
  222. }
  223. break;
  224. /*case '<':
  225. {
  226. int endpos = text.IndexOf('>', d_curPos);
  227. if (endpos != -1)
  228. {
  229. string tag = null;
  230. string param = null;
  231. int tagend = text.IndexOfAny(new char[] { ' ', '=' }, d_curPos);
  232. if (tagend != -1 && tagend < endpos)
  233. {
  234. tag = text.Substring(d_curPos + 1, tagend - d_curPos);
  235. param = text.Substring(tagend + 1, endpos - tagend - 1);
  236. }
  237. else
  238. {
  239. tag = text.Substring(d_curPos + 1, endpos - d_curPos - 1);
  240. }
  241. if (d_text.Length != 0)
  242. save(false);
  243. TagParam(tag, param);
  244. d_curPos = endpos + 1;
  245. break;
  246. }
  247. else
  248. {
  249. d_text.Append(text[d_curPos]);
  250. }
  251. ++d_curPos;
  252. }
  253. break;*/
  254. case '[':
  255. {
  256. int endpos = text.IndexOf(']', d_curPos);
  257. if (endpos != -1)
  258. {
  259. string tag = null;
  260. string param = null;
  261. int tagend = text.IndexOf("[e", d_curPos);
  262. if (tagend != -1 && tagend < endpos)
  263. {
  264. tag = text.Substring(d_curPos + 1, tagend + 1 - d_curPos);
  265. param = text.Substring(tagend + 2, endpos - tagend - 2);
  266. if (!System.Text.RegularExpressions.Regex.IsMatch(param, "^[0-9]+$"))
  267. {
  268. d_text.Append(text[d_curPos]);
  269. ++d_curPos;
  270. break;
  271. }
  272. }
  273. else
  274. {
  275. tag = text.Substring(d_curPos + 1, endpos - d_curPos - 1);
  276. }
  277. if (d_text.Length != 0)
  278. save(false);
  279. TagParam(tag, param);
  280. d_curPos = endpos + 1;
  281. break;
  282. }
  283. else
  284. {
  285. d_text.Append(text[d_curPos]);
  286. }
  287. ++d_curPos;
  288. }
  289. break;
  290. /* case '\n':
  291. {
  292. // 这个是换行
  293. save(true);
  294. d_curPos++;
  295. }
  296. break;*/
  297. default:
  298. {
  299. d_text.Append(text[d_curPos]);
  300. ++d_curPos;
  301. }
  302. break;
  303. }
  304. }
  305. else
  306. {
  307. char c = text[d_curPos];
  308. OnFun fun = null;
  309. if (c < 128 && ((fun = OnFuns[c]) != null))
  310. {
  311. fun(text);
  312. }
  313. else
  314. {
  315. d_text.Append(text[d_curPos]);
  316. ++d_curPos;
  317. }
  318. d_bBegin = false;
  319. }
  320. }
  321. if (d_text.Length != 0)
  322. save(false);
  323. clear();
  324. }
  325. protected void save(bool isNewLine)
  326. {
  327. if (d_text.Length == 0)
  328. {
  329. if (isNewLine == true)
  330. {
  331. if (d_nodeList.Count != 0)
  332. {
  333. NodeBase node = d_nodeList.back();
  334. if (node.isNewLine() == false)
  335. {
  336. node.setNewLine(true);
  337. return;
  338. }
  339. }
  340. // 添加一个换行的结点
  341. LineNode nodeY = CreateNode<LineNode>();
  342. nodeY.SetConfig(currentConfig);
  343. nodeY.font = currentConfig.font;
  344. nodeY.fontSize = currentConfig.fontSize;
  345. nodeY.fs = currentConfig.fontStyle;
  346. nodeY.setNewLine(true);
  347. d_nodeList.Add(nodeY);
  348. return;
  349. }
  350. else
  351. {
  352. return;
  353. }
  354. }
  355. // 为文本
  356. TextNode textNode = CreateNode<TextNode>();
  357. {
  358. textNode.d_text = d_text.ToString();
  359. textNode.SetConfig(currentConfig);
  360. }
  361. textNode.setNewLine(isNewLine);
  362. d_nodeList.Add(textNode);
  363. d_text.Remove(0, d_text.Length);
  364. }
  365. protected void saveX(float value)
  366. {
  367. XSpaceNode node = CreateNode<XSpaceNode>();
  368. node.d_offset = value;
  369. d_nodeList.Add(node);
  370. }
  371. protected void saveY(float value)
  372. {
  373. if (d_nodeList.Count != 0 && d_nodeList.back().isNewLine() == false)
  374. {
  375. d_nodeList.back().setNewLine(true);
  376. }
  377. YSpaceNode node = CreateNode<YSpaceNode>();
  378. node.d_offset = value;
  379. node.setNewLine(true);
  380. d_nodeList.Add(node);
  381. }
  382. protected void saveZ(float value)
  383. {
  384. YSpaceNode node = CreateNode<YSpaceNode>();
  385. node.d_offset = value;
  386. node.setNewLine(false);
  387. d_nodeList.Add(node);
  388. }
  389. protected void saveHy()
  390. {
  391. if (d_text.Length == 0)
  392. return;
  393. string text = d_text.ToString();
  394. d_text.Remove(0, d_text.Length);
  395. HyperlinkNode node = CreateNode<HyperlinkNode>();
  396. string hytext = string.Empty;
  397. if (text[text.Length - 1] == '}')
  398. {
  399. int beginPos = text.IndexOf('{', 0);
  400. if (beginPos != -1)
  401. {
  402. hytext = text.Substring(beginPos, text.Length - beginPos);
  403. node.d_link = hytext.Replace("{", "").Replace("}", "");
  404. text = text.Remove(beginPos, text.Length - beginPos);
  405. }
  406. }
  407. node.d_text = "";
  408. node.SetConfig(currentConfig);
  409. ParseHyText(text, node);
  410. d_nodeList.Add(node);
  411. }
  412. protected void clear()
  413. {
  414. getExternalNode = null;
  415. startConfig.Clear();
  416. currentConfig.Clear();
  417. d_nodeList = null;
  418. d_curPos = 0;
  419. d_text.Clear();
  420. d_bBegin = false;
  421. mOwner = null;
  422. }
  423. protected int d_curPos = 0;
  424. protected Config startConfig;
  425. protected Config currentConfig;
  426. protected List<NodeBase> d_nodeList;
  427. protected StringBuilder d_text = new StringBuilder();
  428. protected bool d_bBegin;
  429. static Color GetColour(uint code)
  430. {
  431. switch (code)
  432. {
  433. case 'R': return Color.red;
  434. case 'G': return Color.green;
  435. case 'B': return Color.blue;
  436. case 'K': return Color.black;
  437. case 'Y': return Color.yellow;
  438. case 'W': return Color.white;
  439. }
  440. return Color.white;
  441. }
  442. }
  443. }