Browse Source

Added elements: Rotate, Unconstrained

Marcin Ziąbek 4 năm trước cách đây
mục cha
commit
6d3c8d0708

+ 30 - 0
QuestPDF.Examples/ElementExamples.cs

@@ -649,6 +649,7 @@ namespace QuestPDF.Examples
                 .Render(container =>
                 {
                     container
+                        .Padding(10)
                         .Border(2)
                         .Row(row =>
                         {
@@ -663,5 +664,34 @@ namespace QuestPDF.Examples
                         });
                 });
         }
+        
+        [Test]
+        public void Floating()
+        {
+            RenderingTest
+                .Create()
+                .PageSize(400, 300)
+                .Render(container =>
+                {
+                    container
+                        .Padding(50)
+                        .Layers(layers =>
+                        {
+                            layers
+                                .PrimaryLayer()
+                                .Background(Colors.Green.Lighten4)
+                                .Extend();
+                            
+                            layers
+                                .Layer()
+                                .Rotate(17)
+                                .TranslateX(-100)
+                                .TranslateY(-50)
+                                .Width(200)
+                                .Height(100)
+                                .Background(Colors.Green.Medium);
+                        });
+                });
+        }
     }
 }

+ 1 - 1
QuestPDF.Examples/Engine/SimpleDocument.cs

@@ -33,7 +33,7 @@ namespace QuestPDF.Examples.Engine
             container.Page(page =>
             {
                 page.Size(new PageSize(Size.Width, Size.Height));
-                page.Content().Container().Element(Container as Container);
+                page.Content().Container().Background(Colors.White).Element(Container as Container);
             });
         }
     }

+ 0 - 1
QuestPDF/Drawing/ImageCanvas.cs

@@ -31,7 +31,6 @@ namespace QuestPDF.Drawing
         public override void BeginPage(Size size)
         {
             var scalingFactor = Metadata.RasterDpi / (float) PageSizes.PointsPerInch;
-            
             var imageInfo = new SKImageInfo((int) (size.Width * scalingFactor), (int) (size.Height * scalingFactor));
             
             Surface = SKSurface.Create(imageInfo);

+ 4 - 40
QuestPDF/Elements/Rotate.cs

@@ -1,36 +1,10 @@
-using System;
-using System.Linq;
-using QuestPDF.Drawing.SpacePlan;
-using QuestPDF.Infrastructure;
+using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
     internal class Rotate : ContainerElement
     {
-        public int TurnCount { get; set; }
-        public int NormalizedTurnCount => (TurnCount % 4 + 4) % 4;
-        
-        internal override ISpacePlan Measure(Size availableSpace)
-        {
-            if (NormalizedTurnCount == 0 || NormalizedTurnCount == 2)
-                return base.Measure(availableSpace);
-            
-            availableSpace = new Size(availableSpace.Height, availableSpace.Width);
-            var childSpace = base.Measure(availableSpace) as Size;
-
-            if (childSpace == null)
-                return new Wrap();
-
-            var targetSpace = new Size(childSpace.Height, childSpace.Width);
-
-            if (childSpace is FullRender)
-                return new FullRender(targetSpace);
-            
-            if (childSpace is PartialRender)
-                return new PartialRender(targetSpace);
-
-            throw new ArgumentException();
-        }
+        public float Angle { get; set; } = 0;
         
         internal override void Draw(Size availableSpace)
         {
@@ -38,20 +12,10 @@ namespace QuestPDF.Elements
             
             if (skiaCanvas == null)
                 return;
-
-            var currentMatrix = skiaCanvas.TotalMatrix;
-
-            if (NormalizedTurnCount == 1 || NormalizedTurnCount == 2)
-                skiaCanvas.Translate(availableSpace.Width, 0);
             
-            if (NormalizedTurnCount == 2  || NormalizedTurnCount == 3)
-                skiaCanvas.Translate(0, availableSpace.Height);
-
-            skiaCanvas.RotateRadians(NormalizedTurnCount * (float) Math.PI / 2f);
-            
-            if (NormalizedTurnCount == 1 || NormalizedTurnCount == 3)
-                availableSpace = new Size(availableSpace.Height, availableSpace.Width);
+            var currentMatrix = skiaCanvas.TotalMatrix;
             
+            skiaCanvas.RotateDegrees(Angle);
             Child?.Draw(availableSpace);
             skiaCanvas.SetMatrix(currentMatrix);
         }

+ 59 - 0
QuestPDF/Elements/SimpleRotate.cs

@@ -0,0 +1,59 @@
+using System;
+using System.Linq;
+using QuestPDF.Drawing.SpacePlan;
+using QuestPDF.Infrastructure;
+
+namespace QuestPDF.Elements
+{
+    internal class SimpleRotate : ContainerElement
+    {
+        public int TurnCount { get; set; }
+        public int NormalizedTurnCount => (TurnCount % 4 + 4) % 4;
+        
+        internal override ISpacePlan Measure(Size availableSpace)
+        {
+            if (NormalizedTurnCount == 0 || NormalizedTurnCount == 2)
+                return base.Measure(availableSpace);
+            
+            availableSpace = new Size(availableSpace.Height, availableSpace.Width);
+            var childSpace = base.Measure(availableSpace) as Size;
+
+            if (childSpace == null)
+                return new Wrap();
+
+            var targetSpace = new Size(childSpace.Height, childSpace.Width);
+
+            if (childSpace is FullRender)
+                return new FullRender(targetSpace);
+            
+            if (childSpace is PartialRender)
+                return new PartialRender(targetSpace);
+
+            throw new ArgumentException();
+        }
+        
+        internal override void Draw(Size availableSpace)
+        {
+            var skiaCanvas = (Canvas as Drawing.SkiaCanvasBase)?.Canvas;
+            
+            if (skiaCanvas == null)
+                return;
+
+            var currentMatrix = skiaCanvas.TotalMatrix;
+
+            if (NormalizedTurnCount == 1 || NormalizedTurnCount == 2)
+                skiaCanvas.Translate(availableSpace.Width, 0);
+            
+            if (NormalizedTurnCount == 2  || NormalizedTurnCount == 3)
+                skiaCanvas.Translate(0, availableSpace.Height);
+
+            skiaCanvas.RotateDegrees(NormalizedTurnCount * 90);
+            
+            if (NormalizedTurnCount == 1 || NormalizedTurnCount == 3)
+                availableSpace = new Size(availableSpace.Height, availableSpace.Width);
+            
+            Child?.Draw(availableSpace);
+            skiaCanvas.SetMatrix(currentMatrix);
+        }
+    }
+}

+ 27 - 0
QuestPDF/Elements/Unconstrained.cs

@@ -0,0 +1,27 @@
+using System;
+using QuestPDF.Drawing.SpacePlan;
+using QuestPDF.Infrastructure;
+
+namespace QuestPDF.Elements
+{
+    internal class Unconstrained : ContainerElement
+    {
+        internal override ISpacePlan Measure(Size availableSpace)
+        {
+            var childSize = Child?.Measure(Size.Max) ?? new FullRender(Size.Zero);
+            
+            if (childSize is PartialRender)
+                return new PartialRender(Size.Zero);
+            
+            if (childSize is FullRender)
+                return new FullRender(Size.Zero);
+            
+            return childSize;
+        }
+
+        internal override void Draw(Size availableSpace)
+        {
+            Child?.Draw(Size.Max);
+        }
+    }
+}

+ 5 - 0
QuestPDF/Fluent/ElementExtensions.cs

@@ -162,5 +162,10 @@ namespace QuestPDF.Fluent
         {
             return element.Element(new Box());
         }
+        
+        public static IContainer Unconstrained(this IContainer element)
+        {
+            return element.Element(new Unconstrained());
+        }
     }
 }

+ 13 - 4
QuestPDF/Fluent/RotateExtensions.cs

@@ -6,9 +6,9 @@ namespace QuestPDF.Fluent
 {
     public static class RotateExtensions
     {
-        private static IContainer Rotate(this IContainer element, Action<Rotate> handler)
+        private static IContainer SimpleRotate(this IContainer element, Action<SimpleRotate> handler)
         {
-            var scale = element as Rotate ?? new Rotate();
+            var scale = element as SimpleRotate ?? new SimpleRotate();
             handler(scale);
             
             return element.Element(scale);
@@ -16,12 +16,21 @@ namespace QuestPDF.Fluent
         
         public static IContainer RotateLeft(this IContainer element)
         {
-            return element.Rotate(x => x.TurnCount--);
+            return element.SimpleRotate(x => x.TurnCount--);
         }
         
         public static IContainer RotateRight(this IContainer element)
         {
-            return element.Rotate(x => x.TurnCount++);
+            return element.SimpleRotate(x => x.TurnCount++);
+        }
+        
+        /// <param name="angle">In degrees</param>
+        public static IContainer Rotate(this IContainer element, float angle)
+        {
+            return element.Element(new Rotate
+            {
+                Angle = angle
+            });
         }
     }
 }

+ 1 - 6
QuestPDF/Fluent/TranslateExtensions.cs

@@ -13,12 +13,7 @@ namespace QuestPDF.Fluent
             
             return element.Element(translate);
         }
-        
-        public static IContainer Translate(this IContainer element, float value)
-        {
-            return element.TranslateX(value).TranslateY(value);
-        }
-        
+
         public static IContainer TranslateX(this IContainer element, float value)
         {
             return element.Translate(x => x.TranslateX = value);