Browse Source

Image: added support for common overloads

Marcin Ziąbek 4 years ago
parent
commit
011668fb4c

+ 57 - 0
QuestPDF.Examples/ComplexLayoutBenchmark.cs

@@ -0,0 +1,57 @@
+using NUnit.Framework;
+using QuestPDF.Examples.Engine;
+using QuestPDF.Fluent;
+using QuestPDF.Helpers;
+using QuestPDF.Infrastructure;
+
+namespace QuestPDF.Examples
+{
+    public class ComplexLayoutBenchmark
+    {
+        [Test]
+        public void ComplexLayout()
+        {
+            RenderingTest
+                .Create()
+                .PageSize(PageSizes.A4)
+                .ProducePdf()
+                .ShowResults()
+                .Render(x => x.Image(new byte[] { 1, 2, 3 }));
+                //.Render(x => GenerateStructure(x, 16));
+        }
+
+        private void GenerateStructure(IContainer container, int level)
+        {
+            if (level <= 0)
+            {
+                container.Background(Placeholders.BackgroundColor()).Height(10);
+                return;
+            }
+
+            level--;
+
+            if (level % 3 == 0)
+            {
+                container
+                    .Border(level / 10f)
+                    .BorderColor(Colors.Black)
+                    .Row(row =>
+                    {
+                        row.RelativeColumn().Element(x => GenerateStructure(x, level));
+                        row.RelativeColumn().Element(x => GenerateStructure(x, level));
+                    });
+            }
+            else
+            {
+                container
+                    .Border(level / 10f)
+                    .BorderColor(Colors.Black)
+                    .Stack(stack =>
+                    {
+                        stack.Item().Element(x => GenerateStructure(x, level));
+                        stack.Item().Element(x => GenerateStructure(x, level));
+                    });
+            }
+        }
+    }
+}

+ 8 - 2
QuestPDF.Examples/Engine/RenderingTest.cs

@@ -18,6 +18,7 @@ namespace QuestPDF.Examples.Engine
     {
         private string FileNamePrefix = "test";
         private Size Size { get; set; }
+        private int? MaxPagesThreshold { get; set; }
         private bool ShowResult { get; set; }
         private RenderingTestResult ResultType { get; set; } = RenderingTestResult.Images;
         
@@ -77,11 +78,16 @@ namespace QuestPDF.Examples.Engine
                 });
             });
         }
+        
+        public void MaxPages(int value)
+        {
+            MaxPagesThreshold = value;
+        }
 
         public void RenderDocument(Action<IDocumentContainer> content)
         {
-            var maxPages = ResultType == RenderingTestResult.Pdf ? 1000 : 10;
-            var document = new SimpleDocument(content, maxPages);
+            MaxPagesThreshold ??= ResultType == RenderingTestResult.Pdf ? 1000 : 10;
+            var document = new SimpleDocument(content, MaxPagesThreshold.Value);
 
             Render(document);
         }

+ 41 - 0
QuestPDF.Examples/ImageExamples.cs

@@ -0,0 +1,41 @@
+using System.IO;
+using NUnit.Framework;
+using QuestPDF.Examples.Engine;
+using QuestPDF.Fluent;
+using QuestPDF.Helpers;
+
+namespace QuestPDF.Examples
+{
+    public class ImageExamples
+    {
+        [Test]
+        public void LoadingImage()
+        {
+            RenderingTest
+                .Create()
+                .PageSize(PageSizes.A5)
+                .ProducePdf()
+                .ShowResults()
+                .Render(page =>
+                {
+                    page.Padding(25).Stack(stack =>
+                    {
+                        stack.Spacing(25);
+                        stack.Item().Image(File.ReadAllBytes("logo.png"));
+                        stack.Item().Image("logo.png");
+                    });
+                });
+        }
+        
+        [Test]
+        public void Exception()
+        {
+            RenderingTest
+                .Create()
+                .PageSize(PageSizes.A5)
+                .ProducePdf()
+                .ShowResults()
+                .Render(page => page.Image("non_existent.png"));
+        }
+    }
+}

+ 3 - 0
QuestPDF.Examples/QuestPDF.Examples.csproj

@@ -23,6 +23,9 @@
       <None Update="LibreBarcode39-Regular.ttf">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </None>
+      <None Update="logo.png">
+        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+      </None>
     </ItemGroup>
 
 </Project>

BIN
QuestPDF.Examples/logo.png


+ 23 - 4
QuestPDF/Fluent/ImageExtensions.cs

@@ -1,4 +1,6 @@
 using System;
+using System.IO;
+using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Elements;
 using QuestPDF.Infrastructure;
 using SkiaSharp;
@@ -7,12 +9,29 @@ namespace QuestPDF.Fluent
 {
     public static class ImageExtensions
     {
-        public static void Image(this IContainer parent, byte[] data, ImageScaling scaling = ImageScaling.FitWidth)
+        public static void Image(this IContainer parent, byte[] imageData, ImageScaling scaling = ImageScaling.FitWidth)
         {
-            if (data == null)
-                return;
+            var image = SKImage.FromEncodedData(imageData);
+            parent.Image(image, scaling);
+        }
+        
+        public static void Image(this IContainer parent, string filePath, ImageScaling scaling = ImageScaling.FitWidth)
+        {
+            var image = SKImage.FromEncodedData(filePath);
+            parent.Image(image, scaling);
+        }
+        
+        public static void Image(this IContainer parent, Stream fileStream, ImageScaling scaling = ImageScaling.FitWidth)
+        {
+            var image = SKImage.FromEncodedData(fileStream);
+            parent.Image(image, scaling);
+        }
+        
+        private static void Image(this IContainer parent, SKImage image, ImageScaling scaling = ImageScaling.FitWidth)
+        {
+            if (image == null)
+                throw new DocumentComposeException("Cannot load or decode provided image.");
             
-            var image = SKImage.FromEncodedData(data);
             var aspectRatio = image.Width / (float)image.Height;
             
             var imageElement = new Image

+ 1 - 1
QuestPDF/QuestPDF.csproj

@@ -4,7 +4,7 @@
         <Authors>MarcinZiabek</Authors>
         <Company>CodeFlint</Company>
         <PackageId>QuestPDF</PackageId>
-        <Version>2021.11.0-beta</Version>
+        <Version>2021.11.0-beta2</Version>
         <PackageDescription>QuestPDF is an open-source, modern and battle-tested library that can help you with generating PDF documents by offering friendly, discoverable and predictable C# fluent API.</PackageDescription>
         <PackageReleaseNotes>Implemented new elements: SkipOnce and Inlined. Added possibility to define global, page-wide test style. Improved exception handling experience.</PackageReleaseNotes>
         <LangVersion>8</LangVersion>