Browse Source

Minor code adjustments

Marcin Ziąbek 3 years ago
parent
commit
3f3ada6d8a

+ 2 - 2
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()
         {

+ 0 - 267
QuestPDF.UnitTests/RowTests.cs

@@ -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
-    }
-}

+ 1 - 1
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();

+ 0 - 101
QuestPDF/Elements/DecorationOld.cs

@@ -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<Element?> GetChildren()
-        {
-            yield return DecorationElement;
-            yield return ContentElement;
-        }
-
-        internal override void CreateProxy(Func<Element, Element> 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
-                }
-            });
-        }
-    }
-}

+ 4 - 4
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;
         }
     }