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