HTMLExportStyles.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Drawing;
  3. using FastReport.Table;
  4. using FastReport.Utils;
  5. namespace FastReport.Export.Html
  6. {
  7. public partial class HTMLExport : ExportBase
  8. {
  9. // TODO:
  10. private bool InlineStyles { get; set; }
  11. private string GetStyleFromObject(ReportComponentBase obj)
  12. {
  13. string style;
  14. if (obj is TextObject)
  15. {
  16. TextObject textObj = obj as TextObject;
  17. style = GetStyle(textObj.Font, textObj.TextColor, textObj.FillColor,
  18. textObj.RightToLeft, textObj.HorzAlign, textObj.Border, textObj.WordWrap, textObj.LineHeight,
  19. textObj.Width, textObj.Height, textObj.Clip);
  20. }
  21. else if (obj is HtmlObject)
  22. {
  23. HtmlObject htmlObj = obj as HtmlObject;
  24. style = GetStyle(DrawUtils.DefaultTextObjectFont, Color.Black, htmlObj.FillColor,
  25. false, HorzAlign.Left, htmlObj.Border, true, 0, htmlObj.Width, htmlObj.Height, false);
  26. }
  27. else
  28. style = GetStyle(null, Color.White, obj.FillColor, false, HorzAlign.Center, obj.Border, false, 0, obj.Width, obj.Height, false);
  29. return style;
  30. }
  31. private string GetStyle(Font Font, Color TextColor, Color FillColor,
  32. bool RTL, HorzAlign HAlign, Border Border, bool WordWrap, float LineHeight, float Width, float Height, bool Clip)
  33. {
  34. FastString style = new FastString(256);
  35. if (Font != null)
  36. {
  37. if (Zoom != 1)
  38. {
  39. using (Font newFont = new Font(Font.FontFamily, Font.Size * Zoom, Font.Style, Font.Unit, Font.GdiCharSet, Font.GdiVerticalFont))
  40. HTMLFontStyle(style, newFont, LineHeight);
  41. }
  42. else
  43. HTMLFontStyle(style, Font, LineHeight);
  44. }
  45. style.Append("text-align:");
  46. if (HAlign == HorzAlign.Left)
  47. style.Append(RTL ? "right" : "left");
  48. else if (HAlign == HorzAlign.Right)
  49. style.Append(RTL ? "left" : "right");
  50. else if (HAlign == HorzAlign.Center)
  51. style.Append("center");
  52. else
  53. style.Append("justify");
  54. style.Append(";");
  55. if (WordWrap)
  56. style.Append("word-wrap:break-word;");
  57. if (Clip)
  58. style.Append("overflow:hidden;");
  59. style.Append("position:absolute;color:").
  60. Append(ExportUtils.HTMLColor(TextColor)).
  61. Append(";background-color:").
  62. Append(FillColor.A == 0 ? "transparent" : ExportUtils.HTMLColor(FillColor)).
  63. Append(";").Append(RTL ? "direction:rtl;" : String.Empty);
  64. Border newBorder = Border;
  65. HTMLBorder(style, newBorder);
  66. style.Append("width:").Append(Px(Math.Abs(Width) * Zoom)).Append("height:").Append(Px(Math.Abs(Height) * Zoom));
  67. return style.ToString();
  68. }
  69. private int UpdateCSSTable(ReportComponentBase obj)
  70. {
  71. var style = GetStyleFromObject(obj);
  72. return UpdateCSSTable(style);
  73. }
  74. private int UpdateCSSTable(string style)
  75. {
  76. int i = cssStyles.IndexOf(style);
  77. if (i == -1)
  78. {
  79. i = cssStyles.Count;
  80. cssStyles.Add(style);
  81. }
  82. return i;
  83. }
  84. private string GetStyle(string style)
  85. {
  86. if (InlineStyles)
  87. {
  88. return InlineStyle(style);
  89. }
  90. else
  91. {
  92. int index = UpdateCSSTable(style);
  93. return GetStyleTag(index);
  94. }
  95. }
  96. private string GetStyle(ReportComponentBase obj)
  97. {
  98. if (InlineStyles)
  99. {
  100. var style = GetStyleFromObject(obj);
  101. return InlineStyle(style);
  102. }
  103. else
  104. {
  105. int index = UpdateCSSTable(obj);
  106. return GetStyleTag(index);
  107. }
  108. }
  109. private string GetStyle(ReportComponentBase obj, string additionalStyle)
  110. {
  111. if (InlineStyles)
  112. {
  113. var style = GetStyleFromObject(obj);
  114. var resultStyle = string.Concat(style, " ", additionalStyle);
  115. return InlineStyle(resultStyle);
  116. }
  117. else
  118. {
  119. int index1 = UpdateCSSTable(obj);
  120. int index2 = UpdateCSSTable(additionalStyle);
  121. return GetStyleTag(index1, index2);
  122. }
  123. }
  124. private static string InlineStyle(string style)
  125. {
  126. return $"style=\"{style}\"";
  127. }
  128. private string GetStyleTag(int index)
  129. {
  130. return String.Format("class=\"{0}s{1}\"",
  131. stylePrefix,
  132. index.ToString()
  133. );
  134. }
  135. private string GetStyleTag(int index1, int index2)
  136. {
  137. return String.Format("class=\"{0}s{1} {0}s{2}\"",
  138. stylePrefix,
  139. index1.ToString(),
  140. index2.ToString()
  141. );
  142. }
  143. }
  144. }