Geen omschrijving

File.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @package php-font-lib
  4. * @link https://github.com/PhenX/php-font-lib
  5. * @author Fabien Ménager <fabien.menager@gmail.com>
  6. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  7. */
  8. namespace FontLib\WOFF;
  9. use FontLib\Table\DirectoryEntry;
  10. /**
  11. * WOFF font file.
  12. *
  13. * @package php-font-lib
  14. *
  15. * @property TableDirectoryEntry[] $directory
  16. */
  17. class File extends \FontLib\TrueType\File {
  18. function parseHeader() {
  19. if (!empty($this->header)) {
  20. return;
  21. }
  22. $this->header = new 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 = (string) gzuncompress($data);
  40. }
  41. // Prepare data ...
  42. $length = mb_strlen($data, '8bit');
  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(DirectoryEntry::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. }