1010class 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