Bez popisu

loca.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Type;
  9. use FontLib\Table\Table;
  10. /**
  11. * `loca` font table.
  12. *
  13. * @package php-font-lib
  14. */
  15. class loca extends Table {
  16. protected function _parse() {
  17. $font = $this->getFont();
  18. $offset = $font->pos();
  19. $indexToLocFormat = $font->getData("head", "indexToLocFormat");
  20. $numGlyphs = $font->getData("maxp", "numGlyphs");
  21. $font->seek($offset);
  22. $data = array();
  23. // 2 bytes
  24. if ($indexToLocFormat == 0) {
  25. $d = $font->read(($numGlyphs + 1) * 2);
  26. $loc = unpack("n*", $d);
  27. for ($i = 0; $i <= $numGlyphs; $i++) {
  28. $data[] = isset($loc[$i + 1]) ? $loc[$i + 1] * 2 : 0;
  29. }
  30. }
  31. // 4 bytes
  32. else {
  33. if ($indexToLocFormat == 1) {
  34. $d = $font->read(($numGlyphs + 1) * 4);
  35. $loc = unpack("N*", $d);
  36. for ($i = 0; $i <= $numGlyphs; $i++) {
  37. $data[] = isset($loc[$i + 1]) ? $loc[$i + 1] : 0;
  38. }
  39. }
  40. }
  41. $this->data = $data;
  42. }
  43. function _encode() {
  44. $font = $this->getFont();
  45. $data = $this->data;
  46. $indexToLocFormat = $font->getData("head", "indexToLocFormat");
  47. $numGlyphs = $font->getData("maxp", "numGlyphs");
  48. $length = 0;
  49. // 2 bytes
  50. if ($indexToLocFormat == 0) {
  51. for ($i = 0; $i <= $numGlyphs; $i++) {
  52. $length += $font->writeUInt16($data[$i] / 2);
  53. }
  54. }
  55. // 4 bytes
  56. else {
  57. if ($indexToLocFormat == 1) {
  58. for ($i = 0; $i <= $numGlyphs; $i++) {
  59. $length += $font->writeUInt32($data[$i]);
  60. }
  61. }
  62. }
  63. return $length;
  64. }
  65. }