Advertisement
Guest User

ttf-info-class.php

a guest
Feb 8th, 2017
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.55 KB | None | 0 0
  1. <?php
  2. /**
  3. * ttfInfo class
  4. * Retrieve data stored in a TTF files 'name' table
  5. *
  6. * @original author Unknown
  7. * found at http://www.phpclasses.org/browse/package/2144.html
  8. *
  9. * @ported for used on http://www.nufont.com
  10. * @author Jason Arencibia
  11. * @version 0.2
  12. * @copyright (c) 2006 GrayTap Media
  13. * @website http://www.graytap.com
  14. * @license GPL 2.0
  15. * @access public
  16. *
  17. * @todo: Make it Retrieve additional information from other tables
  18. *
  19. */
  20. class ttfInfo {
  21. /**
  22. * variable $_dirRestriction
  23. * Restrict the resource pointer to this directory and above.
  24. * Change to 1 for to allow the class to look outside of it current directory
  25. * @protected
  26. * @var int
  27. */
  28. protected $_dirRestriction = 1;
  29. /**
  30. * variable $_dirRestriction
  31. * Restrict the resource pointer to this directory and above.
  32. * Change to 1 for nested directories
  33. * @protected
  34. * @var int
  35. */
  36. protected $_recursive = 0;
  37.  
  38. /**
  39. * variable $fontsdir
  40. * This is to declare this variable as protected
  41. * don't edit this!!!
  42. * @protected
  43. */
  44. protected $fontsdir;
  45. /**
  46. * variable $filename
  47. * This is to declare this varable as protected
  48. * don't edit this!!!
  49. * @protected
  50. */
  51. protected $filename;
  52.  
  53. /**
  54. * function setFontFile()
  55. * set the filename
  56. * @public
  57. * @param string $data the new value
  58. * @return object reference to this
  59. */
  60. public function setFontFile($data)
  61. {
  62. if ($this->_dirRestriction && preg_match('[\.\/|\.\.\/]', $data))
  63. {
  64. $this->exitClass('Error: Directory restriction is enforced!');
  65. }
  66.  
  67. $this->filename = $data;
  68. return $this;
  69. } // public function setFontFile
  70.  
  71. /**
  72. * function setFontsDir()
  73. * set the Font Directory
  74. * @public
  75. * @param string $data the new value
  76. * @return object referrence to this
  77. */
  78. public function setFontsDir($data)
  79. {
  80. if ($this->_dirRestriction && preg_match('[\.\/|\.\.\/]', $data))
  81. {
  82. $this->exitClass('Error: Directory restriction is enforced!');
  83. }
  84.  
  85. $this->fontsdir = $data;
  86. return $this;
  87. } // public function setFontsDir
  88.  
  89. /**
  90. * function readFontsDir()
  91. * @public
  92. * @return information contained in the TTF 'name' table of all fonts in a directory.
  93. */
  94. public function readFontsDir()
  95. {
  96. if (empty($this->fontsdir)) { $this->exitClass('Error: Fonts Directory has not been set with setFontsDir().'); }
  97. if (empty($this->backupDir)){ $this->backupDir = $this->fontsdir; }
  98.  
  99. $this->array = array();
  100. $d = dir($this->fontsdir);
  101.  
  102. while (false !== ($e = $d->read()))
  103. {
  104. if($e != '.' && $e != '..')
  105. {
  106. $e = $this->fontsdir . $e;
  107. if($this->_recursive && is_dir($e))
  108. {
  109. $this->setFontsDir($e);
  110. $this->array = array_merge($this->array, readFontsDir());
  111. }
  112. else if ($this->is_ttf($e) === true)
  113. {
  114. $this->setFontFile($e);
  115. $this->array[$e] = $this->getFontInfo();
  116. }
  117. }
  118. }
  119.  
  120. if (!empty($this->backupDir)){ $this->fontsdir = $this->backupDir; }
  121.  
  122. $d->close();
  123. return $this;
  124. } // public function readFontsDir
  125.  
  126. /**
  127. * function setProtectedVar()
  128. * @public
  129. * @param string $var the new variable
  130. * @param string $data the new value
  131. * @return object reference to this
  132.  
  133. * DISABLED, NO REAL USE YET
  134.  
  135. public function setProtectedVar($var, $data)
  136. {
  137. if ($var == 'filename')
  138. {
  139. $this->setFontFile($data);
  140. } else {
  141. //if (isset($var) && !empty($data))
  142. $this->$var = $data;
  143. }
  144. return $this;
  145. }
  146. */
  147. /**
  148. * function getFontInfo()
  149. * @public
  150. * @return information contained in the TTF 'name' table.
  151. */
  152. public function getFontInfo()
  153. {
  154. $fd = fopen ($this->filename, "r");
  155. $this->text = fread ($fd, filesize($this->filename));
  156. fclose ($fd);
  157.  
  158. $number_of_tables = hexdec($this->dec2ord($this->text[4]).$this->dec2ord($this->text[5]));
  159.  
  160. for ($i=0;$i<$number_of_tables;$i++)
  161. {
  162. $tag = $this->text[12+$i*16].$this->text[12+$i*16+1].$this->text[12+$i*16+2].$this->text[12+$i*16+3];
  163.  
  164. if ($tag == 'name')
  165. {
  166. $this->ntOffset = hexdec(
  167. $this->dec2ord($this->text[12+$i*16+8]).$this->dec2ord($this->text[12+$i*16+8+1]).
  168. $this->dec2ord($this->text[12+$i*16+8+2]).$this->dec2ord($this->text[12+$i*16+8+3]));
  169.  
  170. $offset_storage_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+4]).$this->dec2ord($this->text[$this->ntOffset+5]));
  171. $number_name_records_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+2]).$this->dec2ord($this->text[$this->ntOffset+3]));
  172. }
  173. }
  174.  
  175. $storage_dec = $offset_storage_dec + $this->ntOffset;
  176. $storage_hex = strtoupper(dechex($storage_dec));
  177.  
  178. for ($j=0;$j<$number_name_records_dec;$j++)
  179. {
  180. $platform_id_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+6+$j*12+0]).$this->dec2ord($this->text[$this->ntOffset+6+$j*12+1]));
  181. $name_id_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+6+$j*12+6]).$this->dec2ord($this->text[$this->ntOffset+6+$j*12+7]));
  182. $string_length_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+6+$j*12+8]).$this->dec2ord($this->text[$this->ntOffset+6+$j*12+9]));
  183. $string_offset_dec = hexdec($this->dec2ord($this->text[$this->ntOffset+6+$j*12+10]).$this->dec2ord($this->text[$this->ntOffset+6+$j*12+11]));
  184.  
  185. if (!empty($name_id_dec) and empty($font_tags[$name_id_dec]))
  186. {
  187. for($l=0;$l<$string_length_dec;$l++)
  188. {
  189. if (ord($this->text[$storage_dec+$string_offset_dec+$l]) == '0') { continue; }
  190. else { $font_tags[$name_id_dec] .= ($this->text[$storage_dec+$string_offset_dec+$l]); }
  191. }
  192. }
  193. }
  194. return $font_tags;
  195. } // public function getFontInfo
  196.  
  197. /**
  198. * function getCopyright()
  199. * @public
  200. * @return 'Copyright notice' contained in the TTF 'name' table at index 0
  201. */
  202. public function getCopyright()
  203. {
  204. $this->info = $this->getFontInfo();
  205. return $this->info[0];
  206. } // public function getCopyright
  207.  
  208. /**
  209. * function getFontFamily()
  210. * @public
  211. * @return 'Font Family name' contained in the TTF 'name' table at index 1
  212. */
  213. public function getFontFamily()
  214. {
  215. $this->info = $this->getFontInfo();
  216. return $this->info[1];
  217. } // public function getFontFamily
  218.  
  219. /**
  220. * function getFontSubFamily()
  221. * @public
  222. * @return 'Font Subfamily name' contained in the TTF 'name' table at index 2
  223. */
  224. public function getFontSubFamily()
  225. {
  226. $this->info = $this->getFontInfo();
  227. return $this->info[2];
  228. } // public function getFontSubFamily
  229.  
  230. /**
  231. * function getFontId()
  232. * @public
  233. * @return 'Unique font identifier' contained in the TTF 'name' table at index 3
  234. */
  235. public function getFontId()
  236. {
  237. $this->info = $this->getFontInfo();
  238. return $this->info[3];
  239. } // public function getFontId
  240.  
  241. /**
  242. * function getFullFontName()
  243. * @public
  244. * @return 'Full font name' contained in the TTF 'name' table at index 4
  245. */
  246. public function getFullFontName()
  247. {
  248. $this->info = $this->getFontInfo();
  249. return $this->info[4];
  250. } // public function getFullFontName
  251.  
  252. /**
  253. * function dec2ord()
  254. * Used to lessen redundant calls to multiple functions.
  255. * @protected
  256. * @return object
  257. */
  258. protected function dec2ord($dec)
  259. {
  260. return $this->dec2hex(ord($dec));
  261. } // protected function dec2ord
  262.  
  263. /**
  264. * function dec2hex()
  265. * private function to perform Hexadecimal to decimal with proper padding.
  266. * @protected
  267. * @return object
  268. */
  269. protected function dec2hex($dec)
  270. {
  271. return str_repeat('0', 2-strlen(($hex=strtoupper(dechex($dec))))) . $hex;
  272. } // protected function dec2hex
  273.  
  274. /**
  275. * function dec2hex()
  276. * private function to perform Hexadecimal to decimal with proper padding.
  277. * @protected
  278. * @return object
  279. */
  280. protected function exitClass($message)
  281. {
  282. echo $message;
  283. exit;
  284. } // protected function dec2hex
  285.  
  286. /**
  287. * function dec2hex()
  288. * private helper function to test in the file in question is a ttf.
  289. * @protected
  290. * @return object
  291. */
  292. protected function is_ttf($file)
  293. {
  294. $ext = explode('.', $file);
  295. $ext = $ext[count($ext)-1];
  296. return preg_match("/ttf$/i",$ext) ? true : false;
  297. } // protected function is_ttf
  298. } // class ttfInfo
  299.  
  300. function getFontInfo($resource)
  301. {
  302. $ttfInfo = new ttfInfo;
  303. $ttfInfo->setFontFile($resource);
  304. return $ttfInfo->getFontInfo();
  305. }
  306. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement