Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG-GRAVITYPDF.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Bugfixes
* 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]
* A hyphen inserted at a line break had no bidi direction. It now takes the direction of the word it breaks. [#116] [#137]
* `$extgstates` had no default, so `count()` on it threw a `TypeError` on PHP 8. [mpdf/mpdf#2135] [#20]
* `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. [#577]

### Images and SVG

Expand Down Expand Up @@ -746,6 +747,7 @@ These changes do not change output.
[#564]: https://github.com/GravityPDF/mpdf/pull/564
[#563]: https://github.com/GravityPDF/mpdf/pull/563
[#565]: https://github.com/GravityPDF/mpdf/pull/565
[#577]: https://github.com/GravityPDF/mpdf/pull/577
[#568]: https://github.com/GravityPDF/mpdf/pull/568
[#567]: https://github.com/GravityPDF/mpdf/pull/567
[#566]: https://github.com/GravityPDF/mpdf/pull/566
Expand Down
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ parameters:
count: 1
path: src/Form.php

-
message: '#^Binary operation "\+" between non\-empty\-string and 0 results in an error\.$#'
identifier: binaryOp.invalid
count: 1
path: src/Gradient.php

-
message: '#^Variable \$angle in isset\(\) always exists and is not nullable\.$#'
identifier: isset.variable
Expand Down
4 changes: 2 additions & 2 deletions src/Css/NormalizeProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ protected function parseCssBackground($s)
'p' => false, // position
];

if (preg_match('/(-moz-)*(repeating-)*(linear|radial)-gradient\(.*\)/i', $s, $m)) {
if (preg_match('/(-moz-|-webkit-|-o-)*(repeating-)*(linear|radial)-gradient\(.*\)/i', $s, $m)) {
$background['i'] = $m[0];
return $background;
}
Expand Down Expand Up @@ -856,7 +856,7 @@ protected function processBackgroundProperty($property, $value)
break;

case 'BACKGROUND-IMAGE':
if (preg_match('/(-moz-)*(repeating-)*(linear|radial)-gradient\(.*\)/i', $value, $m)) {
if (preg_match('/(-moz-|-webkit-|-o-)*(repeating-)*(linear|radial)-gradient\(.*\)/i', $value, $m)) {
$this->properties['BACKGROUND-IMAGE'] = $m[0];
return;
}
Expand Down
60 changes: 42 additions & 18 deletions src/Gradient.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,18 @@ public function Gradient($x, $y, $w, $h, $type, $stops = [], $colorspace = 'RGB'
$this->writer->write($s);
}

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

// [<point> || <angle>,] = [<% em px left center right bottom top> || <deg grad rad 0>,]
if (preg_match('/([\-]*[0-9\.]+)(deg|grad|rad)/i', $bgr[0], $m)) {
$angle = $m[1] + 0;
if (strtolower($m[2]) === 'grad') {
$angle *= (360 / 400);
} elseif (strtolower($m[2]) === 'rad') {
$angle = rad2deg($angle);
// [<point> || <angle>,] = [<% em px left center right bottom top> || <deg grad rad turn 0>,]
if (preg_match('/([\-]*[0-9\.]+)(deg|grad|rad|turn)/i', $bgr[0], $m)) {
$perTurn = ['deg' => 360, 'grad' => 400, 'rad' => 2 * M_PI, 'turn' => 1];
$angle = (float) $m[1] * 360 / $perTurn[strtolower($m[2])];
} elseif (trim($first[count($first) - 1]) === '0') {
$angle = 0;
}

if (isset($angle)) {
// CSS Images angles point up at 0 and run clockwise
if (!$legacy) {
$angle = 90 - $angle;
}
while ($angle < 0) {
$angle = fmod($angle, 360);
if ($angle < 0) {
$angle += 360;
}
$angle %= 360;
} elseif (trim($first[count($first) - 1]) === '0') {
$angle = 0;
}

// "to left" names the side the gradient ends at, and a legacy "left" the side it starts from
$to = strtolower($first[0]) === 'to';

if (stripos($bgr[0], 'left') !== false) {
$startx = 1;
$startx = $to ? 1 : 0;
} elseif (stripos($bgr[0], 'right') !== false) {
$startx = 0;
$startx = $to ? 0 : 1;
}

if (stripos($bgr[0], 'top') !== false) {
$starty = 1;
$starty = $to ? 0 : 1;
} elseif (stripos($bgr[0], 'bottom') !== false) {
$starty = 0;
$starty = $to ? 1 : 0;
}

// Corner to corner across the box, so the other two corners take the colour halfway along, as CSS Images
// gives for "to top right"
if ($to && isset($startx, $starty)) {
$endx = 1 - $startx;
$endy = 1 - $starty;
}

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

if (preg_match('/linear-gradient\((.*)\)/', $bg, $m)) {
$g = $this->parseMozLinearGradient($m, $repeat);
$g = $this->parseMozLinearGradient($m, $repeat, (bool) preg_match('/-(moz|webkit|o)-/', $bg));
if (count($g['stops'])) {
return $g;
}
Expand Down
138 changes: 138 additions & 0 deletions tests/Mpdf/LinearGradientDirectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace Mpdf;

/**
* The axis a linear-gradient() background is drawn along. Gradient() registers it as [x0, y0, x1, y1] across the box,
* with y running up, so "to bottom" starts at y = 1 and ends at y = 0.
*/
class LinearGradientDirectionTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
{

/**
* The axis drawn for a block with the given background
*
* @param string $background
*
* @return float[]
*/
private function axis($background)
{
$mpdf = new Mpdf();
$mpdf->WriteHTML('<div style="width: 60mm; height: 30mm; background: ' . $background . '"></div>');
$mpdf->Output('', 'S');

$this->assertCount(1, $mpdf->gradients);

$gradient = reset($mpdf->gradients);

return array_map('floatval', array_slice($gradient['coords'], 0, 4));
}

/**
* A side keyword or an angle runs the axis the way CSS Images gives
*
* @dataProvider axisProvider
*
* @param string $background
* @param float[] $expected
*/
public function testAxis($background, $expected)
{
$this->assertEqualsWithDelta($expected, $this->axis($background), 0.0001);
}

/**
* Backgrounds with a side or corner keyword or a right angle, with the axis each gives
*
* @return array
*/
public function axisProvider()
{
$down = [0.5, 1, 0.5, 0];
$up = [0.5, 0, 0.5, 1];
$right = [0, 0.5, 1, 0.5];
$left = [1, 0.5, 0, 0.5];

return [
'no direction' => ['linear-gradient(red, blue)', $down],
'to bottom' => ['linear-gradient(to bottom, red, blue)', $down],
'to top' => ['linear-gradient(to top, red, blue)', $up],
'to right' => ['linear-gradient(to right, red, blue)', $right],
'to left' => ['linear-gradient(to left, red, blue)', $left],
'repeating, to bottom' => ['repeating-linear-gradient(to bottom, red, blue 5mm)', $down],

// Corner to corner across the box, so the other two corners are halfway along
'to bottom right' => ['linear-gradient(to bottom right, red, blue)', [0, 1, 1, 0]],
'to top left' => ['linear-gradient(to top left, red, blue)', [1, 0, 0, 1]],
'to right top' => ['linear-gradient(to right top, red, blue)', [0, 0, 1, 1]],
'to bottom left' => ['linear-gradient(to bottom left, red, blue)', [1, 1, 0, 0]],

'0deg points up' => ['linear-gradient(0deg, red, blue)', [0, 0, 0, 1]],
'unitless 0 points up' => ['linear-gradient(0, red, blue)', [0, 0, 0, 1]],
'90deg points right' => ['linear-gradient(90deg, red, blue)', [0, 0, 1, 0]],
'180deg points down' => ['linear-gradient(180deg, red, blue)', [0, 1, 0, 0]],
'270deg points left' => ['linear-gradient(270deg, red, blue)', [1, 0, 0, 0]],
'-90deg points left' => ['linear-gradient(-90deg, red, blue)', [1, 0, 0, 0]],
'450deg points right' => ['linear-gradient(450deg, red, blue)', [0, 0, 1, 0]],
'0.5turn points down' => ['linear-gradient(0.5turn, red, blue)', [0, 1, 0, 0]],
'100grad points right' => ['linear-gradient(100grad, red, blue)', [0, 0, 1, 0]],
'3.14159rad points down' => ['linear-gradient(3.14159265rad, red, blue)', [0, 1, 0, 0]],

'-moz- keyword names the start' => ['-moz-linear-gradient(left, red, blue)', $right],
'-moz- top' => ['-moz-linear-gradient(top, red, blue)', $down],
'-webkit- keyword names the start' => ['-webkit-linear-gradient(left, red, blue)', $right],
'-moz- 90deg points up' => ['-moz-linear-gradient(90deg, red, blue)', [0, 0, 0, 1]],
'-webkit- 0deg points right' => ['-webkit-linear-gradient(0deg, red, blue)', [0, 0, 1, 0]],
];
}

/**
* An angle between the sides runs the axis into the right quarter
*
* @dataProvider diagonalProvider
*
* @param string $background
* @param int $dx The sign the axis runs across the box
* @param int $dy The sign the axis runs up the box
*/
public function testDiagonalAxis($background, $dx, $dy)
{
list($x0, $y0, $x1, $y1) = $this->axis($background);

$this->assertSame($dx, $this->sign($x1 - $x0), 'across');
$this->assertSame($dy, $this->sign($y1 - $y0), 'up');
}

/**
* -1, 0 or 1 for a negative, zero or positive difference
*
* @param float $difference
*
* @return int
*/
private function sign($difference)
{
if (abs($difference) < 0.0001) {
return 0;
}

return $difference > 0 ? 1 : -1;
}

/**
* Backgrounds whose axis runs between the sides of the box, with the signs it runs in
*
* @return array
*/
public function diagonalProvider()
{
return [
'45deg' => ['linear-gradient(45deg, red, blue)', 1, 1],
'135deg' => ['linear-gradient(135deg, red, blue)', 1, -1],
'22.5deg' => ['linear-gradient(22.5deg, red, blue)', 1, 1],
'-moz- left top' => ['-moz-linear-gradient(left top, red, blue)', 1, -1],
];
}

}
76 changes: 76 additions & 0 deletions tests/Snapshots/LinearGradientDirectionSnapshotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Snapshots;

/**
* Linear gradient backgrounds run the way CSS Images gives: "to" names the side the gradient ends at, and angles point
* up at 0deg and run clockwise. Prefixed functions keep the legacy reading, where a keyword names the side the
* gradient starts from and angles run counter-clockwise from pointing right.
*
* @group snapshot
*/
class LinearGradientDirectionSnapshotTest extends Snapshot
{

/**
* The fixture's name
*
* @return string
*/
public function getId()
{
return 'linear-gradient-direction';
}

/**
* A red to blue gradient box for each direction, under a caption saying where the red should be
*/
public function generatePdf()
{
$cases = [
'Keywords' => [
['linear-gradient(to bottom, red, blue)', 'to bottom: red at the top, blue at the bottom.'],
['linear-gradient(to top, red, blue)', 'to top: red at the bottom, blue at the top.'],
['linear-gradient(to right, red, blue)', 'to right: red at the left, blue at the right.'],
['linear-gradient(to left, red, blue)', 'to left: red at the right, blue at the left.'],
['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.'],
['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.'],
],
'Angles' => [
['linear-gradient(0deg, red, blue)', '0deg: red at the bottom, blue at the top.'],
['linear-gradient(90deg, red, blue)', '90deg: red at the left, blue at the right.'],
['linear-gradient(180deg, red, blue)', '180deg: red at the top, blue at the bottom.'],
['linear-gradient(270deg, red, blue)', '270deg: red at the right, blue at the left.'],
['linear-gradient(45deg, red, blue)', '45deg: red in the bottom left corner, blue in the top right.'],
['linear-gradient(135deg, red, blue)', '135deg: red in the top left corner, blue in the bottom right.'],
['linear-gradient(0.25turn, red, blue)', '0.25turn: red at the left, blue at the right.'],
],
'Prefixed functions' => [
['-moz-linear-gradient(left, red, blue)', '-moz- left: red at the left, blue at the right.'],
['-webkit-linear-gradient(top, red, blue)', '-webkit- top: red at the top, blue at the bottom.'],
['-webkit-linear-gradient(90deg, red, blue)', '-webkit- 90deg: red at the bottom, blue at the top.'],
],
];

$html = '<style>
body { font-size: 8pt; }
h1 { font-size: 13pt; margin: 0 0 1mm 0; }
h2 { font-size: 10pt; margin: 3mm 0 1mm 0; }
p { margin: 1mm 0 0.5mm 0; }
div.box { width: 60mm; height: 8mm; border: 0.2mm solid #999999; }
</style>
<h1>linear-gradient() directions</h1>';

foreach ($cases as $heading => $gradients) {
$html .= '<h2>' . $heading . '</h2>';
foreach ($gradients as $case) {
$html .= '<p>' . $case[1] . '</p><div class="box" style="background: ' . $case[0] . '"></div>';
}
}

$this->mpdf = $this->createMpdf();

$this->mpdf->WriteHTML($html);
}

}
Binary file not shown.
Loading