Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MathKern
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
4
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
 valueAt
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\FontParser;
6
7/**
8 * One corner kern table from a {@see MathKernRecord}.
9 *
10 * Per OpenType MATH spec, a kern table is a piecewise function of
11 * Y position: `n` correction heights (breakpoints) define `n + 1`
12 * height ranges, each carrying a kern value.
13 *
14 *   - For Y < correctionHeights[0]:                kern = kernValues[0]
15 *   - For correctionHeights[i-1] <= Y < heights[i]: kern = kernValues[i]
16 *   - For Y >= correctionHeights[n-1]:             kern = kernValues[n]
17 *
18 * The painter walks correction heights and picks the kern value
19 * matching the sub/super Y attachment height.
20 */
21final readonly class MathKern
22{
23    /**
24     * @param list<int> $correctionHeights FUnit Y breakpoints,
25     *        strictly increasing per spec.
26     * @param list<int> $kernValues FUnit horizontal kerning at each
27     *        range. Always length `correctionHeights + 1`.
28     */
29    public function __construct(
30        public array $correctionHeights,
31        public array $kernValues,
32    ) {}
33
34    /**
35     * Look up the kern value for a given Y offset above baseline.
36     */
37    public function valueAt(int $heightFunits): int
38    {
39        $i = 0;
40        $count = count($this->correctionHeights);
41        while ($i < $count && $heightFunits >= $this->correctionHeights[$i]) {
42            $i++;
43        }
44        return $this->kernValues[$i] ?? 0;
45    }
46}