Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Font
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
7 / 7
7
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
 getFamily
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getResourceName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParsedData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTextEncoder
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUnicodeToGidMap
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOldToNewGidMap
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Writer;
6
7use Phpdftk\Encoding\TextEncoder;
8use Phpdftk\FontParser\OpenTypeData;
9use Phpdftk\FontParser\TrueTypeData;
10use Phpdftk\Pdf\Core\Font\RegisteredFont;
11
12/**
13 * Opaque font handle returned by PdfWriter::addFont().
14 *
15 * Encapsulates the PDF resource name (F1, F2, ...), parsed font data, and
16 * the text encoder (when the font uses a single-byte encoding such as
17 * WinAnsi). Pass to ContentStream::setFont() to select the font for a
18 * subsequent text run; passing the handle (rather than just the resource
19 * name) is what makes showText() accept UTF-8 directly.
20 */
21final class Font implements RegisteredFont
22{
23    /**
24     * @param array<int, int> $unicodeToGid Unicode codepoint → post-subset GID.
25     *        Only populated for composite (Type 0) fonts; empty for everything
26     *        else. Use this when emitting glyph IDs into a content stream
27     *        rather than the unsubset map on `TrueTypeData`, which points at
28     *        glyphs that no longer exist in the embedded subset.
29     * @param array<int, int> $oldToNewGid Pre-subset GID → post-subset GID.
30     *        Used by external shapers (`phpdftk/text`) that produce GIDs
31     *        against the full font; translate through this map before
32     *        emitting into a content stream so the resulting hex string
33     *        points at the subset's renumbered glyphs.
34     */
35    public function __construct(
36        private readonly string $resourceName,
37        private readonly string $family,
38        private readonly TrueTypeData|OpenTypeData|null $parsedData = null,
39        private readonly ?TextEncoder $encoder = null,
40        private readonly array $unicodeToGid = [],
41        private readonly array $oldToNewGid = [],
42    ) {}
43
44    public function getFamily(): string
45    {
46        return $this->family;
47    }
48
49    /**
50     * @internal Used by Page to emit font operators
51     */
52    public function getResourceName(): string
53    {
54        return $this->resourceName;
55    }
56
57    /**
58     * @internal Used by Page for Unicode text, kerning, ligatures
59     */
60    public function getParsedData(): TrueTypeData|OpenTypeData|null
61    {
62        return $this->parsedData;
63    }
64
65    /**
66     * The text encoder that converts UTF-8 input into the byte sequence the
67     * underlying PDF font expects. Null for composite/CID fonts, which use
68     * the GID-hex path (ContentStream::showTextHex / showUnicodeText).
69     */
70    public function getTextEncoder(): ?TextEncoder
71    {
72        return $this->encoder;
73    }
74
75    /**
76     * Unicode codepoint → post-subset GID map for composite (Type 0) fonts.
77     * Use this when building a hex glyph string for showTextHex; the map on
78     * the parsed font data points at pre-subset GIDs that no longer match
79     * the embedded subset. Returns an empty array for standard or simple
80     * TrueType fonts that don't go through subsetting.
81     *
82     * @return array<int, int>
83     */
84    public function getUnicodeToGidMap(): array
85    {
86        return $this->unicodeToGid;
87    }
88
89    /**
90     * Pre-subset GID → post-subset GID map. Empty for non-subsetted fonts.
91     * External shapers that bind glyph IDs against the full
92     * `OpenTypeData::fullUnicodeToGid` translate through this when emitting.
93     *
94     * @return array<int, int>
95     */
96    public function getOldToNewGidMap(): array
97    {
98        return $this->oldToNewGid;
99    }
100}