font_table.cls.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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$
  8. */
  9. /**
  10. * Generic font table.
  11. *
  12. * @package php-font-lib
  13. */
  14. class Font_Table extends Font_Binary_Stream {
  15. /**
  16. * @var Font_Table_Directory_Entry
  17. */
  18. protected $entry;
  19. protected $def = array();
  20. public $data;
  21. final public function __construct(Font_Table_Directory_Entry $entry) {
  22. $this->entry = $entry;
  23. $entry->setTable($this);
  24. }
  25. /**
  26. * @return Font_TrueType
  27. */
  28. public function getFont(){
  29. return $this->entry->getFont();
  30. }
  31. protected function _encode(){
  32. if (empty($this->data)) {
  33. Font::d(" >> Table is empty");
  34. return 0;
  35. }
  36. return $this->getFont()->pack($this->def, $this->data);
  37. }
  38. protected function _parse(){
  39. $this->data = $this->getFont()->unpack($this->def);
  40. }
  41. protected function _parseRaw(){
  42. $this->data = $this->getFont()->read($this->entry->length);
  43. }
  44. protected function _encodeRaw(){
  45. return $this->getFont()->write($this->data, $this->entry->length);
  46. }
  47. public function toHTML(){
  48. return "<pre>".var_export($this->data, true)."</pre>";
  49. }
  50. final public function encode(){
  51. $this->entry->startWrite();
  52. if (false && empty($this->def)) {
  53. $length = $this->_encodeRaw();
  54. }
  55. else {
  56. $length = $this->_encode();
  57. }
  58. $this->entry->endWrite();
  59. return $length;
  60. }
  61. final public function parse(){
  62. $this->entry->startRead();
  63. if (false && empty($this->def)) {
  64. $this->_parseRaw();
  65. }
  66. else {
  67. $this->_parse();
  68. }
  69. $this->entry->endRead();
  70. }
  71. }