diff --git a/QuestPDF.UnitTests/TestEngine/TestPlan.cs b/QuestPDF.UnitTests/TestEngine/TestPlan.cs index bd803e5..b43e5a7 100644 --- a/QuestPDF.UnitTests/TestEngine/TestPlan.cs +++ b/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); diff --git a/QuestPDF/Drawing/DocumentGenerator.cs b/QuestPDF/Drawing/DocumentGenerator.cs index bf7c5e1..d982e3b 100644 --- a/QuestPDF/Drawing/DocumentGenerator.cs +++ b/QuestPDF/Drawing/DocumentGenerator.cs @@ -80,7 +80,7 @@ namespace QuestPDF.Drawing internal static void RenderPass(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 => diff --git a/QuestPDF/Elements/Dynamic.cs b/QuestPDF/Elements/Dynamic.cs index 41385bf..450ec19 100644 --- a/QuestPDF/Elements/Dynamic.cs +++ b/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); diff --git a/QuestPDF/Elements/DynamicImage.cs b/QuestPDF/Elements/DynamicImage.cs index 514fe16..1837db5 100644 --- a/QuestPDF/Elements/DynamicImage.cs +++ b/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); } } diff --git a/QuestPDF/Elements/Page.cs b/QuestPDF/Elements/Page.cs index 275d876..176ecfa 100644 --- a/QuestPDF/Elements/Page.cs +++ b/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; diff --git a/QuestPDF/Elements/Placeholder.cs b/QuestPDF/Elements/Placeholder.cs index b1020d0..7f9cf70 100644 --- a/QuestPDF/Elements/Placeholder.cs +++ b/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); }); diff --git a/QuestPDF/Elements/Table/Table.cs b/QuestPDF/Elements/Table/Table.cs index 8226108..c68b15e 100644 --- a/QuestPDF/Elements/Table/Table.cs +++ b/QuestPDF/Elements/Table/Table.cs @@ -17,6 +17,7 @@ namespace QuestPDF.Elements.Table public List 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 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() { diff --git a/QuestPDF/Elements/Text/Items/TextBlockElement.cs b/QuestPDF/Elements/Text/Items/TextBlockElement.cs index feb76b9..6dc3530 100644 --- a/QuestPDF/Elements/Text/Items/TextBlockElement.cs +++ b/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)); diff --git a/QuestPDF/Elements/Text/TextBlock.cs b/QuestPDF/Elements/Text/TextBlock.cs index c14f8cc..5f1a32c 100644 --- a/QuestPDF/Elements/Text/TextBlock.cs +++ b/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(); diff --git a/QuestPDF/Infrastructure/Element.cs b/QuestPDF/Infrastructure/Element.cs index bbb63ba..067e93a 100644 --- a/QuestPDF/Infrastructure/Element.cs +++ b/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 create) {