From 3f3ada6d8a83c81a1cfaa98277e8db9815bc0cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Zi=C4=85bek?= Date: Sun, 23 Jan 2022 21:41:45 +0100 Subject: [PATCH] Minor code adjustments --- QuestPDF.UnitTests/ColumnTests.cs | 4 +- QuestPDF.UnitTests/RowTests.cs | 267 ----------------------------- QuestPDF/Elements/Decoration.cs | 2 +- QuestPDF/Elements/DecorationOld.cs | 101 ----------- QuestPDF/Elements/Row.cs | 8 +- 5 files changed, 7 insertions(+), 375 deletions(-) delete mode 100644 QuestPDF.UnitTests/RowTests.cs delete mode 100644 QuestPDF/Elements/DecorationOld.cs diff --git a/QuestPDF.UnitTests/ColumnTests.cs b/QuestPDF.UnitTests/ColumnTests.cs index 694b908..5ea4af3 100644 --- a/QuestPDF.UnitTests/ColumnTests.cs +++ b/QuestPDF.UnitTests/ColumnTests.cs @@ -11,8 +11,6 @@ namespace QuestPDF.UnitTests [TestFixture] public class ColumnTests { - #region Measure - private Column CreateColumnWithTwoItems(TestPlan testPlan) { return new Column @@ -38,6 +36,8 @@ namespace QuestPDF.UnitTests return column; } + #region Measure + [Test] public void Measure_ReturnsWrap_WhenFirstChildWraps() { diff --git a/QuestPDF.UnitTests/RowTests.cs b/QuestPDF.UnitTests/RowTests.cs deleted file mode 100644 index c2d9925..0000000 --- a/QuestPDF.UnitTests/RowTests.cs +++ /dev/null @@ -1,267 +0,0 @@ -using NUnit.Framework; -using QuestPDF.Drawing; -using QuestPDF.Elements; -using QuestPDF.Fluent; -using QuestPDF.Infrastructure; -using QuestPDF.UnitTests.TestEngine; - -namespace QuestPDF.UnitTests -{ - [TestFixture] - public class RowTests - { - #region Measure - - [Test] - public void Measure_ReturnsWrap_WhenLeftChildReturnsWrap() - { - TestPlan - .For(x => new BinaryRow - { - Left = x.CreateChild("left"), - Right = x.CreateChild("right") - }) - .MeasureElement(new Size(400, 300)) - .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.Wrap()) - .CheckMeasureResult(SpacePlan.Wrap()); - } - - [Test] - public void Measure_ReturnsWrap_WhenRightChildReturnsWrap() - { - TestPlan - .For(x => new BinaryRow - { - Left = x.CreateChild("left"), - Right = x.CreateChild("right") - }) - .MeasureElement(new Size(400, 300)) - .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.FullRender(250, 150)) - .ExpectChildMeasure("right", new Size(150, 300), SpacePlan.Wrap()) - .CheckMeasureResult(SpacePlan.Wrap()); - } - - [Test] - public void Measure_ReturnsPartialRender_WhenLeftChildReturnsPartialRender() - { - TestPlan - .For(x => new BinaryRow - { - Left = x.CreateChild("left"), - Right = x.CreateChild("right") - }) - .MeasureElement(new Size(400, 300)) - .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.PartialRender(250, 150)) - .ExpectChildMeasure("right", new Size(150, 300), SpacePlan.FullRender(100, 100)) - .CheckMeasureResult(SpacePlan.PartialRender(350, 150)); - } - - [Test] - public void Measure_ReturnsPartialRender_WhenRightChildReturnsPartialRender() - { - TestPlan - .For(x => new BinaryRow - { - Left = x.CreateChild("left"), - Right = x.CreateChild("right") - }) - .MeasureElement(new Size(400, 300)) - .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.FullRender(250, 150)) - .ExpectChildMeasure("right", new Size(150, 300), SpacePlan.PartialRender(100, 100)) - .CheckMeasureResult(SpacePlan.PartialRender(350, 150)); - } - - [Test] - public void Measure_ReturnsFullRender_WhenBothChildrenReturnFullRender() - { - TestPlan - .For(x => new BinaryRow - { - Left = x.CreateChild("left"), - Right = x.CreateChild("right") - }) - .MeasureElement(new Size(400, 300)) - .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.FullRender(200, 150)) - .ExpectChildMeasure("right", new Size(200, 300), SpacePlan.FullRender(100, 100)) - .CheckMeasureResult(SpacePlan.FullRender(300, 150)); - } - - #endregion - - #region Draw - - [Test] - public void Draw() - { - TestPlan - .For(x => new BinaryRow - { - Left = x.CreateChild("left"), - Right = x.CreateChild("right") - }) - .DrawElement(new Size(400, 300)) - .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.FullRender(250, 150)) - .ExpectChildDraw("left", new Size(250, 300)) - .ExpectCanvasTranslate(250, 0) - .ExpectChildDraw("right", new Size(150, 300)) - .ExpectCanvasTranslate(-250, 0) - .CheckDrawResult(); - } - - #endregion - - #region Structure - - [Test] - public void Structure_RelativeColumnsHandling() - { - // arrange - var childA = TestPlan.CreateUniqueElement(); - var childB = TestPlan.CreateUniqueElement(); - var childC = TestPlan.CreateUniqueElement(); - var childD = TestPlan.CreateUniqueElement(); - var childE = TestPlan.CreateUniqueElement(); - - const int spacing = 25; - var availableSpace = new Size(1100, 400); - - // act - var value = new Container(); - - value.Row(row => - { - row.Spacing(spacing); - - row.ConstantItem(150).Element(childA); - row.ConstantItem(250).Element(childB); - row.RelativeItem(1).Element(childC); - row.RelativeItem(2).Element(childD); - row.RelativeItem(3).Element(childE); - }); - - // assert - var expected = new Container(); - - expected.Row(row => - { - row.Spacing(spacing); - - row.ConstantItem(150).Element(childA); - row.ConstantItem(250).Element(childB); - row.ConstantItem(100).Element(childC); - row.ConstantItem(200).Element(childD); - row.ConstantItem(300).Element(childE); - }); - - TestPlan.CompareOperations(value, expected, availableSpace); - } - - [Test] - public void Structure_Tree() - { - // arrange - var childA = TestPlan.CreateUniqueElement(); - var childB = TestPlan.CreateUniqueElement(); - var childC = TestPlan.CreateUniqueElement(); - var childD = TestPlan.CreateUniqueElement(); - var childE = TestPlan.CreateUniqueElement(); - - const int spacing = 25; - var availableSpace = new Size(1200, 400); - - // act - var value = new Container(); - - value.Row(row => - { - row.Spacing(spacing); - - row.ConstantItem(150).Element(childA); - row.ConstantItem(200).Element(childB); - row.ConstantItem(250).Element(childC); - row.RelativeItem(2).Element(childD); - row.RelativeItem(3).Element(childE); - }); - - // assert - var expected = new BinaryRow - { - Left = new BinaryRow - { - Left = new BinaryRow - { - Left = new Constrained - { - MinWidth = 150, - MaxWidth = 150, - Child = childA - }, - Right = new Constrained - { - MinWidth = 25, - MaxWidth = 25 - } - }, - Right = new BinaryRow - { - Left = new Constrained - { - MinWidth = 200, - MaxWidth = 200, - Child = childB - }, - Right = new Constrained - { - MinWidth = 25, - MaxWidth = 25 - } - } - }, - Right = new BinaryRow - { - Left = new BinaryRow - { - Left = new Constrained - { - MinWidth = 250, - MaxWidth = 250, - Child = childC - }, - Right = new Constrained - { - MinWidth = 25, - MaxWidth = 25 - } - }, - Right = new BinaryRow - { - Left = new Constrained - { - MinWidth = 200, - MaxWidth = 200, - Child = childD - }, - Right = new BinaryRow - { - Left = new Constrained - { - MinWidth = 25, - MaxWidth = 25 - }, - Right = new Constrained - { - MinWidth = 300, - MaxWidth = 300, - Child = childE - } - } - } - } - }; - - TestPlan.CompareOperations(value, expected, availableSpace); - } - - #endregion - } -} \ No newline at end of file diff --git a/QuestPDF/Elements/Decoration.cs b/QuestPDF/Elements/Decoration.cs index 12e9646..c1beb80 100644 --- a/QuestPDF/Elements/Decoration.cs +++ b/QuestPDF/Elements/Decoration.cs @@ -37,7 +37,7 @@ namespace QuestPDF.Elements internal override SpacePlan Measure(Size availableSpace) { - var renderingCommands = PlanLayout(availableSpace); + var renderingCommands = PlanLayout(availableSpace).ToList(); if (renderingCommands.Any(x => x.Measurement.Type == SpacePlanType.Wrap)) return SpacePlan.Wrap(); diff --git a/QuestPDF/Elements/DecorationOld.cs b/QuestPDF/Elements/DecorationOld.cs deleted file mode 100644 index c41c8c7..0000000 --- a/QuestPDF/Elements/DecorationOld.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System; -using System.Collections.Generic; -using QuestPDF.Drawing; -using QuestPDF.Fluent; -using QuestPDF.Infrastructure; - -namespace QuestPDF.Elements -{ - // TODO: remove - internal enum DecorationType - { - Prepend, - Append - } - - // TODO: remove - internal class BinaryDecoration : Element, ICacheable - { - public Element DecorationElement { get; set; } = Empty.Instance; - public Element ContentElement { get; set; } = Empty.Instance; - public DecorationType Type { get; set; } - - internal override IEnumerable GetChildren() - { - yield return DecorationElement; - yield return ContentElement; - } - - internal override void CreateProxy(Func create) - { - DecorationElement = create(DecorationElement); - ContentElement = create(ContentElement); - } - - internal override SpacePlan Measure(Size availableSpace) - { - var decorationMeasure = DecorationElement.Measure(availableSpace); - - if (decorationMeasure.Type == SpacePlanType.Wrap || decorationMeasure.Type == SpacePlanType.PartialRender) - return SpacePlan.Wrap(); - - var decorationSize = decorationMeasure; - var contentMeasure = ContentElement.Measure(new Size(availableSpace.Width, availableSpace.Height - decorationSize.Height)); - - if (contentMeasure.Type == SpacePlanType.Wrap) - return SpacePlan.Wrap(); - - var contentSize = contentMeasure; - var resultSize = new Size(availableSpace.Width, decorationSize.Height + contentSize.Height); - - if (contentSize.Type == SpacePlanType.PartialRender) - return SpacePlan.PartialRender(resultSize); - - if (contentSize.Type == SpacePlanType.FullRender) - return SpacePlan.FullRender(resultSize); - - throw new NotSupportedException(); - } - - internal override void Draw(Size availableSpace) - { - var decorationSize = DecorationElement.Measure(availableSpace); - var contentSize = new Size(availableSpace.Width, availableSpace.Height - decorationSize.Height); - - var translateHeight = Type == DecorationType.Prepend ? decorationSize.Height : contentSize.Height; - Action drawDecoration = () => DecorationElement?.Draw(new Size(availableSpace.Width, decorationSize.Height)); - Action drawContent = () => ContentElement?.Draw(new Size (availableSpace.Width, contentSize.Height)); - - var first = Type == DecorationType.Prepend ? drawDecoration : drawContent; - var second = Type == DecorationType.Prepend ? drawContent : drawDecoration; - - first(); - Canvas.Translate(new Position(0, translateHeight)); - second(); - Canvas.Translate(new Position(0, -translateHeight)); - } - } - - // TODO: remove - internal class DecorationOld : IComponent - { - public Element Header { get; set; } = Empty.Instance; - public Element Content { get; set; } = Empty.Instance; - public Element Footer { get; set; } = Empty.Instance; - - public void Compose(IContainer container) - { - container.Element(new BinaryDecoration - { - Type = DecorationType.Prepend, - DecorationElement = Header, - ContentElement = new BinaryDecoration - { - Type = DecorationType.Append, - ContentElement = Content, - DecorationElement = Footer - } - }); - } - } -} \ No newline at end of file diff --git a/QuestPDF/Elements/Row.cs b/QuestPDF/Elements/Row.cs index ffc157d..3323d72 100644 --- a/QuestPDF/Elements/Row.cs +++ b/QuestPDF/Elements/Row.cs @@ -109,7 +109,7 @@ namespace QuestPDF.Elements return; var widthPerRelativeUnit = (availableWidth - constantWidth - spacingWidth) / relativeWidth; - + foreach (var item in Items.Where(x => x.Type == RowItemType.Relative)) item.Width = item.Size * widthPerRelativeUnit; } @@ -146,12 +146,12 @@ namespace QuestPDF.Elements var rowHeight = renderingCommands.Where(x => !x.RowItem.IsRendered).Max(x => x.Measurement.Height); - renderingCommands.ForEach(command => + foreach (var command in renderingCommands) { command.Size = new Size(command.Size.Width, rowHeight); command.Measurement = command.RowItem.Measure(command.Size); - }); - + } + return renderingCommands; } }