ParagraphToHtml.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System.Drawing;
  2. using System.Text;
  3. using FastReport;
  4. using FastReport.RichTextParser;
  5. /*
  6. * This code translates an internal representation of
  7. * rich paragraph to HTML text
  8. *
  9. */
  10. namespace FastReport.Design.ImportPlugins.RTF
  11. {
  12. internal partial class ImportRtf
  13. {
  14. private string GenerateHTMLText(TextObject memo, Paragraph paragraph)
  15. {
  16. memo.RightToLeft = paragraph.format.text_direction == FastReport.RichTextParser.ParagraphFormat.Direction.RighgToLeft;
  17. //memo.
  18. int run_num = 0;
  19. bool span_condition = false;
  20. StringBuilder sb = new StringBuilder();
  21. RunFormat format;
  22. string colorname = string.Empty;
  23. string fontname = string.Empty;
  24. string fontsize = string.Empty;
  25. Font current_font = memo.Font;
  26. int len;
  27. foreach (Run run in paragraph.runs)
  28. {
  29. switch (run.text)
  30. {
  31. case "\r":
  32. len = 1;
  33. break;
  34. case "\t":
  35. break;
  36. default:
  37. len = run.text.Length;
  38. break;
  39. }
  40. len = run.text != "\r" ? run.text.Length : 1;
  41. format = run.format;
  42. if (run_num == 0)
  43. {
  44. current_format = run.format;
  45. memo.Font = GetFontFromRichStyle(rtf, current_format);
  46. memo.TextColor = current_format.color;
  47. if (format.underline)
  48. {
  49. sb.Append("<u>");
  50. current_format.underline = true;
  51. }
  52. if (format.bold)
  53. {
  54. sb.Append("<b>");
  55. current_format.bold = true;
  56. }
  57. if (format.strike)
  58. {
  59. sb.Append("<strike>");
  60. current_format.strike = true;
  61. }
  62. if (format.italic)
  63. {
  64. sb.Append("<i>");
  65. current_format.italic = true;
  66. }
  67. }
  68. else
  69. {
  70. if (current_format.bold != format.bold)
  71. {
  72. sb.Append(format.bold ? "<b>" : "</b>");
  73. current_format.bold = format.bold;
  74. }
  75. if (current_format.strike != format.strike)
  76. {
  77. sb.Append(format.strike ? "<strike>" : "</strike>");
  78. current_format.strike = format.strike;
  79. }
  80. if (current_format.italic != format.italic)
  81. {
  82. sb.Append(format.italic ? "<i>" : "</i>");
  83. current_format.italic = format.italic;
  84. }
  85. if (current_format.underline != format.underline)
  86. {
  87. sb.Append(format.underline ? "<u>" : "</u>");
  88. current_format.underline = format.underline;
  89. }
  90. if (current_format.script_type != format.script_type)
  91. {
  92. if (format.script_type == RunFormat.ScriptType.Subscript)
  93. sb.Append("<sub>");
  94. else if (format.script_type == RunFormat.ScriptType.Superscript)
  95. sb.Append("<sup>");
  96. else if (current_format.script_type == RunFormat.ScriptType.Subscript)
  97. sb.Append("</sub>");
  98. else if (current_format.script_type == RunFormat.ScriptType.Superscript)
  99. sb.Append("</sup>");
  100. current_format.script_type = format.script_type;
  101. }
  102. if (current_format.color != format.color)
  103. {
  104. colorname = string.Format("color:#{0:X2}{1:X2}{2:X2};", format.color.R, format.color.G, format.color.B);
  105. current_format.color = format.color;
  106. }
  107. if (current_format.font_size != format.font_size)
  108. {
  109. int fs = run.format.font_size / 2;
  110. fontsize = string.Format("font-size:{0}pt;", fs);
  111. current_format.font_size = format.font_size;
  112. }
  113. Font fnt = GetFontFromRichStyle(rtf, format);
  114. if (!current_font.FontFamily.Equals(fnt.FontFamily))
  115. {
  116. current_font = fnt;
  117. fontname = string.Format("font-family:{0};", fnt.FontFamily.Name);
  118. }
  119. else
  120. fontname = string.Empty;
  121. if (colorname.Length > 0 || fontsize.Length > 0 || fontname.Length > 0)
  122. {
  123. sb.Append("<span style=\"");
  124. if (colorname.Length > 0)
  125. sb.Append(colorname);
  126. if (fontsize.Length > 0)
  127. sb.Append(fontsize);
  128. if (fontname.Length > 0)
  129. sb.Append(fontname);
  130. sb.Append("\">");
  131. span_condition = true;
  132. }
  133. }
  134. switch (run.text)
  135. {
  136. case "\r":
  137. sb.Append("<br>");
  138. break;
  139. case "\t":
  140. sb.Append("\t");
  141. break;
  142. default:
  143. sb.Append(run.text);
  144. break;
  145. }
  146. if (span_condition)
  147. {
  148. sb.Append("</span>");
  149. span_condition = false;
  150. }
  151. run_num++;
  152. }
  153. return sb.ToString();
  154. }
  155. }
  156. }