diff --git a/QuestPDF.Examples/BarcodeExamples.cs b/QuestPDF.Examples/BarcodeExamples.cs index 208f979..5711c37 100644 --- a/QuestPDF.Examples/BarcodeExamples.cs +++ b/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 { diff --git a/QuestPDF.Examples/ElementExamples.cs b/QuestPDF.Examples/ElementExamples.cs index 4803557..4fae619 100644 --- a/QuestPDF.Examples/ElementExamples.cs +++ b/QuestPDF.Examples/ElementExamples.cs @@ -1,4 +1,3 @@ -using System; using System.Linq; using NUnit.Framework; using QuestPDF.Examples.Engine; diff --git a/QuestPDF.Examples/Engine/RenderingTest.cs b/QuestPDF.Examples/Engine/RenderingTest.cs index 89d2086..e8aacf2 100644 --- a/QuestPDF.Examples/Engine/RenderingTest.cs +++ b/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 diff --git a/QuestPDF.Examples/TextBenchmark.cs b/QuestPDF.Examples/TextBenchmark.cs index e6ab876..0225508 100644 --- a/QuestPDF.Examples/TextBenchmark.cs +++ b/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 Generate() + { + var chapters = GetBookChapters().ToList(); + + RenderingTest + .Create() + .PageSize(PageSizes.A4) + .FileName() + .ProducePdf() + .ShowResults() + .Render(x => ComposeBook(x, chapters)); + } + [Test] public void Benchmark() { - var subtitleStyle = TextStyle.Default.Size(24).SemiBold().Color(Colors.Blue.Medium); - var normalStyle = TextStyle.Default.Size(14); - - var chapters = GetChapters().ToList(); + 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 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 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 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)); } } diff --git a/QuestPDF.Examples/TextExamples.cs b/QuestPDF.Examples/TextExamples.cs index 5ab3f6a..8789160 100644 --- a/QuestPDF.Examples/TextExamples.cs +++ b/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; diff --git a/QuestPDF.ReportSample/Tests.cs b/QuestPDF.ReportSample/Tests.cs index 08b9995..8624a51 100644 --- a/QuestPDF.ReportSample/Tests.cs +++ b/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 diff --git a/QuestPDF.UnitTests/GridTests.cs b/QuestPDF.UnitTests/GridTests.cs index e52bb57..d14bfe8 100644 --- a/QuestPDF.UnitTests/GridTests.cs +++ b/QuestPDF.UnitTests/GridTests.cs @@ -1,5 +1,4 @@ using FluentAssertions; -using FluentAssertions.Equivalency; using NUnit.Framework; using QuestPDF.Elements; using QuestPDF.Fluent; diff --git a/QuestPDF/Drawing/DocumentContainer.cs b/QuestPDF/Drawing/DocumentContainer.cs index 81414c9..e813839 100644 --- a/QuestPDF/Drawing/DocumentContainer.cs +++ b/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 diff --git a/QuestPDF/Drawing/DocumentGenerator.cs b/QuestPDF/Drawing/DocumentGenerator.cs index 3d9a779..cd60d32 100644 --- a/QuestPDF/Drawing/DocumentGenerator.cs +++ b/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 { diff --git a/QuestPDF/Drawing/DocumentMetadata.cs b/QuestPDF/Drawing/DocumentMetadata.cs index 1f2e2c1..ad30de9 100644 --- a/QuestPDF/Drawing/DocumentMetadata.cs +++ b/QuestPDF/Drawing/DocumentMetadata.cs @@ -1,6 +1,4 @@ using System; -using QuestPDF.Helpers; -using QuestPDF.Infrastructure; namespace QuestPDF.Drawing { diff --git a/QuestPDF/Drawing/SkiaCanvasBase.cs b/QuestPDF/Drawing/SkiaCanvasBase.cs index f6de0ae..9d4b57f 100644 --- a/QuestPDF/Drawing/SkiaCanvasBase.cs +++ b/QuestPDF/Drawing/SkiaCanvasBase.cs @@ -1,6 +1,3 @@ -using System; -using System.Collections.Generic; -using QuestPDF.Elements; using QuestPDF.Infrastructure; using SkiaSharp; diff --git a/QuestPDF/Drawing/SpacePlan/TextRender.cs b/QuestPDF/Drawing/SpacePlan/TextRender.cs index 0288b2e..a4ca9b9 100644 --- a/QuestPDF/Drawing/SpacePlan/TextRender.cs +++ b/QuestPDF/Drawing/SpacePlan/TextRender.cs @@ -1,6 +1,4 @@ -using QuestPDF.Infrastructure; - -namespace QuestPDF.Drawing.SpacePlan +namespace QuestPDF.Drawing.SpacePlan { internal class TextRender : FullRender { diff --git a/QuestPDF/Elements/PageBreak.cs b/QuestPDF/Elements/PageBreak.cs index 11680b9..fd37cd1 100644 --- a/QuestPDF/Elements/PageBreak.cs +++ b/QuestPDF/Elements/PageBreak.cs @@ -1,5 +1,4 @@ -using System; -using QuestPDF.Drawing.SpacePlan; +using QuestPDF.Drawing.SpacePlan; using QuestPDF.Infrastructure; namespace QuestPDF.Elements diff --git a/QuestPDF/Elements/Row.cs b/QuestPDF/Elements/Row.cs index 8ab1f4b..23b109e 100644 --- a/QuestPDF/Elements/Row.cs +++ b/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 diff --git a/QuestPDF/Elements/SimpleRotate.cs b/QuestPDF/Elements/SimpleRotate.cs index 9926138..0d818b9 100644 --- a/QuestPDF/Elements/SimpleRotate.cs +++ b/QuestPDF/Elements/SimpleRotate.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using QuestPDF.Drawing.SpacePlan; using QuestPDF.Infrastructure; diff --git a/QuestPDF/Elements/Stack.cs b/QuestPDF/Elements/Stack.cs index 43227b0..46d1968 100644 --- a/QuestPDF/Elements/Stack.cs +++ b/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; diff --git a/QuestPDF/Elements/Text/TextBlock.cs b/QuestPDF/Elements/Text/TextBlock.cs index 9c8a58a..8391447 100644 --- a/QuestPDF/Elements/Text/TextBlock.cs +++ b/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; diff --git a/QuestPDF/Elements/Text/TextItem.cs b/QuestPDF/Elements/Text/TextItem.cs index a69bcea..d4fd44e 100644 --- a/QuestPDF/Elements/Text/TextItem.cs +++ b/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) diff --git a/QuestPDF/Elements/Unconstrained.cs b/QuestPDF/Elements/Unconstrained.cs index 6734ca2..1522ec2 100644 --- a/QuestPDF/Elements/Unconstrained.cs +++ b/QuestPDF/Elements/Unconstrained.cs @@ -1,5 +1,4 @@ -using System; -using QuestPDF.Drawing.SpacePlan; +using QuestPDF.Drawing.SpacePlan; using QuestPDF.Infrastructure; namespace QuestPDF.Elements diff --git a/QuestPDF/Fluent/RowExtensions.cs b/QuestPDF/Fluent/RowExtensions.cs index 42755d2..6910650 100644 --- a/QuestPDF/Fluent/RowExtensions.cs +++ b/QuestPDF/Fluent/RowExtensions.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using QuestPDF.Elements; using QuestPDF.Infrastructure; diff --git a/QuestPDF/Fluent/StackExtensions.cs b/QuestPDF/Fluent/StackExtensions.cs index 0b9545b..0d23a0c 100644 --- a/QuestPDF/Fluent/StackExtensions.cs +++ b/QuestPDF/Fluent/StackExtensions.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using QuestPDF.Elements; using QuestPDF.Infrastructure; diff --git a/QuestPDF/Infrastructure/Element.cs b/QuestPDF/Infrastructure/Element.cs index 14b8ea7..37e4d55 100644 --- a/QuestPDF/Infrastructure/Element.cs +++ b/QuestPDF/Infrastructure/Element.cs @@ -1,6 +1,5 @@ using System; using QuestPDF.Drawing.SpacePlan; -using QuestPDF.Elements; namespace QuestPDF.Infrastructure { diff --git a/QuestPDF/Infrastructure/ICanvas.cs b/QuestPDF/Infrastructure/ICanvas.cs index 9001161..f5a5ec9 100644 --- a/QuestPDF/Infrastructure/ICanvas.cs +++ b/QuestPDF/Infrastructure/ICanvas.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using SkiaSharp; namespace QuestPDF.Infrastructure diff --git a/QuestPDF/Infrastructure/PageContext.cs b/QuestPDF/Infrastructure/PageContext.cs index 095dfb8..bc2597b 100644 --- a/QuestPDF/Infrastructure/PageContext.cs +++ b/QuestPDF/Infrastructure/PageContext.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using QuestPDF.Elements; namespace QuestPDF.Infrastructure { diff --git a/QuestPDF/Infrastructure/TextStyle.cs b/QuestPDF/Infrastructure/TextStyle.cs index 0d7fdc2..623fa18 100644 --- a/QuestPDF/Infrastructure/TextStyle.cs +++ b/QuestPDF/Infrastructure/TextStyle.cs @@ -1,5 +1,4 @@ -using System; -using QuestPDF.Helpers; +using QuestPDF.Helpers; namespace QuestPDF.Infrastructure {