Browse Source

Code cleanup

Marcin Ziąbek 4 years ago
parent
commit
a124d23862

+ 1 - 4
QuestPDF.Examples/BarcodeExamples.cs

@@ -1,11 +1,8 @@
-using System;
-using System.Linq;
-using NUnit.Framework;
+using NUnit.Framework;
 using QuestPDF.Examples.Engine;
 using QuestPDF.Fluent;
 using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
-using SkiaSharp;
 
 namespace QuestPDF.Examples
 {

+ 0 - 1
QuestPDF.Examples/ElementExamples.cs

@@ -1,4 +1,3 @@
-using System;
 using System.Linq;
 using NUnit.Framework;
 using QuestPDF.Examples.Engine;

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

@@ -1,10 +1,8 @@
 using System;
 using System.Diagnostics;
 using System.Runtime.CompilerServices;
-using QuestPDF.Drawing;
 using QuestPDF.Elements;
 using QuestPDF.Fluent;
-using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Examples.Engine

+ 69 - 42
QuestPDF.Examples/TextBenchmark.cs

@@ -1,11 +1,9 @@
 using System;
-using System.Collections;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using System.Linq;
 using NUnit.Framework;
-using QuestPDF.Drawing;
 using QuestPDF.Examples.Engine;
 using QuestPDF.Fluent;
 using QuestPDF.Helpers;
@@ -14,50 +12,31 @@ using QuestPDF.Infrastructure;
 namespace QuestPDF.Examples
 {
     public class TextBenchmark
-    { 
+    {
         [Test]
-        public void Benchmark()
+        public void Generate()
         {
-            var subtitleStyle = TextStyle.Default.Size(24).SemiBold().Color(Colors.Blue.Medium);
-            var normalStyle = TextStyle.Default.Size(14);
+            var chapters = GetBookChapters().ToList();
             
-            var chapters = GetChapters().ToList();
+            RenderingTest
+                .Create()
+                .PageSize(PageSizes.A4)
+                .FileName()
+                .ProducePdf()
+                .ShowResults()
+                .Render(x => ComposeBook(x, chapters));
+        }
+        
+        [Test]
+        public void Benchmark()
+        {
+            var chapters = GetBookChapters().ToList();
   
             var results = PerformTest(16).ToList();
  
             Console.WriteLine($"Min: {results.Min():F}");
             Console.WriteLine($"Max: {results.Max():F}");
             Console.WriteLine($"Avg: {results.Average():F}");
-
-            IEnumerable<(string title, string content)> GetChapters()
-            {
-                var book = File.ReadAllLines("quo-vadis.txt");
-            
-                var chapterPointers = book
-                    .Select((line, index) => new
-                    {
-                        LineNumber = index,
-                        Text = line
-                    })
-                    .Where(x => x.Text.Length < 50 && x.Text.Contains("Rozdział") || x.Text.Contains("-----"))
-                    .Select(x => x.LineNumber)
-                    .ToList();
-
-                foreach (var index in Enumerable.Range(0, chapterPointers.Count - 1))
-                {
-                    var chapter = chapterPointers[index];
-                    
-                    var title = book[chapter];
-                    
-                    var lineFrom = chapterPointers[index];
-                    var lineTo = chapterPointers[index + 1] - 1;
-                    
-                    var lines = book.Skip(lineFrom + 1).Take(lineTo - lineFrom);
-                    var content = string.Join(Environment.NewLine, lines);
-
-                    yield return (title, content);
-                }
-            }
             
             void GenerateDocument()
             {
@@ -66,7 +45,7 @@ namespace QuestPDF.Examples
                     .PageSize(PageSizes.A4)
                     .FileName()
                     .ProducePdf()
-                    .Render(ComposePage);
+                    .Render(x => ComposeBook(x, chapters));
             }
 
             IEnumerable<float> PerformTest(int attempts)
@@ -83,6 +62,54 @@ namespace QuestPDF.Examples
                     yield return timer.ElapsedMilliseconds;
                 }
             }
+        }
+
+        class BookChapter
+        {
+            public string Title { get; set; }
+            public string Content { get; set; }
+        }
+        
+        private static IEnumerable<BookChapter> GetBookChapters()
+        {
+            var book = File.ReadAllLines("quo-vadis.txt");
+            
+            var chapterPointers = book
+                .Select((line, index) => new
+                {
+                    LineNumber = index,
+                    Text = line
+                })
+                .Where(x => x.Text.Length < 50 && x.Text.Contains("Rozdział") || x.Text.Contains("-----"))
+                .Select(x => x.LineNumber)
+                .ToList();
+
+            foreach (var index in Enumerable.Range(0, chapterPointers.Count - 1))
+            {
+                var chapter = chapterPointers[index];
+                    
+                var title = book[chapter];
+                    
+                var lineFrom = chapterPointers[index];
+                var lineTo = chapterPointers[index + 1] - 1;
+                    
+                var lines = book.Skip(lineFrom + 1).Take(lineTo - lineFrom);
+                var content = string.Join(Environment.NewLine, lines);
+
+                yield return new BookChapter
+                {
+                    Title = title,
+                    Content = content
+                };
+            }
+        }
+        
+        private void ComposeBook(IContainer container, ICollection<BookChapter> chapters)
+        {
+            var subtitleStyle = TextStyle.Default.Size(24).SemiBold().Color(Colors.Blue.Medium);
+            var normalStyle = TextStyle.Default.Size(14);
+            
+            ComposePage(container);
 
             void ComposePage(IContainer container)
             {
@@ -128,10 +155,10 @@ namespace QuestPDF.Examples
                     
                     foreach (var chapter in chapters)
                     {
-                        stack.Item().InternalLink(chapter.title).Row(row =>
+                        stack.Item().InternalLink(chapter.Title).Row(row =>
                         {
-                            row.RelativeColumn().Text(chapter.title);
-                            row.ConstantColumn(100).AlignRight().Text(text => text.PageNumberOfLocation(chapter.title, normalStyle));
+                            row.RelativeColumn().Text(chapter.Title);
+                            row.ConstantColumn(100).AlignRight().Text(text => text.PageNumberOfLocation(chapter.Title, normalStyle));
                         });
                     }
                 });
@@ -141,7 +168,7 @@ namespace QuestPDF.Examples
             {
                 foreach (var chapter in chapters)
                 {
-                    stack.Item().Element(container => Chapter(container, chapter.title, chapter.content));
+                    stack.Item().Element(container => Chapter(container, chapter.Title, chapter.Content));
                 }
             }
             

+ 1 - 3
QuestPDF.Examples/TextExamples.cs

@@ -1,6 +1,4 @@
-using System;
-using System.IO;
-using System.Linq;
+using System.Linq;
 using NUnit.Framework;
 using QuestPDF.Examples.Engine;
 using QuestPDF.Fluent;

+ 1 - 1
QuestPDF.ReportSample/Tests.cs

@@ -28,7 +28,7 @@ namespace QuestPDF.ReportSample
             // target document length should be around 100 pages
             
             // test size
-            const int testSize = 10;
+            const int testSize = 25;
             const decimal performanceTarget = 1; // documents per second
 
             // create report models

+ 0 - 1
QuestPDF.UnitTests/GridTests.cs

@@ -1,5 +1,4 @@
 using FluentAssertions;
-using FluentAssertions.Equivalency;
 using NUnit.Framework;
 using QuestPDF.Elements;
 using QuestPDF.Fluent;

+ 0 - 1
QuestPDF/Drawing/DocumentContainer.cs

@@ -3,7 +3,6 @@ using System.Collections.Generic;
 using System.Linq;
 using QuestPDF.Elements;
 using QuestPDF.Fluent;
-using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Drawing

+ 0 - 4
QuestPDF/Drawing/DocumentGenerator.cs

@@ -1,14 +1,10 @@
 using System;
 using System.Collections.Generic;
-using System.Diagnostics;
 using System.IO;
 using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Drawing.SpacePlan;
 using QuestPDF.Elements;
-using QuestPDF.Fluent;
-using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
-using SkiaSharp;
 
 namespace QuestPDF.Drawing
 {

+ 0 - 2
QuestPDF/Drawing/DocumentMetadata.cs

@@ -1,6 +1,4 @@
 using System;
-using QuestPDF.Helpers;
-using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Drawing
 {

+ 0 - 3
QuestPDF/Drawing/SkiaCanvasBase.cs

@@ -1,6 +1,3 @@
-using System;
-using System.Collections.Generic;
-using QuestPDF.Elements;
 using QuestPDF.Infrastructure;
 using SkiaSharp;
 

+ 1 - 3
QuestPDF/Drawing/SpacePlan/TextRender.cs

@@ -1,6 +1,4 @@
-using QuestPDF.Infrastructure;
-
-namespace QuestPDF.Drawing.SpacePlan
+namespace QuestPDF.Drawing.SpacePlan
 {
     internal class TextRender : FullRender
     {

+ 1 - 2
QuestPDF/Elements/PageBreak.cs

@@ -1,5 +1,4 @@
-using System;
-using QuestPDF.Drawing.SpacePlan;
+using QuestPDF.Drawing.SpacePlan;
 using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements

+ 0 - 1
QuestPDF/Elements/Row.cs

@@ -2,7 +2,6 @@
 using System.Collections.Generic;
 using System.Linq;
 using QuestPDF.Drawing.SpacePlan;
-using QuestPDF.Fluent;
 using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements

+ 0 - 1
QuestPDF/Elements/SimpleRotate.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Linq;
 using QuestPDF.Drawing.SpacePlan;
 using QuestPDF.Infrastructure;
 

+ 0 - 1
QuestPDF/Elements/Stack.cs

@@ -1,6 +1,5 @@
 using System;
 using System.Collections.Generic;
-using System.ComponentModel;
 using System.Linq;
 using QuestPDF.Drawing.SpacePlan;
 using QuestPDF.Fluent;

+ 1 - 2
QuestPDF/Elements/Text/TextBlock.cs

@@ -1,5 +1,4 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
 using System.Linq;
 using QuestPDF.Drawing.SpacePlan;
 using QuestPDF.Infrastructure;

+ 9 - 10
QuestPDF/Elements/Text/TextItem.cs

@@ -1,7 +1,6 @@
 using System;
 using System.Collections.Generic;
 using QuestPDF.Drawing;
-using QuestPDF.Drawing.SpacePlan;
 using QuestPDF.Infrastructure;
 using Size = QuestPDF.Infrastructure.Size;
 
@@ -28,10 +27,8 @@ namespace QuestPDF.Elements.Text
         
         public int StartIndex { get; set; }
         public int EndIndex { get; set; }
-        
         public int TotalIndex { get; set; }
 
-        public bool HasContent => StartIndex < EndIndex;
         public bool IsLast => EndIndex == TotalIndex;
     }
 
@@ -65,9 +62,14 @@ namespace QuestPDF.Elements.Text
         {
             var cacheKey = (request.StartIndex, request.AvailableWidth);
             
-            if (MeasureCache.ContainsKey(cacheKey))
-                return MeasureCache[cacheKey];
+            if (!MeasureCache.ContainsKey(cacheKey))
+                MeasureCache[cacheKey] = MeasureWithoutCache(request);
             
+            return MeasureCache[cacheKey];
+        }
+        
+        internal TextMeasurementResult? MeasureWithoutCache(TextMeasurementRequest request)
+        {
             var paint = Style.ToPaint();
             var fontMetrics = Style.ToFontMetrics();
             
@@ -94,7 +96,7 @@ namespace QuestPDF.Elements.Text
             // measure final text
             var width = paint.MeasureText(text);
             
-            var result = new TextMeasurementResult
+            return new TextMeasurementResult
             {
                 Width = width,
                 
@@ -107,9 +109,6 @@ namespace QuestPDF.Elements.Text
                 EndIndex = request.StartIndex + breakingIndex,
                 TotalIndex = Text.Length
             };
-
-            MeasureCache[cacheKey] = result;
-            return result;
         }
         
         public void Draw(TextDrawingRequest request)
@@ -143,7 +142,7 @@ namespace QuestPDF.Elements.Text
         
         public TextMeasurementResult? Measure(TextMeasurementRequest request)
         {
-            return GetItem(request.PageContext).Measure(request);
+            return GetItem(request.PageContext).MeasureWithoutCache(request);
         }
 
         public void Draw(TextDrawingRequest request)

+ 1 - 2
QuestPDF/Elements/Unconstrained.cs

@@ -1,5 +1,4 @@
-using System;
-using QuestPDF.Drawing.SpacePlan;
+using QuestPDF.Drawing.SpacePlan;
 using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements

+ 0 - 2
QuestPDF/Fluent/RowExtensions.cs

@@ -1,6 +1,4 @@
 using System;
-using System.Collections.Generic;
-using System.Linq;
 using QuestPDF.Elements;
 using QuestPDF.Infrastructure;
 

+ 0 - 2
QuestPDF/Fluent/StackExtensions.cs

@@ -1,6 +1,4 @@
 using System;
-using System.Collections.Generic;
-using System.Linq;
 using QuestPDF.Elements;
 using QuestPDF.Infrastructure;
 

+ 0 - 1
QuestPDF/Infrastructure/Element.cs

@@ -1,6 +1,5 @@
 using System;
 using QuestPDF.Drawing.SpacePlan;
-using QuestPDF.Elements;
 
 namespace QuestPDF.Infrastructure
 {

+ 0 - 1
QuestPDF/Infrastructure/ICanvas.cs

@@ -1,4 +1,3 @@
-using System.Collections.Generic;
 using SkiaSharp;
 
 namespace QuestPDF.Infrastructure

+ 0 - 1
QuestPDF/Infrastructure/PageContext.cs

@@ -1,6 +1,5 @@
 using System;
 using System.Collections.Generic;
-using QuestPDF.Elements;
 
 namespace QuestPDF.Infrastructure
 {

+ 1 - 2
QuestPDF/Infrastructure/TextStyle.cs

@@ -1,5 +1,4 @@
-using System;
-using QuestPDF.Helpers;
+using QuestPDF.Helpers;
 
 namespace QuestPDF.Infrastructure
 {