2b5846d3d9f7430fe79f92e6c0be28668d562a8e
[yaffs-website] / vendor / phenx / php-font-lib / src / FontLib / Table / DirectoryEntry.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 namespace FontLib\Table;
9
10 use FontLib\TrueType\File;
11 use FontLib\Font;
12 use FontLib\BinaryStream;
13
14 /**
15  * Generic Font table directory entry.
16  *
17  * @package php-font-lib
18  */
19 class DirectoryEntry extends BinaryStream {
20   /**
21    * @var File
22    */
23   protected $font;
24
25   /**
26    * @var Table
27    */
28   protected $font_table;
29
30   public $entryLength = 4;
31
32   public $tag;
33   public $checksum;
34   public $offset;
35   public $length;
36
37   protected $origF;
38
39   static function computeChecksum($data) {
40     $len = strlen($data);
41     $mod = $len % 4;
42
43     if ($mod) {
44       $data = str_pad($data, $len + (4 - $mod), "\0");
45     }
46
47     $len = strlen($data);
48
49     $hi = 0x0000;
50     $lo = 0x0000;
51
52     for ($i = 0; $i < $len; $i += 4) {
53       $hi += (ord($data[$i]) << 8) + ord($data[$i + 1]);
54       $lo += (ord($data[$i + 2]) << 8) + ord($data[$i + 3]);
55       $hi += $lo >> 16;
56       $lo = $lo & 0xFFFF;
57       $hi = $hi & 0xFFFF;
58     }
59
60     return ($hi << 8) + $lo;
61   }
62
63   function __construct(File $font) {
64     $this->font = $font;
65     $this->f    = $font->f;
66   }
67
68   function parse() {
69     $this->tag = $this->font->read(4);
70   }
71
72   function open($filename, $mode = self::modeRead) {
73     // void
74   }
75
76   function setTable(Table $font_table) {
77     $this->font_table = $font_table;
78   }
79
80   function encode($entry_offset) {
81     Font::d("\n==== $this->tag ====");
82     //Font::d("Entry offset  = $entry_offset");
83
84     $data = $this->font_table;
85     $font = $this->font;
86
87     $table_offset = $font->pos();
88     $this->offset = $table_offset;
89     $table_length = $data->encode();
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);
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