Font_Binary_Stream

Generic font file binary stream.

Defined (1)

The class is defined in the following location(s).

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