font_woff.cls.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. require_once dirname(__FILE__)."/font_truetype.cls.php";
  10. require_once dirname(__FILE__)."/font_woff_table_directory_entry.cls.php";
  11. require_once dirname(__FILE__)."/font_woff_header.cls.php";
  12. /**
  13. * WOFF font file.
  14. *
  15. * @package php-font-lib
  16. */
  17. class Font_WOFF extends Font_TrueType {
  18. function parseHeader(){
  19. if (!empty($this->header)) {
  20. return;
  21. }
  22. $this->header = new Font_WOFF_Header($this);
  23. $this->header->parse();
  24. }
  25. public function load($file) {
  26. parent::load($file);
  27. $this->parseTableEntries();
  28. $dataOffset = $this->pos() + count($this->directory) * 20;
  29. $fw = $this->getTempFile(false);
  30. $fr = $this->f;
  31. $this->f = $fw;
  32. $offset = $this->header->encode();
  33. foreach($this->directory as $entry) {
  34. // Read ...
  35. $this->f = $fr;
  36. $this->seek($entry->offset);
  37. $data = $this->read($entry->length);
  38. if ($entry->length < $entry->origLength) {
  39. $data = gzuncompress($data);
  40. }
  41. // Prepare data ...
  42. $length = strlen($data);
  43. $entry->length = $entry->origLength = $length;
  44. $entry->offset = $dataOffset;
  45. // Write ...
  46. $this->f = $fw;
  47. // Woff Entry
  48. $this->seek($offset);
  49. $offset += $this->write($entry->tag, 4); // tag
  50. $offset += $this->writeUInt32($dataOffset); // offset
  51. $offset += $this->writeUInt32($length); // length
  52. $offset += $this->writeUInt32($length); // origLength
  53. $offset += $this->writeUInt32(Font_Table_Directory_Entry::computeChecksum($data)); // checksum
  54. // Data
  55. $this->seek($dataOffset);
  56. $dataOffset += $this->write($data, $length);
  57. }
  58. $this->f = $fw;
  59. $this->seek(0);
  60. // Need to re-parse this, don't know why
  61. $this->header = null;
  62. $this->directory = array();
  63. $this->parseTableEntries();
  64. }
  65. }