Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.00% |
19 / 20 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| LinearGradient | |
95.00% |
19 / 20 |
|
83.33% |
5 / 6 |
11 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| x1 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| y1 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| x2 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| y2 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseOptionalLength | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
6.01 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Svg\Gradient; |
| 6 | |
| 7 | /** |
| 8 | * SVG `<linearGradient>` per SVG 2 §13.6. Endpoints `(x1, y1)` and |
| 9 | * `(x2, y2)` define the gradient axis; stops are colour samples along it. |
| 10 | * |
| 11 | * Each endpoint accessor returns null when the attribute is absent so the |
| 12 | * painter can apply the unit-mode-specific defaults (e.g., `x1=0` / |
| 13 | * `x2=1` in `objectBoundingBox` mode, `0` / `100%` in `userSpaceOnUse` |
| 14 | * mode per SVG 2 §13.6.5). |
| 15 | */ |
| 16 | final class LinearGradient extends Gradient |
| 17 | { |
| 18 | public function __construct() |
| 19 | { |
| 20 | parent::__construct('linearGradient'); |
| 21 | } |
| 22 | |
| 23 | public function x1(): ?float |
| 24 | { |
| 25 | return $this->parseOptionalLength('x1'); |
| 26 | } |
| 27 | |
| 28 | public function y1(): ?float |
| 29 | { |
| 30 | return $this->parseOptionalLength('y1'); |
| 31 | } |
| 32 | |
| 33 | public function x2(): ?float |
| 34 | { |
| 35 | return $this->parseOptionalLength('x2'); |
| 36 | } |
| 37 | |
| 38 | public function y2(): ?float |
| 39 | { |
| 40 | return $this->parseOptionalLength('y2'); |
| 41 | } |
| 42 | |
| 43 | private function parseOptionalLength(string $attr): ?float |
| 44 | { |
| 45 | $raw = $this->getAttribute($attr); |
| 46 | if ($raw === null) { |
| 47 | return null; |
| 48 | } |
| 49 | if (preg_match( |
| 50 | '/^\s*([+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)\s*(%|[a-zA-Z]*)/', |
| 51 | $raw, |
| 52 | $m, |
| 53 | ) !== 1) { |
| 54 | return null; |
| 55 | } |
| 56 | $value = (float) $m[1]; |
| 57 | if ($m[2] !== '' && $m[2] !== '%') { |
| 58 | // A gradient coordinate is a `<length>`, so an ABSOLUTE |
| 59 | // unit suffix is meaningful: `x2="1in"` is 96 CSS px, which |
| 60 | // in objectBoundingBox mode is 96 bounding-box widths, not |
| 61 | // one. Throwing the suffix away read `1in` as `1`. |
| 62 | // Font-relative units need context this accessor doesn't |
| 63 | // have, so `absoluteUnitScale()` leaves them at 1. |
| 64 | return $value * \Phpdftk\Svg\Element::absoluteUnitScale($m[2]); |
| 65 | } |
| 66 | // SVG 2 §13.6.5 — a percentage in objectBoundingBox mode (the default |
| 67 | // gradientUnits) is a fraction of the bounding box, so `100%` == `1`. |
| 68 | // Normalise to the [0, 1] fraction the GradientPainter bbox mapping |
| 69 | // expects; parsing `x2="100%"` as `100` stretched the axis 100x so the |
| 70 | // shape sampled a single stop (a solid colour). |
| 71 | if ($m[2] === '%') { |
| 72 | $value /= 100.0; |
| 73 | } |
| 74 | return $value; |
| 75 | } |
| 76 | } |