PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/hellosam/pdf/dompdf/lib/php-font-lib/src/FontLib/BinaryStream.php

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