From fa3d912e4108fdac437a2c83503347d4ae686056 Mon Sep 17 00:00:00 2001 From: MarcinZiabek Date: Tue, 4 Oct 2022 22:26:25 +0200 Subject: [PATCH] RelativeSize, RelativePosition --- QuestPDF.Examples/RelativePositionExamples.cs | 32 ++++++++++++++++ QuestPDF.Examples/RelativeSizeExamples.cs | 38 +++++++++++++++++++ QuestPDF/Elements/RelativePosition.cs | 33 ++++++++++++++++ QuestPDF/Elements/RelativeSize.cs | 36 ++++++++++++++++++ QuestPDF/Fluent/RelativePositionExtensions.cs | 35 +++++++++++++++++ QuestPDF/Fluent/RelativeSizeExtensions.cs | 27 +++++++++++++ 6 files changed, 201 insertions(+) create mode 100644 QuestPDF.Examples/RelativePositionExamples.cs create mode 100644 QuestPDF.Examples/RelativeSizeExamples.cs create mode 100644 QuestPDF/Elements/RelativePosition.cs create mode 100644 QuestPDF/Elements/RelativeSize.cs create mode 100644 QuestPDF/Fluent/RelativePositionExtensions.cs create mode 100644 QuestPDF/Fluent/RelativeSizeExtensions.cs diff --git a/QuestPDF.Examples/RelativePositionExamples.cs b/QuestPDF.Examples/RelativePositionExamples.cs new file mode 100644 index 0000000..1f168a4 --- /dev/null +++ b/QuestPDF.Examples/RelativePositionExamples.cs @@ -0,0 +1,32 @@ +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); + }); + } + } +} \ No newline at end of file diff --git a/QuestPDF.Examples/RelativeSizeExamples.cs b/QuestPDF.Examples/RelativeSizeExamples.cs new file mode 100644 index 0000000..5cf4ed9 --- /dev/null +++ b/QuestPDF.Examples/RelativeSizeExamples.cs @@ -0,0 +1,38 @@ +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); + }); + } + } +} \ No newline at end of file diff --git a/QuestPDF/Elements/RelativePosition.cs b/QuestPDF/Elements/RelativePosition.cs new file mode 100644 index 0000000..ae32561 --- /dev/null +++ b/QuestPDF/Elements/RelativePosition.cs @@ -0,0 +1,33 @@ +using System; +using QuestPDF.Drawing; +using QuestPDF.Infrastructure; + +namespace QuestPDF.Elements +{ + internal class RelativePosition : ContainerElement + { + public float VerticalParent { get; set; } + public float VerticalSelf { get; set; } + + public float HorizontalParent { get; set; } + public float HorizontalSelf { 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 * HorizontalSelf; + var top = availableSpace.Height * VerticalParent + childSize.Height * VerticalSelf; + + Canvas.Translate(new Position(left, top)); + base.Draw(childSize); + Canvas.Translate(new Position(-left, -top)); + } + } +} \ No newline at end of file diff --git a/QuestPDF/Elements/RelativeSize.cs b/QuestPDF/Elements/RelativeSize.cs new file mode 100644 index 0000000..bae0020 --- /dev/null +++ b/QuestPDF/Elements/RelativeSize.cs @@ -0,0 +1,36 @@ +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(); + } + } +} \ No newline at end of file diff --git a/QuestPDF/Fluent/RelativePositionExtensions.cs b/QuestPDF/Fluent/RelativePositionExtensions.cs new file mode 100644 index 0000000..2299bba --- /dev/null +++ b/QuestPDF/Fluent/RelativePositionExtensions.cs @@ -0,0 +1,35 @@ +using System; +using QuestPDF.Elements; +using QuestPDF.Infrastructure; + +namespace QuestPDF.Fluent +{ + public static class RelativePositionExtensions + { + private static IContainer RelativePosition(this IContainer element, Action handler) + { + var relativePosition = element as RelativePosition ?? new RelativePosition(); + handler(relativePosition); + + return element.Element(relativePosition); + } + + public static IContainer RelativePositionVertical(this IContainer element, float parentOffset, float selfOffset) + { + return element.RelativePosition(x => + { + x.VerticalParent = parentOffset; + x.VerticalSelf = selfOffset; + }); + } + + public static IContainer RelativePositionHorizontal(this IContainer element, float parentOffset, float selfOffset) + { + return element.RelativePosition(x => + { + x.HorizontalParent = parentOffset; + x.HorizontalSelf = selfOffset; + }); + } + } +} \ No newline at end of file diff --git a/QuestPDF/Fluent/RelativeSizeExtensions.cs b/QuestPDF/Fluent/RelativeSizeExtensions.cs new file mode 100644 index 0000000..c3f729e --- /dev/null +++ b/QuestPDF/Fluent/RelativeSizeExtensions.cs @@ -0,0 +1,27 @@ +using System; +using QuestPDF.Elements; +using QuestPDF.Infrastructure; + +namespace QuestPDF.Fluent +{ + public static class RelativeSizeExtensions + { + private static IContainer RelativeSize(this IContainer element, Action 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); + } + } +} \ No newline at end of file