PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/extensions/pdf/dompdf/lib/php-font-lib/classes/font_binary_stream.cls.php

https://bitbucket.org/sanbrar/zurmo_invoice
PHP | 339 lines | 220 code | 48 blank | 71 comment | 22 complexity | fcfe0aeea791ac4c3d56fec566ba2f2d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, BSD-2-Clause, GPL-3.0
  1. <?php
  2. /**
  3. * @package php-font-lib
  4. * @link http://php-font-lib.googlecode.com/
  5. * @author Fabien Ménager <fabien.menager@gmail.com>
  6. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  7. * @version $Id: font_binary_stream.cls.php 42 2012-02-05 10:49:20Z fabien.menager $
  8. */
  9. /**
  10. * Generic font file binary stream.
  11. *
  12. * @package php-font-lib
  13. */
  14. class Font_Binary_Stream {
  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. private static $sizes = array(
  33. self::uint8 => 1,
  34. self::int8 => 1,
  35. self::uint16 => 2,
  36. self::int16 => 2,
  37. self::uint32 => 4,
  38. self::int32 => 4,
  39. self::shortFrac => 4,
  40. self::Fixed => 4,
  41. self::FWord => 2,
  42. self::uFWord => 2,
  43. self::F2Dot14 => 2,
  44. self::longDateTime => 8,
  45. self::char => 1,
  46. );
  47. const modeRead = "rb";
  48. const modeWrite = "wb";
  49. const modeReadWrite = "rb+";
  50. /**
  51. * Open a font file in read mode
  52. *
  53. * @param string $filename The file name of the font to open
  54. */
  55. public function load($filename) {
  56. return $this->open($filename, self::modeRead);
  57. }
  58. /**
  59. * Open a font file in a chosen mode
  60. *
  61. * @param string $filename The file name of the font to open
  62. * @param string $mode The opening mode
  63. */
  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");
  67. }
  68. $this->f = fopen($filename, $mode);
  69. return $this->f != false;
  70. }
  71. /**
  72. * Close the internal file pointer
  73. */
  74. public function close() {
  75. return fclose($this->f) != false;
  76. }
  77. /**
  78. * Change the internal file pointer
  79. *
  80. * @param resource $fp
  81. */
  82. public function setFile($fp) {
  83. if (!is_resource($fp)) {
  84. throw new Exception('$fp is not a valid resource');
  85. }
  86. $this->f = $fp;
  87. }
  88. /**
  89. * Create a temporary file in write mode
  90. *
  91. * @return resource the temporary file pointer resource
  92. */
  93. public static function getTempFile($allow_memory = true) {
  94. $f = null;
  95. if ($allow_memory) {
  96. // PHP 5.1+
  97. @fopen("php://temp", "rb+");
  98. }
  99. if (!$f) {
  100. $f = fopen(tempnam(sys_get_temp_dir(), "fnt"), "rb+");
  101. }
  102. return $f;
  103. }
  104. /**
  105. * Move the internal file pinter to $offset bytes
  106. *
  107. * @param int $offset
  108. * @return bool True if the $offset position exists in the file
  109. */
  110. public function seek($offset) {
  111. return fseek($this->f, $offset, SEEK_SET) == 0;
  112. }
  113. /**
  114. * Gives the current position in the file
  115. *
  116. * @return int The current position
  117. */
  118. public function pos() {
  119. return ftell($this->f);
  120. }
  121. public function skip($n) {
  122. fseek($this->f, $n, SEEK_CUR);
  123. }
  124. public function read($n) {
  125. if ($n < 1) return "";
  126. return fread($this->f, $n);
  127. }
  128. public function write($data, $length = null) {
  129. if ($data === null || $data === "") return;
  130. return fwrite($this->f, $data, $length);
  131. }
  132. public function readUInt8() {
  133. return ord($this->read(1));
  134. }
  135. public function writeUInt8($data) {
  136. return $this->write(chr($data), 1);
  137. }
  138. public function readInt8() {
  139. $v = $this->readUInt8();
  140. if ($v >= 0x80) {
  141. $v -= 0x100;
  142. }
  143. return $v;
  144. }
  145. public function writeInt8($data) {
  146. if ($data < 0) {
  147. $data += 0x100;
  148. }
  149. return $this->writeUInt8($data);
  150. }
  151. public function readUInt16() {
  152. $a = unpack("nn", $this->read(2));
  153. //if ($a == null) var_dump(debug_backtrace(false));
  154. return $a["n"];
  155. }
  156. public function writeUInt16($data) {
  157. return $this->write(pack("n", $data), 2);
  158. }
  159. public function readInt16() {
  160. $v = $this->readUInt16();
  161. if ($v >= 0x8000) {
  162. $v -= 0x10000;
  163. }
  164. return $v;
  165. }
  166. public function writeInt16($data) {
  167. if ($data < 0) {
  168. $data += 0x10000;
  169. }
  170. return $this->writeUInt16($data);
  171. }
  172. public function readUInt32() {
  173. $a = unpack("NN", $this->read(4));
  174. return $a["N"];
  175. }
  176. public function writeUInt32($data) {
  177. return $this->write(pack("N", $data), 4);
  178. }
  179. public function readFixed() {
  180. $d = $this->readInt16();
  181. $d2 = $this->readUInt16();
  182. return round($d + $d2 / 0x10000, 4);
  183. }
  184. public function writeFixed($data) {
  185. $left = floor($data);
  186. $right = ($data - $left) * 0x10000;
  187. return $this->writeInt16($left) + $this->writeUInt16($right);
  188. }
  189. public function readLongDateTime() {
  190. $this->readUInt32(); // ignored
  191. $date = $this->readUInt32() - 2082844800;
  192. return strftime("%Y-%m-%d %H:%M:%S", $date);
  193. }
  194. public function writeLongDateTime($data) {
  195. $date = strtotime($data);
  196. $date += 2082844800;
  197. return $this->writeUInt32(0) + $this->writeUInt32($date);
  198. }
  199. public function unpack($def) {
  200. $d = array();
  201. foreach($def as $name => $type) {
  202. $d[$name] = $this->r($type);
  203. }
  204. return $d;
  205. }
  206. public function pack($def, $data) {
  207. $bytes = 0;
  208. foreach($def as $name => $type) {
  209. $bytes += $this->w($type, $data[$name]);
  210. }
  211. return $bytes;
  212. }
  213. /**
  214. * Read a data of type $type in the file from the current position
  215. *
  216. * @param mixed $type The data type to read
  217. * @return mixed The data that was read
  218. */
  219. public function r($type) {
  220. switch($type) {
  221. case self::uint8: return $this->readUInt8();
  222. case self::int8: return $this->readInt8();
  223. case self::uint16: return $this->readUInt16();
  224. case self::int16: return $this->readInt16();
  225. case self::uint32: return $this->readUInt32();
  226. case self::int32: return $this->readUInt32();
  227. case self::shortFrac: return $this->readFixed();
  228. case self::Fixed: return $this->readFixed();
  229. case self::FWord: return $this->readInt16();
  230. case self::uFWord: return $this->readUInt16();
  231. case self::F2Dot14: return $this->readInt16();
  232. case self::longDateTime: return $this->readLongDateTime();
  233. case self::char: return $this->read(1);
  234. default:
  235. if ( is_array($type) ) {
  236. if ($type[0] == self::char) {
  237. return $this->read($type[1]);
  238. }
  239. $ret = array();
  240. for($i = 0; $i < $type[1]; $i++) {
  241. $ret[] = $this->r($type[0]);
  242. }
  243. return $ret;
  244. }
  245. }
  246. }
  247. /**
  248. * Write $data of type $type in the file from the current position
  249. *
  250. * @param mixed $type The data type to write
  251. * @param mixed $data The data to write
  252. * @return int The number of bytes read
  253. */
  254. public function w($type, $data) {
  255. switch($type) {
  256. case self::uint8: return $this->writeUInt8($data);
  257. case self::int8: return $this->writeInt8($data);
  258. case self::uint16: return $this->writeUInt16($data);
  259. case self::int16: return $this->writeInt16($data);
  260. case self::uint32: return $this->writeUInt32($data);
  261. case self::int32: return $this->writeUInt32($data);
  262. case self::shortFrac: return $this->writeFixed($data);
  263. case self::Fixed: return $this->writeFixed($data);
  264. case self::FWord: return $this->writeInt16($data);
  265. case self::uFWord: return $this->writeUInt16($data);
  266. case self::F2Dot14: return $this->writeInt16($data);
  267. case self::longDateTime: return $this->writeLongDateTime($data);
  268. case self::char: return $this->write($data, 1);
  269. default:
  270. if ( is_array($type) ) {
  271. if ($type[0] == self::char) {
  272. return $this->write($data, $type[1]);
  273. }
  274. $ret = 0;
  275. for($i = 0; $i < $type[1]; $i++) {
  276. $ret += $this->w($type[0], $data[$i]);
  277. }
  278. return $ret;
  279. }
  280. }
  281. }
  282. /**
  283. * Converts a Uint32 value to string
  284. *
  285. * @param int $uint32
  286. * @param string The string
  287. */
  288. public function convertUInt32ToStr($uint32) {
  289. return chr(($uint32 >> 24) & 0xFF).chr(($uint32 >> 16) & 0xFF).chr(($uint32 >> 8) & 0xFF).chr($uint32 & 0xFF);
  290. }
  291. }