Line.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using QuestPDF.Drawing;
  3. using QuestPDF.Helpers;
  4. using QuestPDF.Infrastructure;
  5. namespace QuestPDF.Elements
  6. {
  7. public interface ILine
  8. {
  9. }
  10. internal enum LineType
  11. {
  12. Vertical,
  13. Horizontal
  14. }
  15. internal sealed class Line : Element, ILine, IStateful
  16. {
  17. public LineType Type { get; set; } = LineType.Vertical;
  18. public Color Color { get; set; } = Colors.Black;
  19. public float Thickness { get; set; } = 1;
  20. internal override SpacePlan Measure(Size availableSpace)
  21. {
  22. if (IsRendered)
  23. return SpacePlan.Empty();
  24. if (availableSpace.IsNegative())
  25. return SpacePlan.Wrap("The available space is negative.");
  26. if (Type == LineType.Vertical)
  27. {
  28. if (availableSpace.Width + Size.Epsilon < Thickness)
  29. return SpacePlan.Wrap("The line thickness is greater than the available horizontal space.");
  30. return SpacePlan.FullRender(Thickness, 0);
  31. }
  32. if (Type == LineType.Horizontal)
  33. {
  34. if (availableSpace.Height + Size.Epsilon < Thickness)
  35. return SpacePlan.Wrap("The line thickness is greater than the available vertical space.");
  36. return SpacePlan.FullRender(0, Thickness);
  37. }
  38. throw new NotSupportedException();
  39. }
  40. internal override void Draw(Size availableSpace)
  41. {
  42. if (IsRendered)
  43. return;
  44. if (Type == LineType.Vertical)
  45. {
  46. Canvas.DrawFilledRectangle(Position.Zero, new Size(Thickness, availableSpace.Height), Color);
  47. }
  48. else if (Type == LineType.Horizontal)
  49. {
  50. Canvas.DrawFilledRectangle(Position.Zero, new Size(availableSpace.Width, Thickness), Color);
  51. }
  52. IsRendered = true;
  53. }
  54. #region IStateful
  55. private bool IsRendered { get; set; }
  56. public void ResetState(bool hardReset = false) => IsRendered = false;
  57. public object GetState() => IsRendered;
  58. public void SetState(object state) => IsRendered = (bool) state;
  59. #endregion
  60. internal override string? GetCompanionHint()
  61. {
  62. return $"{Type} {Thickness:F1} {Color.ToString()}";
  63. }
  64. }
  65. }