Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
52.94% |
9 / 17 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| LengthContext | |
52.94% |
9 / 17 |
|
66.67% |
2 / 3 |
3.94 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withCurrentFontSize | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| withPercentageBasis | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Css\Cascade; |
| 6 | |
| 7 | /** |
| 8 | * Context needed to resolve relative lengths (em/rem/%/vw/vh/ex/ch/lh/rlh) |
| 9 | * to absolute pixels. |
| 10 | * |
| 11 | * - `parentFontSize` — used when resolving `em` on the `font-size` property |
| 12 | * itself (parent's font-size is the reference). |
| 13 | * - `currentFontSize` — used for everything else; the resolved font-size of |
| 14 | * the element being computed. Two-pass cascade: resolve font-size first |
| 15 | * with `parentFontSize`, then resolve other lengths using the result. |
| 16 | * - `rootFontSize` — `rem` reference; the document root's font-size. |
| 17 | * - `viewportWidth` / `viewportHeight` — `vw` / `vh` references in CSS pixels. |
| 18 | * - `percentageBasis` — the basis a `%` length resolves against; depends on |
| 19 | * the property (width: containing-block width; line-height: own font-size, |
| 20 | * etc.). Callers pass per-property as needed. |
| 21 | */ |
| 22 | final readonly class LengthContext |
| 23 | { |
| 24 | public function __construct( |
| 25 | public float $parentFontSize = 16.0, |
| 26 | public float $currentFontSize = 16.0, |
| 27 | public float $rootFontSize = 16.0, |
| 28 | public float $viewportWidth = 816.0, // 8.5in × 96 DPI |
| 29 | public float $viewportHeight = 1056.0, // 11in × 96 DPI |
| 30 | public float $percentageBasis = 0.0, |
| 31 | ) {} |
| 32 | |
| 33 | public function withCurrentFontSize(float $px): self |
| 34 | { |
| 35 | return new self( |
| 36 | $this->parentFontSize, |
| 37 | $px, |
| 38 | $this->rootFontSize, |
| 39 | $this->viewportWidth, |
| 40 | $this->viewportHeight, |
| 41 | $this->percentageBasis, |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | public function withPercentageBasis(float $px): self |
| 46 | { |
| 47 | return new self( |
| 48 | $this->parentFontSize, |
| 49 | $this->currentFontSize, |
| 50 | $this->rootFontSize, |
| 51 | $this->viewportWidth, |
| 52 | $this->viewportHeight, |
| 53 | $px, |
| 54 | ); |
| 55 | } |
| 56 | } |