f6397c31ec87c8141c25cc21b603b21ce7c133a5
[yaffs-website] / vendor / phenx / php-font-lib / src / FontLib / Table / Type / loca.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\Table\Type;
10 use FontLib\Table\Table;
11
12 /**
13  * `loca` font table.
14  *
15  * @package php-font-lib
16  */
17 class loca extends Table {
18   protected function _parse() {
19     $font   = $this->getFont();
20     $offset = $font->pos();
21
22     $indexToLocFormat = $font->getData("head", "indexToLocFormat");
23     $numGlyphs        = $font->getData("maxp", "numGlyphs");
24
25     $font->seek($offset);
26
27     $data = array();
28
29     // 2 bytes
30     if ($indexToLocFormat == 0) {
31       $d   = $font->read(($numGlyphs + 1) * 2);
32       $loc = unpack("n*", $d);
33
34       for ($i = 0; $i <= $numGlyphs; $i++) {
35         $data[] = $loc[$i + 1] * 2;
36       }
37     }
38
39     // 4 bytes
40     else {
41       if ($indexToLocFormat == 1) {
42         $d   = $font->read(($numGlyphs + 1) * 4);
43         $loc = unpack("N*", $d);
44
45         for ($i = 0; $i <= $numGlyphs; $i++) {
46           $data[] = $loc[$i + 1];
47         }
48       }
49     }
50
51     $this->data = $data;
52   }
53
54   function _encode() {
55     $font = $this->getFont();
56     $data = $this->data;
57
58     $indexToLocFormat = $font->getData("head", "indexToLocFormat");
59     $numGlyphs        = $font->getData("maxp", "numGlyphs");
60     $length           = 0;
61
62     // 2 bytes
63     if ($indexToLocFormat == 0) {
64       for ($i = 0; $i <= $numGlyphs; $i++) {
65         $length += $font->writeUInt16($data[$i] / 2);
66       }
67     }
68
69     // 4 bytes
70     else {
71       if ($indexToLocFormat == 1) {
72         for ($i = 0; $i <= $numGlyphs; $i++) {
73           $length += $font->writeUInt32($data[$i]);
74         }
75       }
76     }
77
78     return $length;
79   }
80 }