RTF_TextUtils.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. using System;
  2. using System.Collections.Generic;
  3. //using System.Linq;
  4. using System.Text;
  5. namespace FastReport.RichTextParser
  6. {
  7. class RTF_TextUtils
  8. {
  9. #if false
  10. public override int PageRTF_Find(string FindWhat)
  11. {
  12. #if false
  13. int line, column;
  14. RTF_View.CommonViewObject paragraph;
  15. int absolute_position = rtf_text.FindString(FindWhat, out paragraph, out line, out column);
  16. return line;
  17. #else
  18. throw new NotImplementedException("Find string disabled at this time");
  19. #endif
  20. }
  21. public override int PageRTF_ReplaceAll(string ReplaceWhat, string ReplaceWith)
  22. {
  23. int replace_count = rtf_text.ReplaceAll(ReplaceWhat, ReplaceWith);
  24. if (replace_count != 0)
  25. {
  26. int height = rtf_text.InvalidateView(ClientRectangle);
  27. // RefreshScrollBar(height);
  28. Refresh();
  29. }
  30. return replace_count;
  31. }
  32. internal int PageRTF_FindString(string findWhat, out CommonViewObject paragraph, out int line, out int position)
  33. {
  34. int abs_position = -1;
  35. int prev_size = 0;
  36. paragraph = null;
  37. line = 0;
  38. position = 0;
  39. foreach (CommonViewObject par in view_objects)
  40. {
  41. abs_position = par.FindString(findWhat, out line, out position);
  42. if (abs_position >= 0)
  43. {
  44. paragraph = par;
  45. break;
  46. }
  47. prev_size += par.Length;
  48. }
  49. return prev_size + abs_position;
  50. }
  51. internal int ReplaceAll(string replaceWhat, string replaceWith)
  52. {
  53. throw new NotImplementedException();
  54. }
  55. internal override void Row_ReplaceText(int start, int len, string value)
  56. {
  57. int cells_length = 0;
  58. int prev_length = 0;
  59. foreach (RTF_CommonRichElement item in cells)
  60. {
  61. prev_length = cells_length;
  62. cells_length += item.Lenght;
  63. if (cells_length < start)
  64. continue;
  65. item.ReplaceText(start - prev_length, len, value);
  66. break;
  67. }
  68. }
  69. internal int Run_ReplaceAll(string findWhat, string replaceWith)
  70. {
  71. if (run.text.Contains(findWhat))
  72. {
  73. run.text = run.text.Replace(findWhat, replaceWith);
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. internal override void Paragraph_ReplaceText(int start, int len, string value)
  79. {
  80. foreach (RTF_Run run in runs)
  81. {
  82. if (run.text.Length <= start)
  83. {
  84. start -= run.text.Length == 0 ? 1 : run.text.Length;
  85. continue;
  86. }
  87. Lenght -= run.text.Length;
  88. string s = run.text.Remove(start, len);
  89. run.text = s.Insert(start, value);
  90. Lenght += run.text.Length;
  91. break;
  92. }
  93. }
  94. /// BODY
  95. int FSelectionStart;
  96. int FSelectionLength;
  97. /// <summary>
  98. ///
  99. /// </summary>
  100. public int SelectionStart { get { return FSelectionStart; } internal set { FSelectionStart = value; } }
  101. /// <summary>
  102. ///
  103. /// </summary>
  104. public int SelectionLength { get { return FSelectionLength; } internal set { FSelectionLength = value; } }
  105. /// <summary>
  106. ///
  107. /// </summary>
  108. public string SelectedText
  109. {
  110. get
  111. {
  112. return Text.Substring(FSelectionStart, FSelectionLength);
  113. }
  114. internal set
  115. {
  116. document.ReplaceText(FSelectionStart, FSelectionLength, value);
  117. }
  118. }
  119. //internal void Body_ReplaceText(int fSelectionStart, int fSelectionLength, string value)
  120. //{
  121. // int par_len, start, len;
  122. // foreach (RTF_Page item in pages)
  123. // {
  124. // par_len = item.Lenght;
  125. // if (par_len <= fSelectionStart)
  126. // {
  127. // // TDOD: check EndOfLine sequence - seems to be platfom dependent LF of CR/LF
  128. // fSelectionStart -= par_len + 2;
  129. // continue;
  130. // }
  131. // start = fSelectionStart;
  132. // len = (par_len - start < fSelectionLength) ? par_len - start : fSelectionLength;
  133. // item.ReplaceText(start, len, value);
  134. // break;
  135. // }
  136. //}
  137. internal string Body_Text2HTML
  138. {
  139. get
  140. {
  141. StringBuilder str = new StringBuilder();
  142. foreach (RTF_Page item in body.pages)
  143. str.AppendLine(item.Text2HTML);
  144. return str.ToString();
  145. }
  146. }
  147. internal string Body_Text
  148. {
  149. get
  150. {
  151. StringBuilder str = new StringBuilder();
  152. foreach (RTF_Page item in body.pages)
  153. str.AppendLine(item.Text);
  154. return str.ToString();
  155. }
  156. }
  157. public int Document_ReplaceAll(string findWhat, string replaceWith)
  158. {
  159. int replacement_counter = 0;
  160. if (this.document.Pages != null)
  161. foreach (RTF_Page page in this.document.Pages)
  162. {
  163. replacement_counter += page.ReplaceAll(findWhat, replaceWith);
  164. }
  165. return replacement_counter;
  166. }
  167. internal void Page_ReplaceText(int fSelectionStart, int fSelectionLength, string value)
  168. {
  169. int par_len, start, len;
  170. foreach (RTF_CommonRichElement item in paragraphs)
  171. {
  172. par_len = item.Lenght;
  173. if (par_len <= fSelectionStart)
  174. {
  175. // TDOD: check EndOfLine sequence - seems to be platfom dependent LF of CR/LF
  176. fSelectionStart -= par_len + 2;
  177. continue;
  178. }
  179. start = fSelectionStart;
  180. len = (par_len - start < fSelectionLength) ? par_len - start : fSelectionLength;
  181. item.ReplaceText(start, len, value);
  182. break;
  183. }
  184. }
  185. internal int Page_ReplaceAll(string findWhat, string replaceWith)
  186. {
  187. int replacement_counter = 0;
  188. //foreach (RTF_CommonRichElement par in this.ParagraphsAndTables)
  189. //{
  190. // replacement_counter += par.RepaceAll(findWhat, replaceWith);
  191. //}
  192. return replacement_counter;
  193. }
  194. internal string Page_Text2HTML
  195. {
  196. get
  197. {
  198. StringBuilder str = new StringBuilder();
  199. foreach (RTF_CommonRichElement item in page.paragraphs)
  200. {
  201. str.AppendLine(item.GetHTMLText(document));
  202. }
  203. return str.ToString();
  204. }
  205. }
  206. internal string Page_Text
  207. {
  208. get
  209. {
  210. StringBuilder str = new StringBuilder();
  211. foreach (RichObject item in page.objects)
  212. {
  213. //str.AppendLine(item.GetText());
  214. }
  215. return str.ToString();
  216. }
  217. }
  218. internal override string Table_GetText()
  219. {
  220. StringBuilder sb = new StringBuilder("TODO: Text of raw cells");
  221. //foreach (RTF_CommonRichElement item in some_row.cells)
  222. //{
  223. // sb.AppendFormat("{0}\t", item.GetText());
  224. //}
  225. return sb.ToString();
  226. }
  227. internal override string Paragraph_GetText()
  228. {
  229. StringBuilder sb = new StringBuilder();
  230. foreach (Run run in Runs)
  231. {
  232. if (run.text.Length == 0)
  233. sb.Append('\t');
  234. else
  235. sb.Append(run.text);
  236. }
  237. return sb.ToString();
  238. }
  239. internal override string Picture_GetText()
  240. {
  241. return string.Empty;
  242. }
  243. internal override string TableGetHTMLText(RTF_DocumentParser doc)
  244. {
  245. throw new NotImplementedException("RTF Table to HTML");
  246. }
  247. internal override string Paragraph_GetHTMLText(RTF_DocumentParser doc)
  248. {
  249. StringBuilder sb = new StringBuilder();
  250. RunFormat prev_format = new RunFormat();
  251. foreach (Run run in Runs)
  252. {
  253. if (run.text.Length == 0)
  254. {
  255. // sb.Append('\t');
  256. sb.Append("&nbsp;&nbsp;&nbsp;&nbsp;");
  257. }
  258. else
  259. {
  260. if (run.format.IsSameAs(prev_format))
  261. sb.Append(run.text);
  262. else
  263. {
  264. if (run.format.bold != prev_format.bold)
  265. {
  266. sb.Append(run.format.bold ? "<b>" : "</b>");
  267. prev_format.bold = run.format.bold;
  268. }
  269. if (run.format.italic != prev_format.italic)
  270. {
  271. sb.Append(run.format.italic ? "<i>" : "</i>");
  272. prev_format.italic = run.format.italic;
  273. }
  274. if (run.format.underline != prev_format.underline)
  275. {
  276. sb.Append(run.format.underline ? "<u>" : "</u>");
  277. prev_format.underline = run.format.underline;
  278. }
  279. if(run.format.script_type != prev_format.script_type)
  280. {
  281. if (run.format.script_type == RunFormat.ScriptType.Subscript)
  282. sb.Append("<sub>");
  283. else if (run.format.script_type == RunFormat.ScriptType.Superscript)
  284. sb.Append("<sup>");
  285. else if(prev_format.script_type == RunFormat.ScriptType.Subscript)
  286. sb.Append("</sub>");
  287. else if (prev_format.script_type == RunFormat.ScriptType.Superscript)
  288. sb.Append("</sup>");
  289. prev_format.script_type = run.format.script_type;
  290. }
  291. if (run.format.color_idx != prev_format.color_idx)
  292. {
  293. RColor col = doc.ColorList[(int)run.format.color_idx];
  294. string colorname = string.Format("\"#{0:X2}{1:X2}{2:X2}\"", col.red, col.green, col.blue);
  295. sb.Append(run.format.color_idx == 0 ? "</font>" : "<font color="+colorname+">");
  296. prev_format.color_idx = run.format.color_idx;
  297. }
  298. //if(run.format.font_size != prev_format.font_size)
  299. //{
  300. // int fs = run.format.font_size / 2;
  301. // sb.Append("<font size=\"" + fs +"\">");
  302. // prev_format.font_size = run.format.font_size;
  303. //}
  304. sb.Append(run.text);
  305. }
  306. }
  307. }
  308. // Clean collected tags
  309. if (prev_format.bold)
  310. sb.Append("</b>");
  311. if (prev_format.italic)
  312. sb.Append("</i>");
  313. if (prev_format.underline)
  314. sb.Append("</u>");
  315. return sb.ToString();
  316. }
  317. internal override int Paragraph_RepaceAll(string findWhat, string replaceWith)
  318. {
  319. int replacement_counter = 0;
  320. foreach (Run run in Runs)
  321. {
  322. replacement_counter += run.ReplaceAll(findWhat, replaceWith);
  323. }
  324. return replacement_counter;
  325. }
  326. internal override int Table_RepaceAll(string findWhat, string replaceWith)
  327. {
  328. throw new NotImplementedException();
  329. }
  330. #endif
  331. }
  332. }