LayoutTestOutputVisualization.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using QuestPDF.Drawing;
  2. using QuestPDF.Fluent;
  3. using QuestPDF.Helpers;
  4. using QuestPDF.Infrastructure;
  5. using SkiaSharp;
  6. namespace QuestPDF.LayoutTests.TestEngine;
  7. internal static class LayoutTestResultVisualization
  8. {
  9. // output settings
  10. private const int OutputImageScale = 2;
  11. private const int Padding = 10;
  12. // document colors
  13. private const string DocumentBackgroundColor = Colors.Grey.Darken2;
  14. private const string PageBackgroundColor = Colors.Grey.Lighten1;
  15. private const string RequiredAreaBackgroundColor = Colors.White;
  16. // grid configuration
  17. private const float GridSize = 10;
  18. private const float GridLineThickness = 0.25f;
  19. private const string GridLineColor = Colors.Grey.Darken3;
  20. // mock drawing settings
  21. private const byte OccludedMockBorderThickness = 5;
  22. private static readonly string[] DefaultElementColors =
  23. {
  24. Colors.Green.Medium,
  25. Colors.Blue.Medium,
  26. Colors.Orange.Medium,
  27. Colors.Lime.Darken2,
  28. Colors.Cyan.Darken2,
  29. Colors.Indigo.Darken2,
  30. Colors.Green.Lighten1,
  31. Colors.Blue.Lighten1,
  32. Colors.Orange.Lighten1,
  33. Colors.Lime.Lighten1,
  34. Colors.Cyan.Lighten1,
  35. Colors.Indigo.Lighten1
  36. };
  37. // implementations
  38. public static void Visualize(LayoutTestResult result, Stream stream)
  39. {
  40. // determine output dimenstions
  41. var numberOfPages = Math.Max(result.ActualLayout.Pages.Count, result.ExpectedLayout.Pages.Count);
  42. var canvasWidth = result.PageSize.Width * 2 + Padding * 4;
  43. var canvasHeight = result.PageSize.Height * numberOfPages + Padding * (numberOfPages + 2);
  44. // create PDF
  45. using var pdf = SKDocument.CreatePdf(stream);
  46. using var canvas = pdf.BeginPage(canvasWidth * OutputImageScale, canvasHeight * OutputImageScale);
  47. canvas.Scale(OutputImageScale, OutputImageScale);
  48. canvas.Clear(SKColor.Parse(DocumentBackgroundColor));
  49. // draw content
  50. var mockColors = AssignColorsToMocks();
  51. DrawDocument();
  52. // finish generation
  53. pdf.EndPage();
  54. pdf.Close();
  55. IDictionary<string, string> AssignColorsToMocks()
  56. {
  57. var mocks = Enumerable
  58. .Concat(result.ActualLayout.Pages, result.ExpectedLayout.Pages)
  59. .SelectMany(x => x.Mocks)
  60. .Select(x => x.MockId)
  61. .Distinct()
  62. .ToList();
  63. return Enumerable
  64. .Range(0, mocks.Count)
  65. .ToDictionary(i => mocks[i], i => DefaultElementColors[i]);
  66. }
  67. void DrawDocument()
  68. {
  69. canvas.Translate(Padding, Padding);
  70. // draw title
  71. using var textPaint = TextStyle.LibraryDefault.FontSize(8).FontColor(Colors.White).Bold().ToPaint().Clone();
  72. textPaint.TextAlign = SKTextAlign.Center;
  73. var actualHeaderPosition = new SKPoint(result.PageSize.Width / 2, textPaint.TextSize / 2);
  74. canvas.DrawText("ACTUAL", actualHeaderPosition, textPaint);
  75. var expectedHeaderPosition = new SKPoint(Padding * 2 + result.PageSize.Width * 1.5f, textPaint.TextSize / 2);
  76. canvas.DrawText("EXPECTED", expectedHeaderPosition, textPaint);
  77. // draw pages
  78. canvas.Save();
  79. canvas.Translate(0, Padding);
  80. foreach (var pageIndex in Enumerable.Range(0, numberOfPages))
  81. {
  82. var actualPage = result.ActualLayout.Pages.ElementAtOrDefault(pageIndex);
  83. var expectedPage = result.ExpectedLayout.Pages.ElementAtOrDefault(pageIndex);
  84. DrawPage(actualPage);
  85. DrawLayoutDifferences(actualPage, expectedPage);
  86. canvas.Translate(result.PageSize.Width + Padding, 0);
  87. canvas.DrawText((pageIndex + 1).ToString(), 0, textPaint.TextSize, textPaint);
  88. canvas.Translate(Padding, 0);
  89. DrawPage(expectedPage);
  90. DrawLayoutDifferences(expectedPage, actualPage);
  91. canvas.Translate(-result.PageSize.Width - Padding * 2, result.PageSize.Height + Padding);
  92. }
  93. canvas.Restore();
  94. }
  95. void DrawPage(LayoutTestResult.PageLayout? pageLayout)
  96. {
  97. // draw page
  98. using var availableAreaPaint = new SKPaint
  99. {
  100. Color = SKColor.Parse(PageBackgroundColor)
  101. };
  102. canvas.DrawRect(0, 0, result.PageSize.Width, result.PageSize.Height, availableAreaPaint);
  103. if (pageLayout == null)
  104. {
  105. DrawGridLines();
  106. return;
  107. }
  108. // draw required area
  109. using var requiredAreaPaint = new SKPaint
  110. {
  111. Color = SKColor.Parse(RequiredAreaBackgroundColor)
  112. };
  113. canvas.DrawRect(0, 0, pageLayout.RequiredArea.Width, pageLayout.RequiredArea.Height, requiredAreaPaint);
  114. // draw mocks
  115. foreach (var mock in pageLayout.Mocks)
  116. DrawMock(mock);
  117. foreach (var mock in pageLayout.Mocks.GetOverlappingItems())
  118. DrawOccludedMock(mock.belowMockId);
  119. DrawGridLines();
  120. }
  121. void DrawMock(LayoutTestResult.MockLayoutPosition mock)
  122. {
  123. var color = mockColors[mock.MockId];
  124. using var mockAreaPaint = new SKPaint
  125. {
  126. Color = SKColor.Parse(color)
  127. };
  128. canvas.Save();
  129. canvas.Translate(mock.Position.X, mock.Position.Y);
  130. canvas.DrawRect(0, 0, mock.Size.Width, mock.Size.Height, mockAreaPaint);
  131. canvas.Restore();
  132. }
  133. void DrawOccludedMock(LayoutTestResult.MockLayoutPosition mock)
  134. {
  135. var color = mockColors[mock.MockId];
  136. using var mockBorderPaint = new SKPaint
  137. {
  138. Color = SKColor.Parse(color),
  139. IsStroke = true,
  140. StrokeWidth = OccludedMockBorderThickness
  141. };
  142. var borderPosition = new SKRect(0, 0, mock.Size.Width, mock.Size.Height);
  143. borderPosition.Inflate(-OccludedMockBorderThickness / 2f, -OccludedMockBorderThickness / 2f);
  144. canvas.Save();
  145. canvas.Translate(mock.Position.X, mock.Position.Y);
  146. canvas.DrawRect(borderPosition, mockBorderPaint);
  147. canvas.Restore();
  148. }
  149. void DrawGridLines()
  150. {
  151. using var paint = new SKPaint
  152. {
  153. Color = SKColor.Parse(GridLineColor),
  154. StrokeWidth = GridLineThickness
  155. };
  156. var verticalLineCount = (int)Math.Floor(result.PageSize.Width / GridSize);
  157. var horizontalLineCount = (int)Math.Floor(result.PageSize.Height / GridSize);
  158. foreach (var i in Enumerable.Range(1, verticalLineCount))
  159. canvas.DrawLine(new SKPoint(i * GridSize, 0), new SKPoint(i * GridSize, result.PageSize.Height), paint);
  160. foreach (var i in Enumerable.Range(1, horizontalLineCount))
  161. canvas.DrawLine(new SKPoint(0, i * GridSize), new SKPoint(result.PageSize.Width, i * GridSize), paint);
  162. }
  163. void DrawLayoutDifferences(LayoutTestResult.PageLayout? target, LayoutTestResult.PageLayout? compareWith)
  164. {
  165. using var targetPath = BuildPathFromLayout(target);
  166. using var compareWithPath = BuildPathFromLayout(compareWith);
  167. using var differencePath = targetPath.Op(compareWithPath, SKPathOp.Difference);
  168. AnnotateInvalidAreaHelper.Annotate(canvas, differencePath);
  169. SKPath BuildPathFromLayout(LayoutTestResult.PageLayout? layout)
  170. {
  171. var resultPath = new SKPath();
  172. if (layout == null)
  173. return resultPath;
  174. foreach (var mock in layout.Mocks)
  175. {
  176. var position = new SKRect(
  177. mock.Position.X,
  178. mock.Position.Y,
  179. mock.Position.X + mock.Size.Width,
  180. mock.Position.Y + mock.Size.Height);
  181. resultPath.AddRect(position);
  182. }
  183. return resultPath;
  184. }
  185. }
  186. }
  187. }