Skip to content

Commit 1678071

Browse files
jakejackson1claude
andcommitted
Share one helper for how far a right float moved and tidy the float tests
indentMoved() replaces the indent bookkeeping pasted into turnPageKeepingPlace() and four places in printbuffer(), and Ln() calls blockIndent() instead of its own copy. moveIntoFrame() names the margins it widens rather than building the keys, and moveToMadePage() restores the page's margins itself. The tests group spans through one helper, share the @page rule the float tests run over, build their provider strings without a closure, and ask plainly that most lines of a column reach its edge. Output is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e7e997b commit 1678071

2 files changed

Lines changed: 80 additions & 99 deletions

File tree

‎src/Mpdf.php‎

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4015,8 +4015,7 @@ function turnPageKeepingPlace()
40154015

40164016
$this->AddPage($this->CurOrientation);
40174017

4018-
// A right float also moves across within a frame that has changed width
4019-
$shift = $this->MarginCorrection + $this->blockIndent() - $indent;
4018+
$shift = $this->MarginCorrection + $this->indentMoved($indent);
40204019
$this->x = $x + $shift;
40214020
$this->SetSpacing($charspacing, $ws);
40224021

@@ -4033,6 +4032,22 @@ function blockIndent()
40334032
return isset($this->blk[$this->blklvl]['outer_left_margin']) ? $this->blk[$this->blklvl]['outer_left_margin'] : 0;
40344033
}
40354034

4035+
/**
4036+
* How far the block being written has moved across since $indent was taken. Only a right float moves like this,
4037+
* when the frame changes width under it.
4038+
*
4039+
* @param float $indent An earlier blockIndent(), updated to the current one
4040+
*
4041+
* @return float
4042+
*/
4043+
function indentMoved(&$indent)
4044+
{
4045+
$was = $indent;
4046+
$indent = $this->blockIndent();
4047+
4048+
return $indent - $was;
4049+
}
4050+
40364051
/**
40374052
* Whether a :first, :left or :right rule for the current page sets a side margin
40384053
*
@@ -4102,39 +4117,40 @@ function moveIntoFrame($previousFrame)
41024117
return 0;
41034118
}
41044119

4105-
// The first block with a set width keeps it and takes the growth in its margin on the side it is not anchored
4106-
// to: the left of a right float, else the right. Every block inside it moves with it.
4107-
$side = null;
4120+
// A block with a set width keeps it. The growth goes into its margin on the side it is not anchored to, the left
4121+
// of a right float and the right of anything else, and every block inside it moves with it.
4122+
$outerMargin = null;
41084123
for ($bl = 1; $bl <= $this->blklvl; $bl++) {
41094124
if (!isset($this->blk[$bl]['width'])) {
41104125
continue;
41114126
}
41124127

41134128
$blk = &$this->blk[$bl];
41144129

4115-
if (!$side && !empty($blk['css_set_width'])) {
4116-
$side = isset($blk['float']) && $blk['float'] === 'R' ? 'left' : 'right';
4117-
$blk['margin_' . $side] += $growth;
4130+
if (!$outerMargin && !empty($blk['css_set_width'])) {
4131+
$rightFloat = isset($blk['float']) && $blk['float'] === 'R';
4132+
$blk[$rightFloat ? 'margin_left' : 'margin_right'] += $growth;
4133+
$outerMargin = $rightFloat ? 'outer_left_margin' : 'outer_right_margin';
41184134
}
41194135

4120-
if (!$side) {
4136+
if (!$outerMargin) {
41214137
$blk['width'] += $growth;
41224138
$blk['inner_width'] += $growth;
41234139
} else {
4124-
$blk['outer_' . $side . '_margin'] += $growth;
4125-
if ($side === 'left' && isset($blk['x0'])) {
4140+
$blk[$outerMargin] += $growth;
4141+
if ($outerMargin === 'outer_left_margin' && isset($blk['x0'])) {
41264142
$blk['x0'] += $growth;
41274143
}
41284144
}
41294145
unset($blk);
41304146
}
41314147

4132-
return $side ? 0 : $growth;
4148+
return $outerMargin ? 0 : $growth;
41334149
}
41344150

41354151
/**
4136-
* Move to a page already made, into the frame it was made with: back to the page a float started on, or on over the
4137-
* pages it made, as the text beside it does
4152+
* Move to a page that already exists, into the frame it was made with. Text beside a float needs this: it goes back
4153+
* to the page the float started on, and on over the pages the float made.
41384154
*
41394155
* @param int $page
41404156
*
@@ -4145,7 +4161,9 @@ function moveToMadePage($page)
41454161
$previousFrame = $this->textFrame();
41464162

41474163
$this->page = $page;
4148-
$this->restoreSideMargins();
4164+
if (isset($this->pageDim[$page]['sideMargins'])) {
4165+
list($this->DeflMargin, $this->DefrMargin) = $this->pageDim[$page]['sideMargins'];
4166+
}
41494167
$this->ResetMargins();
41504168

41514169
if ($this->textFrame() == $previousFrame) {
@@ -4157,16 +4175,6 @@ function moveToMadePage($page)
41574175
return $this->moveIntoFrame($previousFrame);
41584176
}
41594177

4160-
/**
4161-
* Put back the side margins the current page was made with
4162-
*/
4163-
function restoreSideMargins()
4164-
{
4165-
if (isset($this->pageDim[$this->page]['sideMargins'])) {
4166-
list($this->DeflMargin, $this->DefrMargin) = $this->pageDim[$this->page]['sideMargins'];
4167-
}
4168-
}
4169-
41704178
/**
41714179
* Give the rest of the block being written the width it gained on the new page
41724180
*
@@ -10608,9 +10616,7 @@ function Ln($h = '', $collapsible = 0)
1060810616
// Added collapsible to allow collapsible top-margin on new page
1060910617
// Line feed; default value is last cell height
1061010618

10611-
$margin = isset($this->blk[$this->blklvl]['outer_left_margin']) ? $this->blk[$this->blklvl]['outer_left_margin'] : 0;
10612-
10613-
$this->x = $this->lMargin + $margin;
10619+
$this->x = $this->lMargin + $this->blockIndent();
1061410620

1061510621
if ($collapsible && ($this->y == $this->tMargin) && (!$this->ColActive)) {
1061610622
$h = 0;
@@ -17691,9 +17697,7 @@ function printbuffer($arrayaux, $blockstate = 0, $is_table = false, $table_draft
1769117697

1769217698
// Added to correct for OddEven Margins
1769317699
if ($this->page != $oldpage) {
17694-
// A right float moves across within a frame that has changed width
17695-
$bak_x += $this->blockIndent() - $indent;
17696-
$indent = $this->blockIndent();
17700+
$bak_x += $this->indentMoved($indent);
1769717701
if (($this->page - $oldpage) % 2 == 1) {
1769817702
$bak_x += $this->MarginCorrection;
1769917703
}
@@ -17870,9 +17874,7 @@ function printbuffer($arrayaux, $blockstate = 0, $is_table = false, $table_draft
1787017874
}
1787117875
// Added to correct for OddEven Margins
1787217876
if ($this->page != $oldpage) {
17873-
// A right float moves across within a frame that has changed width
17874-
$bak_x += $this->blockIndent() - $indent;
17875-
$indent = $this->blockIndent();
17877+
$bak_x += $this->indentMoved($indent);
1787617878
if (($this->page - $oldpage) % 2 == 1) {
1787717879
$bak_x += $this->MarginCorrection;
1787817880
}
@@ -17904,9 +17906,7 @@ function printbuffer($arrayaux, $blockstate = 0, $is_table = false, $table_draft
1790417906
$this->WriteFlowingBlock($vetor[0], $vetor[18]); // mPDF 5.7.1
1790517907
// Added to correct for OddEven Margins
1790617908
if ($this->page != $oldpage) {
17907-
// A right float moves across within a frame that has changed width
17908-
$bak_x += $this->blockIndent() - $indent;
17909-
$indent = $this->blockIndent();
17909+
$bak_x += $this->indentMoved($indent);
1791017910
if (($this->page - $oldpage) % 2 == 1) {
1791117911
$bak_x += $this->MarginCorrection;
1791217912
$this->x = $bak_x;
@@ -17941,9 +17941,7 @@ function printbuffer($arrayaux, $blockstate = 0, $is_table = false, $table_draft
1794117941
$this->finishFlowingBlock(true); // true = END of flowing block
1794217942
// Added to correct for OddEven Margins
1794317943
if ($this->page != $oldpage) {
17944-
// A right float moves across within a frame that has changed width
17945-
$bak_x += $this->blockIndent() - $indent;
17946-
$indent = $this->blockIndent();
17944+
$bak_x += $this->indentMoved($indent);
1794717945
if (($this->page - $oldpage) % 2 == 1) {
1794817946
$bak_x += $this->MarginCorrection;
1794917947
$this->x = $bak_x;

‎tests/Mpdf/PseudoPageFrameTest.php‎

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
class PseudoPageFrameTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
1111
{
1212

13+
/** A frame 150mm wide on a right page and 170mm on a left page, for a float to run over. */
14+
const WIDER_LEFT_PAGES = '@page { margin-left: 30mm; margin-right: 30mm; } @page :left { margin-left: 20mm; margin-right: 20mm; }';
15+
1316
/**
1417
* A story on a named page starts beside a photograph and carries on over the full width
1518
*/
@@ -118,8 +121,8 @@ public function testColumnsAreLaidOutAcrossTheFrameOfEachPage()
118121
}
119122

120123
/**
121-
* A float and the text beside it run over pages of different widths, the text flowing back onto pages the float
122-
* has already made
124+
* Text beside a float that runs over pages of different widths is set in the frame of each page, including the
125+
* pages the float made before the text reached them
123126
*
124127
* @dataProvider floatProvider
125128
*
@@ -129,20 +132,16 @@ public function testColumnsAreLaidOutAcrossTheFrameOfEachPage()
129132
*/
130133
public function testTextBesideAFloatTakesTheFrameOfEachPage($body, $rightPage, $leftPage)
131134
{
132-
$mpdf = $this->write(
133-
'@page { margin-left: 30mm; margin-right: 30mm; }
134-
@page :left { margin-left: 20mm; margin-right: 20mm; }',
135-
$body
136-
);
135+
$mpdf = $this->write(self::WIDER_LEFT_PAGES, $body);
137136

138137
$this->assertColumns($mpdf, 1, $rightPage);
139138
$this->assertColumns($mpdf, 2, $leftPage);
140139
$this->assertColumns($mpdf, 3, $rightPage);
141140
}
142141

143142
/**
144-
* A float 40% of a frame 150mm wide on a right page, so 60mm wide on the 170mm of a left page too, followed by a
145-
* block beside it or inside the same block as it
143+
* Left and right floats, each followed by a block of text or inside the same block as the text. A float is 40% of
144+
* the 150mm frame of a right page, so 60mm wide on every page.
146145
*
147146
* @return array[]
148147
*/
@@ -172,42 +171,38 @@ public function floatProvider()
172171
*/
173172
public function testAFloatAndTheBlockAroundItArePaintedInTheFrameOfEachPage($body, $painted)
174173
{
175-
$mpdf = $this->write(
176-
'@page { margin-left: 30mm; margin-right: 30mm; }
177-
@page :left { margin-left: 20mm; margin-right: 20mm; }',
178-
$body
179-
);
174+
$mpdf = $this->write(self::WIDER_LEFT_PAGES, $body);
180175

181176
$this->assertSame($painted, $this->painted($mpdf));
182177
}
183178

184179
/**
185-
* A block that runs on past its right float, one that ends beside it and so goes on below it, the float alone, and
186-
* a block that follows the float on the page it ends on: cleared of it, or written by the next call
180+
* A right float, with the background on the block around it (which runs on past the float, or ends beside it), on
181+
* the float, or on a block that follows it (cleared of it, or written by a later WriteHTML() call)
187182
*
188183
* @return array[]
189184
*/
190185
public function paintedFloatProvider()
191186
{
192-
$float = function ($style) {
193-
return '<div style="float: right; width: 40%; ' . $style . '">' . $this->story(12) . '</div>';
194-
};
187+
$float = '<div style="float: right; width: 40%">' . $this->story(12) . '</div>';
188+
$paintedFloat = '<div style="float: right; width: 40%; background: #0f0">' . $this->story(12) . '</div>';
195189
$frames = [1 => [30.0, 180.0], 2 => [20.0, 190.0], 3 => [30.0, 180.0], 4 => [20.0, 190.0]];
190+
$afterFloat = [4 => [20.0, 190.0], 5 => [30.0, 180.0]];
196191

197192
return [
198-
'block running past the float' => ['<div style="background: #0f0">' . $float('') . $this->story(20) . '</div>', $frames],
199-
'block ending beside the float' => ['<div style="background: #0f0">' . $float('') . $this->story(2) . '</div>' . $this->story(10), $frames],
193+
'block running past the float' => ['<div style="background: #0f0">' . $float . $this->story(20) . '</div>', $frames],
194+
'block ending beside the float' => ['<div style="background: #0f0">' . $float . $this->story(2) . '</div>' . $this->story(10), $frames],
200195
'float' => [
201-
'<div>' . $float('background: #0f0') . $this->story(20) . '</div>',
196+
'<div>' . $paintedFloat . $this->story(20) . '</div>',
202197
[1 => [120.0, 180.0], 2 => [130.0, 190.0], 3 => [120.0, 180.0], 4 => [130.0, 190.0]],
203198
],
204199
'block cleared of the float' => [
205-
$float('') . $this->story(2) . '<div style="clear: both; background: #0f0">' . $this->story(10) . '</div>',
206-
[4 => [20.0, 190.0], 5 => [30.0, 180.0]],
200+
$float . $this->story(2) . '<div style="clear: both; background: #0f0">' . $this->story(10) . '</div>',
201+
$afterFloat,
207202
],
208203
'block written after the float' => [
209-
[$float('') . $this->story(2), '<div style="background: #0f0">' . $this->story(10) . '</div>'],
210-
[4 => [20.0, 190.0], 5 => [30.0, 180.0]],
204+
[$float . $this->story(2), '<div style="background: #0f0">' . $this->story(10) . '</div>'],
205+
$afterFloat,
211206
],
212207
];
213208
}
@@ -223,12 +218,7 @@ public function paintedFloatProvider()
223218
*/
224219
private function frames($css, $body, $pages)
225220
{
226-
$frames = [];
227-
foreach ($this->write($css, $body)->drawnBoxes as $box) {
228-
$frames[$box[0]] = $this->widen(isset($frames[$box[0]]) ? $frames[$box[0]] : null, $box);
229-
}
230-
231-
return array_slice($this->rounded($frames), 0, $pages, true);
221+
return array_slice($this->spans($this->write($css, $body)->drawnBoxes), 0, $pages, true);
232222
}
233223

234224
/**
@@ -252,8 +242,8 @@ private function write($css, $body)
252242
}
253243

254244
/**
255-
* Every line drawn on the page lies in one of the columns, and the lines of each column fill it: together they
256-
* span it, and most of them, the lines of justified text that are not the last of a paragraph, end at its right edge
245+
* Every line drawn on the page lies in one of the columns, and the lines of each column fill it: together they span
246+
* it, and most of them end at its right edge, as every line of justified text does but the last of a paragraph
257247
*
258248
* @param \Mpdf\TextRecordingMpdf $mpdf
259249
* @param int $page
@@ -277,17 +267,17 @@ private function assertColumns(TextRecordingMpdf $mpdf, $page, $columns)
277267
}
278268
$this->assertNotNull($in, sprintf('A line on page %d runs from %.1f to %.1f', $page, $box[1], $box[2]));
279269

280-
$filled[$in] = $this->widen(isset($filled[$in]) ? $filled[$in] : null, $box);
270+
$filled[] = [$in, $box[1], $box[2]];
281271
$rightEdges[$in][] = sprintf('%.1f', $box[2]);
282272
}
283273

274+
$filled = $this->spans($filled);
284275
ksort($filled);
285-
$this->assertSame($columns, $this->rounded($filled));
276+
$this->assertSame($columns, $filled);
286277

287278
foreach ($rightEdges as $i => $edges) {
288-
$counts = array_count_values($edges);
289-
arsort($counts);
290-
$this->assertSame(sprintf('%.1f', $columns[$i][1]), (string) key($counts), sprintf('Most lines of column %d on page %d end short of it', $i, $page));
279+
$atEdge = count(array_keys($edges, sprintf('%.1f', $columns[$i][1])));
280+
$this->assertGreaterThan(count($edges) / 2, $atEdge, sprintf('Most lines of column %d on page %d end short of it', $i, $page));
291281
}
292282
}
293283

@@ -300,40 +290,33 @@ private function assertColumns(TextRecordingMpdf $mpdf, $page, $columns)
300290
*/
301291
private function painted(TextRecordingMpdf $mpdf)
302292
{
303-
$spans = [];
293+
$boxes = [];
304294
foreach ($mpdf->pages as $page => $stream) {
305-
preg_match_all('/([\d.]+) [\d.-]+ ([\d.]+) [\d.-]+ re\b/', $stream, $rects, PREG_SET_ORDER);
295+
preg_match_all('/(-?[\d.]+) -?[\d.]+ (-?[\d.]+) -?[\d.]+ re\b/', $stream, $rects, PREG_SET_ORDER);
306296
foreach ($rects as $rect) {
307297
$x = $rect[1] / Mpdf::SCALE;
308-
$spans[$page] = $this->widen(isset($spans[$page]) ? $spans[$page] : null, [$page, $x, $x + $rect[2] / Mpdf::SCALE]);
298+
$boxes[] = [$page, $x, $x + $rect[2] / Mpdf::SCALE];
309299
}
310300
}
311301

312-
return $this->rounded($spans);
302+
return $this->spans($boxes);
313303
}
314304

315305
/**
316-
* A span widened to take in a drawn line
306+
* The narrowest left edge and widest right edge of the boxes under each key, rounded to a tenth of a millimetre
317307
*
318-
* @param float[]|null $span The left and right edges found so far, or null for none
319-
* @param array $box The page, left and right edge of the line
308+
* @param array[] $boxes Each a key, such as the page, then a left and right edge
320309
*
321-
* @return float[]
310+
* @return array[] By key, in the order the keys first appear
322311
*/
323-
private function widen($span, $box)
312+
private function spans($boxes)
324313
{
325-
return $span ? [min($span[0], $box[1]), max($span[1], $box[2])] : [$box[1], $box[2]];
326-
}
314+
$spans = [];
315+
foreach ($boxes as $box) {
316+
list($key, $left, $right) = $box;
317+
$spans[$key] = isset($spans[$key]) ? [min($spans[$key][0], $left), max($spans[$key][1], $right)] : [$left, $right];
318+
}
327319

328-
/**
329-
* Spans rounded to a tenth of a millimetre
330-
*
331-
* @param array[] $spans
332-
*
333-
* @return array[]
334-
*/
335-
private function rounded($spans)
336-
{
337320
return array_map(static function ($span) {
338321
return [round($span[0], 1), round($span[1], 1)];
339322
}, $spans);

0 commit comments

Comments
 (0)