MockChild.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using QuestPDF.Drawing;
  2. using QuestPDF.Helpers;
  3. using QuestPDF.Infrastructure;
  4. using QuestPDF.Skia;
  5. namespace QuestPDF.LayoutTests.TestEngine;
  6. internal class MockDrawingCommand
  7. {
  8. public string MockId { get; set; }
  9. public int PageNumber { get; set; }
  10. public Position Position { get; set; }
  11. public Size Size { get; set; }
  12. }
  13. internal class ElementMock : Element
  14. {
  15. public string MockId { get; set; }
  16. public float TotalWidth { get; set; }
  17. public float TotalHeight { get; set; }
  18. private float HeightOffset { get; set; }
  19. internal List<MockDrawingCommand> DrawingCommands { get; } = new();
  20. internal override SpacePlan Measure(Size availableSpace)
  21. {
  22. if (TotalWidth > availableSpace.Width)
  23. return SpacePlan.Wrap("The content requires more horizontal space than available.");
  24. if (availableSpace.Height < Size.Epsilon)
  25. return SpacePlan.Wrap("The content requires more vertical space than available.");
  26. var remainingHeight = TotalHeight - HeightOffset;
  27. if (remainingHeight < Size.Epsilon)
  28. return SpacePlan.FullRender(Size.Zero);
  29. if (remainingHeight > availableSpace.Height)
  30. return SpacePlan.PartialRender(TotalWidth, availableSpace.Height);
  31. return SpacePlan.FullRender(TotalWidth, remainingHeight);
  32. }
  33. internal override void Draw(Size availableSpace)
  34. {
  35. var height = Math.Min(TotalHeight - HeightOffset, availableSpace.Height);
  36. var size = new Size(TotalWidth, height);
  37. HeightOffset += height;
  38. using var paint = new SkPaint();
  39. paint.SetSolidColor(Colors.Grey.Medium);
  40. Canvas.DrawRectangle(Position.Zero, size, paint);
  41. var matrix = Canvas.GetCurrentMatrix();
  42. DrawingCommands.Add(new MockDrawingCommand
  43. {
  44. MockId = MockId,
  45. PageNumber = PageContext.CurrentPage,
  46. Position = new Position(matrix.TranslateX / matrix.ScaleX, matrix.TranslateY / matrix.ScaleY),
  47. Size = availableSpace
  48. });
  49. if (HeightOffset > TotalHeight - Size.Epsilon)
  50. HeightOffset = 0;
  51. }
  52. }