LayoutTestOutputVisualization.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 = new SKPaint
  74. {
  75. TextSize = 8,
  76. Color = Colors.White.ColorToCode(),
  77. Typeface = SKTypeface.FromFamilyName("Calibri", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright),
  78. TextAlign = SKTextAlign.Center
  79. };
  80. var actualHeaderPosition = new SKPoint(result.PageSize.Width / 2, textPaint.TextSize / 2);
  81. canvas.DrawText("ACTUAL", actualHeaderPosition, textPaint);
  82. var expectedHeaderPosition = new SKPoint(Padding * 2 + result.PageSize.Width * 1.5f, textPaint.TextSize / 2);
  83. canvas.DrawText("EXPECTED", expectedHeaderPosition, textPaint);
  84. // draw pages
  85. canvas.Save();
  86. canvas.Translate(0, Padding);
  87. foreach (var pageIndex in Enumerable.Range(0, numberOfPages))
  88. {
  89. var actualPage = result.ActualLayout.Pages.ElementAtOrDefault(pageIndex);
  90. var expectedPage = result.ExpectedLayout.Pages.ElementAtOrDefault(pageIndex);
  91. DrawPage(actualPage);
  92. DrawLayoutDifferences(actualPage, expectedPage);
  93. canvas.Translate(result.PageSize.Width + Padding, 0);
  94. canvas.DrawText((pageIndex + 1).ToString(), 0, textPaint.TextSize, textPaint);
  95. canvas.Translate(Padding, 0);
  96. DrawPage(expectedPage);
  97. DrawLayoutDifferences(expectedPage, actualPage);
  98. canvas.Translate(-result.PageSize.Width - Padding * 2, result.PageSize.Height + Padding);
  99. }
  100. canvas.Restore();
  101. }
  102. void DrawPage(LayoutTestResult.PageLayout? pageLayout)
  103. {
  104. // draw page
  105. using var availableAreaPaint = new SKPaint
  106. {
  107. Color = SKColor.Parse(PageBackgroundColor)
  108. };
  109. canvas.DrawRect(0, 0, result.PageSize.Width, result.PageSize.Height, availableAreaPaint);
  110. if (pageLayout == null)
  111. {
  112. DrawGridLines();
  113. return;
  114. }
  115. // draw required area
  116. using var requiredAreaPaint = new SKPaint
  117. {
  118. Color = SKColor.Parse(RequiredAreaBackgroundColor)
  119. };
  120. canvas.DrawRect(0, 0, pageLayout.RequiredArea.Width, pageLayout.RequiredArea.Height, requiredAreaPaint);
  121. // draw mocks
  122. foreach (var mock in pageLayout.Mocks)
  123. DrawMock(mock);
  124. foreach (var mock in pageLayout.Mocks.GetOverlappingItems())
  125. DrawOccludedMock(mock.Below);
  126. DrawGridLines();
  127. }
  128. void DrawMock(LayoutTestResult.MockLayoutPosition mock)
  129. {
  130. var color = mockColors[mock.MockId];
  131. using var mockAreaPaint = new SKPaint
  132. {
  133. Color = SKColor.Parse(color)
  134. };
  135. canvas.Save();
  136. canvas.Translate(mock.Position.X, mock.Position.Y);
  137. canvas.DrawRect(0, 0, mock.Size.Width, mock.Size.Height, mockAreaPaint);
  138. canvas.Restore();
  139. }
  140. void DrawOccludedMock(LayoutTestResult.MockLayoutPosition mock)
  141. {
  142. var color = mockColors[mock.MockId];
  143. using var mockBorderPaint = new SKPaint
  144. {
  145. Color = SKColor.Parse(color),
  146. IsStroke = true,
  147. StrokeWidth = OccludedMockBorderThickness
  148. };
  149. var borderPosition = new SKRect(0, 0, mock.Size.Width, mock.Size.Height);
  150. borderPosition.Inflate(-OccludedMockBorderThickness / 2f, -OccludedMockBorderThickness / 2f);
  151. canvas.Save();
  152. canvas.Translate(mock.Position.X, mock.Position.Y);
  153. canvas.DrawRect(borderPosition, mockBorderPaint);
  154. canvas.Restore();
  155. }
  156. void DrawGridLines()
  157. {
  158. using var paint = new SKPaint
  159. {
  160. Color = SKColor.Parse(Colors.Black).WithAlpha(GridLineTransparency),
  161. StrokeWidth = GridLineThickness
  162. };
  163. var verticalLineCount = (int)Math.Floor(result.PageSize.Width / GridSize);
  164. var horizontalLineCount = (int)Math.Floor(result.PageSize.Height / GridSize);
  165. foreach (var i in Enumerable.Range(1, verticalLineCount))
  166. canvas.DrawLine(new SKPoint(i * GridSize, 0), new SKPoint(i * GridSize, result.PageSize.Height), paint);
  167. foreach (var i in Enumerable.Range(1, horizontalLineCount))
  168. canvas.DrawLine(new SKPoint(0, i * GridSize), new SKPoint(result.PageSize.Width, i * GridSize), paint);
  169. }
  170. void DrawLayoutDifferences(LayoutTestResult.PageLayout? target, LayoutTestResult.PageLayout? compareWith)
  171. {
  172. using var targetPath = BuildPathFromLayout(target);
  173. using var compareWithPath = BuildPathFromLayout(compareWith);
  174. using var differencePath = targetPath.Op(compareWithPath, SKPathOp.Difference);
  175. AnnotateInvalidAreaHelper.Annotate(canvas, differencePath);
  176. SKPath BuildPathFromLayout(LayoutTestResult.PageLayout? layout)
  177. {
  178. var resultPath = new SKPath();
  179. if (layout == null)
  180. return resultPath;
  181. foreach (var mock in layout.Mocks)
  182. {
  183. var position = new SKRect(
  184. mock.Position.X,
  185. mock.Position.Y,
  186. mock.Position.X + mock.Size.Width,
  187. mock.Position.Y + mock.Size.Height);
  188. resultPath.AddRect(position);
  189. }
  190. return resultPath;
  191. }
  192. }
  193. }
  194. }