MockChild.cs 2.2 KB

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