Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
LayoutContext
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
5 / 5
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withOrigin
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 withContainingBlock
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 withLengthContext
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 withFloatContext
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\HtmlToPdf\Layout;
6
7use Phpdftk\Css\Cascade\LengthContext;
8use Phpdftk\FontParser\OpenTypeData;
9
10/**
11 * Per-layout-step context: the containing block's content width / height,
12 * the current X / Y origin where the next child will go, the
13 * `LengthContext` used by the CSS resolver for em / rem / vw / vh / %, and
14 * the default font supplied to {@see InlineLayout} for text measurement.
15 *
16 * Layout creates child contexts (via `with*()`) when it descends into a
17 * box, shifting origin and updating the containing-block measurements so
18 * `%` resolves correctly.
19 *
20 * `defaultFont` is null when no font is wired in yet — in that case
21 * inline layout falls back to producing zero-height placeholders so
22 * block layout can still run end-to-end. Hosts that want real typography
23 * provide a parsed `OpenTypeData` here (see `phpdftk/font-parser`).
24 */
25final readonly class LayoutContext
26{
27    public function __construct(
28        public float $containingBlockWidth,
29        public float $containingBlockHeight,
30        public float $originX,
31        public float $originY,
32        public LengthContext $lengthContext,
33        public ?OpenTypeData $defaultFont = null,
34        /**
35         * Optional multi-font selector. When set, `InlineLayout` picks the
36         * shaping font per box from this resolver — falling back to
37         * `defaultFont` when no `font-family` matches.
38         */
39        public ?FontResolver $fontResolver = null,
40        /**
41         * Tracks active floats per CSS 2.1 §9.5 for the current block
42         * formatting context. `InlineLayout` queries this to shorten
43         * line boxes that overlap a float's vertical extent. Null when
44         * no BFC has registered any floats yet.
45         */
46        public ?FloatContext $floatContext = null,
47    ) {}
48
49    public function withOrigin(float $x, float $y): self
50    {
51        return new self(
52            $this->containingBlockWidth,
53            $this->containingBlockHeight,
54            $x,
55            $y,
56            $this->lengthContext,
57            $this->defaultFont,
58            $this->fontResolver,
59            $this->floatContext,
60        );
61    }
62
63    public function withContainingBlock(float $width, float $height): self
64    {
65        return new self(
66            $width,
67            $height,
68            $this->originX,
69            $this->originY,
70            $this->lengthContext,
71            $this->defaultFont,
72            $this->fontResolver,
73            $this->floatContext,
74        );
75    }
76
77    public function withLengthContext(LengthContext $ctx): self
78    {
79        return new self(
80            $this->containingBlockWidth,
81            $this->containingBlockHeight,
82            $this->originX,
83            $this->originY,
84            $ctx,
85            $this->defaultFont,
86            $this->fontResolver,
87            $this->floatContext,
88        );
89    }
90
91    public function withFloatContext(?FloatContext $ctx): self
92    {
93        return new self(
94            $this->containingBlockWidth,
95            $this->containingBlockHeight,
96            $this->originX,
97            $this->originY,
98            $this->lengthContext,
99            $this->defaultFont,
100            $this->fontResolver,
101            $ctx,
102        );
103    }
104}