BlazorExportDraw.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Text;
  5. using FastReport.Utils;
  6. using FastReport.Export;
  7. using System.Drawing.Imaging;
  8. using Microsoft.AspNetCore.Components;
  9. using static FastReport.Web.Blazor.Export.BlazorConstants;
  10. namespace FastReport.Web.Blazor.Export
  11. {
  12. public partial class BlazorExport
  13. {
  14. private static void HTMLFontStyle(StringBuilder FFontDesc, Font font, float LineHeight)
  15. {
  16. FFontDesc.Append((((font.Style & FontStyle.Bold) > 0) ? "font-weight:bold;" : String.Empty) +
  17. (((font.Style & FontStyle.Italic) > 0) ? "font-style:italic;" : "font-style:normal;"));
  18. if ((font.Style & FontStyle.Underline) > 0 && (font.Style & FontStyle.Strikeout) > 0)
  19. FFontDesc.Append("text-decoration:underline|line-through;");
  20. else if ((font.Style & FontStyle.Underline) > 0)
  21. FFontDesc.Append("text-decoration:underline;");
  22. else if ((font.Style & FontStyle.Strikeout) > 0)
  23. FFontDesc.Append("text-decoration:line-through;");
  24. FFontDesc.Append("font-family:").Append(font.Name).Append(';');
  25. FFontDesc.Append("font-size:").Append(Px(font.Size * 96 / 72)).Append("px;");
  26. if (LineHeight > 0)
  27. FFontDesc.Append("line-height:").Append(Px(LineHeight)).Append("px;");
  28. else
  29. FFontDesc.Append("line-height: 1.2;");
  30. }
  31. private static string HTMLBorderStyle(BorderLine line)
  32. {
  33. switch (line.Style)
  34. {
  35. case LineStyle.Dash:
  36. case LineStyle.DashDot:
  37. case LineStyle.DashDotDot:
  38. return "dashed";
  39. case LineStyle.Dot:
  40. return "dotted";
  41. case LineStyle.Double:
  42. return "double";
  43. default:
  44. return "solid";
  45. }
  46. }
  47. private float HTMLBorderWidth(BorderLine line)
  48. {
  49. if (line.Style == LineStyle.Double)
  50. return (line.Width * 3 * Zoom);
  51. else
  52. return line.Width * Zoom;
  53. }
  54. private string HTMLBorderWidthPx(BorderLine line)
  55. {
  56. if (line.Style != LineStyle.Double && line.Width == 1 && Zoom == 1)
  57. return "1px;";
  58. float width;
  59. if (line.Style == LineStyle.Double)
  60. width = line.Width * 3 * Zoom;
  61. else
  62. width = line.Width * Zoom;
  63. return ExportUtils.FloatToString(width) + "px;";
  64. }
  65. private void HTMLBorder(StringBuilder BorderDesc, Border border)
  66. {
  67. if (border.Lines > 0)
  68. {
  69. // bottom
  70. if ((border.Lines & BorderLines.Bottom) > 0)
  71. BorderDesc.Append("border-bottom-width:").
  72. Append(HTMLBorderWidthPx(border.BottomLine)).
  73. Append("border-bottom-color:").
  74. Append(HTMLColor(border.BottomLine.Color)).Append(";border-bottom-style:").
  75. Append(HTMLBorderStyle(border.BottomLine)).Append(';');
  76. else
  77. BorderDesc.Append("border-bottom:none;");
  78. // top
  79. if ((border.Lines & BorderLines.Top) > 0)
  80. BorderDesc.Append("border-top-width:").
  81. Append(HTMLBorderWidthPx(border.TopLine)).
  82. Append("border-top-color:").
  83. Append(HTMLColor(border.TopLine.Color)).Append(";border-top-style:").
  84. Append(HTMLBorderStyle(border.TopLine)).Append(';');
  85. else
  86. BorderDesc.Append("border-top:none;");
  87. // left
  88. if ((border.Lines & BorderLines.Left) > 0)
  89. BorderDesc.Append("border-left-width:").
  90. Append(HTMLBorderWidthPx(border.LeftLine)).
  91. Append("border-left-color:").
  92. Append(HTMLColor(border.LeftLine.Color)).Append(";border-left-style:").
  93. Append(HTMLBorderStyle(border.LeftLine)).Append(';');
  94. else
  95. BorderDesc.Append("border-left:none;");
  96. // right
  97. if ((border.Lines & BorderLines.Right) > 0)
  98. BorderDesc.Append("border-right-width:").
  99. Append(HTMLBorderWidthPx(border.RightLine)).
  100. Append("border-right-color:").
  101. Append(HTMLColor(border.RightLine.Color)).Append(";border-right-style:").
  102. Append(HTMLBorderStyle(border.RightLine)).Append(';');
  103. else
  104. BorderDesc.Append("border-right:none;");
  105. }
  106. else
  107. BorderDesc.Append("border:none;");
  108. }
  109. private bool HTMLBorderWidthValues(ReportComponentBase obj, out float left, out float top, out float right, out float bottom)
  110. {
  111. Border border = obj.Border;
  112. left = 0;
  113. top = 0;
  114. right = 0;
  115. bottom = 0;
  116. if (border.Lines > 0)
  117. {
  118. if ((border.Lines & BorderLines.Left) > 0)
  119. left += HTMLBorderWidth(border.LeftLine);
  120. if ((border.Lines & BorderLines.Right) > 0)
  121. right += HTMLBorderWidth(border.RightLine);
  122. if ((border.Lines & BorderLines.Top) > 0)
  123. top += HTMLBorderWidth(border.TopLine);
  124. if ((border.Lines & BorderLines.Bottom) > 0)
  125. bottom += HTMLBorderWidth(border.BottomLine);
  126. return true;
  127. }
  128. return false;
  129. }
  130. private void HTMLGetStylesHeader(IRenderContext context)
  131. {
  132. if (singlePage && pageBreaks)
  133. {
  134. StringBuilder header = new StringBuilder(256);
  135. header.AppendLine("<!--");
  136. header.Append($"div.").Append(pageStyleName).
  137. Append(" { page-break-after: always; page-break-inside: avoid; ");
  138. if (d.page.Landscape)
  139. {
  140. header.Append("width:").Append(Px(maxHeight * Zoom)).Append(" !important;")
  141. .Append("transform: rotate(90deg); -webkit-transform: rotate(90deg)");
  142. }
  143. header.AppendLine("}")
  144. .AppendLine("-->");
  145. CreateElement(context, STYLE,
  146. type: "text/css",
  147. media: "print",
  148. textContent: header.ToString());
  149. }
  150. }
  151. private static void CreateElement(IRenderContext context, string elementName,
  152. string? type = null, string? name = null, string? media = null, string? style = null, string? textContent = null)
  153. {
  154. context.OpenElement(elementName);
  155. if(type != null)
  156. context.AddAttribute(TYPE, type);
  157. if(name != null)
  158. context.AddAttribute(NAME, name);
  159. if(style != null)
  160. context.AddAttribute(STYLE, style);
  161. if(media != null)
  162. context.AddAttribute(MEDIA, media);
  163. if (textContent != null)
  164. context.AddContent(textContent);
  165. context.CloseElement();
  166. }
  167. private StringBuilder HTMLGetStyleHeader(int index)
  168. {
  169. StringBuilder header = new StringBuilder(32);
  170. return header.Append('.').
  171. Append(GetStyleTag(index)).
  172. Append(" { ");
  173. }
  174. private static RenderFragment HTMLGetAncor(int ancorName)
  175. {
  176. return builder =>
  177. {
  178. var context = new RenderContext(builder);
  179. CreateElement(context, A,
  180. name: $"PageN{ancorName}",
  181. style: "padding:0;margin:0;font-size:1px");
  182. };
  183. }
  184. private string HTMLGetImage(int CurrentPage, string hash,
  185. MemoryStream pictureStream, bool isSvg)
  186. {
  187. if (EmbedPictures)
  188. {
  189. ImageFormat format = GetImageFormat();
  190. string embedImgType = isSvg ? "svg+xml" : format.ToString();
  191. string embedPreffix = "data:image/" + embedImgType + ";base64,";
  192. return embedPreffix + GetBase64Image(pictureStream, hash);
  193. }
  194. else
  195. {
  196. PreparedPages[CurrentPage].Pictures.Add(pictureStream);
  197. PreparedPages[CurrentPage].Guids.Add(hash);
  198. return WebImagePrefix + "=" + hash;
  199. }
  200. }
  201. private string GetBase64Image(MemoryStream pictureStream, string hash)
  202. {
  203. string base64Image;
  204. if (!EmbeddedImages.TryGetValue(hash, out base64Image))
  205. {
  206. base64Image = Convert.ToBase64String(pictureStream.ToArray());
  207. EmbeddedImages.Add(hash, base64Image);
  208. }
  209. return base64Image;
  210. }
  211. }
  212. }