Browse Source

Fixed: the Alignment element enforces size constraints in both direction, even if only one is provided

MarcinZiabek 3 years ago
parent
commit
d2ba21821f

+ 29 - 0
QuestPDF.Examples/AlignmentExamples.cs

@@ -0,0 +1,29 @@
+using NUnit.Framework;
+using QuestPDF.Examples.Engine;
+using QuestPDF.Fluent;
+using QuestPDF.Helpers;
+
+namespace QuestPDF.Examples
+{
+    public class AlignmentExamples
+    {
+        [Test]
+        public void Example()
+        {
+            RenderingTest
+                .Create()
+                .PageSize(400, 200)
+                .ProduceImages()
+                .ShowResults()
+                .Render(container =>
+                {
+                    container
+                        .Padding(25)
+                        .Border(1)
+                        .AlignBottom()
+                        .Background(Colors.Grey.Lighten1)
+                        .Text("Test");
+                });
+        }
+    }
+}

+ 38 - 0
QuestPDF.UnitTests/AlignmentTests.cs

@@ -86,5 +86,43 @@ namespace QuestPDF.UnitTests
                 .ExpectCanvasTranslate(new Position(-300, 0))
                 .ExpectCanvasTranslate(new Position(-300, 0))
                 .CheckDrawResult();
                 .CheckDrawResult();
         }
         }
+        
+        [Test]
+        public void Draw_HorizontalCenter_VerticalNone()
+        {
+            TestPlan
+                .For(x => new Alignment
+                {
+                    Horizontal = HorizontalAlignment.Center,
+                    Vertical = null,
+                    
+                    Child = x.CreateChild()
+                })
+                .DrawElement(new Size(400, 300))
+                .ExpectChildMeasure(expectedInput: new Size(400, 300), returns: SpacePlan.FullRender(new Size(100, 50)))
+                .ExpectCanvasTranslate(new Position(150, 0))
+                .ExpectChildDraw(new Size(100, 300))
+                .ExpectCanvasTranslate(new Position(-150, 0))
+                .CheckDrawResult();
+        }
+        
+        [Test]
+        public void Draw_HorizontalNone_VerticalMiddle()
+        {
+            TestPlan
+                .For(x => new Alignment
+                {
+                    Horizontal = null,
+                    Vertical = VerticalAlignment.Middle,
+                    
+                    Child = x.CreateChild()
+                })
+                .DrawElement(new Size(400, 300))
+                .ExpectChildMeasure(expectedInput: new Size(400, 300), returns: SpacePlan.FullRender(new Size(100, 50)))
+                .ExpectCanvasTranslate(new Position(0, 125))
+                .ExpectChildDraw(new Size(400, 50))
+                .ExpectCanvasTranslate(new Position(0, -125))
+                .CheckDrawResult();
+        }
     }
     }
 }
 }

+ 11 - 7
QuestPDF/Elements/Alignment.cs

@@ -6,19 +6,23 @@ namespace QuestPDF.Elements
 {
 {
     internal class Alignment : ContainerElement
     internal class Alignment : ContainerElement
     {
     {
-        public VerticalAlignment Vertical { get; set; } = VerticalAlignment.Top;
-        public HorizontalAlignment Horizontal { get; set; } = HorizontalAlignment.Left;
+        public VerticalAlignment? Vertical { get; set; }
+        public HorizontalAlignment? Horizontal { get; set; }
         
         
         internal override void Draw(Size availableSpace)
         internal override void Draw(Size availableSpace)
         {
         {
             if (Child == null)
             if (Child == null)
                 return;
                 return;
             
             
-            var childSize = base.Measure(availableSpace);
+            var childMeasurement = base.Measure(availableSpace);
             
             
-            if (childSize.Type == SpacePlanType.Wrap)
+            if (childMeasurement.Type == SpacePlanType.Wrap)
                 return;
                 return;
-            
+
+            var childSize = new Size(
+                Horizontal.HasValue ? childMeasurement.Width : availableSpace.Width,
+                Vertical.HasValue ? childMeasurement.Height : availableSpace.Height);
+
             var top = GetTopOffset(availableSpace, childSize);
             var top = GetTopOffset(availableSpace, childSize);
             var left = GetLeftOffset(availableSpace, childSize);
             var left = GetLeftOffset(availableSpace, childSize);
             
             
@@ -36,7 +40,7 @@ namespace QuestPDF.Elements
                 VerticalAlignment.Top => 0,
                 VerticalAlignment.Top => 0,
                 VerticalAlignment.Middle => difference / 2,
                 VerticalAlignment.Middle => difference / 2,
                 VerticalAlignment.Bottom => difference,
                 VerticalAlignment.Bottom => difference,
-                _ => throw new NotSupportedException()
+                _ => 0
             };
             };
         }
         }
         
         
@@ -49,7 +53,7 @@ namespace QuestPDF.Elements
                 HorizontalAlignment.Left => 0,
                 HorizontalAlignment.Left => 0,
                 HorizontalAlignment.Center => difference / 2,
                 HorizontalAlignment.Center => difference / 2,
                 HorizontalAlignment.Right => difference,
                 HorizontalAlignment.Right => difference,
-                _ => throw new NotSupportedException()
+                _ => 0
             };
             };
         }
         }
     }
     }

+ 2 - 2
QuestPDF/Fluent/GridExtensions.cs

@@ -60,8 +60,8 @@ namespace QuestPDF.Fluent
         {
         {
             var descriptor = new GridDescriptor();
             var descriptor = new GridDescriptor();
             
             
-            if (element is Alignment alignment)
-                descriptor.Alignment(alignment.Horizontal);
+            if (element is Alignment alignment && alignment.Horizontal.HasValue)
+                descriptor.Alignment(alignment.Horizontal.Value);
             
             
             handler(descriptor);
             handler(descriptor);
             element.Component(descriptor.Grid);
             element.Component(descriptor.Grid);