diff --git a/QuestPDF/Elements/Row.cs b/QuestPDF/Elements/Row.cs index 118f7c6..c40fc45 100644 --- a/QuestPDF/Elements/Row.cs +++ b/QuestPDF/Elements/Row.cs @@ -79,75 +79,6 @@ namespace QuestPDF.Elements public ICollection Children { get; internal set; } = new List(); public float Spacing { get; set; } = 0; - public Element Compose(float availableWidth) - { - var elements = ReduceRows(AddSpacing(Children)); - return BuildTree(elements.ToArray()); - - ICollection ReduceRows(ICollection elements) - { - var constantWidth = elements - .Where(x => x is ConstantRowElement) - .Cast() - .Sum(x => x.Width); - - var relativeWidth = elements - .Where(x => x is RelativeRowElement) - .Cast() - .Sum(x => x.Width); - - var widthPerRelativeUnit = (availableWidth - constantWidth) / relativeWidth; - - return elements - .Select(x => - { - if (x is RelativeRowElement r) - return new ConstantRowElement(r.Width * widthPerRelativeUnit) - { - Child = x.Child - }; - - return x; - }) - .Select(x => new Constrained - { - MinWidth = x.Width, - MaxWidth = x.Width, - Child = x.Child - }) - .Cast() - .ToList(); - } - - ICollection AddSpacing(ICollection elements) - { - if (Spacing < Size.Epsilon) - return elements; - - return elements - .SelectMany(x => new[] { new ConstantRowElement(Spacing), x }) - .Skip(1) - .ToList(); - } - - Element BuildTree(Span elements) - { - if (elements.IsEmpty) - return Empty.Instance; - - if (elements.Length == 1) - return elements[0]; - - var half = elements.Length / 2; - - return new SimpleRow - { - Left = BuildTree(elements.Slice(0, half)), - Right = BuildTree(elements.Slice(half)) - }; - } - } - internal override ISpacePlan Measure(Size availableSpace) { return Compose(availableSpace.Width).Measure(availableSpace); @@ -157,5 +88,79 @@ namespace QuestPDF.Elements { Compose(availableSpace.Width).Draw(canvas, availableSpace); } + + #region structure + + private Element Compose(float availableWidth) + { + var elements = AddSpacing(Children, Spacing); + var rowElements = ReduceRows(elements, availableWidth); + return BuildTree(rowElements.ToArray()); + } + + private static ICollection ReduceRows(ICollection elements, float availableWidth) + { + var constantWidth = elements + .Where(x => x is ConstantRowElement) + .Cast() + .Sum(x => x.Width); + + var relativeWidth = elements + .Where(x => x is RelativeRowElement) + .Cast() + .Sum(x => x.Width); + + var widthPerRelativeUnit = (availableWidth - constantWidth) / relativeWidth; + + return elements + .Select(x => + { + if (x is RelativeRowElement r) + return new ConstantRowElement(r.Width * widthPerRelativeUnit) + { + Child = x.Child + }; + + return x; + }) + .Select(x => new Constrained + { + MinWidth = x.Width, + MaxWidth = x.Width, + Child = x.Child + }) + .Cast() + .ToList(); + } + + private static ICollection AddSpacing(ICollection elements, float spacing) + { + if (spacing < Size.Epsilon) + return elements; + + return elements + .SelectMany(x => new[] { new ConstantRowElement(spacing), x }) + .Skip(1) + .ToList(); + } + + private static Element BuildTree(Span elements) + { + if (elements.IsEmpty) + return Empty.Instance; + + if (elements.Length == 1) + return elements[0]; + + var half = elements.Length / 2; + + return new SimpleRow + { + Left = BuildTree(elements.Slice(0, half)), + Right = BuildTree(elements.Slice(half)) + }; + } + + #endregion } } \ No newline at end of file