Skip to content

Commit

Permalink
fixing cell sizing
Browse files Browse the repository at this point in the history
[patch][fix]
TableCell was not properly sized, resulting in content alignment breaking the clip area.
  • Loading branch information
X39 committed Nov 22, 2023
1 parent 24f3cd7 commit 2d77187
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
9 changes: 6 additions & 3 deletions source/X39.Solutions.PdfTemplate/Controls/TableCellControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class TableCellControl : AlignableContentControl
/// </summary>
[Parameter]
public ColumnLength Width { get; set; } = new();

private readonly List<float> _heights = new();

/// <inheritdoc />
Expand Down Expand Up @@ -56,9 +56,11 @@ protected override Size DoArrange(
_heights.Add(size.Height);
}

var w = Math.Min(width, remainingSize.Width);
var h = Math.Min(height, remainingSize.Height);
return new Size(
Math.Min(width, remainingSize.Width),
Math.Min(height, remainingSize.Height));
HorizontalAlignment is EHorizontalAlignment.Stretch ? Math.Max(w, remainingSize.Width) : w,
VerticalAlignment is EVerticalAlignment.Stretch ? Math.Max(h, remainingSize.Height) : h);
}

/// <inheritdoc />
Expand All @@ -70,6 +72,7 @@ protected override void DoRender(ICanvas canvas, in Size parentSize, CultureInfo
control.Render(canvas, parentSize, cultureInfo);
canvas.Translate(0, height);
}

canvas.PopState();
}

Expand Down
33 changes: 28 additions & 5 deletions test/X39.Solutions.PdfTemplate.Test/Controls/TableControlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public async Task TableWith2X50PXLinesWillScaleToFullPageSize()
// Assert that the two <td> elements take 50% of the width each
mockCanvas.AssertClip(0, new Rectangle(0, 0, 200, 100)); // table
mockCanvas.AssertClip(1, new Rectangle(0, 0, 200, 100)); // tr
mockCanvas.AssertClip(2, new Rectangle(0, 0, 50, 100)); // td
mockCanvas.AssertClip(2, new Rectangle(0, 0, 100, 100)); // td
mockCanvas.AssertClip(3, new Rectangle(0, 0, 50, 100)); // line
mockCanvas.AssertClip(4, new Rectangle(100, 0, 50, 100)); // td
mockCanvas.AssertClip(4, new Rectangle(100, 0, 100, 100)); // td
mockCanvas.AssertClip(5, new Rectangle(100, 0, 50, 100)); // line
}
[Theory]
Expand Down Expand Up @@ -129,7 +129,7 @@ @for i from 0 to {{amount}} {
for (var i = 0; i < amount; i++)
{
var width = 200F / amount;
mockCanvas.AssertClip(2 + i, new Rectangle(width * i, 0, 0, 0)); // td
mockCanvas.AssertClip(2 + i, new Rectangle(width * i, 0, width, 0)); // td
}
}
[Fact]
Expand All @@ -153,8 +153,31 @@ public async Task TableWith2EmptyColsWillScaleToFullPageSizeEachColHalf()
// Assert that the two <td> elements take 50% of the width each
mockCanvas.AssertClip(0, new Rectangle(0, 0, 200, 0)); // table
mockCanvas.AssertClip(1, new Rectangle(0, 0, 200, 0)); // tr
mockCanvas.AssertClip(2, new Rectangle(0, 0, 0, 0)); // td
mockCanvas.AssertClip(3, new Rectangle(100, 0, 0, 0)); // td
mockCanvas.AssertClip(2, new Rectangle(0, 0, 100, 0)); // td
mockCanvas.AssertClip(3, new Rectangle(100, 0, 100, 0)); // td
}
[Fact]
public async Task RightAlignedContentIsNotClippedAway()
{
var control = await CreateControl<TableControl>(
$$"""
<table>
<tr>
<td><line horizontalAlignment="right" length="20px" thickness="20px"/></td>
</tr>
</table>
""");
var pageSize = new Size(200, 200);
var mockCanvas = new CanvasMock();
control.Measure(pageSize, pageSize, pageSize, CultureInfo.InvariantCulture);
control.Arrange(pageSize, pageSize, pageSize, CultureInfo.InvariantCulture);
control.Render(mockCanvas, pageSize, CultureInfo.InvariantCulture);
mockCanvas.AssertState();
// Assert that the two <td> elements take 50% of the width each
mockCanvas.AssertClip(0, new Rectangle(0, 0, 200, 20)); // table
mockCanvas.AssertClip(1, new Rectangle(0, 0, 200, 20)); // tr
mockCanvas.AssertClip(2, new Rectangle(0, 0, 200, 20)); // td
mockCanvas.AssertClip(3, new Rectangle(180, 0, 20, 20)); // line
}

private async Task<T> CreateControl<T>([LanguageInjection(InjectedLanguage.XML)] string template) where T : IControl
Expand Down

0 comments on commit 2d77187

Please sign in to comment.