Fixed: the Alignment element enforces size constraints in both direction, even if only one is provided
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
using NUnit.Framework;
|
||||
using QuestPDF.Examples.Engine;
|
||||
using QuestPDF.Fluent;
|
||||
using QuestPDF.Helpers;
|
||||
|
||||
namespace QuestPDF.Examples
|
||||
{
|
||||
public class AlignmentExamples
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
RenderingTest
|
||||
.Create()
|
||||
.PageSize(400, 200)
|
||||
.ProduceImages()
|
||||
.ShowResults()
|
||||
.Render(container =>
|
||||
{
|
||||
container
|
||||
.Padding(25)
|
||||
.Border(1)
|
||||
.AlignBottom()
|
||||
.Background(Colors.Grey.Lighten1)
|
||||
.Text("Test");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,5 +86,43 @@ namespace QuestPDF.UnitTests
|
||||
.ExpectCanvasTranslate(new Position(-300, 0))
|
||||
.CheckDrawResult();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Draw_HorizontalCenter_VerticalNone()
|
||||
{
|
||||
TestPlan
|
||||
.For(x => new Alignment
|
||||
{
|
||||
Horizontal = HorizontalAlignment.Center,
|
||||
Vertical = null,
|
||||
|
||||
Child = x.CreateChild()
|
||||
})
|
||||
.DrawElement(new Size(400, 300))
|
||||
.ExpectChildMeasure(expectedInput: new Size(400, 300), returns: SpacePlan.FullRender(new Size(100, 50)))
|
||||
.ExpectCanvasTranslate(new Position(150, 0))
|
||||
.ExpectChildDraw(new Size(100, 300))
|
||||
.ExpectCanvasTranslate(new Position(-150, 0))
|
||||
.CheckDrawResult();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Draw_HorizontalNone_VerticalMiddle()
|
||||
{
|
||||
TestPlan
|
||||
.For(x => new Alignment
|
||||
{
|
||||
Horizontal = null,
|
||||
Vertical = VerticalAlignment.Middle,
|
||||
|
||||
Child = x.CreateChild()
|
||||
})
|
||||
.DrawElement(new Size(400, 300))
|
||||
.ExpectChildMeasure(expectedInput: new Size(400, 300), returns: SpacePlan.FullRender(new Size(100, 50)))
|
||||
.ExpectCanvasTranslate(new Position(0, 125))
|
||||
.ExpectChildDraw(new Size(400, 50))
|
||||
.ExpectCanvasTranslate(new Position(0, -125))
|
||||
.CheckDrawResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,19 +6,23 @@ namespace QuestPDF.Elements
|
||||
{
|
||||
internal class Alignment : ContainerElement
|
||||
{
|
||||
public VerticalAlignment Vertical { get; set; } = VerticalAlignment.Top;
|
||||
public HorizontalAlignment Horizontal { get; set; } = HorizontalAlignment.Left;
|
||||
public VerticalAlignment? Vertical { get; set; }
|
||||
public HorizontalAlignment? Horizontal { get; set; }
|
||||
|
||||
internal override void Draw(Size availableSpace)
|
||||
{
|
||||
if (Child == null)
|
||||
return;
|
||||
|
||||
var childSize = base.Measure(availableSpace);
|
||||
var childMeasurement = base.Measure(availableSpace);
|
||||
|
||||
if (childSize.Type == SpacePlanType.Wrap)
|
||||
if (childMeasurement.Type == SpacePlanType.Wrap)
|
||||
return;
|
||||
|
||||
|
||||
var childSize = new Size(
|
||||
Horizontal.HasValue ? childMeasurement.Width : availableSpace.Width,
|
||||
Vertical.HasValue ? childMeasurement.Height : availableSpace.Height);
|
||||
|
||||
var top = GetTopOffset(availableSpace, childSize);
|
||||
var left = GetLeftOffset(availableSpace, childSize);
|
||||
|
||||
@@ -36,7 +40,7 @@ namespace QuestPDF.Elements
|
||||
VerticalAlignment.Top => 0,
|
||||
VerticalAlignment.Middle => difference / 2,
|
||||
VerticalAlignment.Bottom => difference,
|
||||
_ => throw new NotSupportedException()
|
||||
_ => 0
|
||||
};
|
||||
}
|
||||
|
||||
@@ -49,7 +53,7 @@ namespace QuestPDF.Elements
|
||||
HorizontalAlignment.Left => 0,
|
||||
HorizontalAlignment.Center => difference / 2,
|
||||
HorizontalAlignment.Right => difference,
|
||||
_ => throw new NotSupportedException()
|
||||
_ => 0
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,8 +60,8 @@ namespace QuestPDF.Fluent
|
||||
{
|
||||
var descriptor = new GridDescriptor();
|
||||
|
||||
if (element is Alignment alignment)
|
||||
descriptor.Alignment(alignment.Horizontal);
|
||||
if (element is Alignment alignment && alignment.Horizontal.HasValue)
|
||||
descriptor.Alignment(alignment.Horizontal.Value);
|
||||
|
||||
handler(descriptor);
|
||||
element.Component(descriptor.Grid);
|
||||
|
||||
Reference in New Issue
Block a user