|
| 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 | +} |
0 commit comments