Compare commits

..

1 Commits

Author SHA1 Message Date
MarcinZiabek b658344a46 Experimental version of controlling content repeatability 2022-09-21 19:15:11 +02:00
26 changed files with 89 additions and 675 deletions
-146
View File
@@ -1,146 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using QuestPDF.Elements;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
namespace QuestPDF.Examples
{
public class ProcessRunningTime
{
public TimeSpan FluentTime { get; set; }
public TimeSpan GenerationTime { get; set; }
public float Size { get; set; }
}
public class GenerationBenchmark
{
public const int TestSize = 4096;
[Test]
public void BenchmarkAsync()
{
RunTest(() => Enumerable
.Range(0, TestSize)
.AsParallel() // difference
.Select(GenerateAndCollect)
.ToList());
}
[Test]
public void BenchmarkSync()
{
RunTest(() => Enumerable
.Range(0, TestSize)
.Select(GenerateAndCollect)
.ToList());
}
public void RunTest(Func<IEnumerable<ProcessRunningTime>> handler)
{
var totalFluentTime = TimeSpan.Zero;
var totalGenerationTime = TimeSpan.Zero;
var stopWatch = new Stopwatch();
stopWatch.Start();
var results = handler();
stopWatch.Stop();
foreach (var result in results)
{
totalFluentTime += result.FluentTime;
totalGenerationTime += result.GenerationTime;
}
Console.WriteLine($"Fluent: {totalFluentTime:g}");
Console.WriteLine($"Generation: {totalGenerationTime:g}");
Console.WriteLine($"Total: {stopWatch.Elapsed:g}");
}
static ProcessRunningTime GenerateAndCollect(int attemptNumber)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var container = new Container();
container
.Padding(10)
.MinimalBox()
.Border(1)
.Column(column =>
{
column.Item().Text($"Attempts {attemptNumber}");
const int numberOfRows = 100;
const int numberOfColumns = 10;
for (var y = 0; y < numberOfRows; y++)
{
column.Item().Row(row =>
{
for (var x = 0; x < numberOfColumns; x++)
{
row.RelativeItem()
.Background(Colors.Red.Lighten5)
.Padding(3)
.Background(Colors.Red.Lighten4)
.Padding(3)
.Background(Colors.Red.Lighten3)
.Padding(3)
.Background(Colors.Red.Lighten2)
.Padding(3)
.Background(Colors.Red.Lighten1)
.Padding(3)
.Background(Colors.Red.Medium)
.Padding(3)
.Background(Colors.Red.Darken1)
.Padding(3)
.Background(Colors.Red.Darken2)
.Padding(3)
.Background(Colors.Red.Darken3)
.Padding(3)
.Background(Colors.Red.Darken4)
.Height(3);
}
});
}
});
var fluentTime = stopwatch.Elapsed;
stopwatch.Reset();
stopwatch.Start();
var size = Document
.Create(x => x.Page(page => page.Content().Element(container)))
.GeneratePdf()
.Length;
var generationTime = stopwatch.Elapsed;
return new ProcessRunningTime
{
FluentTime = fluentTime,
GenerationTime = generationTime,
Size = size
};
}
}
}
@@ -1,36 +0,0 @@
using NUnit.Framework;
using QuestPDF.Examples.Engine;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
namespace QuestPDF.Examples
{
public class RelativePaddingExamples
{
[Test]
public void ItemTypes()
{
RenderingTest
.Create()
.ProduceImages()
.PageSize(250, 250)
.ShowResults()
.Render(container =>
{
container
.Width(250)
.Height(250)
.Padding(50)
.Background(Colors.Grey.Lighten2)
.RelativePaddingLeft(0.1f)
.RelativePaddingTop(0.2f)
.RelativePaddingRight(0.3f)
.RelativePaddingBottom(0.4f)
.Background(Colors.Grey.Darken2);
});
}
}
}
@@ -1,32 +0,0 @@
using System.Linq;
using NUnit.Framework;
using QuestPDF.Examples.Engine;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
namespace QuestPDF.Examples
{
public class RelativePositionExamples
{
[Test]
public void ItemTypes()
{
RenderingTest
.Create()
.ProduceImages()
.PageSize(500, 500)
.ShowResults()
.Render(container =>
{
container
.Padding(100)
.Background(Colors.Grey.Lighten2)
.RelativePositionVertical(0.5f, -0.5f)
.RelativePositionHorizontal(1f, -0.5f)
.RelativeWidth(0.4f)
.RelativeHeight(0.6f)
.Background(Colors.Grey.Darken2);
});
}
}
}
-38
View File
@@ -1,38 +0,0 @@
using System.Linq;
using NUnit.Framework;
using QuestPDF.Examples.Engine;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
namespace QuestPDF.Examples
{
public class RelativeSizeExamples
{
[Test]
public void ItemTypes()
{
RenderingTest
.Create()
.ProduceImages()
.PageSize(600, 600)
.ShowResults()
.Render(container =>
{
container
.AlignMiddle()
.AlignCenter()
.Width(400)
.Height(400)
.Background(Colors.Grey.Lighten2)
.AlignMiddle()
.AlignCenter()
.Container()
.AlignMiddle()
.AlignCenter()
.RelativeWidth(0.25f)
.RelativeHeight(0.5f)
.Background(Colors.Grey.Darken2);
});
}
}
}
-93
View File
@@ -1,93 +0,0 @@
using NUnit.Framework;
using QuestPDF.Examples.Engine;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
namespace QuestPDF.Examples
{
public class ShrinkExamples
{
[Test]
public void Shrink_Without()
{
RenderingTest
.Create()
.PageSize(300, 200)
.ProduceImages()
.ShowResults()
.Render(container =>
{
container
.Padding(20)
.Border(2)
.Background(Colors.Grey.Lighten2)
.Padding(20)
.Text("This is test.")
.FontSize(20);
});
}
[Test]
public void Shrink_Horizontal()
{
RenderingTest
.Create()
.PageSize(300, 200)
.ProduceImages()
.ShowResults()
.Render(container =>
{
container
.Padding(20)
.Border(2)
.ShrinkHorizontal()
.Background(Colors.Grey.Lighten2)
.Padding(20)
.Text("This is test.")
.FontSize(20);
});
}
[Test]
public void Shrink_Vertical()
{
RenderingTest
.Create()
.PageSize(300, 200)
.ProduceImages()
.ShowResults()
.Render(container =>
{
container
.Padding(20)
.Border(2)
.ShrinkVertical()
.Background(Colors.Grey.Lighten2)
.Padding(20)
.Text("This is test.")
.FontSize(20);
});
}
[Test]
public void Shrink_Both()
{
RenderingTest
.Create()
.PageSize(300, 200)
.ProduceImages()
.ShowResults()
.Render(container =>
{
container
.Padding(20)
.Border(2)
.Shrink()
.Background(Colors.Grey.Lighten2)
.Padding(20)
.Text("This is test.")
.FontSize(20);
});
}
}
}
@@ -7,20 +7,18 @@ using QuestPDF.UnitTests.TestEngine;
namespace QuestPDF.UnitTests
{
[TestFixture]
public class ShrinkTests
public class BoxTests
{
[Test]
public void Measure() => SimpleContainerTests.Measure<Shrink>();
public void Measure() => SimpleContainerTests.Measure<MinimalBox>();
[Test]
public void Draw_Wrap()
{
TestPlan
.For(x => new Shrink
.For(x => new MinimalBox
{
Child = x.CreateChild(),
ShrinkVertical = true,
ShrinkHorizontal = true
Child = x.CreateChild()
})
.DrawElement(new Size(400, 300))
.ExpectChildMeasure(expectedInput: new Size(400, 300), returns: SpacePlan.Wrap())
@@ -31,11 +29,9 @@ namespace QuestPDF.UnitTests
public void Measure_PartialRender()
{
TestPlan
.For(x => new Shrink
.For(x => new MinimalBox
{
Child = x.CreateChild(),
ShrinkVertical = true,
ShrinkHorizontal = true
Child = x.CreateChild()
})
.MeasureElement(new Size(400, 300))
.ExpectChildMeasure(expectedInput: new Size(400, 300), returns: SpacePlan.PartialRender(200, 100))
@@ -47,11 +43,9 @@ namespace QuestPDF.UnitTests
public void Measure_FullRender()
{
TestPlan
.For(x => new Shrink
.For(x => new MinimalBox
{
Child = x.CreateChild(),
ShrinkVertical = true,
ShrinkHorizontal = true
Child = x.CreateChild()
})
.MeasureElement(new Size(500, 400))
.ExpectChildMeasure(expectedInput: new Size(500, 400), returns: SpacePlan.FullRender(300, 200))
+20
View File
@@ -64,6 +64,8 @@ namespace QuestPDF.Drawing
var container = new DocumentContainer();
document.Compose(container);
var content = container.Compose();
ApplyRepeatContent(content);
ApplyDefaultTextStyle(content, TextStyle.LibraryDefault);
var debuggingState = Settings.EnableDebugging ? ApplyDebugging(content) : null;
@@ -192,5 +194,23 @@ namespace QuestPDF.Drawing
foreach (var child in content.GetChildren())
ApplyDefaultTextStyle(child, documentDefaultTextStyle);
}
internal static void ApplyRepeatContent(this Element? content, bool enabled = false)
{
if (content == null)
return;
if (content is RepeatContent repeatContent)
{
ApplyRepeatContent(repeatContent.Child, repeatContent.Repeat);
return;
}
foreach (var child in content.GetChildren())
ApplyRepeatContent(child, enabled);
if (!enabled)
content.CreateProxy(y => y is IContent ? new ShowOnce { Child = y } : y);
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ namespace QuestPDF.Elements
{
public delegate void DrawOnCanvas(SKCanvas canvas, Size availableSpace);
internal class Canvas : Element, ICacheable
internal class Canvas : Element, ICacheable, IContent
{
public DrawOnCanvas Handler { get; set; }
+1 -1
View File
@@ -6,7 +6,7 @@ using QuestPDF.Infrastructure;
namespace QuestPDF.Elements
{
internal class DynamicHost : Element, IStateResettable
internal class DynamicHost : Element, IStateResettable, IContent
{
private DynamicComponentProxy Child { get; }
private object InitialComponentState { get; set; }
+1 -1
View File
@@ -6,7 +6,7 @@ using SkiaSharp;
namespace QuestPDF.Elements
{
internal class DynamicImage : Element
internal class DynamicImage : Element, IContent
{
public Func<Size, byte[]>? Source { get; set; }
+1 -1
View File
@@ -5,7 +5,7 @@ using SkiaSharp;
namespace QuestPDF.Elements
{
internal class Image : Element, ICacheable
internal class Image : Element, ICacheable, IContent
{
public SKImage? InternalImage { get; set; }
+1 -1
View File
@@ -15,7 +15,7 @@ namespace QuestPDF.Elements
Horizontal
}
internal class Line : Element, ILine, ICacheable
internal class Line : Element, ILine, ICacheable, IContent
{
public LineType Type { get; set; } = LineType.Vertical;
public string Color { get; set; } = Colors.Black;
+18
View File
@@ -0,0 +1,18 @@
using QuestPDF.Drawing;
using QuestPDF.Infrastructure;
namespace QuestPDF.Elements
{
internal class MinimalBox : ContainerElement
{
internal override void Draw(Size availableSpace)
{
var targetSize = base.Measure(availableSpace);
if (targetSize.Type == SpacePlanType.Wrap)
return;
base.Draw(targetSize);
}
}
}
-65
View File
@@ -1,65 +0,0 @@
using System;
using QuestPDF.Drawing;
using QuestPDF.Infrastructure;
namespace QuestPDF.Elements
{
internal class RelativePadding : ContainerElement
{
public float Top { get; set; }
public float Right { get; set; }
public float Bottom { get; set; }
public float Left { get; set; }
internal override SpacePlan Measure(Size availableSpace)
{
if (Child == null)
return SpacePlan.FullRender(0, 0);
var internalSpace = InternalSpace(availableSpace);
if (internalSpace.Width < 0 || internalSpace.Height < 0)
return SpacePlan.Wrap();
var measure = base.Measure(internalSpace);
if (measure.Type == SpacePlanType.Wrap)
return SpacePlan.Wrap();
if (measure.Type == SpacePlanType.PartialRender)
return SpacePlan.PartialRender(availableSpace);
if (measure.Type == SpacePlanType.FullRender)
return SpacePlan.FullRender(availableSpace);
throw new NotSupportedException();
}
internal override void Draw(Size availableSpace)
{
if (Child == null)
return;
var internalOffset = InternalOffset(availableSpace);
var internalSpace = InternalSpace(availableSpace);
Canvas.Translate(internalOffset);
base.Draw(internalSpace);
Canvas.Translate(internalOffset.Reverse());
}
private Position InternalOffset(Size availableSpace)
{
return new Position(
availableSpace.Width * Left,
availableSpace.Height * Top);
}
private Size InternalSpace(Size availableSpace)
{
return new Size(
availableSpace.Width * (1f - Left - Right),
availableSpace.Height * (1f - Top - Bottom));
}
}
}
-33
View File
@@ -1,33 +0,0 @@
using System;
using QuestPDF.Drawing;
using QuestPDF.Infrastructure;
namespace QuestPDF.Elements
{
internal class RelativePosition : ContainerElement
{
public float VerticalParent { get; set; }
public float VerticalChild { get; set; }
public float HorizontalParent { get; set; }
public float HorizontalChild { get; set; }
internal override void Draw(Size availableSpace)
{
if (Child == null)
return;
var childSize = base.Measure(availableSpace);
if (childSize.Type == SpacePlanType.Wrap)
return;
var left = availableSpace.Width * HorizontalParent + childSize.Width * HorizontalChild;
var top = availableSpace.Height * VerticalParent + childSize.Height * VerticalChild;
Canvas.Translate(new Position(left, top));
base.Draw(childSize);
Canvas.Translate(new Position(-left, -top));
}
}
}
-36
View File
@@ -1,36 +0,0 @@
using System;
using QuestPDF.Drawing;
using QuestPDF.Infrastructure;
namespace QuestPDF.Elements
{
internal class RelativeSize : ContainerElement
{
public float? WidthFactor { get; set; } = 1f;
public float? HeightFactor { get; set; } = 1f;
internal override SpacePlan Measure(Size availableSpace)
{
var internalSpace = new Size(
availableSpace.Width * (WidthFactor ?? 1),
availableSpace.Height * (HeightFactor ?? 1));
var childSpace = Child?.Measure(internalSpace) ?? SpacePlan.FullRender(0, 0);
if (childSpace.Type == SpacePlanType.Wrap)
return SpacePlan.Wrap();
var targetSpace = new Size(
WidthFactor.HasValue ? internalSpace.Width : childSpace.Width,
HeightFactor.HasValue ? internalSpace.Height : childSpace.Height);
if (childSpace.Type == SpacePlanType.PartialRender)
return SpacePlan.PartialRender(targetSpace);
if (childSpace.Type == SpacePlanType.FullRender)
return SpacePlan.FullRender(targetSpace);
throw new ArgumentException();
}
}
}
+9
View File
@@ -0,0 +1,9 @@
using QuestPDF.Infrastructure;
namespace QuestPDF.Elements
{
internal class RepeatContent : ContainerElement
{
public bool Repeat { get; set; }
}
}
-27
View File
@@ -1,27 +0,0 @@
using QuestPDF.Drawing;
using QuestPDF.Infrastructure;
namespace QuestPDF.Elements
{
internal class Shrink : ContainerElement
{
public bool ShrinkVertical { get; set; }
public bool ShrinkHorizontal { get; set; }
internal override void Draw(Size availableSpace)
{
var childSize = base.Measure(availableSpace);
if (childSize.Type == SpacePlanType.Wrap)
return;
var targetSize = new Size(
ShrinkVertical ? childSize.Width : availableSpace.Width,
ShrinkHorizontal ? childSize.Height : availableSpace.Height);
// TODO: adjust offset for RTL mode
base.Draw(targetSize);
}
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ using QuestPDF.Infrastructure;
namespace QuestPDF.Elements.Text
{
internal class TextBlock : Element, IStateResettable
internal class TextBlock : Element, IStateResettable, IContent
{
public HorizontalAlignment Alignment { get; set; } = HorizontalAlignment.Left;
public List<ITextBlockItem> Items { get; set; } = new List<ITextBlockItem>();
+2 -2
View File
@@ -12,7 +12,7 @@ namespace QuestPDF.Fluent
{
var container = new Container();
Decoration.Before = container;
return container;
return container.RepeatContent();
}
public void Before(Action<IContainer> handler)
@@ -36,7 +36,7 @@ namespace QuestPDF.Fluent
{
var container = new Container();
Decoration.After = container;
return container;
return container.RepeatContent();
}
public void After(Action<IContainer> handler)
+19
View File
@@ -154,6 +154,17 @@ namespace QuestPDF.Fluent
});
}
[Obsolete("This element has been renamed since version 2022.1. Please use the MinimalBox method.")]
public static IContainer Box(this IContainer element)
{
return element.Element(new MinimalBox());
}
public static IContainer MinimalBox(this IContainer element)
{
return element.Element(new MinimalBox());
}
public static IContainer Unconstrained(this IContainer element)
{
return element.Element(new Unconstrained());
@@ -184,5 +195,13 @@ namespace QuestPDF.Fluent
{
return element.Element(new ScaleToFit());
}
public static IContainer RepeatContent(this IContainer element, bool enabled = true)
{
return element.Element(new RepeatContent
{
Repeat = enabled
});
}
}
}
@@ -1,37 +0,0 @@
using System;
using QuestPDF.Elements;
using QuestPDF.Infrastructure;
namespace QuestPDF.Fluent
{
public static class RelativePaddingExtensions
{
private static IContainer RelativePadding(this IContainer element, Action<RelativePadding> handler)
{
var relativePadding = element as RelativePadding ?? new RelativePadding();
handler(relativePadding);
return element.Element(relativePadding);
}
public static IContainer RelativePaddingTop(this IContainer element, float value)
{
return element.RelativePadding(x => x.Top += value);
}
public static IContainer RelativePaddingBottom(this IContainer element, float value)
{
return element.RelativePadding(x => x.Bottom += value);
}
public static IContainer RelativePaddingLeft(this IContainer element, float value)
{
return element.RelativePadding(x => x.Left += value);
}
public static IContainer RelativePaddingRight(this IContainer element, float value)
{
return element.RelativePadding(x => x.Right += value);
}
}
}
@@ -1,35 +0,0 @@
using System;
using QuestPDF.Elements;
using QuestPDF.Infrastructure;
namespace QuestPDF.Fluent
{
public static class RelativePositionExtensions
{
private static IContainer RelativePosition(this IContainer element, Action<RelativePosition> handler)
{
var relativePosition = element as RelativePosition ?? new RelativePosition();
handler(relativePosition);
return element.Element(relativePosition);
}
public static IContainer RelativePositionVertical(this IContainer element, float parentOffset, float childOffset)
{
return element.RelativePosition(x =>
{
x.VerticalParent = parentOffset;
x.VerticalChild = childOffset;
});
}
public static IContainer RelativePositionHorizontal(this IContainer element, float parentOffset, float childOffset)
{
return element.RelativePosition(x =>
{
x.HorizontalParent = parentOffset;
x.HorizontalChild = childOffset;
});
}
}
}
-27
View File
@@ -1,27 +0,0 @@
using System;
using QuestPDF.Elements;
using QuestPDF.Infrastructure;
namespace QuestPDF.Fluent
{
public static class RelativeSizeExtensions
{
private static IContainer RelativeSize(this IContainer element, Action<RelativeSize> handler)
{
var relativeSize = element as RelativeSize ?? new RelativeSize();
handler(relativeSize);
return element.Element(relativeSize);
}
public static IContainer RelativeWidth(this IContainer element, float value)
{
return element.RelativeSize(x => x.WidthFactor = value);
}
public static IContainer RelativeHeight(this IContainer element, float value)
{
return element.RelativeSize(x => x.HeightFactor = value);
}
}
}
-48
View File
@@ -1,48 +0,0 @@
using System;
using QuestPDF.Elements;
using QuestPDF.Infrastructure;
namespace QuestPDF.Fluent
{
public static class ShrinkExtensions
{
private static IContainer Shrink(this IContainer element, Action<Shrink> handler)
{
var shrink = element as Shrink ?? new Shrink();
handler(shrink);
return element.Element(shrink);
}
public static IContainer Shrink(this IContainer element)
{
return element.ShrinkVertical().ShrinkHorizontal();
}
public static IContainer ShrinkVertical(this IContainer element)
{
return element.Shrink(x => x.ShrinkVertical = true);
}
public static IContainer ShrinkHorizontal(this IContainer element)
{
return element.Shrink(x => x.ShrinkHorizontal = true);
}
#region Obsolete
[Obsolete("This element has been renamed since version 2022.1. Please use the Shrink method.")]
public static IContainer Box(this IContainer element)
{
return element.Element(new Shrink());
}
[Obsolete("This element has been renamed since version 2022.11. Please use the Shrink method.")]
public static IContainer MinimalBox(this IContainer element)
{
return element.Element(new Shrink());
}
#endregion
}
}
+7
View File
@@ -0,0 +1,7 @@
namespace QuestPDF.Infrastructure
{
internal interface IContent
{
}
}