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
12 * Generic font file binary stream.
14 * @package php-font-lib
18 * @var resource The file pointer
33 const longDateTime
= 12;
36 const modeRead
= "rb";
37 const modeWrite
= "wb";
38 const modeReadWrite
= "rb+";
40 static function backtrace() {
41 var_dump(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS
));
45 * Open a font file in read mode
47 * @param string $filename The file name of the font to open
51 public function load($filename) {
52 return $this->open($filename, self
::modeRead
);
56 * Open a font file in a chosen mode
58 * @param string $filename The file name of the font to open
59 * @param string $mode The opening mode
64 public function open($filename, $mode = self
::modeRead
) {
65 if (!in_array($mode, array(self
::modeRead
, self
::modeWrite
, self
::modeReadWrite
))) {
66 throw new \
Exception("Unkown file open mode");
69 $this->f
= fopen($filename, $mode);
71 return $this->f
!= false;
75 * Close the internal file pointer
77 public function close() {
78 return fclose($this->f
) != false;
82 * Change the internal file pointer
88 public function setFile($fp) {
89 if (!is_resource($fp)) {
90 throw new \
Exception('$fp is not a valid resource');
97 * Create a temporary file in write mode
99 * @param bool $allow_memory Allow in-memory files
101 * @return resource the temporary file pointer resource
103 public static function getTempFile($allow_memory = true) {
107 $f = fopen("php://temp", "rb+");
110 $f = fopen(tempnam(sys_get_temp_dir(), "fnt"), "rb+");
117 * Move the internal file pinter to $offset bytes
121 * @return bool True if the $offset position exists in the file
123 public function seek($offset) {
124 return fseek($this->f
, $offset, SEEK_SET
) == 0;
128 * Gives the current position in the file
130 * @return int The current position
132 public function pos() {
133 return ftell($this->f
);
136 public function skip($n) {
137 fseek($this->f
, $n, SEEK_CUR
);
140 public function read($n) {
145 return fread($this->f
, $n);
148 public function write($data, $length = null) {
149 if ($data === null ||
$data === "" ||
$data === false) {
153 return fwrite($this->f
, $data, $length);
156 public function readUInt8() {
157 return ord($this->read(1));
160 public function readUInt8Many($count) {
161 return array_values(unpack("C*", $this->read($count)));
164 public function writeUInt8($data) {
165 return $this->write(chr($data), 1);
168 public function readInt8() {
169 $v = $this->readUInt8();
178 public function readInt8Many($count) {
179 return array_values(unpack("c*", $this->read($count)));
182 public function writeInt8($data) {
187 return $this->writeUInt8($data);
190 public function readUInt16() {
191 $a = unpack("nn", $this->read(2));
196 public function readUInt16Many($count) {
197 return array_values(unpack("n*", $this->read($count * 2)));
200 public function readUFWord() {
201 return $this->readUInt16();
204 public function writeUInt16($data) {
205 return $this->write(pack("n", $data), 2);
208 public function writeUFWord($data) {
209 return $this->writeUInt16($data);
212 public function readInt16() {
213 $a = unpack("nn", $this->read(2));
223 public function readInt16Many($count) {
224 $vals = array_values(unpack("n*", $this->read($count * 2)));
225 foreach ($vals as &$v) {
234 public function readFWord() {
235 return $this->readInt16();
238 public function writeInt16($data) {
243 return $this->writeUInt16($data);
246 public function writeFWord($data) {
247 return $this->writeInt16($data);
250 public function readUInt32() {
251 $a = unpack("NN", $this->read(4));
256 public function writeUInt32($data) {
257 return $this->write(pack("N", $data), 4);
260 public function readFixed() {
261 $d = $this->readInt16();
262 $d2 = $this->readUInt16();
264 return round($d +
$d2 / 0x10000, 4);
267 public function writeFixed($data) {
268 $left = floor($data);
269 $right = ($data - $left) * 0x10000;
271 return $this->writeInt16($left) +
$this->writeUInt16($right);
274 public function readLongDateTime() {
275 $this->readUInt32(); // ignored
276 $date = $this->readUInt32() - 2082844800;
278 # PHP_INT_MIN isn't defined in PHP < 7.0
279 $php_int_min = defined("PHP_INT_MIN") ? PHP_INT_MIN
: ~PHP_INT_MAX
;
281 if (is_string($date) ||
$date > PHP_INT_MAX ||
$date < $php_int_min) {
285 return strftime("%Y-%m-%d %H:%M:%S", $date);
288 public function writeLongDateTime($data) {
289 $date = strtotime($data);
292 return $this->writeUInt32(0) +
$this->writeUInt32($date);
295 public function unpack($def) {
297 foreach ($def as $name => $type) {
298 $d[$name] = $this->r($type);
304 public function pack($def, $data) {
306 foreach ($def as $name => $type) {
307 $bytes +
= $this->w($type, $data[$name]);
314 * Read a data of type $type in the file from the current position
316 * @param mixed $type The data type to read
318 * @return mixed The data that was read
320 public function r($type) {
323 return $this->readUInt8();
325 return $this->readInt8();
327 return $this->readUInt16();
329 return $this->readInt16();
331 return $this->readUInt32();
333 return $this->readUInt32();
334 case self
::shortFrac
:
335 return $this->readFixed();
337 return $this->readFixed();
339 return $this->readInt16();
341 return $this->readUInt16();
343 return $this->readInt16();
344 case self
::longDateTime
:
345 return $this->readLongDateTime();
347 return $this->read(1);
349 if (is_array($type)) {
350 if ($type[0] == self
::char
) {
351 return $this->read($type[1]);
353 if ($type[0] == self
::uint16
) {
354 return $this->readUInt16Many($type[1]);
356 if ($type[0] == self
::int16
) {
357 return $this->readInt16Many($type[1]);
359 if ($type[0] == self
::uint8
) {
360 return $this->readUInt8Many($type[1]);
362 if ($type[0] == self
::int8
) {
363 return $this->readInt8Many($type[1]);
367 for ($i = 0; $i < $type[1]; $i++
) {
368 $ret[] = $this->r($type[0]);
379 * Write $data of type $type in the file from the current position
381 * @param mixed $type The data type to write
382 * @param mixed $data The data to write
384 * @return int The number of bytes read
386 public function w($type, $data) {
389 return $this->writeUInt8($data);
391 return $this->writeInt8($data);
393 return $this->writeUInt16($data);
395 return $this->writeInt16($data);
397 return $this->writeUInt32($data);
399 return $this->writeUInt32($data);
400 case self
::shortFrac
:
401 return $this->writeFixed($data);
403 return $this->writeFixed($data);
405 return $this->writeInt16($data);
407 return $this->writeUInt16($data);
409 return $this->writeInt16($data);
410 case self
::longDateTime
:
411 return $this->writeLongDateTime($data);
413 return $this->write($data, 1);
415 if (is_array($type)) {
416 if ($type[0] == self
::char
) {
417 return $this->write($data, $type[1]);
421 for ($i = 0; $i < $type[1]; $i++
) {
422 if (isset($data[$i])) {
423 $ret +
= $this->w($type[0], $data[$i]);
435 * Converts a Uint32 value to string
439 * @return string The string
441 public function convertUInt32ToStr($uint32) {
442 return chr(($uint32 >> 24) & 0xFF) . chr(($uint32 >> 16) & 0xFF) . chr(($uint32 >> 8) & 0xFF) . chr($uint32 & 0xFF);