Brak opisu

DirectoryEntry.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Table;
  9. use FontLib\TrueType\File;
  10. use FontLib\Font;
  11. use FontLib\BinaryStream;
  12. /**
  13. * Generic Font table directory entry.
  14. *
  15. * @package php-font-lib
  16. */
  17. class DirectoryEntry extends BinaryStream {
  18. /**
  19. * @var File
  20. */
  21. protected $font;
  22. /**
  23. * @var Table
  24. */
  25. protected $font_table;
  26. public $entryLength = 4;
  27. public $tag;
  28. public $checksum;
  29. public $offset;
  30. public $length;
  31. protected $origF;
  32. /**
  33. * @param string $data
  34. *
  35. * @return int
  36. */
  37. static function computeChecksum($data) {
  38. $len = mb_strlen($data, '8bit');
  39. $mod = $len % 4;
  40. if ($mod) {
  41. $data = str_pad($data, $len + (4 - $mod), "\0");
  42. }
  43. $len = mb_strlen($data, '8bit');
  44. $hi = 0x0000;
  45. $lo = 0x0000;
  46. for ($i = 0; $i < $len; $i += 4) {
  47. $hi += (ord($data[$i]) << 8) + ord($data[$i + 1]);
  48. $lo += (ord($data[$i + 2]) << 8) + ord($data[$i + 3]);
  49. $hi += $lo >> 16;
  50. $lo = $lo & 0xFFFF;
  51. $hi = $hi & 0xFFFF;
  52. }
  53. return ($hi << 8) + $lo;
  54. }
  55. function __construct(File $font) {
  56. $this->font = $font;
  57. $this->f = $font->f;
  58. }
  59. function parse() {
  60. $this->tag = $this->font->read(4);
  61. }
  62. function open($filename, $mode = self::modeRead) {
  63. // void
  64. }
  65. function setTable(Table $font_table) {
  66. $this->font_table = $font_table;
  67. }
  68. function encode($entry_offset) {
  69. Font::d("\n==== $this->tag ====");
  70. //Font::d("Entry offset = $entry_offset");
  71. $data = $this->font_table;
  72. $font = $this->font;
  73. $table_offset = $font->pos();
  74. $this->offset = $table_offset;
  75. $table_length = $data->encode();
  76. $font->seek($table_offset);
  77. $table_data = $font->read($table_length);
  78. $font->seek($entry_offset);
  79. $font->write($this->tag, 4);
  80. $font->writeUInt32(self::computeChecksum($table_data));
  81. $font->writeUInt32($table_offset);
  82. $font->writeUInt32($table_length);
  83. Font::d("Bytes written = $table_length");
  84. $font->seek($table_offset + $table_length);
  85. }
  86. /**
  87. * @return File
  88. */
  89. function getFont() {
  90. return $this->font;
  91. }
  92. function startRead() {
  93. $this->font->seek($this->offset);
  94. }
  95. function endRead() {
  96. //
  97. }
  98. function startWrite() {
  99. $this->font->seek($this->offset);
  100. }
  101. function endWrite() {
  102. //
  103. }
  104. }