| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package php-font-lib |
| 5 |
* @link https://github.com/dompdf/php-font-lib |
| 6 |
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 7 |
*/ |
| 8 |
namespace WCPOS\Vendor\FontLib; |
| 9 |
|
| 10 |
/** |
| 11 |
* Generic font file binary stream. |
| 12 |
* |
| 13 |
* @package php-font-lib |
| 14 |
*/ |
| 15 |
class BinaryStream |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @var resource The file pointer |
| 19 |
*/ |
| 20 |
protected $f; |
| 21 |
const uint8 = 1; |
| 22 |
const int8 = 2; |
| 23 |
const uint16 = 3; |
| 24 |
const int16 = 4; |
| 25 |
const uint32 = 5; |
| 26 |
const int32 = 6; |
| 27 |
const shortFrac = 7; |
| 28 |
const Fixed = 8; |
| 29 |
const FWord = 9; |
| 30 |
const uFWord = 10; |
| 31 |
const F2Dot14 = 11; |
| 32 |
const longDateTime = 12; |
| 33 |
const char = 13; |
| 34 |
const modeRead = "rb"; |
| 35 |
const modeWrite = "wb"; |
| 36 |
const modeReadWrite = "rb+"; |
| 37 |
static function backtrace() |
| 38 |
{ |
| 39 |
\var_dump(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS)); |
| 40 |
} |
| 41 |
/** |
| 42 |
* Open a font file in read mode |
| 43 |
* |
| 44 |
* @param string $filename The file name of the font to open |
| 45 |
* |
| 46 |
* @return bool |
| 47 |
*/ |
| 48 |
public function load($filename) |
| 49 |
{ |
| 50 |
return $this->open($filename, self::modeRead); |
| 51 |
} |
| 52 |
/** |
| 53 |
* Open a font file in a chosen mode |
| 54 |
* |
| 55 |
* @param string $filename The file name of the font to open |
| 56 |
* @param string $mode The opening mode |
| 57 |
* |
| 58 |
* @throws \Exception |
| 59 |
* @return bool |
| 60 |
*/ |
| 61 |
public function open($filename, $mode = self::modeRead) |
| 62 |
{ |
| 63 |
if (!\in_array($mode, array(self::modeRead, self::modeWrite, self::modeReadWrite))) { |
| 64 |
throw new \Exception("Unknown file open mode"); |
| 65 |
} |
| 66 |
$this->f = \fopen($filename, $mode); |
| 67 |
return $this->f != \false; |
| 68 |
} |
| 69 |
/** |
| 70 |
* Close the internal file pointer |
| 71 |
*/ |
| 72 |
public function close() |
| 73 |
{ |
| 74 |
return \fclose($this->f) != \false; |
| 75 |
} |
| 76 |
/** |
| 77 |
* Change the internal file pointer |
| 78 |
* |
| 79 |
* @param resource $fp |
| 80 |
* |
| 81 |
* @throws \Exception |
| 82 |
*/ |
| 83 |
public function setFile($fp) |
| 84 |
{ |
| 85 |
if (!\is_resource($fp)) { |
| 86 |
throw new \Exception('$fp is not a valid resource'); |
| 87 |
} |
| 88 |
$this->f = $fp; |
| 89 |
} |
| 90 |
/** |
| 91 |
* Create a temporary file in write mode |
| 92 |
* |
| 93 |
* @param bool $allow_memory Allow in-memory files |
| 94 |
* |
| 95 |
* @return resource the temporary file pointer resource |
| 96 |
*/ |
| 97 |
public static function getTempFile($allow_memory = \true) |
| 98 |
{ |
| 99 |
$f = null; |
| 100 |
if ($allow_memory) { |
| 101 |
$f = \fopen("php://temp", "rb+"); |
| 102 |
} else { |
| 103 |
$f = \fopen(\tempnam(\sys_get_temp_dir(), "fnt"), "rb+"); |
| 104 |
} |
| 105 |
return $f; |
| 106 |
} |
| 107 |
/** |
| 108 |
* Move the internal file pinter to $offset bytes |
| 109 |
* |
| 110 |
* @param int $offset |
| 111 |
* |
| 112 |
* @return bool True if the $offset position exists in the file |
| 113 |
*/ |
| 114 |
public function seek($offset) |
| 115 |
{ |
| 116 |
return \fseek($this->f, (int) $offset, \SEEK_SET) == 0; |
| 117 |
} |
| 118 |
/** |
| 119 |
* Gives the current position in the file |
| 120 |
* |
| 121 |
* @return int The current position |
| 122 |
*/ |
| 123 |
public function pos() |
| 124 |
{ |
| 125 |
return \ftell($this->f); |
| 126 |
} |
| 127 |
public function skip($n) |
| 128 |
{ |
| 129 |
\fseek($this->f, $n, \SEEK_CUR); |
| 130 |
} |
| 131 |
/** |
| 132 |
* @param int $n The number of bytes to read |
| 133 |
* |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
public function read($n) |
| 137 |
{ |
| 138 |
if ($n < 1) { |
| 139 |
return ""; |
| 140 |
} |
| 141 |
return (string) \fread($this->f, $n); |
| 142 |
} |
| 143 |
public function write($data, $length = null) |
| 144 |
{ |
| 145 |
if ($data === null || $data === "" || $data === \false) { |
| 146 |
return 0; |
| 147 |
} |
| 148 |
return \fwrite($this->f, $data, $length); |
| 149 |
} |
| 150 |
public function readUInt8() |
| 151 |
{ |
| 152 |
$byte = $this->read(1); |
| 153 |
if ($byte === '') { |
| 154 |
return 0; |
| 155 |
} |
| 156 |
return \ord($byte); |
| 157 |
} |
| 158 |
public function readUInt8Many($count) |
| 159 |
{ |
| 160 |
return \array_values(\unpack("C*", $this->read($count))); |
| 161 |
} |
| 162 |
public function writeUInt8($data) |
| 163 |
{ |
| 164 |
return $this->write(\chr($data), 1); |
| 165 |
} |
| 166 |
public function readInt8() |
| 167 |
{ |
| 168 |
$v = $this->readUInt8(); |
| 169 |
if ($v >= 0x80) { |
| 170 |
$v -= 0x100; |
| 171 |
} |
| 172 |
return $v; |
| 173 |
} |
| 174 |
public function readInt8Many($count) |
| 175 |
{ |
| 176 |
return \array_values(\unpack("c*", $this->read($count))); |
| 177 |
} |
| 178 |
public function writeInt8($data) |
| 179 |
{ |
| 180 |
if ($data < 0) { |
| 181 |
$data += 0x100; |
| 182 |
} |
| 183 |
return $this->writeUInt8($data); |
| 184 |
} |
| 185 |
public function readUInt16() |
| 186 |
{ |
| 187 |
$a = \unpack("nn", $this->read(2)); |
| 188 |
return $a["n"]; |
| 189 |
} |
| 190 |
public function readUInt16Many($count) |
| 191 |
{ |
| 192 |
return \array_values(\unpack("n*", $this->read($count * 2))); |
| 193 |
} |
| 194 |
public function readUFWord() |
| 195 |
{ |
| 196 |
return $this->readUInt16(); |
| 197 |
} |
| 198 |
public function writeUInt16($data) |
| 199 |
{ |
| 200 |
return $this->write(\pack("n", $data), 2); |
| 201 |
} |
| 202 |
public function writeUFWord($data) |
| 203 |
{ |
| 204 |
return $this->writeUInt16($data); |
| 205 |
} |
| 206 |
public function readInt16() |
| 207 |
{ |
| 208 |
$a = \unpack("nn", $this->read(2)); |
| 209 |
$v = $a["n"]; |
| 210 |
if ($v >= 0x8000) { |
| 211 |
$v -= 0x10000; |
| 212 |
} |
| 213 |
return $v; |
| 214 |
} |
| 215 |
public function readInt16Many($count) |
| 216 |
{ |
| 217 |
$vals = \array_values(\unpack("n*", $this->read($count * 2))); |
| 218 |
foreach ($vals as &$v) { |
| 219 |
if ($v >= 0x8000) { |
| 220 |
$v -= 0x10000; |
| 221 |
} |
| 222 |
} |
| 223 |
return $vals; |
| 224 |
} |
| 225 |
public function readFWord() |
| 226 |
{ |
| 227 |
return $this->readInt16(); |
| 228 |
} |
| 229 |
public function writeInt16($data) |
| 230 |
{ |
| 231 |
if ($data < 0) { |
| 232 |
$data += 0x10000; |
| 233 |
} |
| 234 |
return $this->writeUInt16($data); |
| 235 |
} |
| 236 |
public function writeFWord($data) |
| 237 |
{ |
| 238 |
return $this->writeInt16($data); |
| 239 |
} |
| 240 |
public function readUInt32() |
| 241 |
{ |
| 242 |
$a = \unpack("NN", $this->read(4)); |
| 243 |
return $a["N"]; |
| 244 |
} |
| 245 |
public function writeUInt32($data) |
| 246 |
{ |
| 247 |
return $this->write(\pack("N", $data), 4); |
| 248 |
} |
| 249 |
public function readFixed() |
| 250 |
{ |
| 251 |
$d = $this->readInt16(); |
| 252 |
$d2 = $this->readUInt16(); |
| 253 |
return \round($d + $d2 / 0x10000, 4); |
| 254 |
} |
| 255 |
public function writeFixed($data) |
| 256 |
{ |
| 257 |
$left = \floor($data); |
| 258 |
$right = ($data - $left) * 0x10000; |
| 259 |
return $this->writeInt16($left) + $this->writeUInt16($right); |
| 260 |
} |
| 261 |
public function readLongDateTime() |
| 262 |
{ |
| 263 |
$this->readUInt32(); |
| 264 |
// ignored |
| 265 |
$date = $this->readUInt32() - 2082844800; |
| 266 |
# PHP_INT_MIN isn't defined in PHP < 7.0 |
| 267 |
$php_int_min = \defined("PHP_INT_MIN") ? \PHP_INT_MIN : ~\PHP_INT_MAX; |
| 268 |
if (\is_string($date) || $date > \PHP_INT_MAX || $date < $php_int_min) { |
| 269 |
$date = 0; |
| 270 |
} |
| 271 |
return \date("Y-m-d H:i:s", $date); |
| 272 |
} |
| 273 |
public function writeLongDateTime($data) |
| 274 |
{ |
| 275 |
$date = \strtotime($data); |
| 276 |
$date += 2082844800; |
| 277 |
return $this->writeUInt32(0) + $this->writeUInt32($date); |
| 278 |
} |
| 279 |
public function unpack($def) |
| 280 |
{ |
| 281 |
$d = array(); |
| 282 |
foreach ($def as $name => $type) { |
| 283 |
$d[$name] = $this->r($type); |
| 284 |
} |
| 285 |
return $d; |
| 286 |
} |
| 287 |
public function pack($def, $data) |
| 288 |
{ |
| 289 |
$bytes = 0; |
| 290 |
foreach ($def as $name => $type) { |
| 291 |
$bytes += $this->w($type, $data[$name]); |
| 292 |
} |
| 293 |
return $bytes; |
| 294 |
} |
| 295 |
/** |
| 296 |
* Read a data of type $type in the file from the current position |
| 297 |
* |
| 298 |
* @param mixed $type The data type to read |
| 299 |
* |
| 300 |
* @return mixed The data that was read |
| 301 |
*/ |
| 302 |
public function r($type) |
| 303 |
{ |
| 304 |
switch ($type) { |
| 305 |
case self::uint8: |
| 306 |
return $this->readUInt8(); |
| 307 |
case self::int8: |
| 308 |
return $this->readInt8(); |
| 309 |
case self::uint16: |
| 310 |
return $this->readUInt16(); |
| 311 |
case self::int16: |
| 312 |
return $this->readInt16(); |
| 313 |
case self::uint32: |
| 314 |
return $this->readUInt32(); |
| 315 |
case self::int32: |
| 316 |
return $this->readUInt32(); |
| 317 |
case self::shortFrac: |
| 318 |
return $this->readFixed(); |
| 319 |
case self::Fixed: |
| 320 |
return $this->readFixed(); |
| 321 |
case self::FWord: |
| 322 |
return $this->readInt16(); |
| 323 |
case self::uFWord: |
| 324 |
return $this->readUInt16(); |
| 325 |
case self::F2Dot14: |
| 326 |
return $this->readInt16(); |
| 327 |
case self::longDateTime: |
| 328 |
return $this->readLongDateTime(); |
| 329 |
case self::char: |
| 330 |
return $this->read(1); |
| 331 |
default: |
| 332 |
if (\is_array($type)) { |
| 333 |
if ($type[0] == self::char) { |
| 334 |
return $this->read($type[1]); |
| 335 |
} |
| 336 |
if ($type[0] == self::uint16) { |
| 337 |
return $this->readUInt16Many($type[1]); |
| 338 |
} |
| 339 |
if ($type[0] == self::int16) { |
| 340 |
return $this->readInt16Many($type[1]); |
| 341 |
} |
| 342 |
if ($type[0] == self::uint8) { |
| 343 |
return $this->readUInt8Many($type[1]); |
| 344 |
} |
| 345 |
if ($type[0] == self::int8) { |
| 346 |
return $this->readInt8Many($type[1]); |
| 347 |
} |
| 348 |
$ret = array(); |
| 349 |
for ($i = 0; $i < $type[1]; $i++) { |
| 350 |
$ret[] = $this->r($type[0]); |
| 351 |
} |
| 352 |
return $ret; |
| 353 |
} |
| 354 |
return null; |
| 355 |
} |
| 356 |
} |
| 357 |
/** |
| 358 |
* Write $data of type $type in the file from the current position |
| 359 |
* |
| 360 |
* @param mixed $type The data type to write |
| 361 |
* @param mixed $data The data to write |
| 362 |
* |
| 363 |
* @return int The number of bytes read |
| 364 |
*/ |
| 365 |
public function w($type, $data) |
| 366 |
{ |
| 367 |
switch ($type) { |
| 368 |
case self::uint8: |
| 369 |
return $this->writeUInt8($data); |
| 370 |
case self::int8: |
| 371 |
return $this->writeInt8($data); |
| 372 |
case self::uint16: |
| 373 |
return $this->writeUInt16($data); |
| 374 |
case self::int16: |
| 375 |
return $this->writeInt16($data); |
| 376 |
case self::uint32: |
| 377 |
return $this->writeUInt32($data); |
| 378 |
case self::int32: |
| 379 |
return $this->writeUInt32($data); |
| 380 |
case self::shortFrac: |
| 381 |
return $this->writeFixed($data); |
| 382 |
case self::Fixed: |
| 383 |
return $this->writeFixed($data); |
| 384 |
case self::FWord: |
| 385 |
return $this->writeInt16($data); |
| 386 |
case self::uFWord: |
| 387 |
return $this->writeUInt16($data); |
| 388 |
case self::F2Dot14: |
| 389 |
return $this->writeInt16($data); |
| 390 |
case self::longDateTime: |
| 391 |
return $this->writeLongDateTime($data); |
| 392 |
case self::char: |
| 393 |
return $this->write($data, 1); |
| 394 |
default: |
| 395 |
if (\is_array($type)) { |
| 396 |
if ($type[0] == self::char) { |
| 397 |
return $this->write($data, $type[1]); |
| 398 |
} |
| 399 |
$ret = 0; |
| 400 |
for ($i = 0; $i < $type[1]; $i++) { |
| 401 |
if (isset($data[$i])) { |
| 402 |
$ret += $this->w($type[0], $data[$i]); |
| 403 |
} |
| 404 |
} |
| 405 |
return $ret; |
| 406 |
} |
| 407 |
return null; |
| 408 |
} |
| 409 |
} |
| 410 |
/** |
| 411 |
* Converts a Uint32 value to string |
| 412 |
* |
| 413 |
* @param int $uint32 |
| 414 |
* |
| 415 |
* @return string The string |
| 416 |
*/ |
| 417 |
public function convertUInt32ToStr($uint32) |
| 418 |
{ |
| 419 |
return \chr($uint32 >> 24 & 0xff) . \chr($uint32 >> 16 & 0xff) . \chr($uint32 >> 8 & 0xff) . \chr($uint32 & 0xff); |
| 420 |
} |
| 421 |
} |
| 422 |
|