@@ -3619,13 +3619,8 @@ function AddPage(
36193619
36203620 $this->printfloatbuffer();
36213621
3622- $previousFrame = $this->textFrame();
3623-
36243622 // Move to next page
3625- $this->page++;
3626-
3627- $this->ResetMargins();
3628- $this->moveIntoFrame($previousFrame);
3623+ $widthGained = $this->moveToMadePage($this->page + 1);
36293624 $this->SetAutoPageBreak($this->autoPageBreak, $this->bMargin);
36303625 $this->x = $this->lMargin;
36313626 $this->y = $this->tMargin;
@@ -3669,6 +3664,7 @@ function AddPage(
36693664 $this->cMarginL = $bak_cml;
36703665 $this->cMarginR = $bak_cmr;
36713666 $this->divwidth = $bak_dw;
3667+ $this->widenBlockBeingWritten($widthGained);
36723668
36733669 return '';
36743670 }
@@ -3969,12 +3965,7 @@ function AddPage(
39693965 $this->cMarginL = $bak_cml;
39703966 $this->cMarginR = $bak_cmr;
39713967 $this->divwidth = $bak_dw;
3972-
3973- // The rest of the block being written takes its width on the new page
3974- if ($widthGained && $this->divwidth && !$this->flowingBlockAttr['is_table']) {
3975- $this->divwidth += $widthGained;
3976- $this->flowingBlockAttr['width'] += $widthGained * Mpdf::SCALE;
3977- }
3968+ $this->widenBlockBeingWritten($widthGained);
39783969
39793970 $this->lineheight = $bak_lh;
39803971 }
@@ -4020,13 +4011,26 @@ function turnPageKeepingPlace()
40204011 $ws = $this->ws;
40214012 $charspacing = $this->charspacing;
40224013 $this->ResetSpacing();
4014+ $indent = $this->blockIndent();
40234015
40244016 $this->AddPage($this->CurOrientation);
40254017
4026- $this->x = $x + $this->MarginCorrection;
4018+ // A right float also moves across within a frame that has changed width
4019+ $shift = $this->MarginCorrection + $this->blockIndent() - $indent;
4020+ $this->x = $x + $shift;
40274021 $this->SetSpacing($charspacing, $ws);
40284022
4029- return $this->MarginCorrection;
4023+ return $shift;
4024+ }
4025+
4026+ /**
4027+ * How far the block being written sits in from the left of the frame
4028+ *
4029+ * @return float
4030+ */
4031+ function blockIndent()
4032+ {
4033+ return isset($this->blk[$this->blklvl]['outer_left_margin']) ? $this->blk[$this->blklvl]['outer_left_margin'] : 0;
40304034 }
40314035
40324036 /**
@@ -4098,29 +4102,82 @@ function moveIntoFrame($previousFrame)
40984102 return 0;
40994103 }
41004104
4101- $gained = 0;
4102- $taken = 0; // The growth a block with a set width, and every block inside it, puts in its right margin
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;
41034108 for ($bl = 1; $bl <= $this->blklvl; $bl++) {
41044109 if (!isset($this->blk[$bl]['width'])) {
41054110 continue;
41064111 }
41074112
41084113 $blk = &$this->blk[$bl];
4109- $blk['outer_right_margin'] += $taken;
41104114
4111- if (!empty($blk['css_set_width']) && $taken != $growth) {
4112- $blk['margin_right'] += $growth - $taken;
4113- $blk['outer_right_margin'] += $growth - $taken;
4114- $taken = $growth;
4115+ if (!$side && !empty($blk['css_set_width'])) {
4116+ $side = isset($blk['float']) && $blk['float'] === 'R' ? 'left' : 'right';
4117+ $blk['margin_' . $side] += $growth;
41154118 }
41164119
4117- $gained = $growth - $taken;
4118- $blk['width'] += $gained;
4119- $blk['inner_width'] += $gained;
4120+ if (!$side) {
4121+ $blk['width'] += $growth;
4122+ $blk['inner_width'] += $growth;
4123+ } else {
4124+ $blk['outer_' . $side . '_margin'] += $growth;
4125+ if ($side === 'left' && isset($blk['x0'])) {
4126+ $blk['x0'] += $growth;
4127+ }
4128+ }
41204129 unset($blk);
41214130 }
41224131
4123- return $gained;
4132+ return $side ? 0 : $growth;
4133+ }
4134+
4135+ /**
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
4138+ *
4139+ * @param int $page
4140+ *
4141+ * @return float The width the block being written gained, or 0
4142+ */
4143+ function moveToMadePage($page)
4144+ {
4145+ $previousFrame = $this->textFrame();
4146+
4147+ $this->page = $page;
4148+ $this->restoreSideMargins();
4149+ $this->ResetMargins();
4150+
4151+ if ($this->textFrame() == $previousFrame) {
4152+ return 0;
4153+ }
4154+
4155+ $this->pgwidth = $this->w - $this->lMargin - $this->rMargin;
4156+
4157+ return $this->moveIntoFrame($previousFrame);
4158+ }
4159+
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+
4170+ /**
4171+ * Give the rest of the block being written the width it gained on the new page
4172+ *
4173+ * @param float $gained As moveIntoFrame() gave it
4174+ */
4175+ function widenBlockBeingWritten($gained)
4176+ {
4177+ if ($gained && $this->divwidth && !$this->flowingBlockAttr['is_table']) {
4178+ $this->divwidth += $gained;
4179+ $this->flowingBlockAttr['width'] += $gained * Mpdf::SCALE;
4180+ }
41244181 }
41254182
41264183 /**
@@ -11612,6 +11669,7 @@ function _beginpage(
1161211669 $this->ResetMargins();
1161311670 $this->pgwidth = $this->w - $this->lMargin - $this->rMargin;
1161411671 $this->SetAutoPageBreak($this->autoPageBreak, $this->bMargin);
11672+ $this->pageDim[$this->page]['sideMargins'] = [$this->DeflMargin, $this->DefrMargin];
1161511673
1161611674 // Reset column top margin
1161711675 $this->y0 = $this->tMargin;
@@ -15265,8 +15323,7 @@ function WriteHTML($html, $mode = HTMLParserMode::DEFAULT_MODE, $init = true, $c
1526515323 // Writes after the marker so not overwritten later by page background etc.
1526615324 $this->pages[$this->page] = preg_replace('/(___BACKGROUND___PATTERNS' . $this->uniqstr . ')/', '\\1' . "\n" . $s . "\n", $this->pages[$this->page]);
1526715325 $this->pageBackgrounds = [];
15268- $this->page = $new_page;
15269- $this->ResetMargins();
15326+ $this->moveToMadePage($new_page);
1527015327 $this->Reset();
1527115328 $this->pageoutput[$this->page] = [];
1527215329 }
@@ -16629,9 +16686,10 @@ function ClearFloats($clear, $blklvl = 0)
1662916686 // Writes after the marker so not overwritten later by page background etc.
1663016687 $this->pages[$this->page] = preg_replace('/(___BACKGROUND___PATTERNS' . $this->uniqstr . ')/', '\\1' . "\n" . $s . "\n", $this->pages[$this->page]);
1663116688 $this->pageBackgrounds = [];
16632- $this->page = $new_page;
16689+ $this->moveToMadePage($new_page);
16690+ } else {
16691+ $this->ResetMargins();
1663316692 }
16634- $this->ResetMargins();
1663516693 $this->pageoutput[$this->page] = [];
1663616694
1663716695 $this->y = (round($end * 1000) % 1000000) / 1000; // mod changes operands to integers before processing
@@ -17289,6 +17347,7 @@ function printbuffer($arrayaux, $blockstate = 0, $is_table = false, $table_draft
1728917347 $blockdir = $cell_dir;
1729017348 }
1729117349 $oldpage = $this->page;
17350+ $indent = $this->blockIndent();
1729217351
1729317352 // ADDED for Out of Block now done as Flowing Block
1729417353 if ($this->divwidth == 0) {
@@ -17632,6 +17691,9 @@ function printbuffer($arrayaux, $blockstate = 0, $is_table = false, $table_draft
1763217691
1763317692 // Added to correct for OddEven Margins
1763417693 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();
1763517697 if (($this->page - $oldpage) % 2 == 1) {
1763617698 $bak_x += $this->MarginCorrection;
1763717699 }
@@ -17808,6 +17870,9 @@ function printbuffer($arrayaux, $blockstate = 0, $is_table = false, $table_draft
1780817870 }
1780917871 // Added to correct for OddEven Margins
1781017872 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();
1781117876 if (($this->page - $oldpage) % 2 == 1) {
1781217877 $bak_x += $this->MarginCorrection;
1781317878 }
@@ -17839,6 +17904,9 @@ function printbuffer($arrayaux, $blockstate = 0, $is_table = false, $table_draft
1783917904 $this->WriteFlowingBlock($vetor[0], $vetor[18]); // mPDF 5.7.1
1784017905 // Added to correct for OddEven Margins
1784117906 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();
1784217910 if (($this->page - $oldpage) % 2 == 1) {
1784317911 $bak_x += $this->MarginCorrection;
1784417912 $this->x = $bak_x;
@@ -17873,6 +17941,9 @@ function printbuffer($arrayaux, $blockstate = 0, $is_table = false, $table_draft
1787317941 $this->finishFlowingBlock(true); // true = END of flowing block
1787417942 // Added to correct for OddEven Margins
1787517943 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();
1787617947 if (($this->page - $oldpage) % 2 == 1) {
1787717948 $bak_x += $this->MarginCorrection;
1787817949 $this->x = $bak_x;
0 commit comments