From e07cd25762b22c4cc9ea164ced5d292e7da873d7 Mon Sep 17 00:00:00 2001 From: Indrek Altpere Date: Wed, 3 Feb 2016 09:34:49 +0200 Subject: [PATCH 1/2] Inline the readInt16, according to cachegrind output this reduces the overall cost of this function by ~30% --- src/FontLib/BinaryStream.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/FontLib/BinaryStream.php b/src/FontLib/BinaryStream.php index 2b6b7eb..c305109 100644 --- a/src/FontLib/BinaryStream.php +++ b/src/FontLib/BinaryStream.php @@ -210,7 +210,8 @@ public function writeUFWord($data) { } public function readInt16() { - $v = $this->readUInt16(); + $a = unpack("nn", $this->read(2)); + $v = $a["n"]; if ($v >= 0x8000) { $v -= 0x10000; From 84f1b65071cf5f6c53047474b1449e8d5c4937f1 Mon Sep 17 00:00:00 2001 From: Indrek Altpere Date: Wed, 3 Feb 2016 09:50:11 +0200 Subject: [PATCH 2/2] Instead of calling $table->getFont() twice for each outline, pass it along to init and parse functions, reduces overall parsing time by another 20% for FreeSerif --- src/FontLib/Glyph/Outline.php | 8 +++----- src/FontLib/Table/Type/glyf.php | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/FontLib/Glyph/Outline.php b/src/FontLib/Glyph/Outline.php index 12091a9..330db09 100644 --- a/src/FontLib/Glyph/Outline.php +++ b/src/FontLib/Glyph/Outline.php @@ -42,8 +42,7 @@ class Outline extends BinaryStream { * * @return Outline */ - static function init(glyf $table, $offset, $size) { - $font = $table->getFont(); + static function init(glyf $table, $offset, $size, BinaryStream $font) { $font->seek($offset); if ($font->readInt16() > -1) { @@ -55,7 +54,7 @@ static function init(glyf $table, $offset, $size) { $glyph = new OutlineComposite($table, $offset, $size); } - $glyph->parse(); + $glyph->parse($font); return $glyph; } @@ -73,8 +72,7 @@ function __construct(glyf $table, $offset = null, $size = null) { $this->size = $size; } - function parse() { - $font = $this->getFont(); + function parse(BinaryStream $font) { $font->seek($this->offset); if (!$this->size) { diff --git a/src/FontLib/Table/Type/glyf.php b/src/FontLib/Table/Type/glyf.php index cbc04d2..1fbec3f 100644 --- a/src/FontLib/Table/Type/glyf.php +++ b/src/FontLib/Table/Type/glyf.php @@ -31,7 +31,7 @@ protected function _parse() { foreach ($real_loca as $gid => $location) { $_offset = $offset + $loca[$gid]; $_size = $loca[$gid + 1] - $loca[$gid]; - $data[$gid] = Outline::init($this, $_offset, $_size); + $data[$gid] = Outline::init($this, $_offset, $_size, $font); } $this->data = $data;