Skip to content

Commit a1681e9

Browse files
committed
Check for existence of glyf table before use
1 parent 34c5dc9 commit a1681e9

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/FontLib/AdobeFontMetrics.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,24 @@ function write($file, $encoding = null) {
8282

8383
$glyf = $font->getData("glyf");
8484
$glyphIndexArray = $font->getUnicodeCharMap();
85+
$hasGlyphs = $glyf instanceof glyf && is_array($glyphIndexArray);
8586

8687
// capHeight is based on capital H
87-
if (\array_key_exists(72, $glyphIndexArray)) {
88+
if ($hasGlyphs && \array_key_exists(72, $glyphIndexArray)) {
8889
$upperH = $glyf[$glyphIndexArray[72]];
8990
$upperH->parseData();
9091
$this->addPair("CapHeight", $font->normalizeFUnit($upperH->yMax));
9192
}
9293

9394
// xHeight is based on lowercase x
94-
if (\array_key_exists(120, $glyphIndexArray)) {
95+
if ($hasGlyphs && \array_key_exists(120, $glyphIndexArray)) {
9596
$lowerX = $glyf[$glyphIndexArray[120]];
9697
$lowerX->parseData();
9798
$this->addPair("XHeight", $font->normalizeFUnit($lowerX->yMax));
9899
}
99100

100101
// ascender is based on lowercase d
101-
if (\array_key_exists(100, $glyphIndexArray)) {
102+
if ($hasGlyphs && \array_key_exists(100, $glyphIndexArray)) {
102103
$lowerD = $glyf[$glyphIndexArray[100]];
103104
$lowerD->parseData();
104105
$this->addPair("Ascender", $font->normalizeFUnit($lowerD->yMax));
@@ -110,11 +111,11 @@ function write($file, $encoding = null) {
110111
}
111112

112113
// descender is based on lowercase p
113-
if (\array_key_exists(112, $glyphIndexArray)) {
114+
if ($hasGlyphs && \array_key_exists(112, $glyphIndexArray)) {
114115
$lowerP = $glyf[$glyphIndexArray[112]];
115116
$lowerP->parseData();
116117
$this->addPair("Descender", $font->normalizeFUnit($lowerP->yMin));
117-
} elseif (isset($hhea["ascent"])) {
118+
} elseif (isset($hhea["descent"])) {
118119
$this->addPair("Descender", $font->normalizeFUnit($hhea["descent"]));
119120
}
120121
else {

src/FontLib/TrueType/File.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,11 @@ function setSubset($subset) {
310310

311311
/** @var glyf $glyf */
312312
$glyf = $this->getTableObject("glyf");
313-
$gids = $glyf->getGlyphIDs($gids);
314-
315-
sort($gids);
316-
317-
$this->glyph_subset = $gids;
313+
if ($glyf) {
314+
$gids = $glyf->getGlyphIDs($gids);
315+
sort($gids);
316+
$this->glyph_subset = $gids;
317+
}
318318
$this->glyph_all = array_values($glyphIndexArray); // FIXME
319319
}
320320

@@ -443,7 +443,10 @@ protected function readTable($tag) {
443443
* @return Table
444444
*/
445445
public function getTableObject($name) {
446-
return $this->data[$name];
446+
if (\array_key_exists($name, $this->data)) {
447+
return $this->data[$name];
448+
}
449+
return null;
447450
}
448451

449452
public function setTableObject($name, Table $data) {

0 commit comments

Comments
 (0)