Jelajahi Sumber

Removed the Initialize method

MarcinZiabek 3 tahun lalu
induk
melakukan
b86946b396

+ 6 - 6
QuestPDF.UnitTests/TestEngine/TestPlan.cs

@@ -195,7 +195,7 @@ namespace QuestPDF.UnitTests.TestEngine
         
         public TestPlan CheckMeasureResult(SpacePlan expected)
         {
-            Element.VisitChildren(x => x?.Initialize(null, Canvas));
+            Element.InjectDependencies(null, Canvas);
             
             var actual = Element.Measure(OperationInput);
             
@@ -210,7 +210,7 @@ namespace QuestPDF.UnitTests.TestEngine
         
         public TestPlan CheckDrawResult()
         {
-            Element.VisitChildren(x => x?.Initialize(null, Canvas));
+            Element.InjectDependencies(null, Canvas);
             Element.Draw(OperationInput);
             return this;
         }
@@ -253,10 +253,10 @@ namespace QuestPDF.UnitTests.TestEngine
             availableSpace ??= new Size(400, 300);
             
             var canvas = new FreeCanvas();
-            value.VisitChildren(x => x.Initialize(null, canvas));
+            value.InjectDependencies(null, canvas);
             var valueMeasure = value.Measure(availableSpace.Value);
             
-            expected.VisitChildren(x => x.Initialize(null, canvas));
+            expected.InjectDependencies(null, canvas);
             var expectedMeasure = expected.Measure(availableSpace.Value);
             
             valueMeasure.Should().BeEquivalentTo(expectedMeasure);
@@ -267,11 +267,11 @@ namespace QuestPDF.UnitTests.TestEngine
             availableSpace ??= new Size(400, 300);
             
             var valueCanvas = new OperationRecordingCanvas();
-            value.VisitChildren(x => x.Initialize(null, valueCanvas));
+            value.InjectDependencies(null, valueCanvas);
             value.Draw(availableSpace.Value);
             
             var expectedCanvas = new OperationRecordingCanvas();
-            expected.VisitChildren(x => x.Initialize(null, expectedCanvas));
+            expected.InjectDependencies(null, expectedCanvas);
             expected.Draw(availableSpace.Value);
             
             valueCanvas.Operations.Should().BeEquivalentTo(expectedCanvas.Operations);

+ 13 - 1
QuestPDF/Drawing/DocumentGenerator.cs

@@ -80,7 +80,7 @@ namespace QuestPDF.Drawing
         internal static void RenderPass<TCanvas>(PageContext pageContext, TCanvas canvas, Container content, DebuggingState? debuggingState)
             where TCanvas : ICanvas, IRenderingCanvas
         {
-            content.VisitChildren(x => x?.Initialize(pageContext, canvas));
+            InjectDependencies(content, pageContext, canvas);
             content.VisitChildren(x => (x as IStateResettable)?.ResetState());
             
             canvas.BeginDocument();
@@ -141,6 +141,18 @@ namespace QuestPDF.Drawing
             }
         }
 
+        internal static void InjectDependencies(this Element content, IPageContext pageContext, ICanvas canvas)
+        {
+            content.VisitChildren(x =>
+            {
+                if (x == null)
+                    return;
+                
+                x.PageContext = pageContext;
+                x.Canvas = canvas;
+            });
+        }
+
         private static void ApplyCaching(Container content)
         {
             content.VisitChildren(x =>

+ 1 - 1
QuestPDF/Elements/Dynamic.cs

@@ -92,7 +92,7 @@ namespace QuestPDF.Elements
             container.ApplyDefaultTextStyle(TextStyle);
             container.ApplyContentDirection(ContentDirection);
             
-            container.VisitChildren(x => x?.Initialize(PageContext, Canvas));
+            container.InjectDependencies(PageContext, Canvas);
             container.VisitChildren(x => (x as IStateResettable)?.ResetState());
 
             container.Size = container.Measure(Size.Max);

+ 1 - 1
QuestPDF/Elements/DynamicImage.cs

@@ -29,7 +29,7 @@ namespace QuestPDF.Elements
                 InternalImage = SKImage.FromEncodedData(imageData)
             };
             
-            imageElement.Initialize(PageContext, Canvas);
+            imageElement.InjectDependencies(PageContext, Canvas);
             imageElement.Draw(availableSpace);
         }
     }

+ 1 - 1
QuestPDF/Elements/Page.cs

@@ -8,7 +8,7 @@ namespace QuestPDF.Elements
 {
     internal class Page : IComponent
     {
-        public TextStyle DefaultTextStyle { get; set; } = new TextStyle();
+        public TextStyle DefaultTextStyle { get; set; } = TextStyle.Default;
         
         public Size MinSize { get; set; } = PageSizes.A4;
         public Size MaxSize { get; set; } = PageSizes.A4;

+ 1 - 0
QuestPDF/Elements/Placeholder.cs

@@ -25,6 +25,7 @@ namespace QuestPDF.Elements
                 {
                     if (string.IsNullOrWhiteSpace(Text))
                         x.MaxHeight(32).Image(ImageData, ImageScaling.FitArea);
+                    
                     else
                         x.Text(Text).FontSize(14);
                 });

+ 16 - 10
QuestPDF/Elements/Table/Table.cs

@@ -17,6 +17,7 @@ namespace QuestPDF.Elements.Table
         public List<TableCell> Cells { get; set; } = new();
         public bool ExtendLastCellsToTableBottom { get; set; }
         
+        private bool CacheInitialized { get; set; }
         private int StartingRowsCount { get; set; }
         private int RowsCount { get; set; }
         private int CurrentRow { get; set; }
@@ -27,16 +28,6 @@ namespace QuestPDF.Elements.Table
         private TableCell[][] CellsCache { get; set; }
         private int MaxRow { get; set; }
         
-        internal override void Initialize(IPageContext pageContext, ICanvas canvas)
-        {
-            StartingRowsCount = Cells.Select(x => x.Row).DefaultIfEmpty(0).Max();
-            RowsCount = Cells.Select(x => x.Row + x.RowSpan - 1).DefaultIfEmpty(0).Max();
-            Cells = Cells.OrderBy(x => x.Row).ThenBy(x => x.Column).ToList();
-            BuildCache();
-
-            base.Initialize(pageContext, canvas);
-        }
-
         internal override IEnumerable<Element?> GetChildren()
         {
             return Cells;
@@ -44,11 +35,26 @@ namespace QuestPDF.Elements.Table
 
         public void ResetState()
         {
+            Initialize();
+            
             foreach (var x in Cells)
                 x.IsRendered = false;
             
             CurrentRow = 1;
         }
+        
+        private void Initialize()
+        {
+            if (!CacheInitialized)
+                return;
+
+            StartingRowsCount = Cells.Select(x => x.Row).DefaultIfEmpty(0).Max();
+            RowsCount = Cells.Select(x => x.Row + x.RowSpan - 1).DefaultIfEmpty(0).Max();
+            Cells = Cells.OrderBy(x => x.Row).ThenBy(x => x.Column).ToList();
+            BuildCache();
+
+            CacheInitialized = true;
+        }
 
         private void BuildCache()
         {

+ 2 - 2
QuestPDF/Elements/Text/Items/TextBlockElement.cs

@@ -12,7 +12,7 @@ namespace QuestPDF.Elements.Text.Items
         public TextMeasurementResult? Measure(TextMeasurementRequest request)
         {
             Element.VisitChildren(x => (x as IStateResettable)?.ResetState());
-            Element.VisitChildren(x => x.Initialize(request.PageContext, request.Canvas));
+            Element.InjectDependencies(request.PageContext, request.Canvas);
 
             var measurement = Element.Measure(new Size(request.AvailableWidth, Size.Max.Height));
 
@@ -37,7 +37,7 @@ namespace QuestPDF.Elements.Text.Items
         public void Draw(TextDrawingRequest request)
         {
             Element.VisitChildren(x => (x as IStateResettable)?.ResetState());
-            Element.VisitChildren(x => x.Initialize(request.PageContext, request.Canvas));
+            Element.InjectDependencies(request.PageContext, request.Canvas);
             
             request.Canvas.Translate(new Position(0, request.TotalAscent));
             Element.Draw(new Size(request.TextSize.Width, -request.TotalAscent));

+ 1 - 1
QuestPDF/Elements/Text/TextBlock.cs

@@ -19,7 +19,7 @@ namespace QuestPDF.Elements.Text
         private int CurrentElementIndex { get; set; }
 
         private bool FontFallbackApplied { get; set; } = false;
-        
+
         public void ResetState()
         {
             ApplyFontFallback();

+ 0 - 6
QuestPDF/Infrastructure/Element.cs

@@ -15,12 +15,6 @@ namespace QuestPDF.Infrastructure
             yield break;
         }
 
-        internal virtual void Initialize(IPageContext pageContext, ICanvas canvas)
-        {
-            PageContext = pageContext;
-            Canvas = canvas;
-        }
-
         internal virtual void CreateProxy(Func<Element?, Element?> create)
         {