ContinuousBlock.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using QuestPDF.Drawing;
  2. using QuestPDF.Helpers;
  3. using QuestPDF.Skia;
  4. namespace QuestPDF.LayoutTests.TestEngine;
  5. internal class ContinuousBlock : Element, IStateful
  6. {
  7. public float TotalWidth { get; set; }
  8. public float TotalHeight { get; set; }
  9. internal override SpacePlan Measure(Size availableSpace)
  10. {
  11. if (TotalWidth > availableSpace.Width + Size.Epsilon)
  12. return SpacePlan.Wrap("The content requires more horizontal space than available.");
  13. if (availableSpace.Height < Size.Epsilon)
  14. return SpacePlan.Wrap("The content requires more vertical space than available.");
  15. var remainingHeight = TotalHeight - HeightOffset;
  16. if (remainingHeight < Size.Epsilon)
  17. return SpacePlan.FullRender(Size.Zero);
  18. if (remainingHeight > availableSpace.Height)
  19. return SpacePlan.PartialRender(TotalWidth, availableSpace.Height);
  20. return SpacePlan.FullRender(TotalWidth, remainingHeight);
  21. }
  22. internal override void Draw(Size availableSpace)
  23. {
  24. var height = Math.Min(TotalHeight - HeightOffset, availableSpace.Height);
  25. var size = new Size(TotalWidth, height);
  26. HeightOffset += height;
  27. using var paint = new SkPaint();
  28. paint.SetSolidColor(Colors.Grey.Medium);
  29. Canvas.DrawRectangle(Position.Zero, size, paint);
  30. if (HeightOffset > TotalHeight - Size.Epsilon)
  31. HeightOffset = 0;
  32. }
  33. #region IStateful
  34. private float HeightOffset { get; set; }
  35. public void ResetState(bool hardReset = false)
  36. {
  37. if (hardReset)
  38. HeightOffset = 0;
  39. }
  40. public object GetState() => HeightOffset;
  41. public void SetState(object state) => HeightOffset = (float) state;
  42. #endregion
  43. }