Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Glyf
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
1 / 1
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
1 / 1
12
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
17/**
18 * GLYF table class
19 *
20 * @category   Pop
21 * @package    Pop\Pdf
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    6.0.0
26 */
27class Glyf extends AbstractTable
28{
29
30    /**
31     * Font table properties
32     * @var array
33     */
34    protected array $properties = [
35        'glyphs'      => [],
36        'glyphWidths' => []
37    ];
38
39    /**
40     * Constructor
41     *
42     * Instantiate a TTF 'glyf' table object.
43     *
44     * @param  \Pop\Pdf\Build\Font\TrueType $font
45     */
46    public function __construct(\Pop\Pdf\Build\Font\TrueType $font)
47    {
48        $locaLength = count($font->tables['loca']->offsets);
49        $j = 0;
50
51        foreach ($font->tables['loca']->offsets as $offset) {
52            $bytePos = $font->tableInfo['glyf']->offset + $offset;
53            $ary = unpack(
54                'nnumberOfContours/' .
55                'nxMin/' .
56                'nyMin/' .
57                'nxMax/' .
58                'nyMax', $font->read($bytePos, 10)
59            );
60            $ary = $font->shiftToSigned($ary);
61            $ary['xMin']  = $font->toEmSpace($ary['xMin']);
62            $ary['yMin']  = $font->toEmSpace($ary['yMin']);
63            $ary['xMax']  = $font->toEmSpace($ary['xMax']);
64            $ary['yMax']  = $font->toEmSpace($ary['yMax']);
65            $ary['width'] = $ary['xMin'] + $ary['xMax'];
66            $this->properties['glyphWidths'][] = $ary['width'];
67
68            $bytePos += 10;
69            $ary['endPtsOfContours'] = [];
70            $ary['instructionLength'] = null;
71            $ary['instructions'] = null;
72            $ary['flags'] = null;
73
74            // The simple and composite glyph descriptions may not be necessary.
75            // If simple glyph.
76            if ($ary['numberOfContours'] > 0) {
77                $endPtsLength = $ary['numberOfContours'] * 2;
78                $endPts       = array_values(unpack('n*', $font->read($bytePos, $endPtsLength)));
79                for ($i = 0; $i < $ary['numberOfContours']; $i++) {
80                    $ary['endPtsOfContours'][$i] = $endPts[$i];
81                }
82                $bytePos += $endPtsLength;
83
84                $ar = unpack('ninstructionLength', $font->read($bytePos, 2));
85                $ary['instructionLength'] = $ar['instructionLength'];
86                $bytePos += 2;
87                if ($ary['instructionLength'] > 0) {
88                    $instructionData = $font->read($bytePos, $ary['instructionLength']);
89                    $readLength      = ($instructionData !== null) ? strlen($instructionData) : 0;
90                    $instructionBytes = ($readLength > 0) ? array_values(unpack('C*', $instructionData)) : [];
91                    for ($i = 0; $i < $ary['instructionLength']; $i++) {
92                        if ($i < $readLength) {
93                            $ary['instructions'][$i] = $instructionBytes[$i];
94                        } else {
95                            $ary['instructions'][$i] = null;
96                        }
97                    }
98                    $bytePos += $readLength;
99                }
100                $bytePos++;
101                $byte = $font->read($bytePos, 1);
102                if (strlen($byte) != 0) {
103                    $ar = unpack('Cflags', $byte);
104                    $ary['flags'] = $ar['flags'];
105                } else {
106                    $ary['flags'] = 0;
107                }
108                if ($j < ($locaLength - 1)) {
109                    $this->properties['glyphs'][] = $ary;
110                }
111            // Stopped here. Still need to get the x & y coordinates of the simple glyph.
112            // Else, if composite glyph.
113            } else {
114                if ($j < ($locaLength - 1)) {
115                    // Composite glyph goes here.
116                }
117            }
118            $j++;
119        }
120    }
121
122}