Minor refactorization
This commit is contained in:
+74
-69
@@ -79,75 +79,6 @@ namespace QuestPDF.Elements
|
||||
public ICollection<RowElement> Children { get; internal set; } = new List<RowElement>();
|
||||
public float Spacing { get; set; } = 0;
|
||||
|
||||
public Element Compose(float availableWidth)
|
||||
{
|
||||
var elements = ReduceRows(AddSpacing(Children));
|
||||
return BuildTree(elements.ToArray());
|
||||
|
||||
ICollection<Element> ReduceRows(ICollection<RowElement> elements)
|
||||
{
|
||||
var constantWidth = elements
|
||||
.Where(x => x is ConstantRowElement)
|
||||
.Cast<ConstantRowElement>()
|
||||
.Sum(x => x.Width);
|
||||
|
||||
var relativeWidth = elements
|
||||
.Where(x => x is RelativeRowElement)
|
||||
.Cast<RelativeRowElement>()
|
||||
.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<Element>()
|
||||
.ToList();
|
||||
}
|
||||
|
||||
ICollection<RowElement> AddSpacing(ICollection<RowElement> elements)
|
||||
{
|
||||
if (Spacing < Size.Epsilon)
|
||||
return elements;
|
||||
|
||||
return elements
|
||||
.SelectMany(x => new[] { new ConstantRowElement(Spacing), x })
|
||||
.Skip(1)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
Element BuildTree(Span<Element> 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<Element> ReduceRows(ICollection<RowElement> elements, float availableWidth)
|
||||
{
|
||||
var constantWidth = elements
|
||||
.Where(x => x is ConstantRowElement)
|
||||
.Cast<ConstantRowElement>()
|
||||
.Sum(x => x.Width);
|
||||
|
||||
var relativeWidth = elements
|
||||
.Where(x => x is RelativeRowElement)
|
||||
.Cast<RelativeRowElement>()
|
||||
.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<Element>()
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private static ICollection<RowElement> AddSpacing(ICollection<RowElement> 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<Element> 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user