LayoutTestOutputVisualization.cs 8.7 KB

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