Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
FontFace
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\HtmlToPdf\Layout;
6
7use Phpdftk\FontParser\FontFaceData;
8
9/**
10 * One face inside a CSS font family — a single `FontFaceData` with its
11 * `font-weight` / `font-style` / `font-stretch` metadata so
12 * {@see FontResolver} can run CSS Fonts 4 §6 font-matching (weight matching
13 * + style matching + stretch matching) over a multi-face family.
14 *
15 * Weight values follow the CSS 1–1000 keyword/integer system (400 = normal,
16 * 700 = bold). Style is the keyword string (`normal`, `italic`, or `oblique`)
17 * — normalised to lower-case at construction. Stretch is the percentage
18 * along the spec's [50%, 200%] axis (50 = ultra-condensed, 100 = normal,
19 * 200 = ultra-expanded).
20 */
21final readonly class FontFace
22{
23    public FontFaceData $data;
24    public int $weight;
25    public string $style;
26    public float $stretch;
27
28    public function __construct(
29        FontFaceData $data,
30        int $weight = 400,
31        string $style = 'normal',
32        float $stretch = 100.0,
33    ) {
34        if ($weight < 1 || $weight > 1000) {
35            throw new \InvalidArgumentException(sprintf(
36                'FontFace weight must be 1-1000 per CSS Fonts 4 §3.2; got %d',
37                $weight,
38            ));
39        }
40        $lcStyle = strtolower($style);
41        if (!in_array($lcStyle, ['normal', 'italic', 'oblique'], true)) {
42            throw new \InvalidArgumentException(sprintf(
43                'FontFace style must be normal|italic|oblique per CSS Fonts 4 §3.3; got "%s"',
44                $style,
45            ));
46        }
47        if ($stretch < 50.0 || $stretch > 200.0) {
48            throw new \InvalidArgumentException(sprintf(
49                'FontFace stretch must be 50-200 percent per CSS Fonts 4 §3.4; got %.2f',
50                $stretch,
51            ));
52        }
53        $this->data = $data;
54        $this->weight = $weight;
55        $this->style = $lcStyle;
56        $this->stretch = $stretch;
57    }
58}