Skip to content

Commit 2f557bc

Browse files
jakejackson1claude
andauthored
Keep everything in the last column when balancing columns (#475) (#488)
* Keep everything in the last column when balancing columns Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Add a snapshot of balanced columns closing on a block that ends in a child block Each section closes two balanced columns on a panel with a background, no bottom padding or border, and a sign-off block at its end, so the panel's closing rectangle starts at the foot of the content. The last section is right to left. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 52319dd commit 2f557bc

5 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎src/Mpdf.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25565,7 +25565,8 @@ function printcolumnbuffer()
2556525565
$newcolumn = 0;
2556625566
foreach ($this->columnbuffer as $key => $s) {
2556725567
if (isset($s['rel_y'])) { // only process position sensitive data
25568-
if ($s['rel_y'] >= $cbr[$newcolumn]) {
25568+
// The last break is the foot of the content, so whatever sits at or below it stays in the last column
25569+
if ($newcolumn < $this->NbCol - 1 && $s['rel_y'] >= $cbr[$newcolumn]) {
2556925570
$newcolumn++;
2557025571
} else {
2557125572
$newcolumn = $last_new_col;

‎tests/Mpdf/ColumnBalanceTest.php‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* Balancing columns at their end keeps everything from the last break down in the last column
7+
*/
8+
class ColumnBalanceTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
9+
{
10+
use PageStreams;
11+
12+
/**
13+
* Blocks with a background whose last child is a block, so the background's closing rectangle starts at the
14+
* foot of the content
15+
*
16+
* @return string[][]
17+
*/
18+
public function blocksEndingInAChild()
19+
{
20+
$text = str_repeat('Lorem ipsum dolor sit amet, consectetur adipiscing elit. ', 4);
21+
22+
return [
23+
'text then a div' => ['<div style="background-color: #ddd">' . $text . '<div>Last line</div></div>'],
24+
'paragraphs then a div' => [
25+
'<div style="background-color: #ddd">'
26+
. str_repeat('<p style="margin: 0; text-indent: 5mm">' . $text . '</p>', 2)
27+
. '<div>Last line</div></div>',
28+
],
29+
];
30+
}
31+
32+
/**
33+
* @dataProvider blocksEndingInAChild
34+
*
35+
* @param string $block
36+
*/
37+
public function testBalancingRaisesNoWarning($block)
38+
{
39+
$this->assertDrawsSilently(function (Mpdf $mpdf) use ($block) {
40+
$mpdf->WriteHTML($this->balanced($block));
41+
});
42+
}
43+
44+
/**
45+
* @dataProvider blocksEndingInAChild
46+
*
47+
* @param string $block
48+
*/
49+
public function testNoRectangleLandsRightOfTheLastColumn($block)
50+
{
51+
$mpdf = $this->mpdf();
52+
$mpdf->WriteHTML($this->balanced($block));
53+
$right = ($mpdf->w - $mpdf->rMargin) * Mpdf::SCALE;
54+
$pages = $this->pages($this->output($mpdf));
55+
56+
preg_match_all('/(-?\d+\.\d+) -?\d+\.\d+ (-?\d+\.\d+) -?\d+\.\d+ re/', $pages[0], $rectangles, PREG_SET_ORDER);
57+
58+
$this->assertNotEmpty($rectangles);
59+
foreach ($rectangles as $rectangle) {
60+
$this->assertLessThanOrEqual($right + 0.01, $rectangle[1] + $rectangle[2], $rectangle[0]);
61+
}
62+
}
63+
64+
/**
65+
* $block in two columns that are balanced when they close
66+
*
67+
* @param string $block
68+
*
69+
* @return string
70+
*/
71+
private function balanced($block)
72+
{
73+
return '<columns column-count="2" />' . $block . '<columns column-count="0" />';
74+
}
75+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Snapshots;
4+
5+
/**
6+
* Balanced columns that close on a block with a background whose last child is a block, left to right and right to
7+
* left. The background's closing rectangle stays in the last column rather than one column past it (#475).
8+
*
9+
* @group snapshot
10+
*/
11+
class ColumnBalanceSnapshotTest extends Snapshot
12+
{
13+
14+
/**
15+
* @return string
16+
*/
17+
public function getId()
18+
{
19+
return 'column-balance';
20+
}
21+
22+
/**
23+
* Three two-column sections, the last right to left, each balanced as it closes on a panel that ends in a sign-off block
24+
*/
25+
public function generatePdf()
26+
{
27+
$this->mpdf = $this->createMpdf();
28+
29+
$text = str_repeat('Lorem ipsum dolor sit amet, consectetur adipiscing elit. ', 4);
30+
// No bottom padding or border, so the panel's closing rectangle starts at the foot of the sign-off
31+
$panel = 'background-color: #9bc4e2; padding: 2mm 2mm 0 2mm';
32+
$signOff = '<div style="font-weight: bold">Sign-off</div>';
33+
34+
$this->mpdf->WriteHTML('<h3>Text, then a block</h3>');
35+
$this->mpdf->WriteHTML($this->balanced('<div style="' . $panel . '">' . $text . $signOff . '</div>'));
36+
37+
$this->mpdf->WriteHTML('<h3>Indented paragraphs, then a block</h3>');
38+
$this->mpdf->WriteHTML($this->balanced(
39+
'<div style="' . $panel . '">'
40+
. str_repeat('<p style="margin: 0; text-indent: 5mm">' . $text . '</p>', 2)
41+
. $signOff . '</div>'
42+
));
43+
44+
$this->mpdf->WriteHTML('<h3>Right to left</h3>');
45+
$this->mpdf->SetDirectionality('rtl');
46+
$this->mpdf->WriteHTML($this->balanced('<div style="' . $panel . '">' . $text . $signOff . '</div>'));
47+
}
48+
49+
/**
50+
* $block in two columns that are balanced when they close
51+
*
52+
* @param string $block
53+
*
54+
* @return string
55+
*/
56+
private function balanced($block)
57+
{
58+
return '<columns column-count="2" column-gap="6" />' . $block . '<columns column-count="0" />';
59+
}
60+
61+
}
33.5 KB
Binary file not shown.

‎tests/data/snapshots/rtl.pdf‎

-2 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)