9e54b3fb04fa440e4b7a75f3a730faba9c344705
[yaffs-website] / vendor / phenx / php-font-lib / src / FontLib / WOFF / File.php
1 <?php
2 /**
3  * @package php-font-lib
4  * @link    https://github.com/PhenX/php-font-lib
5  * @author  Fabien Ménager <fabien.menager@gmail.com>
6  * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7  */
8
9 namespace FontLib\WOFF;
10
11 use FontLib\Table\DirectoryEntry;
12
13 /**
14  * WOFF font file.
15  *
16  * @package php-font-lib
17  *
18  * @property TableDirectoryEntry[] $directory
19  */
20 class File extends \FontLib\TrueType\File {
21   function parseHeader() {
22     if (!empty($this->header)) {
23       return;
24     }
25
26     $this->header = new Header($this);
27     $this->header->parse();
28   }
29
30   public function load($file) {
31     parent::load($file);
32
33     $this->parseTableEntries();
34     $dataOffset = $this->pos() + count($this->directory) * 20;
35
36     $fw = $this->getTempFile(false);
37     $fr = $this->f;
38
39     $this->f = $fw;
40     $offset  = $this->header->encode();
41
42     foreach ($this->directory as $entry) {
43       // Read ...
44       $this->f = $fr;
45       $this->seek($entry->offset);
46       $data = $this->read($entry->length);
47
48       if ($entry->length < $entry->origLength) {
49         $data = gzuncompress($data);
50       }
51
52       // Prepare data ...
53       $length        = strlen($data);
54       $entry->length = $entry->origLength = $length;
55       $entry->offset = $dataOffset;
56
57       // Write ...
58       $this->f = $fw;
59
60       // Woff Entry
61       $this->seek($offset);
62       $offset += $this->write($entry->tag, 4); // tag
63       $offset += $this->writeUInt32($dataOffset); // offset
64       $offset += $this->writeUInt32($length); // length
65       $offset += $this->writeUInt32($length); // origLength
66       $offset += $this->writeUInt32(DirectoryEntry::computeChecksum($data)); // checksum
67
68       // Data
69       $this->seek($dataOffset);
70       $dataOffset += $this->write($data, $length);
71     }
72
73     $this->f = $fw;
74     $this->seek(0);
75
76     // Need to re-parse this, don't know why
77     $this->header    = null;
78     $this->directory = array();
79     $this->parseTableEntries();
80   }
81 }