Skip to content

Commit c1225aa

Browse files
jakejackson1claude
andcommitted
Turn a positioned block for rotate: 90deg on PHP 8 as on PHP 7
The positioned-block code compared the rotate value with == 90, -90 and 180. "90deg" == 90 is true on PHP 5.6 to 7.4 and false on PHP 8, so the same document turned the block on one and not the other. The angle is now read by Mpdf\Utils\Rotation::angle(), which tables already used as a private method: whole degrees, with or without "deg", wrapped to between -180 and 180, and kept only if the caller can draw it. A positioned block draws 90, -90 and 180, and a table 90 and -90, as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2c9a90b commit c1225aa

8 files changed

Lines changed: 174 additions & 34 deletions

File tree

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Bugfixes
139139
* When a hyphenation hyphen moved to the next line, its record stayed on the line before. In a bidi paragraph that drew a hyphen in the wrong place and dropped the one that moved. [mpdf/mpdf#1831] [#145]
140140
* A hyphen inserted at a line break had no bidi direction. It now takes the direction of the word it breaks. [#116] [#137]
141141
* `$extgstates` had no default, so `count()` on it threw a `TypeError` on PHP 8. [mpdf/mpdf#2135] [#20]
142+
* `rotate: 90deg` on a positioned block turned it on PHP 5.6 to 7.4 but not on PHP 8, where `"90deg" == 90` is false. The angle is now read as tables read it: whole degrees, with or without `deg`, so `270deg` turns the block as `-90` does. A positioned block also takes `180deg` as it takes `180`. [#579]
142143

143144
### Images and SVG
144145

@@ -723,3 +724,4 @@ These changes do not change output.
723724
[#556]: https://github.com/GravityPDF/mpdf/pull/556
724725
[#558]: https://github.com/GravityPDF/mpdf/pull/558
725726
[#562]: https://github.com/GravityPDF/mpdf/pull/562
727+
[#579]: https://github.com/GravityPDF/mpdf/pull/579

‎phpstan-baseline.neon‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,13 +621,13 @@ parameters:
621621
-
622622
message: '#^Variable \$preroth might not be defined\.$#'
623623
identifier: variable.undefined
624-
count: 6
624+
count: 4
625625
path: src/Mpdf.php
626626

627627
-
628628
message: '#^Variable \$prerotw might not be defined\.$#'
629629
identifier: variable.undefined
630-
count: 6
630+
count: 4
631631
path: src/Mpdf.php
632632

633633
-
@@ -645,13 +645,13 @@ parameters:
645645
-
646646
message: '#^Variable \$rot_bpos might not be defined\.$#'
647647
identifier: variable.undefined
648-
count: 4
648+
count: 2
649649
path: src/Mpdf.php
650650

651651
-
652652
message: '#^Variable \$rot_rpos might not be defined\.$#'
653653
identifier: variable.undefined
654-
count: 4
654+
count: 2
655655
path: src/Mpdf.php
656656

657657
-

‎src/Mpdf.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Mpdf\Utils\NumericString;
2525
use Mpdf\Utils\UtfString;
2626
use Mpdf\Utils\Path;
27+
use Mpdf\Utils\Rotation;
2728
use Mpdf\Writer\OptionalContentWriter;
2829
use Psr\Log\NullLogger;
2930
use Mpdf\Unicode\Ucdn;
@@ -15242,9 +15243,9 @@ function WriteFixedPosHTML($html, $x, $y, $w, $h, $overflow = 'visible', $boundi
1524215243
$this->Reset();
1524315244
$p = $this->cssManager->MergeCSS('BLOCK', $tag, $attr);
1524415245
$this->fixedPosBlockCascadeCSS = $this->blk[1]['cascadeCSS'];
15245-
if (isset($p['ROTATE']) && ($p['ROTATE'] == 90 || $p['ROTATE'] == -90 || $p['ROTATE'] == 180)) {
15246-
$rotate = $p['ROTATE'];
15247-
} // mPDF 6
15246+
if (isset($p['ROTATE'])) {
15247+
$rotate = Rotation::angle($p['ROTATE'], [90, -90, 180]);
15248+
}
1524815249
if (isset($p['OVERFLOW'])) {
1524915250
$overflow = strtolower($p['OVERFLOW']);
1525015251
}

‎src/Tag/Table.php‎

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Mpdf\Css\Border;
66
use Mpdf\Mpdf;
7+
use Mpdf\Utils\Rotation;
78

89
class Table extends Tag
910
{
@@ -216,7 +217,7 @@ public function open($attr, &$ahtml, &$ihtml)
216217
}
217218
}
218219
if (!empty($properties['ROTATE']) && $this->mpdf->tableLevel == 1) {
219-
$this->mpdf->table_rotate = $this->parseTableRotate($properties['ROTATE']);
220+
$this->mpdf->table_rotate = Rotation::angle($properties['ROTATE'], [90, -90]);
220221
}
221222
if (isset($properties['TOPNTAIL'])) {
222223
$table['topntail'] = $properties['TOPNTAIL'];
@@ -500,7 +501,7 @@ public function open($attr, &$ahtml, &$ihtml)
500501
}
501502
}
502503
if (isset($attr['ROTATE']) && $this->mpdf->tableLevel == 1) {
503-
$this->mpdf->table_rotate = $this->parseTableRotate($attr['ROTATE']);
504+
$this->mpdf->table_rotate = Rotation::angle($attr['ROTATE'], [90, -90]);
504505
}
505506

506507
//++++++++++++++++++++++++++++
@@ -1338,29 +1339,4 @@ private function tbsqrt($val, $iteration = 3)
13381339

13391340
return $ret;
13401341
}
1341-
1342-
/**
1343-
* @param string $rotate
1344-
* @return int
1345-
*/
1346-
private function parseTableRotate($rotate)
1347-
{
1348-
if (1 !== preg_match('/^(-?[0-9]+)(?:deg)?$/', $rotate, $matches)) {
1349-
return 0;
1350-
}
1351-
1352-
$rotationDegrees = (int) $matches[1] % 360;
1353-
if ($rotationDegrees > 180) {
1354-
$rotationDegrees -= 360;
1355-
} elseif ($rotationDegrees < -180) {
1356-
$rotationDegrees += 360;
1357-
}
1358-
1359-
// Only 90 and -90 are supported
1360-
if ($rotationDegrees !== 90 && $rotationDegrees !== -90) {
1361-
$rotationDegrees = 0;
1362-
}
1363-
1364-
return $rotationDegrees;
1365-
}
13661342
}

‎src/Utils/Rotation.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Mpdf\Utils;
4+
5+
class Rotation
6+
{
7+
8+
/**
9+
* Read mPDF's rotate property: whole degrees, with or without "deg", wrapped to between -180 and 180
10+
*
11+
* @param string|int $rotate
12+
* @param int[] $supported The angles the caller can draw
13+
*
14+
* @return int The angle, or 0 when it is not one of $supported
15+
*/
16+
public static function angle($rotate, array $supported)
17+
{
18+
if (1 !== preg_match('/^(-?[0-9]+)(?:deg)?$/i', trim((string) $rotate), $matches)) {
19+
return 0;
20+
}
21+
22+
$degrees = (int) $matches[1] % 360;
23+
if ($degrees > 180) {
24+
$degrees -= 360;
25+
} elseif ($degrees <= -180) {
26+
$degrees += 360;
27+
}
28+
29+
return in_array($degrees, $supported, true) ? $degrees : 0;
30+
}
31+
32+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* mPDF's rotate property turns an absolutely positioned block a quarter or half turn, whether the angle is written
7+
* with "deg" or without, and on every PHP version
8+
*/
9+
class PositionedBlockRotateTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
10+
{
11+
12+
/**
13+
* The first four numbers of the matrix transformRotate() writes for each turn
14+
*/
15+
const CLOCKWISE = '0.0000 -1.0000 1.0000 0.0000';
16+
const COUNTER_CLOCKWISE = '0.0000 1.0000 -1.0000 0.0000';
17+
const HALF_TURN = '-1.0000 -0.0000 0.0000 -1.0000';
18+
19+
/**
20+
* The turns a positioned block with the given rotate value is drawn with
21+
*
22+
* @param string $rotate
23+
*
24+
* @return string[]
25+
*/
26+
private function turns($rotate)
27+
{
28+
$mpdf = new Mpdf();
29+
$mpdf->compress = false;
30+
$mpdf->WriteHTML('<div style="position: absolute; top: 50mm; left: 50mm; width: 40mm; rotate: ' . $rotate . '">Turned</div>');
31+
$pdf = $mpdf->Output('', 'S');
32+
33+
return array_values(array_filter([self::CLOCKWISE, self::COUNTER_CLOCKWISE, self::HALF_TURN], function ($turn) use ($pdf) {
34+
return strpos($pdf, $turn . ' ') !== false;
35+
}));
36+
}
37+
38+
/**
39+
* The block is drawn turned the way the value gives, or not turned at all
40+
*
41+
* @dataProvider rotateProvider
42+
*
43+
* @param string $rotate
44+
* @param string[] $expected
45+
*/
46+
public function testRotate($rotate, $expected)
47+
{
48+
$this->assertSame($expected, $this->turns($rotate));
49+
}
50+
51+
/**
52+
* rotate values, with the turn each gives
53+
*
54+
* @return array
55+
*/
56+
public function rotateProvider()
57+
{
58+
return [
59+
'90' => ['90', [self::CLOCKWISE]],
60+
'90deg' => ['90deg', [self::CLOCKWISE]],
61+
'-90' => ['-90', [self::COUNTER_CLOCKWISE]],
62+
'-90deg' => ['-90deg', [self::COUNTER_CLOCKWISE]],
63+
'270deg is -90deg' => ['270deg', [self::COUNTER_CLOCKWISE]],
64+
'-270deg is 90deg' => ['-270deg', [self::CLOCKWISE]],
65+
'180' => ['180', [self::HALF_TURN]],
66+
'180deg' => ['180deg', [self::HALF_TURN]],
67+
'-180deg is 180deg' => ['-180deg', [self::HALF_TURN]],
68+
'45deg is not supported' => ['45deg', []],
69+
'not an angle' => ['auto', []],
70+
];
71+
}
72+
73+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Snapshots;
4+
5+
/**
6+
* Absolutely positioned blocks turned by mPDF's rotate property, with the angle written with and without "deg"
7+
*
8+
* @group snapshot
9+
*/
10+
class PositionedBlockRotateSnapshotTest extends Snapshot
11+
{
12+
13+
/**
14+
* The fixture's name
15+
*
16+
* @return string
17+
*/
18+
public function getId()
19+
{
20+
return 'positioned-block-rotate';
21+
}
22+
23+
/**
24+
* A positioned block for each rotate value, beside a caption saying which way it should be turned
25+
*/
26+
public function generatePdf()
27+
{
28+
$cases = [
29+
['90', 'rotate: 90. Turned clockwise: the text reads top to bottom.'],
30+
['90deg', 'rotate: 90deg. Turned clockwise: the text reads top to bottom.'],
31+
['-90deg', 'rotate: -90deg. Turned counter-clockwise: the text reads bottom to top.'],
32+
['270deg', 'rotate: 270deg, the same as -90deg. The text reads bottom to top.'],
33+
['180deg', 'rotate: 180deg. Turned upside down: the text reads right to left, upside down.'],
34+
['45deg', 'rotate: 45deg, which mPDF does not support. Not turned: the text reads left to right.'],
35+
];
36+
37+
$html = '<style>
38+
body { font-size: 9pt; }
39+
h1 { font-size: 15pt; margin: 0; }
40+
div.caption { position: absolute; left: 20mm; width: 90mm; font-size: 8pt; color: #606060; }
41+
div.turned { position: absolute; left: 130mm; width: 34mm; border: 0.3mm solid #1f3a93; background: #d6eaf8; padding: 1mm; }
42+
</style>
43+
<h1>rotate on a positioned block</h1>';
44+
45+
foreach ($cases as $i => $case) {
46+
$top = 25 + $i * 44;
47+
$html .= '<div class="caption" style="top: ' . $top . 'mm">' . $case[1] . '</div>'
48+
. '<div class="turned" style="top: ' . $top . 'mm; rotate: ' . $case[0] . '">Turned block ' . $case[0] . '</div>';
49+
}
50+
51+
$this->mpdf = $this->createMpdf();
52+
53+
$this->mpdf->WriteHTML($html);
54+
}
55+
56+
}
37.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)