Skip to content

Commit 8c5640b

Browse files
committed
Merge pull request #34 from efbiaiinzinz/master
Optimized versions of uint16/int16/uint8/int8 multi-readers
2 parents 835a188 + bccb423 commit 8c5640b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/FontLib/BinaryStream.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ public function readUInt8() {
157157
return ord($this->read(1));
158158
}
159159

160+
public function readUInt8Many($count) {
161+
return array_values(unpack("C*", $this->read($count)));
162+
}
163+
160164
public function writeUInt8($data) {
161165
return $this->write(chr($data), 1);
162166
}
@@ -171,6 +175,10 @@ public function readInt8() {
171175
return $v;
172176
}
173177

178+
public function readInt8Many($count) {
179+
return array_values(unpack("c*", $this->read($count)));
180+
}
181+
174182
public function writeInt8($data) {
175183
if ($data < 0) {
176184
$data += 0x100;
@@ -185,6 +193,10 @@ public function readUInt16() {
185193
return $a["n"];
186194
}
187195

196+
public function readUInt16Many($count) {
197+
return array_values(unpack("n*", $this->read($count * 2)));
198+
}
199+
188200
public function readUFWord() {
189201
return $this->readUInt16();
190202
}
@@ -207,6 +219,17 @@ public function readInt16() {
207219
return $v;
208220
}
209221

222+
public function readInt16Many($count) {
223+
$vals = array_values(unpack("n*", $this->read($count * 2)));
224+
foreach ($vals as &$v) {
225+
if ($v >= 0x8000) {
226+
$v -= 0x10000;
227+
}
228+
}
229+
230+
return $vals;
231+
}
232+
210233
public function readFWord() {
211234
return $this->readInt16();
212235
}
@@ -319,6 +342,18 @@ public function r($type) {
319342
if ($type[0] == self::char) {
320343
return $this->read($type[1]);
321344
}
345+
if ($type[0] == self::uint16) {
346+
return $this->readUInt16Many($type[1]);
347+
}
348+
if ($type[0] == self::int16) {
349+
return $this->readInt16Many($type[1]);
350+
}
351+
if ($type[0] == self::uint8) {
352+
return $this->readUInt8Many($type[1]);
353+
}
354+
if ($type[0] == self::int8) {
355+
return $this->readInt8Many($type[1]);
356+
}
322357

323358
$ret = array();
324359
for ($i = 0; $i < $type[1]; $i++) {

0 commit comments

Comments
 (0)