Debian Codegraph @

vendor/dompdf/php-font-lib/src/FontLib/Table/DirectoryEntry.php

raw
1 <?php
2 /**
3 * @package php-font-lib
4 * @link https://github.com/dompdf/php-font-lib
5 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
6 */
7 namespace FontLib\Table;
8
9 use FontLib\TrueType\File;
10 use FontLib\Font;
11 use FontLib\BinaryStream;
12
13 /**
14 * Generic Font table directory entry.
15 *
16 * @package php-font-lib
17 */
18 class DirectoryEntry extends BinaryStream {
19 /**
20 * @var File
21 */
22 protected $font;
23
24 /**
25 * @var Table
26 */
27 protected $font_table;
28
29 public $entryLength = 4;
30
31 public $tag;
32 public $checksum;
33 public $offset;
34 public $length;
35
36 protected $origF;
37
38 /**
39 * @param string $data
40 *
41 * @return int
42 */
43 static function computeChecksum($data) {
44 $len = mb_strlen($data, '8bit');
45 $mod = $len % 4;
46
47 if ($mod) {
48 $data = str_pad($data, $len + (4 - $mod), "\0");
49 }
50
51 $table = unpack("N*", $data);
52 return array_sum($table);
53 }
54
55 function __construct(File $font) {
56 $this->font = $font;
57 $this->f = $font->f;
58 }
59
60 function parse() {
61 $this->tag = $this->font->read(4);
62 }
63
64 function open($filename, $mode = self::modeRead) {
65 // void
66 }
67
68 function setTable(Table $font_table) {
69 $this->font_table = $font_table;
70 }
71
72 function encode($entry_offset) {
73 Font::d("\n==== $this->tag ====");
74 //Font::d("Entry offset = $entry_offset");
75
76 $data = $this->font_table;
77 $font = $this->font;
78
79 $table_offset = $font->pos();
80 $this->offset = $table_offset;
81 $table_length = $data->encode();
82
83 $font->seek($table_offset + $table_length);
84 $pad = 0;
85 $mod = $table_length % 4;
86 if ($mod != 0) {
87 $pad = 4 - $mod;
88 $font->write(str_pad("", $pad, "\0"), $pad);
89 }
90
91 $font->seek($table_offset);
92 $table_data = $font->read($table_length);
93
94 $font->seek($entry_offset);
95
96 $font->write($this->tag, 4);
97 $font->writeUInt32(self::computeChecksum($table_data));
98 $font->writeUInt32($table_offset);
99 $font->writeUInt32($table_length);
100
101 Font::d("Bytes written = $table_length");
102
103 $font->seek($table_offset + $table_length + $pad);
104 }
105
106 /**
107 * @return File
108 */
109 function getFont() {
110 return $this->font;
111 }
112
113 function startRead() {
114 $this->font->seek($this->offset);
115 }
116
117 function endRead() {
118 //
119 }
120
121 function startWrite() {
122 $this->font->seek($this->offset);
123 }
124
125 function endWrite() {
126 //
127 }
128 }
129