Нет описания

Collection.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\TrueType;
  9. use Countable;
  10. use FontLib\BinaryStream;
  11. use Iterator;
  12. use OutOfBoundsException;
  13. /**
  14. * TrueType collection font file.
  15. *
  16. * @package php-font-lib
  17. */
  18. class Collection extends BinaryStream implements Iterator, Countable {
  19. /**
  20. * Current iterator position.
  21. *
  22. * @var integer
  23. */
  24. private $position = 0;
  25. protected $collectionOffsets = array();
  26. protected $collection = array();
  27. protected $version;
  28. protected $numFonts;
  29. function parse() {
  30. if (isset($this->numFonts)) {
  31. return;
  32. }
  33. $this->read(4); // tag name
  34. $this->version = $this->readFixed();
  35. $this->numFonts = $this->readUInt32();
  36. for ($i = 0; $i < $this->numFonts; $i++) {
  37. $this->collectionOffsets[] = $this->readUInt32();
  38. }
  39. }
  40. /**
  41. * @param int $fontId
  42. *
  43. * @throws OutOfBoundsException
  44. * @return File
  45. */
  46. function getFont($fontId) {
  47. $this->parse();
  48. if (!isset($this->collectionOffsets[$fontId])) {
  49. throw new OutOfBoundsException();
  50. }
  51. if (isset($this->collection[$fontId])) {
  52. return $this->collection[$fontId];
  53. }
  54. $font = new File();
  55. $font->f = $this->f;
  56. $font->setTableOffset($this->collectionOffsets[$fontId]);
  57. return $this->collection[$fontId] = $font;
  58. }
  59. function current() {
  60. return $this->getFont($this->position);
  61. }
  62. function key() {
  63. return $this->position;
  64. }
  65. function next() {
  66. return ++$this->position;
  67. }
  68. function rewind() {
  69. $this->position = 0;
  70. }
  71. function valid() {
  72. $this->parse();
  73. return isset($this->collectionOffsets[$this->position]);
  74. }
  75. function count() {
  76. $this->parse();
  77. return $this->numFonts;
  78. }
  79. }