Skip to content

Commit d9102f9

Browse files
jakejackson1claude
andcommitted
Run linear gradients the way CSS Images gives for "to" keywords and angles
"to bottom" and "to top" were read as the legacy start sides, so they drew upside down, and so did the vertical half of the corner keywords. Angles followed the legacy convention, 0deg pointing right and turning counter-clockwise, so 90deg drew bottom to top. An unprefixed gradient now points up at 0deg and turns clockwise, and reads the turn unit. A "to" keyword names the side the gradient ends at, and a corner runs corner to corner across the box, so the other two corners meet halfway. -moz-, -webkit- and -o- gradients keep the legacy angles, and their side keyword names the side they start from. The -webkit- and -o- prefixes are kept through NormalizeProperties so they can be told apart. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 60b8263 commit d9102f9

7 files changed

Lines changed: 259 additions & 26 deletions

‎CHANGELOG-GRAVITYPDF.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Bugfixes
131131
* 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]
132132
* A hyphen inserted at a line break had no bidi direction. It now takes the direction of the word it breaks. [#116] [#137]
133133
* `$extgstates` had no default, so `count()` on it threw a `TypeError` on PHP 8. [mpdf/mpdf#2135] [#20]
134+
* `linear-gradient(to bottom, …)` and `to top` were drawn upside down, and so was the vertical half of `to bottom right` and the other corners. Angles ran counter-clockwise from pointing right, so `90deg` drew bottom to top. A gradient now runs as CSS Images gives: `0deg` points up and angles turn clockwise, `turn` is read, and a corner keyword leaves the other two corners halfway along. `-moz-`, `-webkit-` and `-o-` gradients keep their legacy angles, and their side keyword, such as `left`, names the side the gradient starts from. [#0]
134135

135136
### Images and SVG
136137

‎phpstan-baseline.neon‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ parameters:
6666
count: 1
6767
path: src/Form.php
6868

69-
-
70-
message: '#^Binary operation "\+" between non\-empty\-string and 0 results in an error\.$#'
71-
identifier: binaryOp.invalid
72-
count: 1
73-
path: src/Gradient.php
74-
7569
-
7670
message: '#^Variable \$angle in isset\(\) always exists and is not nullable\.$#'
7771
identifier: isset.variable

‎src/Css/NormalizeProperties.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ protected function parseCssBackground($s)
401401
'p' => false, // position
402402
];
403403

404-
if (preg_match('/(-moz-)*(repeating-)*(linear|radial)-gradient\(.*\)/i', $s, $m)) {
404+
if (preg_match('/(-moz-|-webkit-|-o-)*(repeating-)*(linear|radial)-gradient\(.*\)/i', $s, $m)) {
405405
$background['i'] = $m[0];
406406
return $background;
407407
}
@@ -787,7 +787,7 @@ protected function processBackgroundProperty($property, $value)
787787
break;
788788

789789
case 'BACKGROUND-IMAGE':
790-
if (preg_match('/(-moz-)*(repeating-)*(linear|radial)-gradient\(.*\)/i', $value, $m)) {
790+
if (preg_match('/(-moz-|-webkit-|-o-)*(repeating-)*(linear|radial)-gradient\(.*\)/i', $value, $m)) {
791791
$this->properties['BACKGROUND-IMAGE'] = $m[0];
792792
return;
793793
}

‎src/Gradient.php‎

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,18 @@ public function Gradient($x, $y, $w, $h, $type, $stops = [], $colorspace = 'RGB'
621621
$this->writer->write($s);
622622
}
623623

624-
private function parseMozLinearGradient($m, $repeat)
624+
/**
625+
* Reads the arguments of a linear gradient into the start point or angle Gradient() works from, where y runs up the
626+
* box and angles run counter-clockwise from pointing right
627+
*
628+
* @param string[] $m The match whose second element holds the arguments
629+
* @param bool $repeat Whether the gradient repeats
630+
* @param bool $legacy Whether the function is prefixed, where a keyword names the start side and angles already
631+
* run counter-clockwise from pointing right
632+
*
633+
* @return array
634+
*/
635+
private function parseMozLinearGradient($m, $repeat, $legacy)
625636
{
626637
$g = [];
627638
$g['type'] = self::TYPE_LINEAR;
@@ -646,7 +657,7 @@ private function parseMozLinearGradient($m, $repeat)
646657
}
647658
// Is first part $bgr[0] a valid point/angle?
648659
$first = preg_split('/\s+/', trim($bgr[0]));
649-
if (preg_match('/(left|center|right|bottom|top|deg|grad|rad)/i', $bgr[0]) && !preg_match('/(<#|rgb|rgba|hsl|hsla)/i', $bgr[0])) {
660+
if (preg_match('/(left|center|right|bottom|top|deg|grad|rad|turn)/i', $bgr[0]) && !preg_match('/(<#|rgb|rgba|hsl|hsla)/i', $bgr[0])) {
650661
$startStops = 1;
651662
} elseif (trim($first[count($first) - 1]) === '0') {
652663
$startStops = 1;
@@ -661,32 +672,45 @@ private function parseMozLinearGradient($m, $repeat)
661672
// first part a valid point/angle?
662673
if ($startStops === 1) { // default values
663674

664-
// [<point> || <angle>,] = [<% em px left center right bottom top> || <deg grad rad 0>,]
665-
if (preg_match('/([\-]*[0-9\.]+)(deg|grad|rad)/i', $bgr[0], $m)) {
666-
$angle = $m[1] + 0;
667-
if (strtolower($m[2]) === 'grad') {
668-
$angle *= (360 / 400);
669-
} elseif (strtolower($m[2]) === 'rad') {
670-
$angle = rad2deg($angle);
675+
// [<point> || <angle>,] = [<% em px left center right bottom top> || <deg grad rad turn 0>,]
676+
if (preg_match('/([\-]*[0-9\.]+)(deg|grad|rad|turn)/i', $bgr[0], $m)) {
677+
$perTurn = ['deg' => 360, 'grad' => 400, 'rad' => 2 * M_PI, 'turn' => 1];
678+
$angle = (float) $m[1] * 360 / $perTurn[strtolower($m[2])];
679+
} elseif (trim($first[count($first) - 1]) === '0') {
680+
$angle = 0;
681+
}
682+
683+
if (isset($angle)) {
684+
// CSS Images angles point up at 0 and run clockwise
685+
if (!$legacy) {
686+
$angle = 90 - $angle;
671687
}
672-
while ($angle < 0) {
688+
$angle = fmod($angle, 360);
689+
if ($angle < 0) {
673690
$angle += 360;
674691
}
675-
$angle %= 360;
676-
} elseif (trim($first[count($first) - 1]) === '0') {
677-
$angle = 0;
678692
}
679693

694+
// "to left" names the side the gradient ends at, and a legacy "left" the side it starts from
695+
$to = strtolower($first[0]) === 'to';
696+
680697
if (stripos($bgr[0], 'left') !== false) {
681-
$startx = 1;
698+
$startx = $to ? 1 : 0;
682699
} elseif (stripos($bgr[0], 'right') !== false) {
683-
$startx = 0;
700+
$startx = $to ? 0 : 1;
684701
}
685702

686703
if (stripos($bgr[0], 'top') !== false) {
687-
$starty = 1;
704+
$starty = $to ? 0 : 1;
688705
} elseif (stripos($bgr[0], 'bottom') !== false) {
689-
$starty = 0;
706+
$starty = $to ? 1 : 0;
707+
}
708+
709+
// Corner to corner across the box, so the other two corners take the colour halfway along, as CSS Images
710+
// gives for "to top right"
711+
if ($to && isset($startx, $starty)) {
712+
$endx = 1 - $startx;
713+
$endy = 1 - $starty;
690714
}
691715

692716
// Check for %? ?% or %%
@@ -954,7 +978,7 @@ public function parseMozGradient($bg)
954978
$repeat = strpos($bg, 'repeating-') !== false;
955979

956980
if (preg_match('/linear-gradient\((.*)\)/', $bg, $m)) {
957-
$g = $this->parseMozLinearGradient($m, $repeat);
981+
$g = $this->parseMozLinearGradient($m, $repeat, (bool) preg_match('/-(moz|webkit|o)-/', $bg));
958982
if (count($g['stops'])) {
959983
return $g;
960984
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace Mpdf;
4+
5+
/**
6+
* The axis a linear-gradient() background is drawn along. Gradient() registers it as [x0, y0, x1, y1] across the box,
7+
* with y running up, so "to bottom" starts at y = 1 and ends at y = 0.
8+
*/
9+
class LinearGradientDirectionTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
10+
{
11+
12+
/**
13+
* The axis drawn for a block with the given background
14+
*
15+
* @param string $background
16+
*
17+
* @return float[]
18+
*/
19+
private function axis($background)
20+
{
21+
$mpdf = new Mpdf();
22+
$mpdf->WriteHTML('<div style="width: 60mm; height: 30mm; background: ' . $background . '"></div>');
23+
$mpdf->Output('', 'S');
24+
25+
$this->assertCount(1, $mpdf->gradients);
26+
27+
$gradient = reset($mpdf->gradients);
28+
29+
return array_map('floatval', array_slice($gradient['coords'], 0, 4));
30+
}
31+
32+
/**
33+
* A side keyword or an angle runs the axis the way CSS Images gives
34+
*
35+
* @dataProvider axisProvider
36+
*
37+
* @param string $background
38+
* @param float[] $expected
39+
*/
40+
public function testAxis($background, $expected)
41+
{
42+
$this->assertEqualsWithDelta($expected, $this->axis($background), 0.0001);
43+
}
44+
45+
/**
46+
* Backgrounds with a side or corner keyword or a right angle, with the axis each gives
47+
*
48+
* @return array
49+
*/
50+
public function axisProvider()
51+
{
52+
$down = [0.5, 1, 0.5, 0];
53+
$up = [0.5, 0, 0.5, 1];
54+
$right = [0, 0.5, 1, 0.5];
55+
$left = [1, 0.5, 0, 0.5];
56+
57+
return [
58+
'no direction' => ['linear-gradient(red, blue)', $down],
59+
'to bottom' => ['linear-gradient(to bottom, red, blue)', $down],
60+
'to top' => ['linear-gradient(to top, red, blue)', $up],
61+
'to right' => ['linear-gradient(to right, red, blue)', $right],
62+
'to left' => ['linear-gradient(to left, red, blue)', $left],
63+
'repeating, to bottom' => ['repeating-linear-gradient(to bottom, red, blue 5mm)', $down],
64+
65+
// Corner to corner across the box, so the other two corners are halfway along
66+
'to bottom right' => ['linear-gradient(to bottom right, red, blue)', [0, 1, 1, 0]],
67+
'to top left' => ['linear-gradient(to top left, red, blue)', [1, 0, 0, 1]],
68+
'to right top' => ['linear-gradient(to right top, red, blue)', [0, 0, 1, 1]],
69+
'to bottom left' => ['linear-gradient(to bottom left, red, blue)', [1, 1, 0, 0]],
70+
71+
'0deg points up' => ['linear-gradient(0deg, red, blue)', [0, 0, 0, 1]],
72+
'unitless 0 points up' => ['linear-gradient(0, red, blue)', [0, 0, 0, 1]],
73+
'90deg points right' => ['linear-gradient(90deg, red, blue)', [0, 0, 1, 0]],
74+
'180deg points down' => ['linear-gradient(180deg, red, blue)', [0, 1, 0, 0]],
75+
'270deg points left' => ['linear-gradient(270deg, red, blue)', [1, 0, 0, 0]],
76+
'-90deg points left' => ['linear-gradient(-90deg, red, blue)', [1, 0, 0, 0]],
77+
'450deg points right' => ['linear-gradient(450deg, red, blue)', [0, 0, 1, 0]],
78+
'0.5turn points down' => ['linear-gradient(0.5turn, red, blue)', [0, 1, 0, 0]],
79+
'100grad points right' => ['linear-gradient(100grad, red, blue)', [0, 0, 1, 0]],
80+
'3.14159rad points down' => ['linear-gradient(3.14159265rad, red, blue)', [0, 1, 0, 0]],
81+
82+
'-moz- keyword names the start' => ['-moz-linear-gradient(left, red, blue)', $right],
83+
'-moz- top' => ['-moz-linear-gradient(top, red, blue)', $down],
84+
'-webkit- keyword names the start' => ['-webkit-linear-gradient(left, red, blue)', $right],
85+
'-moz- 90deg points up' => ['-moz-linear-gradient(90deg, red, blue)', [0, 0, 0, 1]],
86+
'-webkit- 0deg points right' => ['-webkit-linear-gradient(0deg, red, blue)', [0, 0, 1, 0]],
87+
];
88+
}
89+
90+
/**
91+
* An angle between the sides runs the axis into the right quarter
92+
*
93+
* @dataProvider diagonalProvider
94+
*
95+
* @param string $background
96+
* @param int $dx The sign the axis runs across the box
97+
* @param int $dy The sign the axis runs up the box
98+
*/
99+
public function testDiagonalAxis($background, $dx, $dy)
100+
{
101+
list($x0, $y0, $x1, $y1) = $this->axis($background);
102+
103+
$this->assertSame($dx, $this->sign($x1 - $x0), 'across');
104+
$this->assertSame($dy, $this->sign($y1 - $y0), 'up');
105+
}
106+
107+
/**
108+
* -1, 0 or 1 for a negative, zero or positive difference
109+
*
110+
* @param float $difference
111+
*
112+
* @return int
113+
*/
114+
private function sign($difference)
115+
{
116+
if (abs($difference) < 0.0001) {
117+
return 0;
118+
}
119+
120+
return $difference > 0 ? 1 : -1;
121+
}
122+
123+
/**
124+
* Backgrounds whose axis runs between the sides of the box, with the signs it runs in
125+
*
126+
* @return array
127+
*/
128+
public function diagonalProvider()
129+
{
130+
return [
131+
'45deg' => ['linear-gradient(45deg, red, blue)', 1, 1],
132+
'135deg' => ['linear-gradient(135deg, red, blue)', 1, -1],
133+
'22.5deg' => ['linear-gradient(22.5deg, red, blue)', 1, 1],
134+
'-moz- left top' => ['-moz-linear-gradient(left top, red, blue)', 1, -1],
135+
];
136+
}
137+
138+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Snapshots;
4+
5+
/**
6+
* Linear gradient backgrounds run the way CSS Images gives: "to" names the side the gradient ends at, and angles point
7+
* up at 0deg and run clockwise. Prefixed functions keep the legacy reading, where a keyword names the side the
8+
* gradient starts from and angles run counter-clockwise from pointing right.
9+
*
10+
* @group snapshot
11+
*/
12+
class LinearGradientDirectionSnapshotTest extends Snapshot
13+
{
14+
15+
/**
16+
* The fixture's name
17+
*
18+
* @return string
19+
*/
20+
public function getId()
21+
{
22+
return 'linear-gradient-direction';
23+
}
24+
25+
/**
26+
* A red to blue gradient box for each direction, under a caption saying where the red should be
27+
*/
28+
public function generatePdf()
29+
{
30+
$cases = [
31+
'Keywords' => [
32+
['linear-gradient(to bottom, red, blue)', 'to bottom: red at the top, blue at the bottom.'],
33+
['linear-gradient(to top, red, blue)', 'to top: red at the bottom, blue at the top.'],
34+
['linear-gradient(to right, red, blue)', 'to right: red at the left, blue at the right.'],
35+
['linear-gradient(to left, red, blue)', 'to left: red at the right, blue at the left.'],
36+
['linear-gradient(to bottom right, red, blue)', 'to bottom right: red in the top left corner, blue in the bottom right, and the other two corners the same purple.'],
37+
['linear-gradient(to top left, red, blue)', 'to top left: red in the bottom right corner, blue in the top left, and the other two corners the same purple.'],
38+
],
39+
'Angles' => [
40+
['linear-gradient(0deg, red, blue)', '0deg: red at the bottom, blue at the top.'],
41+
['linear-gradient(90deg, red, blue)', '90deg: red at the left, blue at the right.'],
42+
['linear-gradient(180deg, red, blue)', '180deg: red at the top, blue at the bottom.'],
43+
['linear-gradient(270deg, red, blue)', '270deg: red at the right, blue at the left.'],
44+
['linear-gradient(45deg, red, blue)', '45deg: red in the bottom left corner, blue in the top right.'],
45+
['linear-gradient(135deg, red, blue)', '135deg: red in the top left corner, blue in the bottom right.'],
46+
['linear-gradient(0.25turn, red, blue)', '0.25turn: red at the left, blue at the right.'],
47+
],
48+
'Prefixed functions' => [
49+
['-moz-linear-gradient(left, red, blue)', '-moz- left: red at the left, blue at the right.'],
50+
['-webkit-linear-gradient(top, red, blue)', '-webkit- top: red at the top, blue at the bottom.'],
51+
['-webkit-linear-gradient(90deg, red, blue)', '-webkit- 90deg: red at the bottom, blue at the top.'],
52+
],
53+
];
54+
55+
$html = '<style>
56+
body { font-size: 8pt; }
57+
h1 { font-size: 13pt; margin: 0 0 1mm 0; }
58+
h2 { font-size: 10pt; margin: 3mm 0 1mm 0; }
59+
p { margin: 1mm 0 0.5mm 0; }
60+
div.box { width: 60mm; height: 8mm; border: 0.2mm solid #999999; }
61+
</style>
62+
<h1>linear-gradient() directions</h1>';
63+
64+
foreach ($cases as $heading => $gradients) {
65+
$html .= '<h2>' . $heading . '</h2>';
66+
foreach ($gradients as $case) {
67+
$html .= '<p>' . $case[1] . '</p><div class="box" style="background: ' . $case[0] . '"></div>';
68+
}
69+
}
70+
71+
$this->mpdf = $this->createMpdf();
72+
73+
$this->mpdf->WriteHTML($html);
74+
}
75+
76+
}
53.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)