PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/application/third_party/dompdf/lib/php-font-lib/classes/font_binary_stream.cls.php

https://gitlab.com/imron02/sppone
PHP | 681 lines | 443 code | 96 blank | 142 comment | 44 complexity | a525f3eb5ab0f346c21280127564d14a MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. <<<<<<< HEAD
  2. <?php
  3. /**
  4. * @package php-font-lib
  5. * @link http://php-font-lib.googlecode.com/
  6. * @author Fabien Ménager <fabien.menager@gmail.com>
  7. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  8. * @version $Id: font_binary_stream.cls.php 42 2012-02-05 10:49:20Z fabien.menager $
  9. */
  10. /**
  11. * Generic font file binary stream.
  12. *
  13. * @package php-font-lib
  14. */
  15. class Font_Binary_Stream {
  16. /**
  17. * @var resource The file pointer
  18. */
  19. protected $f;
  20. const uint8 = 1;
  21. const int8 = 2;
  22. const uint16 = 3;
  23. const int16 = 4;
  24. const uint32 = 5;
  25. const int32 = 6;
  26. const shortFrac = 7;
  27. const Fixed = 8;
  28. const FWord = 9;
  29. const uFWord = 10;
  30. const F2Dot14 = 11;
  31. const longDateTime = 12;
  32. const char = 13;
  33. private static $sizes = array(
  34. self::uint8 => 1,
  35. self::int8 => 1,
  36. self::uint16 => 2,
  37. self::int16 => 2,
  38. self::uint32 => 4,
  39. self::int32 => 4,
  40. self::shortFrac => 4,
  41. self::Fixed => 4,
  42. self::FWord => 2,
  43. self::uFWord => 2,
  44. self::F2Dot14 => 2,
  45. self::longDateTime => 8,
  46. self::char => 1,
  47. );
  48. const modeRead = "rb";
  49. const modeWrite = "wb";
  50. const modeReadWrite = "rb+";
  51. /**
  52. * Open a font file in read mode
  53. *
  54. * @param string $filename The file name of the font to open
  55. */
  56. public function load($filename) {
  57. return $this->open($filename, self::modeRead);
  58. }
  59. /**
  60. * Open a font file in a chosen mode
  61. *
  62. * @param string $filename The file name of the font to open
  63. * @param string $mode The opening mode
  64. */
  65. public function open($filename, $mode = self::modeRead) {
  66. if (!in_array($mode, array(self::modeRead, self::modeWrite, self::modeReadWrite))) {
  67. throw new Exception("Unkown file open mode");
  68. }
  69. $this->f = fopen($filename, $mode);
  70. return $this->f != false;
  71. }
  72. /**
  73. * Close the internal file pointer
  74. */
  75. public function close() {
  76. return fclose($this->f) != false;
  77. }
  78. /**
  79. * Change the internal file pointer
  80. *
  81. * @param resource $fp
  82. */
  83. public function setFile($fp) {
  84. if (!is_resource($fp)) {
  85. throw new Exception('$fp is not a valid resource');
  86. }
  87. $this->f = $fp;
  88. }
  89. /**
  90. * Create a temporary file in write mode
  91. *
  92. * @return resource the temporary file pointer resource
  93. */
  94. public static function getTempFile($allow_memory = true) {
  95. $f = null;
  96. if ($allow_memory) {
  97. // PHP 5.1+
  98. @fopen("php://temp", "rb+");
  99. }
  100. if (!$f) {
  101. $f = fopen(tempnam(sys_get_temp_dir(), "fnt"), "rb+");
  102. }
  103. return $f;
  104. }
  105. /**
  106. * Move the internal file pinter to $offset bytes
  107. *
  108. * @param int $offset
  109. * @return bool True if the $offset position exists in the file
  110. */
  111. public function seek($offset) {
  112. return fseek($this->f, $offset, SEEK_SET) == 0;
  113. }
  114. /**
  115. * Gives the current position in the file
  116. *
  117. * @return int The current position
  118. */
  119. public function pos() {
  120. return ftell($this->f);
  121. }
  122. public function skip($n) {
  123. fseek($this->f, $n, SEEK_CUR);
  124. }
  125. public function read($n) {
  126. if ($n < 1) return "";
  127. return fread($this->f, $n);
  128. }
  129. public function write($data, $length = null) {
  130. if ($data === null || $data === "") return;
  131. return fwrite($this->f, $data, $length);
  132. }
  133. public function readUInt8() {
  134. return ord($this->read(1));
  135. }
  136. public function writeUInt8($data) {
  137. return $this->write(chr($data), 1);
  138. }
  139. public function readInt8() {
  140. $v = $this->readUInt8();
  141. if ($v >= 0x80) {
  142. $v -= 0x100;
  143. }
  144. return $v;
  145. }
  146. public function writeInt8($data) {
  147. if ($data < 0) {
  148. $data += 0x100;
  149. }
  150. return $this->writeUInt8($data);
  151. }
  152. public function readUInt16() {
  153. $a = unpack("nn", $this->read(2));
  154. //if ($a == null) var_dump(debug_backtrace(false));
  155. return $a["n"];
  156. }
  157. public function writeUInt16($data) {
  158. return $this->write(pack("n", $data), 2);
  159. }
  160. public function readInt16() {
  161. $v = $this->readUInt16();
  162. if ($v >= 0x8000) {
  163. $v -= 0x10000;
  164. }
  165. return $v;
  166. }
  167. public function writeInt16($data) {
  168. if ($data < 0) {
  169. $data += 0x10000;
  170. }
  171. return $this->writeUInt16($data);
  172. }
  173. public function readUInt32() {
  174. $a = unpack("NN", $this->read(4));
  175. return $a["N"];
  176. }
  177. public function writeUInt32($data) {
  178. return $this->write(pack("N", $data), 4);
  179. }
  180. public function readFixed() {
  181. $d = $this->readInt16();
  182. $d2 = $this->readUInt16();
  183. return round($d + $d2 / 0x10000, 4);
  184. }
  185. public function writeFixed($data) {
  186. $left = floor($data);
  187. $right = ($data - $left) * 0x10000;
  188. return $this->writeInt16($left) + $this->writeUInt16($right);
  189. }
  190. public function readLongDateTime() {
  191. $this->readUInt32(); // ignored
  192. $date = $this->readUInt32() - 2082844800;
  193. return strftime("%Y-%m-%d %H:%M:%S", $date);
  194. }
  195. public function writeLongDateTime($data) {
  196. $date = strtotime($data);
  197. $date += 2082844800;
  198. return $this->writeUInt32(0) + $this->writeUInt32($date);
  199. }
  200. public function unpack($def) {
  201. $d = array();
  202. foreach($def as $name => $type) {
  203. $d[$name] = $this->r($type);
  204. }
  205. return $d;
  206. }
  207. public function pack($def, $data) {
  208. $bytes = 0;
  209. foreach($def as $name => $type) {
  210. $bytes += $this->w($type, $data[$name]);
  211. }
  212. return $bytes;
  213. }
  214. /**
  215. * Read a data of type $type in the file from the current position
  216. *
  217. * @param mixed $type The data type to read
  218. * @return mixed The data that was read
  219. */
  220. public function r($type) {
  221. switch($type) {
  222. case self::uint8: return $this->readUInt8();
  223. case self::int8: return $this->readInt8();
  224. case self::uint16: return $this->readUInt16();
  225. case self::int16: return $this->readInt16();
  226. case self::uint32: return $this->readUInt32();
  227. case self::int32: return $this->readUInt32();
  228. case self::shortFrac: return $this->readFixed();
  229. case self::Fixed: return $this->readFixed();
  230. case self::FWord: return $this->readInt16();
  231. case self::uFWord: return $this->readUInt16();
  232. case self::F2Dot14: return $this->readInt16();
  233. case self::longDateTime: return $this->readLongDateTime();
  234. case self::char: return $this->read(1);
  235. default:
  236. if ( is_array($type) ) {
  237. if ($type[0] == self::char) {
  238. return $this->read($type[1]);
  239. }
  240. $ret = array();
  241. for($i = 0; $i < $type[1]; $i++) {
  242. $ret[] = $this->r($type[0]);
  243. }
  244. return $ret;
  245. }
  246. }
  247. }
  248. /**
  249. * Write $data of type $type in the file from the current position
  250. *
  251. * @param mixed $type The data type to write
  252. * @param mixed $data The data to write
  253. * @return int The number of bytes read
  254. */
  255. public function w($type, $data) {
  256. switch($type) {
  257. case self::uint8: return $this->writeUInt8($data);
  258. case self::int8: return $this->writeInt8($data);
  259. case self::uint16: return $this->writeUInt16($data);
  260. case self::int16: return $this->writeInt16($data);
  261. case self::uint32: return $this->writeUInt32($data);
  262. case self::int32: return $this->writeUInt32($data);
  263. case self::shortFrac: return $this->writeFixed($data);
  264. case self::Fixed: return $this->writeFixed($data);
  265. case self::FWord: return $this->writeInt16($data);
  266. case self::uFWord: return $this->writeUInt16($data);
  267. case self::F2Dot14: return $this->writeInt16($data);
  268. case self::longDateTime: return $this->writeLongDateTime($data);
  269. case self::char: return $this->write($data, 1);
  270. default:
  271. if ( is_array($type) ) {
  272. if ($type[0] == self::char) {
  273. return $this->write($data, $type[1]);
  274. }
  275. $ret = 0;
  276. for($i = 0; $i < $type[1]; $i++) {
  277. $ret += $this->w($type[0], $data[$i]);
  278. }
  279. return $ret;
  280. }
  281. }
  282. }
  283. /**
  284. * Converts a Uint32 value to string
  285. *
  286. * @param int $uint32
  287. * @param string The string
  288. */
  289. public function convertUInt32ToStr($uint32) {
  290. return chr(($uint32 >> 24) & 0xFF).chr(($uint32 >> 16) & 0xFF).chr(($uint32 >> 8) & 0xFF).chr($uint32 & 0xFF);
  291. }
  292. }
  293. =======
  294. <?php
  295. /**
  296. * @package php-font-lib
  297. * @link http://php-font-lib.googlecode.com/
  298. * @author Fabien Ménager <fabien.menager@gmail.com>
  299. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  300. * @version $Id: font_binary_stream.cls.php 42 2012-02-05 10:49:20Z fabien.menager $
  301. */
  302. /**
  303. * Generic font file binary stream.
  304. *
  305. * @package php-font-lib
  306. */
  307. class Font_Binary_Stream {
  308. /**
  309. * @var resource The file pointer
  310. */
  311. protected $f;
  312. const uint8 = 1;
  313. const int8 = 2;
  314. const uint16 = 3;
  315. const int16 = 4;
  316. const uint32 = 5;
  317. const int32 = 6;
  318. const shortFrac = 7;
  319. const Fixed = 8;
  320. const FWord = 9;
  321. const uFWord = 10;
  322. const F2Dot14 = 11;
  323. const longDateTime = 12;
  324. const char = 13;
  325. private static $sizes = array(
  326. self::uint8 => 1,
  327. self::int8 => 1,
  328. self::uint16 => 2,
  329. self::int16 => 2,
  330. self::uint32 => 4,
  331. self::int32 => 4,
  332. self::shortFrac => 4,
  333. self::Fixed => 4,
  334. self::FWord => 2,
  335. self::uFWord => 2,
  336. self::F2Dot14 => 2,
  337. self::longDateTime => 8,
  338. self::char => 1,
  339. );
  340. const modeRead = "rb";
  341. const modeWrite = "wb";
  342. const modeReadWrite = "rb+";
  343. /**
  344. * Open a font file in read mode
  345. *
  346. * @param string $filename The file name of the font to open
  347. */
  348. public function load($filename) {
  349. return $this->open($filename, self::modeRead);
  350. }
  351. /**
  352. * Open a font file in a chosen mode
  353. *
  354. * @param string $filename The file name of the font to open
  355. * @param string $mode The opening mode
  356. */
  357. public function open($filename, $mode = self::modeRead) {
  358. if (!in_array($mode, array(self::modeRead, self::modeWrite, self::modeReadWrite))) {
  359. throw new Exception("Unkown file open mode");
  360. }
  361. $this->f = fopen($filename, $mode);
  362. return $this->f != false;
  363. }
  364. /**
  365. * Close the internal file pointer
  366. */
  367. public function close() {
  368. return fclose($this->f) != false;
  369. }
  370. /**
  371. * Change the internal file pointer
  372. *
  373. * @param resource $fp
  374. */
  375. public function setFile($fp) {
  376. if (!is_resource($fp)) {
  377. throw new Exception('$fp is not a valid resource');
  378. }
  379. $this->f = $fp;
  380. }
  381. /**
  382. * Create a temporary file in write mode
  383. *
  384. * @return resource the temporary file pointer resource
  385. */
  386. public static function getTempFile($allow_memory = true) {
  387. $f = null;
  388. if ($allow_memory) {
  389. // PHP 5.1+
  390. @fopen("php://temp", "rb+");
  391. }
  392. if (!$f) {
  393. $f = fopen(tempnam(sys_get_temp_dir(), "fnt"), "rb+");
  394. }
  395. return $f;
  396. }
  397. /**
  398. * Move the internal file pinter to $offset bytes
  399. *
  400. * @param int $offset
  401. * @return bool True if the $offset position exists in the file
  402. */
  403. public function seek($offset) {
  404. return fseek($this->f, $offset, SEEK_SET) == 0;
  405. }
  406. /**
  407. * Gives the current position in the file
  408. *
  409. * @return int The current position
  410. */
  411. public function pos() {
  412. return ftell($this->f);
  413. }
  414. public function skip($n) {
  415. fseek($this->f, $n, SEEK_CUR);
  416. }
  417. public function read($n) {
  418. if ($n < 1) return "";
  419. return fread($this->f, $n);
  420. }
  421. public function write($data, $length = null) {
  422. if ($data === null || $data === "") return;
  423. return fwrite($this->f, $data, $length);
  424. }
  425. public function readUInt8() {
  426. return ord($this->read(1));
  427. }
  428. public function writeUInt8($data) {
  429. return $this->write(chr($data), 1);
  430. }
  431. public function readInt8() {
  432. $v = $this->readUInt8();
  433. if ($v >= 0x80) {
  434. $v -= 0x100;
  435. }
  436. return $v;
  437. }
  438. public function writeInt8($data) {
  439. if ($data < 0) {
  440. $data += 0x100;
  441. }
  442. return $this->writeUInt8($data);
  443. }
  444. public function readUInt16() {
  445. $a = unpack("nn", $this->read(2));
  446. //if ($a == null) var_dump(debug_backtrace(false));
  447. return $a["n"];
  448. }
  449. public function writeUInt16($data) {
  450. return $this->write(pack("n", $data), 2);
  451. }
  452. public function readInt16() {
  453. $v = $this->readUInt16();
  454. if ($v >= 0x8000) {
  455. $v -= 0x10000;
  456. }
  457. return $v;
  458. }
  459. public function writeInt16($data) {
  460. if ($data < 0) {
  461. $data += 0x10000;
  462. }
  463. return $this->writeUInt16($data);
  464. }
  465. public function readUInt32() {
  466. $a = unpack("NN", $this->read(4));
  467. return $a["N"];
  468. }
  469. public function writeUInt32($data) {
  470. return $this->write(pack("N", $data), 4);
  471. }
  472. public function readFixed() {
  473. $d = $this->readInt16();
  474. $d2 = $this->readUInt16();
  475. return round($d + $d2 / 0x10000, 4);
  476. }
  477. public function writeFixed($data) {
  478. $left = floor($data);
  479. $right = ($data - $left) * 0x10000;
  480. return $this->writeInt16($left) + $this->writeUInt16($right);
  481. }
  482. public function readLongDateTime() {
  483. $this->readUInt32(); // ignored
  484. $date = $this->readUInt32() - 2082844800;
  485. return strftime("%Y-%m-%d %H:%M:%S", $date);
  486. }
  487. public function writeLongDateTime($data) {
  488. $date = strtotime($data);
  489. $date += 2082844800;
  490. return $this->writeUInt32(0) + $this->writeUInt32($date);
  491. }
  492. public function unpack($def) {
  493. $d = array();
  494. foreach($def as $name => $type) {
  495. $d[$name] = $this->r($type);
  496. }
  497. return $d;
  498. }
  499. public function pack($def, $data) {
  500. $bytes = 0;
  501. foreach($def as $name => $type) {
  502. $bytes += $this->w($type, $data[$name]);
  503. }
  504. return $bytes;
  505. }
  506. /**
  507. * Read a data of type $type in the file from the current position
  508. *
  509. * @param mixed $type The data type to read
  510. * @return mixed The data that was read
  511. */
  512. public function r($type) {
  513. switch($type) {
  514. case self::uint8: return $this->readUInt8();
  515. case self::int8: return $this->readInt8();
  516. case self::uint16: return $this->readUInt16();
  517. case self::int16: return $this->readInt16();
  518. case self::uint32: return $this->readUInt32();
  519. case self::int32: return $this->readUInt32();
  520. case self::shortFrac: return $this->readFixed();
  521. case self::Fixed: return $this->readFixed();
  522. case self::FWord: return $this->readInt16();
  523. case self::uFWord: return $this->readUInt16();
  524. case self::F2Dot14: return $this->readInt16();
  525. case self::longDateTime: return $this->readLongDateTime();
  526. case self::char: return $this->read(1);
  527. default:
  528. if ( is_array($type) ) {
  529. if ($type[0] == self::char) {
  530. return $this->read($type[1]);
  531. }
  532. $ret = array();
  533. for($i = 0; $i < $type[1]; $i++) {
  534. $ret[] = $this->r($type[0]);
  535. }
  536. return $ret;
  537. }
  538. }
  539. }
  540. /**
  541. * Write $data of type $type in the file from the current position
  542. *
  543. * @param mixed $type The data type to write
  544. * @param mixed $data The data to write
  545. * @return int The number of bytes read
  546. */
  547. public function w($type, $data) {
  548. switch($type) {
  549. case self::uint8: return $this->writeUInt8($data);
  550. case self::int8: return $this->writeInt8($data);
  551. case self::uint16: return $this->writeUInt16($data);
  552. case self::int16: return $this->writeInt16($data);
  553. case self::uint32: return $this->writeUInt32($data);
  554. case self::int32: return $this->writeUInt32($data);
  555. case self::shortFrac: return $this->writeFixed($data);
  556. case self::Fixed: return $this->writeFixed($data);
  557. case self::FWord: return $this->writeInt16($data);
  558. case self::uFWord: return $this->writeUInt16($data);
  559. case self::F2Dot14: return $this->writeInt16($data);
  560. case self::longDateTime: return $this->writeLongDateTime($data);
  561. case self::char: return $this->write($data, 1);
  562. default:
  563. if ( is_array($type) ) {
  564. if ($type[0] == self::char) {
  565. return $this->write($data, $type[1]);
  566. }
  567. $ret = 0;
  568. for($i = 0; $i < $type[1]; $i++) {
  569. $ret += $this->w($type[0], $data[$i]);
  570. }
  571. return $ret;
  572. }
  573. }
  574. }
  575. /**
  576. * Converts a Uint32 value to string
  577. *
  578. * @param int $uint32
  579. * @param string The string
  580. */
  581. public function convertUInt32ToStr($uint32) {
  582. return chr(($uint32 >> 24) & 0xFF).chr(($uint32 >> 16) & 0xFF).chr(($uint32 >> 8) & 0xFF).chr($uint32 & 0xFF);
  583. }
  584. }
  585. >>>>>>> cd2971f4105274ebfe9a8811448dc073ef510718