ElementObserver.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System.Diagnostics;
  2. namespace QuestPDF.LayoutTests.TestEngine;
  3. internal class ElementObserver : ContainerElement
  4. {
  5. public string? ObserverId { get; set; }
  6. public DrawingRecorder? DrawingRecorder { get; set; }
  7. internal override void Draw(Size availableSpace)
  8. {
  9. Debug.Assert(ObserverId != null);
  10. Debug.Assert(DrawingRecorder != null);
  11. var matrix = Canvas.GetCurrentMatrix();
  12. DrawingRecorder?.Record(new ElementDrawingEvent
  13. {
  14. ObserverId = ObserverId,
  15. PageNumber = PageContext.CurrentPage,
  16. Position = new Position(matrix.TranslateX, matrix.TranslateY),
  17. Size = ObserverId == "$document" ? Child.Measure(availableSpace) : availableSpace
  18. });
  19. var matrixBeforeDraw = Canvas.GetCurrentMatrix().ToMatrix4x4();
  20. base.Draw(availableSpace);
  21. var matrixAfterDraw = Canvas.GetCurrentMatrix().ToMatrix4x4();
  22. if (matrixAfterDraw != matrixBeforeDraw)
  23. throw new InvalidOperationException("Canvas state was not restored after drawing operation.");
  24. }
  25. }