Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Name
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Pdf\Build\Font\TrueType\Table;
16
17use Pop\Pdf\Build\Font;
18use Pop\Utils\ArrayObject as Data;
19
20/**
21 * NAME table class
22 *
23 * @category   Pop
24 * @package    Pop\Pdf
25 * @author     Nick Sagona, III <nick@popphp.org>
26 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
27 * @license    https://www.popphp.org/license     New BSD License
28 * @version    6.0.0
29 */
30class Name extends AbstractTable
31{
32
33    /**
34     * Font table properties
35     * @var array
36     */
37    protected array $properties = [];
38
39    /**
40     * TrueType font info names
41     * @var array
42     */
43    protected array $names = [
44        0  => 'copyright',
45        1  => 'fontFamily',
46        2  => 'fontSubFamily',
47        3  => 'uniqueId',
48        4  => 'fullName',
49        5  => 'version',
50        6  => 'postscriptName',
51        7  => 'trademark',
52        8  => 'manufacturer',
53        9  => 'designer',
54        10 => 'description',
55        11 => 'vendorUrl',
56        12 => 'designerUrl',
57        13 => 'license',
58        14 => 'licenseUrl',
59        16 => 'preferredFamily',
60        17 => 'preferredSubFamily',
61        18 => 'compatibleFull',
62        19 => 'sampleText'
63    ];
64
65    /**
66     * Constructor
67     *
68     * Instantiate a TTF 'name' table object.
69     *
70     * @param  Font\TrueType $font
71     */
72    public function __construct(Font\TrueType $font)
73    {
74        // TrueType::parseName(), the only caller, guards on
75        // isset($this->properties['tableInfo']['name']) before constructing
76        // this class, so $tableInfo['name'] is always already set here.
77        $tableInfo = $font->tableInfo;
78
79        $tableInfo['name']['header'] = new Data(
80            unpack(
81                'nformatSelector/' .
82                'nnameRecordsCount/' .
83                'nstorageOffset', $font->read($font->tableInfo['name']->offset, 6)
84            )
85        );
86
87        $font->tableInfo = $tableInfo;
88
89        $bytePos = $font->tableInfo['name']->offset + 6;
90
91        for ($j = 0; $j < $font->tableInfo['name']->header->nameRecordsCount; $j++) {
92            $ttfRecord = unpack(
93                'nplatformId/' .
94                'nencodingId/' .
95                'nlanguageId/' .
96                'nnameId/' .
97                'nlength/' .
98                'noffset', $font->read($bytePos, 12)
99            );
100
101            $ttfRecordOffset = $bytePos + 12;
102            $nextBytePos = $font->tableInfo['name']->offset + $font->tableInfo['name']->header->storageOffset + $ttfRecord['offset'];
103
104            $ttfValue = $font->read($nextBytePos, $ttfRecord['length']);
105
106            if ($ttfRecord['platformId'] != 1) {
107                $ttfValue = @iconv('UTF-16be', 'UTF-8//TRANSLIT', $ttfValue);
108            }
109            if (($ttfValue != '') && isset($ttfRecord['nameId']) && isset($this->names[$ttfRecord['nameId']])) {
110                $this->properties[$this->names[$ttfRecord['nameId']]] = $ttfValue;
111            }
112
113            $bytePos = $ttfRecordOffset;
114        }
115    }
116
117}